/** 
* @alias design_behaviors.js
* @projectDescription France 2 & France 3, versions 2.0
* @author	[FTVI] Guyllaume Doyer
*/

/* !!! DO NOT MODIFY FUNCTIONS ORDER !! */
/* (required to pass jslint validation  */

/* handleTargetBlank(),handleCustomPopup(),handlePrintLinks(),handleFontResizR(),
 * buildRndSimpleCorners(),
 * handleAccordions(),handleLightAccordions(),
 * handleFocusedInputs(),handlePixLegendDimensions(),
 * cleanArticle(),
 * sortableOnItsStrate(),sortableOnItsCol(),
 * finishDesign(),
 * handleEegg() */


var userScreenW = screen.width;
var isScreenWgte1024 = ( !isNaN(userScreenW) && userScreenW >= 1280) ? true : false;
var BrowserIsLtIE7 = (jQuery.browser.msie && jQuery.browser.version < 7) ? true : false;



// ----------------------------------------------------- \\
// This function emulates target="_blank"
// context param is optional
// ----------------------------------------------------- \\
function handleTargetBlank(context)
{
	$("a.targBlank, area.targBlank", context).unbind('click.targBlank').bind('click.targBlank',function() { window.open( $(this).attr("href") ,'',''); return false; });
}


// ----------------------------------------------------- \\
// This function creates pop-ups with dimensions
// in rel attribute
// ----------------------------------------------------- \\
function handleCustomPopup(context)
{
	$("a.customPopup", context).click(function() {
		var dimsBrut = $(this).attr("rel");
		var dims = dimsBrut.split('£');
		window.open( $(this).attr("href") ,'','width='+dims[0]+',height='+dims[1]); return false;
	});
}


// ----------------------------------------------------- \\
// This function handles "print" links
// context param is optional
// ----------------------------------------------------- \\
function handlePrintLinks(context)
{
	$(".printLnk", context).click(function() { window.print(); return false; });
}




// ----------------------------------------------------- \\
// This function handles font-resizing (increase, decrease,
// reset to default)
// ----------------------------------------------------- \\
function handleFontResizR(ctnrSelector)
{
	
	function getCurrentFontSize()
	{
		// Until Jquery fixed its bug on the .css("font-size") returned values
		// we have to get the font-size with the getStyle function above
		// for Gecko browsers
		var tmp_currentFontSize;
		if ( $.browser.safari)
		{
			tmp_currentFontSize = $("body").css("font-size");
		}
		else
		{
			var tmpojb = document.getElementsByTagName("body")[0];
			tmp_currentFontSize = getStyle(tmpojb);
		}
		
		// If the browser "translate" font-size from % to px like Firefox and Safari do
		if ( tmp_currentFontSize.indexOf("px") >= 0 ){ tmp_currentFontSize = parseFloat(tmp_currentFontSize) * defaultFontUnitRatio; }
		
		var currentFontSize = parseFloat(tmp_currentFontSize);
		
		return currentFontSize;
	}
	
	var defaultFontUnitRatio = (100 / 16); // 100 / default browser font-size (16px) : 1px => x%
	var defaultFontSize = getCurrentFontSize();
	
	/* This function will be no longer required when jquery team will have fix the probleme
	 * with .css() method with values whose unit is different than px
	 */
	function getStyle(x)
	{
		var y;
		if (x.currentStyle) { y = x.currentStyle.fontSize; }
		else if (window.getComputedStyle) { y = window.getComputedStyle(x,null).fontSize; }
		return y;
	}
	
	// Increase font-size
	$(ctnrSelector + " .fontBigger").click( function()
	{
		var currentFontSize = getCurrentFontSize();
		var newFontSize = currentFontSize + defaultFontUnitRatio + "%";
		
		$("body").css("font-size", newFontSize);
	} );


	// Decrease font-size
	$(ctnrSelector + " .fontSmaller").click( function()
	{
		var currentFontSize = getCurrentFontSize();
		var newFontSize = currentFontSize - defaultFontUnitRatio + "%";
		
		$("body").css("font-size", newFontSize);
	} );
	
	
	// Reset font-size to default value
	$(ctnrSelector + " .fontDefault").click( function()
	{
		var newFontSize = defaultFontSize + "%";
		$("body").css("font-size", newFontSize);
	});
}




