/**
 * story.js
 **/





/**
 * displays a specific story in the modal window
 *
 * @param int storyID the story ID to display
 * @return void
 * @author Tim Cromwell
 **/
function showStory(storyID)
{
	// hide the popup
	hideModalWindow();
	
	// create the url required to retrieve ajax information
	var ajaxURL = '/ajax-data/story/' + storyID;
	
	$.get(ajaxURL, 
		function(data) 
		{
	
//	new Ajax.Request(ajaxURL, {
//	  method: 'get',
//	  	onSuccess: function( response )
//		{
			// Convert the response from JSON
		//	var data = eval('(' + response.responseText.replace(/\n/g, '') + ')');
			data = eval('(' + data.replace(/\n/g, '') + ')');

			// ascertain the story image
			var storyImage = '';
			if (data.story.photo.path)
				storyImage = data.story.workspace + data.story.photo.path + '/' + data.story.photo.filename;			
			
			var htmlContent = '<h3>' + data.story.title + '</h3>';
			htmlContent += '<ul class="popup_story_layout">';
			htmlContent += '<li class="story_image">';
			if (storyImage != '')
				htmlContent += '<img src="' + storyImage + '" alt="Story Image" />';
			htmlContent += '</li>';
			htmlContent += '<li class="story_content">' + data.story.content + '</li>';
			htmlContent += '</ul>';

			// the html form
			htmlContent += '<form method="post" action="#">';
			htmlContent += '<input type="button" name="tellYourStory" class="tell_us_your_story_button" title="Tell us your Story" onclick="tellYourStoryForm()" />';
			
			// if a tv commercial has been applied to the item
			if (data.story.video_code != '')
			{
				htmlContent += '<input type="button" name="viewCommercial" class="view_tv_commercial_button" title="View the TV Commercial" onclick="showMedia(' + data.story.story_id + ')" />';
			}
			htmlContent += '</form>';

			// insert the content into the popup
			$('div#modal_window_ajax_content').html(htmlContent);

			// display the popup
			showModalWindow();
			
			// replace all headings
			Cufon.replace('h3');
			Cufon.now();
//		}
		
	});	
}



/**
 * displays the someone's missing promotional story in the modal window
 *
 * @param int storyID the story ID to display
 * @return void
 * @author Tim Cromwell
 **/
function showMissingStory(storyID)
{
	// hide the popup
	hideModalWindow();
	
	// create the url required to retrieve ajax information
	var ajaxURL = '/ajax-data/story/' + storyID;
	
	$.get(ajaxURL, 
		function(data) 
		{
	
//	new Ajax.Request(ajaxURL, {
//	  method: 'get',
//	  	onSuccess: function( response )
//		{
			// Convert the response from JSON
		//	var data = eval('(' + response.responseText.replace(/\n/g, '') + ')');
			data = eval('(' + data.replace(/\n/g, '') + ')');

			// ascertain the story image
			var storyImage = '';
			if (data.story.photo.path)
				storyImage = data.story.workspace + data.story.photo.path + '/' + data.story.photo.filename;			
			
			var htmlContent = '<h3>' + data.story.title + '</h3>';
			htmlContent += '<ul class="popup_story_layout">';
			htmlContent += '<li class="story_image">';
			if (storyImage != '')
				htmlContent += '<img src="' + storyImage + '" alt="Story Image" />';
			htmlContent += '</li>';
			htmlContent += '<li class="story_content">' + data.story.content + '</li>';
			htmlContent += '</ul>';

			// the html form
			htmlContent += '<form method="post" action="#">';
			htmlContent += '<input type="button" name="someonesmissing" class="someones_missing_button" title="Someone\'s Missing" onclick="document.location = \'/someones-missing/\';" />';
			
			// if a tv commercial has been applied to the item
			if (data.story.video_code != '')
			{
				htmlContent += '<input type="button" name="viewCommercial" class="view_tv_commercial_button" title="View the TV Commercial" onclick="showMedia(' + data.story.story_id + ')" />';
			}
			htmlContent += '</form>';

			// insert the content into the popup
			$('div#modal_window_ajax_content').html(htmlContent);

			// display the popup
			showModalWindow();
			
			// replace all headings
			Cufon.replace('h3');
			Cufon.now();
//		}
		
	});	
}



/**
 * displays a specific your story in the modal window
 *
 * @param int storyID the story ID to display
 * @return void
 * @author Tim Cromwell
 **/
function showYourStory(storyID)
{
	// hide the popup
	hideModalWindow();
	
	// create the url required to retrieve ajax information
	var ajaxURL = '/ajax-data/story/' + storyID;
	
	$.get(ajaxURL, 
		function(data) 
		{
	
//	new Ajax.Request(ajaxURL, {
//	  method: 'get',
//	  	onSuccess: function( response )
//		{
			// Convert the response from JSON
			data = eval('(' + data.replace(/\n/g, '') + ')');
			
			// ascertain the story image
			var storyImage = '';
			if (data.story.photo.path)
				storyImage = data.story.workspace + data.story.photo.path + '/' + data.story.photo.filename;
			
			// the html content
			var htmlContent = '<h3>' + data.story.title + '</h3>';
			
			if (storyImage != '')
			{
				htmlContent += '<ul class="popup_story_layout">';
				htmlContent += '<li class="story_image">';
				htmlContent += '<img src="' + storyImage + '" alt="Story Image" />';
				htmlContent += '</li>';
				htmlContent += '<li class="story_content">' + data.story.content + '</li>';
				htmlContent += '</ul>';
			}
			else
			{
				htmlContent += '<div id="your_story_content">';
				htmlContent += data.story.content;
				htmlContent += '</div>';				
			}
			
			// the html form
			htmlContent += '<form method="post" action="#">';
			htmlContent += '<input type="button" name="tellYourStory" class="tell_us_your_story_button" title="Tell us your Story" onclick="tellYourStoryForm()" />';
			htmlContent += '</form>';

			// insert the content into the popup
			$('div#modal_window_ajax_content').html(htmlContent);
			
			// display the popup
			showModalWindow();
			
			// replace all headings
			Cufon.replace('h3');
			Cufon.now();
//		}
	});
}


