/** 
* @projectDescription France 2 & France 3, versions 2.0
*
* @author	Mururoa Project Team
* @version	0.1 
*/


// ---------------------------------------------------------------------------------------------- \\
// Jquery init event
// ---------------------------------------------------------------------------------------------- \\
$(document).ready(function()
{
	/* Start Common Functions */
	handleMenu("#eMainMenuLine", ".eSMenu");
	
	searchengine();
	iniContentsListeners();
	handleFocusedInputs("#searchform");
	cleanAds();
	deleteCookies();
	/* End Common Functions */


});


$(window).load(function()
{	
	cleanFlashLayers("#adVdoBlk");
	fixDisappearingAdTop();
	moveAd("#adSky01", "#adSkyscrapper01 .adCtnt");
	moveAd("#adTop01", "#eAdBlkLargeBanner .adCtnt");
	moveAd("#adMiddle01", "#adVdoBlk .ctnt");
	moveAd("#adBottomRight", "#ad_links_block_01 .ctnt");
});


function iniContentsListeners(context)
{
	handleVideoLinks(context);
	handleTargetBlank(context);
}



// ----------------------------------------------------- \\
// This function MUSTN'T be called on page ready or load
// event.
// ----------------------------------------------------- \\
function initAjaxNewContentsHandlers(context)
{
	iniContentsListeners(context);
	iniContentsListenersSpecific(context);
}



// ----------------------------------------------------- \\
// This function has to (and will) be overridden in the 
// page specific js files
// ----------------------------------------------------- \\
function iniContentsListenersSpecific()
{
	// none
}



// Default Vars
var alreadyLoadedSMenuProg = false;
var loadingSMenuProg = false;
var reqLoadingSMenuProg;
var alreadyLoadedSMenuReg = true;
var loadingSMenuReg = true;
var reqLoadingSMenuReg;
var reqLoadingTooltip = false;
var userAcceptCookies = true;

var ajaxLoadingPixSRC = preload(ajaxLoadingPixPath);
var ajaxLoadingPix = '<img class="throbber" src="' + ajaxLoadingPixSRC + '" alt="Pictogramme : chargement en cours" />';
var ajaxLoadingMsg = '<span class="ajaxRequesting">' + ajaxLoadingPix + '<span> Chargement en cours</span></span>';

var isTopShowAnimRunning = false;
var isFlashNewsAnimRunning = false;

var posSMenuPrograms = (ftvi_portail == "france3" ) ? 68  : -6;

var alreadyLoadedAreaMap = false;

var userScreenW = screen.width;
var isScreenWgte1024 = ( !isNaN(userScreenW) && userScreenW >= 1280) ? true : false;
//alert(userScreenW + ", " + isScreenWgte1024);


// ----------------------------------------------------- \\
// This function allows you to preload the element whose
// src is given in parameter
// ----------------------------------------------------- \\
function preload(eltSRC)
{
	$("<img>").attr("src", eltSRC);	
	return eltSRC;
}



// ----------------------------------------------------- \\
// This function does not do anything
// ----------------------------------------------------- \\
function fakeFunction()
{
	// none
}




// ----------------------------------------------------- \\
// This function removes the element whose selector is
// given in parameter
// ----------------------------------------------------- \\
function kill(selector)
{
	$(selector).remove();
}



// ----------------------------------------------------- \\
// This function fix disappearing Top Ad (didn't worked
// with just css
// ----------------------------------------------------- \\
function fixDisappearingAdTop()
{
	// If Browser is Internet Explorer
	if($.browser.msie)
	{
		$("#eAdBlkLargeBanner").css("zoom","1");
	}
}



// ----------------------------------------------------- \\
// This function removes unwanted elements in pub
// ----------------------------------------------------- \\
function cleanAds()
{
	// If Browser is Internet Explorer & version < 7
	if($.browser.msie && jQuery.browser.version < 7)
	{
		$("#adVdoBlk").find("br").remove();
		$("#adVdoBlk").find("img[src*=vide.gif]").remove();
		$("#adVdoBlk").find("img[src*=pub.gif]").remove()
	}
}