// ----------------------------------------------------- \\
// This function builds simple round courners (1px) on 
// matched elements
// ----------------------------------------------------- \\
function buildRndSimpleCorners()
{	
	var before = '<span class="top"><span class="right">&nbsp;</span><span class="left">&nbsp;</span></span>';
	var after = '<span class="bot"><span class="right">&nbsp;</span><span class="left">&nbsp;</span></span>';

	$(".jsMadeRndCrnrs").prepend(before).append(after);
}




// ----------------------------------------------------- \\
// This function hides by default each element of an 
// accordion menu and listens to the click on these items, 
// then triggers an accordion effect (expand/collpse)
// ----------------------------------------------------- \\ 
function handleAccordions(parentSpecificSelector)
{
	$(parentSpecificSelector + " .accordionBlk dt").not(".alwaysOpened, a.enabledLink").click(function()
	{
		$(parentSpecificSelector + " .accordionBlk dt, " + parentSpecificSelector + " .accordionBlk dd").removeClass("expanded");
		$(this).addClass("expanded").next("dd").addClass("expanded");
		
		return false;
	});
}




// ----------------------------------------------------- \\
// This function hides by default each element of an 
// accordion menu and listens to the click on these items, 
// then triggers an accordion effect (expand/collpse)
// ----------------------------------------------------- \\ 
function handleLightAccordions(parentSpecificSelector)
{
	$(parentSpecificSelector + " .accordionBlk.lightVersion dt").click(function()
	{
		$(parentSpecificSelector + " .accordionBlk.lightVersion dd," + parentSpecificSelector + " .accordionBlk.lightVersion dt").removeClass("expanded");
		$(this).addClass("expanded").next("dd").addClass("expanded");
		
		return false;
	});
}



// ----------------------------------------------------- \\
// This function resets the value of each matched element 
// on focus event and add/remove a class when focused
// ----------------------------------------------------- \\
function handleFocusedInputs(ctnrSelector)
{	
	var iniValue = "";
	iniValue = $(ctnrSelector + " .js2resetValue").attr("value");

	$(ctnrSelector + " .js2resetValue").focus( function()
	{
		$(this).attr("value", ""); // resets value attribute
		$("input").parent(".focusedField").removeClass("focusedField"); // forces removing of the ".focusedField" class on other inputs
		$("input").focus( function() { this.hideFocus = true; } );
		$(this).parent(".fieldCtnr").addClass("focusedField");
	});
	$(ctnrSelector + " .js2resetValue").blur( function()
	{
		if ( typeof($(this).attr("value")) === "undefined" || $(this).attr("value") == iniValue ) { $(this).attr("value", iniValue); }
		$("input").parent(".fieldCtnr").removeClass("focusedField");
	});
}




// ----------------------------------------------------- \\
// This function sets the sizes of the legend block to
// fits to its related pics
// ----------------------------------------------------- \\
function handlePixLegendDimensions(selector)
{
	$(selector + " .pxBlk").each(function()
	{
		var pix = $(this).children("img.ctntPx");
		var pixW = pix.width() + parseInt(pix.css("border-left-width"), 10) + parseInt(pix.css("border-right-width"), 10) + parseInt(pix.css("padding-left"), 10) + parseInt(pix.css("padding-right"), 10);
		var legend = $(this).children(".legend");
		var legendExtradim = parseInt(legend.css("padding-left"), 10) + parseInt(legend.css("padding-right"), 10);
		legend.width(pixW - legendExtradim);
	});
}




