
// Fonction d'affiche pour différents blocs
// Bloc d'outils dans le menu
var displayTools = function(oEventsManager) {
	displayBloc(oEventsManager, ".widget", ".tools");
}

var displayLanguage = function(oEventsManager) {
	displayBloc(oEventsManager, ".right_content .fl .language", ".others");
}

var displayPro = function(oEventsManager) {
	displayBloc(oEventsManager, ".right_content .fl .grey_link", ".bubble_links");
}

var displayBloc = function(oEventsManager, parentSelector, childSelector)
{
	var aParent = $$(parentSelector);
	if ( aParent && aParent.length > 0)
	{
		var timer;
		var delay_time = 500;
		var hideElement = function(){
			this.setStyle('display', 'none');
		};
		aParent.each(function(elem)
		{
			var oChild = elem.getElement(childSelector);
			if ( oChild )
			{
				elem.addEvent('mouseenter',
					function()
					{
						$clear(timer);
						oChild.setStyle('display', 'block');
					}
				);
				elem.addEvent('mouseleave', function(){
					timer = hideElement.delay(delay_time, oChild);
				});

				oChild.addEvent('mouseenter',
					function()
					{
						$clear(timer);
					}
				);
				oChild.addEvent('mouseleave',
					function()
					{
						timer = hideElement.delay(delay_time, oChild);
					}
				);
			}
		});
	}
	oEventsManager.fireEvent('nextStep');
}

// Fonction pour la gestion du sous menu
var displayMenu = function(oEventsManager)
{
	var aTimer = new Array();
	var timer;
	var timer2;
	var delay_time = 100;
	var hideElement = function(){
		this.setStyle('display', 'none');
	};
	var changeBack = function(){
		this.setStyle('backgroundColor', 'transparent');
		if($("actual")) $("actual").setStyle('backgroundColor', '#147b98');
	};

	var aToggler = $$(".sous_menu .sub");
	if (aToggler.length > 0)
	{
		aToggler.each(
			function(elem, index)
			{
				aTimer[index] = new Array(1, 2);
				var oContent = elem.getNext(".submenu");
				
				elem.addEvent('mouseenter',
					function()
					{
						$clear(aTimer[index][2]);
						aToggler.setStyle('backgroundColor', 'transparent');
						if($("actual")) $("actual").setStyle('backgroundColor', '#147b98');
						elem.setStyle('backgroundColor', '#147b98');
					}
				);
				elem.addEvent('mouseleave', function(){
					aTimer[index][2] = changeBack.delay(delay_time, elem);
				});

				if ( oContent )
				{
					oContent.addEvents({
						'mouseenter' :
						function()
						{
							$clear(aTimer[index][1]);
							$clear(aTimer[index][2]);
						},
						'mouseleave':
						function()
						{
							aTimer[index][1] = hideElement.delay(delay_time, oContent);
							aTimer[index][2] = changeBack.delay(delay_time, elem);
						}
					});

					elem.addEvent('mouseenter',
						function()
						{
							$clear(aTimer[index][1]);
							oContent.setStyle('display', 'none');
							oContent.setStyle('display', 'block');
						}
					);
					elem.addEvent('mouseleave', function(){
						aTimer[index][1] = hideElement.delay(delay_time, oContent);
					});

				}
			}
		);
	}
	oEventsManager.fireEvent('nextStep');
}

var footerMenu = function(oEventsManager)
{
	$$('.footer .right_content div.fl').each(function(el){

		var img = el.getChildren('img');
		if ( img && img.get('src') == 'img/global/grey_right_side.gif')
		{
			el.addEvents({
				'mouseenter' : function() { img.set('src','./img/global/grey_right_side_on.gif'); },
				'mouseleave' : function() {	img.set('src','./img/global/grey_right_side.gif'); }
			});
		}
	});
	oEventsManager.fireEvent('nextStep');
}

/**
 * fulltext search action
 *
 */
var fullTextSearch = function( oEventsManager )
{
	// search engine
	if( $('fulltext_search_form') )
	{
		var fulltext_keyword = $('search_text').value;
		
		$('search_text').addEvent('focus', function(e){
			if( this.value == fulltext_keyword )
				this.value = '';	
		});
		
		$('search_text').addEvent('blur', function(e){
			if( this.value == '' )
				this.value = fulltext_keyword;	
		});
		
		$('fulltext_search_button').addEvent( 'click', function(){
			$('fulltext_search_form').submit();
		});
	}
	oEventsManager.fireEvent('nextStep');
}

/**
 * header loading
 *
 */
var headerLoad = function( oEventsManager )
{
	if ( !bSteno )
	{
		var flashvars, sID, sSWF;
		if ( $chk(bHome) )
		{
			if( $chk( sXMLPath ) )
				flashvars = { source : sXMLPath };
			else
				flashvars = { source : 'xml/diaporama-fr.xml' };

			sID = "bg_flash_object";
			sSWF = "flash/header_home.swf"
		}
		else
		{
			flashvars = { defaultBackgound : sURLTetiere, logoUrl : sURLHome};
			sID = "bg_flash_object";
			sSWF = "flash/header_content.swf"
		}

		var params = {
			menu: "false",
			scale: "noScale",
			allowFullscreen: "true",
			allowScriptAccess: "always",
			bgcolor: "#FFFFFF",
			wmode: "transparent"
		};
		var attributes = {
			id: sID
		};

		swfobject.embedSWF(sSWF, "bg_flash_container", "100%", "100%", "9.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
	}
	
	oEventsManager.fireEvent('nextStep');
}


oEventsManager.registerEvent('displayLanguage', displayLanguage);
oEventsManager.registerEvent('displayTools', displayTools);
oEventsManager.registerEvent('displayPro', displayPro);
oEventsManager.registerEvent('displayMenu', displayMenu);
oEventsManager.registerEvent('footerMenu', footerMenu);
oEventsManager.registerEvent('fullTextSearch', fullTextSearch);
oEventsManager.registerEvent('headerLoad', headerLoad);


/**
 * font
 *
 */
var iFontSize = 1;
var iFontSizeMax = 1.4;
var iFontSizeMin = 0.5;
function setFontSmaller()
{
	iFontSize = iFontSize - 0.1;
	if (iFontSize < iFontSizeMin)
		iFontSize = iFontSizeMin;
	$$('.content_page').setStyle('font-size', iFontSize + 'em');
}
function setFontBigger()
{
	iFontSize = iFontSize + 0.1;
	if (iFontSize > iFontSizeMax)
		iFontSize = iFontSizeMax;
	$$('.content_page').setStyle('font-size', iFontSize + 'em');
}

var oFavorites;
function add_selection( sLink, sName, sType, sId, sRubrique)
{
	var bReturn = false;
	if ( !oFavorites )
		oFavorites = new FavoriCart("FavCart", {display_number : -3, cookie_name:sCookie_name});
	
	bReturn = oFavorites.add_fav(sLink, sName, sType, sId, sRubrique);
	if($("fav")) oFavorites.display_list();
	return bReturn;
}

function remove_selection(sName, sId)
{
	var bReturn = false;
	if ( !oFavorites )
		oFavorites = new FavoriCart("FavCart", {display_number : -3, cookie_name:sCookie_name});
	bReturn = oFavorites.remove_fav(sName);
	
	if(bReturn == true && sId != undefined) {
		var elem = $(sId);

		if(elem) {
			var parent = elem.getParent('div.favorites');
			elem.dispose();
			
			if (parent && parent.getElements("div").length == 0)
				parent.dispose();
		}
	}
	if($("fav")) oFavorites.display_list();
	return bReturn;
}