// ----------------------------------------------------- \\
// This function clean the flash object that are not
// correctly formed to be fully cross-browser proof
// (Especialy on Firefox in which they could be not
// transparent when required
// ----------------------------------------------------- \\
function cleanFlashLayers(ctnrSelector)
{	
	var flashObjNb = $(ctnrSelector + " object").length;
	
	// Si on a au moins un Object
	if(flashObjNb >= 1)
	{
		$(ctnrSelector + " object param[name*=wmode]").attr("value","transparent"); // Fix no "transparent" value on wmode param
		
		// If Browser is not Internet Explorer
		if(!$.browser.msie)
		{
			// Remove "embed" tags since they prevent Flash elements
			// to be overlayed by absolute positioned elements
			var jqobj = $(".cmnBlk.adVdoBlk object embed");
			if (jqobj.length >= 1)
			{
			 var src = jqobj.attr("src");
			 var w = jqobj.attr("width");
			 var h = jqobj.attr("height");
			 var newobj = '<!--[if !IE]><--><object data="' + src + '" type="application/x-shockwave-flash" width="' + w + '" height="' + h + '"><param name="wmode" value="transparent" /></object><!--><![endif]-->';
			 jqobj.remove();
			 $(ctnrSelector + " object").append(newobj);
			}
		}
	}
}




// ----------------------------------------------------- \\
// This functions handle VideoBlocks and its content items
// changing behaviors
// ----------------------------------------------------- \\
function handleVDOBlockScroller(parentSelector)
{
	var dataSelector = parentSelector + " .vdoItemsData";
	
	$(parentSelector + " .jsJump2PreviousItem").click(function()
	{
		var firstItem = $(parentSelector + " .vdoItemsData:first");
		if ( firstItem.hasClass("current") == true ) // If the first item is the current one, the "previous" item must be the last one
		{
			firstItem.removeClass("current");
			$(dataSelector + ":last").addClass("current");
		}
		else
		{
			$(dataSelector + ".current").not(dataSelector + ":first").removeClass("current").prev().addClass("current");
		}
		updateCtntPix(parentSelector);
	});
	
	$(parentSelector + " .jsJump2NextItem").click(function()
	{
		var lastItem = $(parentSelector + " .vdoItemsData:last");
		if ( lastItem.hasClass("current") == true ) // If the last item is the current one, the "next" item must be the first one
		{
			lastItem.removeClass("current");
			$(dataSelector + ":first").addClass("current");
		}
		else
		{
			$(dataSelector + ".current").not(dataSelector + ":last").removeClass("current").next().addClass("current");
		}
		updateCtntPix(parentSelector);
	});
	
	function updateCtntPix(parentSelector)
	{
		var jqCtntPix = $(parentSelector + " .pxBlk img.ctntPx");
		jqCtntPix = jqCtntPix.attr("src",PixURL); // PixURL is defined in conf.js		
		var newPixSRC = $(parentSelector + " .vdoItemsData.current .itemSTitle").attr("title");
		
		if (newPixSRC.indexOf(".JPG") > -1){ var ext = ".JPG"; }
		else { var ext = ".jpg"; }

		newPixSRC = (newPixSRC === undefined) ? whitePixURL : newPixSRC.substring(0,newPixSRC.indexOf(ext)) + "_192_144" + ext; // whitePixURL is defined in conf.js
		jqCtntPix.attr("src",newPixSRC);
		
		updateVdoLnk(parentSelector); // Then updates the video link
	}
	
	function updateVdoLnk(parentSelector)
	{
		var newVidHref = $(parentSelector + " .vdoItemsData.current a").attr("href");
		$(parentSelector + " .pxBlk .targBlank").attr("href", newVidHref);
	}
}




// ----------------------------------------------------- \\
// This function handles Video Links. Gets its id then
// call the function to submit the related form
// ----------------------------------------------------- \\
function handleVideoLinks()
{
	$(".handleVdoLnk").click(function()
	{
		var vdoID = $(this).attr("id");
		vdoID = vdoID.replace(/\D/g,''); // Removes every no-numeric characters from the string
		submitVdoRelatedForm(vdoID);
		return false;
	});
}



// ----------------------------------------------------- \\
// This function submits the form related to the Video 
// Link whose id is given as the paramater
// ----------------------------------------------------- \\
function submitVdoRelatedForm(vdoID)
{
	var itemCtnr = $(".vdoItemsData[id=videoToCall_" + vdoID +"]");
	itemCtnr.children("form").attr("onsubmit","return false;").attr("target","myPopUp");
	var tmp = itemCtnr.children("form").attr("action");
	// VdoRelatedBaseURL is defined in conf.js
	var url = (tmp.indexOf(VdoRelatedBaseURL)) ? VdoRelatedBaseURL + tmp : tmp;
	itemCtnr.children("form").attr("action", url);
	itemCtnr.find("form input[name=courant]").attr("value", vdoID);
	
	window.open(url,'myPopUp','width=634,height=469,status=no,location=no,toolbar=no,scrollbars=auto,menubar=no,resizable=yes');
	itemCtnr.children("form").trigger("submit");
}



