/**
*	By: Sumanth on 7th november, 08
*	File contains the functions used for promoted networks
*/
// JSON array of list of videos for promotional networks
var VIDEO_ARRAY = new Array();
//	Maximum number of videos per page on promote network widget for video stories
var VIDEO_MAX_LENGTH=3;
//	Page no. of promote network widget for video stories
var VIDEO_LIST_PAGE=0;
//	The page to start with for promote network widget for video stories
var startPage=0;
//	List the minimum of
var end=0;
//	Previously active story item(first time, it is the first one with unique_id = 0)
var previously_opened=0;
//	To play the video automatically
var play_video = 1;
/**
*	This function is to display the video story content in a new network promoted.
*	The contents: list of videos to be played on the promoted network page
*/
function showForumVideo(network_id){
	try{
	var videoListDisplayBox = "<div style='padding:0px;margin:0px'>";
	// The first video number of the current page
	var startPage = VIDEO_MAX_LENGTH * VIDEO_LIST_PAGE;
	var end = Math.min(startPage+VIDEO_MAX_LENGTH,VIDEO_ARRAY.length);
	// list of three videos of a particular page.
	for(i=startPage;i<end;i++){
		videoListDisplayBox += "<div id='videoToBePlayed"+i+"' style='margin:3px 0px 0px 0px;padding:0px;background-color:"+(i == previously_opened ? "#ffffff" : "#eeeeee")+";height:72px;'>"+
									"<div style='margin:0px;padding:5px 8px 0px 7px;float:left;'>"+
										"<table cellspacing='0' cellpadding='0' border='1' bordercolor='#6F6F6F'>"+
											"<tr>"+
												"<td>"+
													"<div align='left' style='padding:0px;margin:0px;position:relative;'>"+
														"<div id='videoImage"+i+"' align='center' style='padding:0px;margin:0px;'>"+
															"<span ><img src='"+S3_DYNAMIC_ASSETS_PATH+"/story-images/"+VIDEO_ARRAY[i]["id"]+"-73x73."+VIDEO_ARRAY[i]["image_ext"]+"' style='height:57px;width:75px;'/></span>"+
														"</div>"+
														"<div id='videoPlayButton"+i+"' style='display:"+(i == previously_opened ? "none" : "block")+";cursor:pointer;padding:0px;margin:0px;position:absolute;top:35%;left:38%;'>"+
															"<a href='javascript:void(0);' onclick=\"javascript:playVideoShowStory("+network_id+","+VIDEO_ARRAY[i]["id"]+","+i+",'"+VIDEO_ARRAY[i]["video_url"]["url"]+"');\" class='play-button'></a>"+
														"</div>"+
													"</div>"+
												"</td>"+
											"</tr>"+
										"</table>"+
									"</div>"+
									"<div align='left' style='margin:1px;padding:3px 0px 0px 0px;float:left;width:240px;font-size:12px;font-weight:bold;'>"+
										"<a href='javascript:void(0);' style='text-decoration:none;color:#2262AA;' onclick=\"javascript:playVideoShowStory("+network_id+","+VIDEO_ARRAY[i]["id"]+","+i+",'"+VIDEO_ARRAY[i]["video_url"]["url"]+"');\" onmouseover='this.style.textDecoration=\"underline\";'  onmouseout='this.style.textDecoration=\"none\";'>"+VIDEO_ARRAY[i]["title"]+"</a>"+
									"</div>"+
									"<br clear='all' />"+
								"</div>";
	}}catch(e){}
	videoListDisplayBox += "</div>";
	$('videoListDisplay').innerHTML = videoListDisplayBox;
	// navigations for the list of videos
	try{
	if(VIDEO_ARRAY.length > VIDEO_MAX_LENGTH){
		var videoPaginationInnerhtml = "<div style='margin:0px;padding:0px;'>";
		if(VIDEO_LIST_PAGE == 0){
			videoPaginationInnerhtml += "<div style='margin:0px;padding:4px 0px 0px 10px;float:left;'>"+
											"<span style='text-decoration:none;color:#868686' onclick='javascript: return false;'>&#60;&#32;Previous</span>"+
										"</div>";
		}else{
			videoPaginationInnerhtml += "<div style='margin:0px;padding:4px 0px 0px 10px;float:left;'>"+
											"<span style='text-decoration:none;cursor:pointer;color:#01519A;' onmouseover='this.style.textDecoration=\"underline\";' onmouseout='this.style.textDecoration=\"none\";' onclick='javascript: VIDEO_LIST_PAGE--;showForumVideo("+network_id+");return false;'>&#60;&#32;Previous</span>"+
										"</div>";
		}
		if(end < VIDEO_ARRAY.length){
			videoPaginationInnerhtml += "<div style='margin:0px;padding:4px 10px 0px 0px;float:right;'>"+
											"<span style='text-decoration:none;cursor:pointer;color:#01519A;' onmouseover='this.style.textDecoration=\"underline\";' onmouseout='this.style.textDecoration=\"none\";' onclick='javascript: VIDEO_LIST_PAGE++;showForumVideo("+network_id+");return false;'>Next&#32;&#62;</span>"+
										"</div>";
		}else{
			videoPaginationInnerhtml += "<div style='margin:0px;padding:4px 10px 0px 0px;float:right;'>"+
											"<span style='text-decoration:none;color:#868686' onclick='javascript:return false;'>Next&#32;&#62;</span>"+
										"</div>";
		}
		videoPaginationInnerhtml += "<br clear='all'/>"+
	                             "</div><br clear='all'/>";
		$('videoPagination').innerHTML = videoPaginationInnerhtml;
		refreshSnapshots();
	}
	}catch(e){}
}