// ----------------------------------------------------- \\
// This function performs some modifications on the
// rendered content of the article (removing spacers, fixing
// page top anchors, etc ...)
// ----------------------------------------------------- \\
function cleanArticle()
{
	var context = "#articleBlk01";
	
	// Remov table lines containing a unique IMG element whose src contains vide.gif
	$("img[src*=vide.gif]", context).parent("td").parent("tr").each(function() { if ( $(this).children("td").length == 1 ) { $(this).remove(); } });
	
	// Set bottom margin to 0 for pics whose src contains some specific values		
	$("img[src*=f_haut.gif], img[src*=logo_f3_petit.gif], img[src*=logo_f2_petit.gif], img[src*=picto_camera.gif]", context).css("margin-bottom","0");
	
	// Fix "go to page top" anchors
	$("a[href=#TOP]", context).attr("href","#ePageTop");

	// Puts color on some tables
	$("table.ePFondCnt2").parent("td").addClass("encadre");//background", "#FEE3E4");

}




// ----------------------------------------------------- \\
// This function lets you sort your floated block
// into their strate
// ----------------------------------------------------- \\
function sortableOnItsStrate()
{
	$(".strate").sortable(
	{
		items : "sortableOnItsStrate",
		zIndex : 99999,
		containment : "document",
		hoverClass: "sortableOnItsColHover",
		helper: "sortableOnItsColHelp"
	}
	);
}




// ----------------------------------------------------- \\
// This function lets you sort your floated block
// into their col
// ----------------------------------------------------- \\
function sortableOnItsCol(disabled)
{

	if (typeof(destroy) !== "undefined" && disabled === false)
	{
		sortableDestroy();
	}
	else
	{
		$(".SortableContentCol").sortable(
		{
			items : ".sortableOnItsCol",
			zIndex : 99999,
			containment : "document",
			hoverClass: "sortableOnItsColHover",
			helper: "sortableOnItsColHelp"
		});
		
	
		$(".sortableOnItsCol .sortMover").mouseover( function(){ $(this).css("cursor", "move"); } ); // force cursor
		$(".sortableOnItsCol input").click( function(){ $("input").blur(); $(this).focus(); } ); // force focus
	}

}




// ----------------------------------------------------- \\
// This function finishes design elements
// ----------------------------------------------------- \\
function finishDesign()
{
	$('.vdoItemsData form').parent('.vdoItemsData').addClass('accessib');
}



// ----------------------------------------------------- \\
// This function finishes design elements only on IE6
// ----------------------------------------------------- \\
function finishDesignIE6()
{
	$('#sondage *, .sMenu .eMenuLv03Blk').css({zoom:1});
	
}





// ----------------------------------------------------- \\
// This function handles Eegg
// ----------------------------------------------------- \\
function handleEegg()
{
	var eEgg = 
	{

		listen: function()
		{
			
			var keyVal = $("input#KEYWORDS", $(this)).val();
			
			if ( keyVal == ":egg-sortable" ) { eEgg.makeSortable(); return true; }
			else if ( keyVal == ":egg-roundcorners" ) { eEgg.roundCorners(); return true; }
			else if ( keyVal == ":egg-noads" ) { eEgg.noAds(); return true; }
			  else if ( keyVal == ":egg-ads" ) { eEgg.ads(); return true; }
			else if ( keyVal == ":egg-accessibar" ) { eEgg.showAccessibar(); return true; }
			  else if ( keyVal == ":egg-noaccessibar" ) { eEgg.hideAccessibar(); return true; }
			else if ( keyVal == ":egg-team" ) { eEgg.team(); return true; }
			else if ( keyVal == ":egg-reset" ) { eEgg.reset(); return true; }
			else if ( keyVal == ":egg" ) { eEgg.doAll(); return true; }
			
			else
			{
				return false;
			}
			
		},
		
		doAll: function() { eEgg.roundCorners().makeSortable().showAccessibar().noAds().team(); return this; },
		makeSortable: function() { sortableOnItsCol(); return this; },
		roundCorners: function() { buildRndSimpleCorners(); return this; },
		noAds: function() { $(".eAdBlk").hide(); return this;} ,
		  ads: function() { $(".eAdBlk").show(); return this; },
		team: function() { /* TO DO */ return this; },
		showAccessibar: function() { $("#eAccessibar").show(); return this; },
		  hideAccessibar: function() { $("#eAccessibar").hide(); return this; },
		reset: function() { /* TO DO */ return this; }

	};

	
	return eEgg.listen();
		
}