// ----------------------------------------------------- \\
// This function deletes cookies
// ----------------------------------------------------- \\
function deleteCookies()
{
	$("#resetMyPref").click( function()
	{
		$.cookie('ftvi_f2_showTooltipSortableBlocks', null);
		$.cookie('ftvi_myregion', null);
		
		// Prevent default action (href, action,...)
		return false;
	});
}



// ----------------------------------------------------- \\
// This function gets, in an URL, the value of the param
// given in the function call
// ----------------------------------------------------- \\
function getURLParamValue(requestedURL, requestedParamName)
{
	var pos = requestedURL.indexOf(requestedParamName); // gets start position of the param
	
	if (pos != -1 & requestedParamName != "")
	{
		var tmp = requestedURL.substr(pos); // truncates the string from this position to its end
		var start = pos + requestedParamName.length; // gets the start position of the param value
		
		// Gets end position of the param value
		var end_pos;
		
			if (tmp.indexOf("&amp;") != -1) { end_pos = tmp.indexOf("&amp;"); } // case where there are others params after, separate by "&amp;"
			else if (tmp.indexOf("&") != -1 ) { end_pos = tmp.indexOf("&"); } // case where there are others params after, separate by "&"
			else if (tmp.indexOf("#") != -1 ) { end_pos = tmp.indexOf("#"); } // case where there are others params after, separate by "#"
			else { end_pos = tmp.length; } // case where there are no others params after
		
		var requestedParamValue = tmp.substring(requestedParamName.length + 1,end_pos); // truncates the string from 0 to end of the param value
		
		return requestedParamValue;
	}
	else
	{
		return false;
	}
}

// ----------------------------------------------------- \\
// This function launches the search engine
// ----------------------------------------------------- \\
function searchengine()
{
	$("#searchform").submit(function()
	{	
		a = document.forms['searchform'];
	
		if (a.eSearchInOpt01.checked==true)
		{
			a.action = 'http://recherche.'+ftvi_portail+'.fr/cgi-bin/findall';
			a.target = '_top';
			
			a.SITERUBRIQUE.value = "ACCUEIL";
			if (OAS_sitepage.indexOf("guide")!=-1)		a.SITERUBRIQUE.value = "GUIDE";
			if (OAS_sitepage.indexOf("programmes")!=-1)	a.SITERUBRIQUE.value = "PROGRAMMES";
			if (OAS_sitepage.indexOf("video")!=-1)		a.SITERUBRIQUE.value = "VIDEO";
			if (OAS_sitepage.indexOf("sport")!=-1) 		a.SITERUBRIQUE.value = "SPORT";
			if (OAS_sitepage.indexOf("jeux")!=-1)		a.SITERUBRIQUE.value = "JEUX";
			if (OAS_sitepage.indexOf("sorties")!=-1)	a.SITERUBRIQUE.value = "SORTIES";
			if (OAS_sitepage.indexOf("france4")!=-1)	a.SITERUBRIQUE.value = "FRANCE4";
			
			a.REGION.value = '';
			
			return true;
		}
		else if (a.eSearchInOpt02.checked==true)
		{
			a.action = 'http://recherche-yahoo.'+ftvi_portail+'.fr/index-fr.php';
			a.target = '_blank';
			a.p.value = a.KEYWORDS.value;
			return true;
		}
		else
		{
			return false;
		}
	});
}



// ----------------------------------------------------- \\
// This function moves ads after they've been called (in
// order to load them at the end of the page to avoid
// conflicts between badly coded ad scripts and our 
// clean non obtrusive javascript
// ----------------------------------------------------- \\
function moveAd(adContentSelector, adMove2Selector)
{
	var jqobj = $(adContentSelector);

	if (jqobj.length > 0)
	{
		// Moves the loaded ad to its "normal" container
		var tmp = jqobj.contents().not("script").appendTo(adMove2Selector);
		
		// If the browser is Internet Explore, fixes disappearing element
		if($.browser.msie){
			$(adMove2Selector).css("visibility", "visible");
			$("#eAdBlkLargeBanner, #adSkyscrapper01, #adSkyscrapper02").css("zoom","1");
			}

		// Removes contents juste moved to avoid having ads twice
		jqobj.contents().not("script").remove();
	}	
}

	
	// affiche une DIV
	function affiche(fiche) {
	document.getElementById('affiche').innerHTML = document.getElementById(fiche).innerHTML;
	}