/**
 * displays the "tell your story" form in the modal window
 *
 * @return void
 * @author Tim Cromwell
 **/
function tellYourStoryForm()
{
	// hide the popup
	hideModalWindow();
	
	var htmlContent = '<h3>Tell us your story</h3>';
	htmlContent += '<p>Have you got a story to tell? Send it to <strong><em><a href="mailto:stories@notbornyet.com">stories@notbornyet.com</a></em></strong>.</p>';
	htmlContent += '<p>So that your story is told, please ensure that your story complies with our <a href="#" onclick="storyGuidelines()">story guidelines</a>.</p>';
//	htmlContent += '<div id="save_result" style="display: none;"></div>';

	// the html form
//	htmlContent += '<form method="post" action="#">';
//	htmlContent += '<label class="auto_width">Please enter your story below</label><br />';
//	htmlContent += '<textarea name="yourStory" id="yourStory" rows=8 style="width: 720px;">Enter your story here...</textarea><br />';
//	htmlContent += '<div class="form_group_right">';
//	htmlContent += '<span class="secondary_action"><a href="#" onclick="hideModalWindow();">Cancel</a></span><input type="button" name="yourStorySubmit" class="submit_button" title="Submit" onclick="submitTellYourStoryForm()" /><br />';
//	htmlContent += '</div>';
//	htmlContent += '</form>';
	
	// insert the content into the popup
	$('div#modal_window_ajax_content').html(htmlContent);
	
	// display the popup
	showModalWindow();
	
	// replace all headings
	Cufon.replace('h3');
	Cufon.now();
}

/**
 * processes the "tell your story" form in the modal window
 *
 * @return void
 * @author Tim Cromwell
 **/
function submitTellYourStoryForm()
{
	// submit the contents of the story via ajax..
	
	// create the url required to retrieve ajax information
	var ajaxURL = '/wp-content/themes/notbornyet/json_request.php?requestType=sendStory';
		
	new Ajax.Request(ajaxURL, {
	  	method: 'post',
		parameters: 'storyDetail=' + Base64.encode($$('#yourStory')[0].value),
	  	onSuccess: function( response )
		{
			// Convert the response from JSON
			var data = eval('('+response.responseText+')');

			var htmlContent = '<h3>Tell us your story</h3>';
			
			// if the data was successfully updated
			if (data == true)
				htmlContent += '<p><strong>Thank you for telling us your story.</strong> Be sure to keep an eye on our "<a href="/stories/your-stories/">your stories</a>" section for your story in the coming days.</p>';
			else
				htmlContent += '<p><strong>Unfortunately we could not process your story at this time.  Please try again later.</strong></p>';

			htmlContent += '<p><a href="#" onclick="hideModalWindow();">Close</a></p>';

			// insert the content into the popup
			$$('div#modal_window_ajax_content')[0].update(htmlContent);
		}
	});
	
}


/**
 * displays the "tell your story" guidelines in the modal window
 *
 * @return void
 * @author Tim Cromwell
 **/
function storyGuidelines()
{
	// create the url required to retrieve ajax information
	var ajaxURL = '/ajax-data/other-content/42';
	
	$.get(ajaxURL, 
		function(data) 
		{
	
//	new Ajax.Request(ajaxURL, {
//	  method: 'get',
//	  	onSuccess: function( response )
//		{
			// Convert the response from JSON
			data = eval('(' + data.replace(/\n/g, '') + ')');

			// the html content
			var htmlContent = '<h3>Tell us your story - Guidelines</h3>';
			htmlContent += data.other_content.content;
			htmlContent += '<p><a href="#" onclick="tellYourStoryForm();">&laquo; Back</a></p>';

			// insert the content into the popup
			$('div#modal_window_ajax_content').html(htmlContent);
			
			// replace all headings
			Cufon.replace('h3');
			Cufon.now();
//		}
	});
}


/**
 * displays media for a specific story in the modal window
 *
 * @param int storyID the story ID to display
 * @return void
 * @author Tim Cromwell
 **/
function showMedia(storyID)
{
	// hide the popup
	hideModalWindow();
	
	// create the url required to retrieve ajax information
	var ajaxURL = '/ajax-data/story/' + storyID;
	
	$.get(ajaxURL, 
		function(data) 
		{
	
//	new Ajax.Request(ajaxURL, {
//	  method: 'get',
//	  	onSuccess: function( response )
//		{
			// Convert the response from JSON
			data = eval('(' + data.replace(/\n/g, '') + ')');

			// the html content
			var htmlContent = '<h3>' + data.story.title + '</h3>';
			htmlContent += '<div style="margin: 0 auto; width: 550px;">' + data.story.video_code + '</div>';

			// insert the content into the popup
			$('div#modal_window_ajax_content').html(htmlContent);
			
			// display the popup
			showModalWindow();
			
			// replace all headings
			Cufon.replace('h3');
			Cufon.now();
//		}
	});
}