/**
*	This fucntion is to fetch the video url to be played on click for a particular video story
*/
var playCurrentvideo = function(video_url){
	var currentPlayedInnerhtml ="<object align='center' height='246' width='300'><param name='movie' value='"+video_url+"'></param>"+
									"<param name='allowFullScreen' value='true'></param>"+
									"<embed src='"+video_url+"&autoplay="+play_video+"' type='application/x-shockwave-flash' wmode='transparent' allowfullscreen='true' align='center' style='width:300px;height:246px;'></embed>"+
								"</object>";
	$('videoBoxForForum').innerHTML = currentPlayedInnerhtml;
}
/**
*	This function is to paly the video and display the story information.
*	This also takes care of css changes of the video list.
*/
var playVideoShowStory = function(network_id, story_id, unique_id, video_url){
	if (previously_opened != unique_id) {
		try {
			//	Change the background color to white of the clicked div
			$('videoToBePlayed'+unique_id).style.backgroundColor = "#FFF";
			//	Hide the play button icon of the clicked div
			$('videoPlayButton'+unique_id).style.display = "none";
		} catch(e){}
		try {
			//	Change the background color to white of the clicked div
			$('videoToBePlayed'+previously_opened).style.backgroundColor = "#eeeeee";
			//	Hide the play button icon of the clicked div
			$('videoPlayButton'+previously_opened).style.display = "block";
		} catch(e){}
		//	To play the video on click
		playCurrentvideo(video_url);
		//	Widget from which the following function is called
		if(network_id !=0){
		var from = "pfn_widget";
			//	To display the video story info on click
			showPromotionalForumNetworkStory(network_id, story_id, from);
		}
		//	Now the recently activated story is made as the previous so that, it will be the previous by the next click event
		previously_opened = unique_id;
	}
}

/**
*	function for displaying story information of promotional forum network.
*/
//global array for storing promote forum network's displayed stories id.
var PREVIOUSLY_DISPLAYED_STORY_ID = 0;
function showPromotionalForumNetworkStory (network_id, story_id, from){
	if($('story_content_'+story_id+'') != null){
		//close all the previously opened story content box.
		$('story_content_'+PREVIOUSLY_DISPLAYED_STORY_ID).style.display = 'none';
		//show current story content box.
		$('story_content_'+story_id+'').style.display="block";
		PREVIOUSLY_DISPLAYED_STORY_ID = story_id;
	}else{
		//send request
		getPromotionalForumNetworkStoryInfo(network_id, story_id, from);
	}
}

/**
*	function for fetching story information of promotional forum network.
*/
var getPromotionalForumNetworkStoryInfo = function(network_id, story_id, from){
	//Hide the story div till the story is loaded
	$('promoteForumNetworkStoryContent').style.display='none';
	//show loading image
	$('promoteFNStoryLoadingIcon').style.display="block";
	var postParameters = "network_id="+network_id+"&story_id="+story_id+"&from="+from;
	var ajaxRequestOptions = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: postParameters,
		// Handle successful response
		onSuccess: function(response){getPromotionalForumNetworkStoryInfoCallback(response, story_id);},
		// Handle 404
		on404: getPromotionalForumNetworkStoryInfo404Callback,
		// Handle other errors
		onFailure: getPromotionalForumNetworkStoryInfoFailureCallback
	}
	new Ajax.Request('/get-promote-forum-network-stories', ajaxRequestOptions);
}
/**
 * fetching story information of promotional forum network callback
 */
var getPromotionalForumNetworkStoryInfoCallback = function (response, story_id){
	//show back the closed story div as soon as the data has come
	$('promoteForumNetworkStoryContent').style.display='block';
	//hide loading image
	$('promoteFNStoryLoadingIcon').style.display="none";
	//close all the previously opened story content box.
	if (PREVIOUSLY_DISPLAYED_STORY_ID != 0) {
		$('story_content_' + PREVIOUSLY_DISPLAYED_STORY_ID).style.display = 'none';
	}
	PREVIOUSLY_DISPLAYED_STORY_ID = story_id;
	//insert the response to box.
	var responseText = "<div id='story_content_"+story_id+"' style='padding:0px;margin:0px;'>"+response.responseText+"</div>";
	new Insertion.Bottom('promoteForumNetworkStoryContent',responseText);
}
/**
 * fetching story information of promotional forum network failure
 */
var getPromotionalForumNetworkStoryInfoFailureCallback = function (){
	alert("Error on page");
}
/**
 * fetching story information of promotional forum network 404
 */
var getPromotionalForumNetworkStoryInfo404Callback= function (){
	alert("Error page is not found");
}

/**
*	Mininmized the forum news network and store the cookies information
*/
function minimizedPromoteForumNetworkBox(){
	$('promoteForumNetworkWidgetMinimizedBox').innerHTML = "<a style='cursor:pointer' href='' onclick='javascript: maximizedPromoteForumNetworkBox();return false;' class='maximized-icon'></a>";
	$('promoteForumNetworkWidgetStoryContainerBox').style.display = 'none';
	setCookie('pfn_minimized', 1, 30);
}

/**
*	Maximized the forum news network and store the cookies information
*/
function maximizedPromoteForumNetworkBox(){
	$('promoteForumNetworkWidgetMinimizedBox').innerHTML = "<a style='cursor:pointer' href='' onclick='javascript: minimizedPromoteForumNetworkBox();return false;' class='minimized-icon'></a>";
	$('promoteForumNetworkWidgetStoryContainerBox').style.display = 'block';
	setCookie('pfn_minimized', 0, 30);
}
