//Initialize EventManager object to control JS loading synchro
var oEventsManager = new EventsManager( {iDelayTimeOut: 30000} );

var fontManager = function() {
	// Gestion font
	if( $('smallerFont') )
	{
		$('smallerFont').addEvent('click', setFontSmaller);
		$('biggerFont').addEvent('click', setFontBigger);
	}

	oEventsManager.fireEvent('nextStep');
}

var weatherManager = function() {
	if( $('days') )
	{
		$('weather').addEvent('mouseover', function() {
			$('days').setStyle('display', 'block');
			$$('.raining a').addClass('active');
		});
		$('days').addEvent('mouseleave', function() {
			$('days').setStyle('display', 'none');
			$$('.raining a').removeClass('active');
		});
	}

	oEventsManager.fireEvent('nextStep');
}

var iProSelectedId = 0;
var iProIntervalId;
var nodeproManager = function() {
	if( $$('.node_pro .slideshow .prev').length )
	{
		var aLinks = $$('.node_pro .element');

		$$('.node_pro .slideshow .prev').addEvent('click', function()
		{
			$clear(iProIntervalId);

			prevPro(aLinks);

			iProIntervalId = nextPro.periodical( 5000, null, [aLinks]);
		});

		$$('.node_pro .slideshow .next').addEvent('click', function()
		{
			$clear(iProIntervalId);

			nextPro(aLinks);

			iProIntervalId = nextPro.periodical( 5000, null, [aLinks]);
		});

		iProIntervalId = nextPro.periodical( 5000, null, [aLinks]);
	}

	oEventsManager.fireEvent('nextStep');
}

var flashDiapoManager = function()
{
	// set center pict with all picture of diaporama
	var aDiapoIMG = $$('.diaporama img');
	aDiapoIMG.each( function( elem, index )
	{
		var oSize = elem.getSize();
		var iWidth = oSize.x;

		elem.setStyle('margin-left', '-'+(iWidth/2)+'px');
	});

	// use for not display picture in the middle of head during load
	$$('.diaporama').setStyle( 'opacity', 1 );

	if( $$('.info_diapo li a').length > 1 )
	{
		$$('.diaporama img').setStyle('opacity', 0);
		$$('.diaporama img')[0].setStyle('opacity', 1);
		var aSelectors = $$('.info_diapo li a');
		var aDiapos = $$('.diaporama img');
		var aLegend = $$('.info_diapo p');

		aSelectors.each(function(elem, index)
		{
			elem.addEvent('click', function()
			{
				nextDiapo(aSelectors, aDiapos, aLegend, index);
			});
		});
	}

	if( $('flash_map') )
	{
		var params = {
			menu: "false",
			scale: "noScale",
			wmode: "transparent",
			allowFullscreen: "true",
			allowScriptAccess: "always",
			bgcolor: "#e7d6c2"
		};

		var flashvars = {};

		swfobject.embedSWF("swf/carte.swf", "flash_map", "338", "240", "10.0", "swf/expressInstall.swf", flashvars, params, {name:"flashContent"});
	}
	else
		NextImage();

	oEventsManager.fireEvent('nextStep');
}

var manageInput = function()
{
	var aValueDefault = Array();
	$$('input[type=text], textarea').each( function(oElem, iIndex) {
		aValueDefault[iIndex] = oElem.value;
		oElem.addEvent('focus', function(){
			if(aValueDefault[iIndex] == oElem.value)
			{
				oElem.value = '';
			}
		});
		oElem.addEvent('blur', function(){
			if(oElem.value == '')
			{
				oElem.value = aValueDefault[iIndex];
			}
		});
	});

	$$('form').addEvent('submit', function() {
		$$('input[type=text], textarea').each( function(oElem, iIndex) {
			if(aValueDefault[iIndex] == oElem.value && oElem.hasClass('required') )
			{
				oElem.value = '';
			}
		});
	});

	oEventsManager.fireEvent('nextStep');
}

if ( oEventsManager && oEventsManager != null && oEventsManager != undefined )
{
	oEventsManager.registerEvent('fontManager', fontManager);
	oEventsManager.registerEvent('weatherManager', weatherManager);
	oEventsManager.registerEvent('nodeproManager', nodeproManager);
	oEventsManager.registerEvent('flashDiapoManager', flashDiapoManager);
	oEventsManager.registerEvent('manageInput', manageInput);
}

function nextPro(links)
{
	if (iProSelectedId < links.length - 1)
		iProSelectedId++;
	// on affiche le second élément
	else
		iProSelectedId = 1;

	// Si on est sur le dernier élément du noeud, il faut renvoyer le bloc sur le premier element
	if( iProSelectedId == 1 )
		$('pro_content').setStyle('left', '0px');

	var iMove = -175*iProSelectedId;
	var oFxNode = new Fx.Morph($('pro_content'));
	oFxNode.start({ 'left' : iMove + 'px' });
}

function prevPro(links)
{
	if (iProSelectedId > 0 )
		iProSelectedId--;
	else
		iProSelectedId = $$('#pro_content .element').length-2; // Affichage de l'avant dernier élément, le dernier étant le doublon

	// Si on est sur le premeir élément du noeud, il faut renvoyer sur le doublon
	if( iProSelectedId == $$('#pro_content .element').length-2 )
	{
		var iMove = ($$('#pro_content .element').length-1)*175;
		$('pro_content').setStyle('left', '-'+iMove+'px');
	}

	iMove = -175*iProSelectedId;
	var oFxNode = new Fx.Morph($('pro_content'));
	oFxNode.start({ 'left' : iMove + 'px' });
}

var iDiapoSelectedId = 0;
var iDiapoIntervalId;
function NextImage()
{
	if( $$('.info_diapo li a').length > 1 )
	{
		var aSelectors = $$('.info_diapo li a');
		var aDiapos = $$('.diaporama img');
		var aLegend = $$('.info_diapo p');

		aSelectors.each(function(elem, index)
		{
			elem.addEvent('click', function()
			{
				$clear(iDiapoIntervalId);

				nextDiapo(aSelectors, aDiapos, aLegend, index);

				iDiapoIntervalId = nextDiapo.periodical( 8000, null, [aSelectors, aDiapos, aLegend]);
			});
		});

		iDiapoIntervalId = nextDiapo.periodical( 8000, null, [aSelectors, aDiapos, aLegend]);
	}
}

function nextDiapo(selectors, diapos, legends, machindex)
{
	selectors[iDiapoSelectedId].getParent().removeClass('active');
	legends[iDiapoSelectedId].removeClass('active');
	diapos[iDiapoSelectedId].removeClass('active');

	var iPreviousId = iDiapoSelectedId;

	if (!$defined(machindex))
	{
		if (iDiapoSelectedId < selectors.length - 1)
			iDiapoSelectedId++;
		else
			iDiapoSelectedId = 0;
	}
	else
	{
		iDiapoSelectedId = machindex;
	}

	selectors[iDiapoSelectedId].getParent().addClass('active');
	legends[iDiapoSelectedId].addClass('active');
	var oFxDiapoHide = new Fx.Morph(diapos[iPreviousId], {
		onComplete : function() {
			var oFxDiapoDisplay = new Fx.Morph(diapos[iDiapoSelectedId]);
			oFxDiapoDisplay.start({ 'opacity' : 1 });
		}
	});
	oFxDiapoHide.start({ 'opacity' : 0 });
}

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

/*
 * Favorites
 *
 */
var oFavorites;
function add_selection( sLink, sName, sType, sId, sRubrique)
{
	var bReturn = false;
	if ( !oFavorites )
		oFavorites = new FavoriCart("FavCart", {display_number : -5, cookie_name:sCookie_name, record_separator: '¤¤', info_separator: '%%'});

	bReturn = oFavorites.add_fav(sLink, sName, sType, sId, sRubrique);
	if($("favorites")) oFavorites.display_list();
	return bReturn;
}

function remove_selection(sName, sId)
{
	var bReturn = false;
	if ( !oFavorites )
		oFavorites = new FavoriCart("FavCart", {display_number : -5, cookie_name:sCookie_name, record_separator: '¤¤', info_separator: '%%'});

	bReturn = oFavorites.remove_fav(sName);

	if(bReturn == true && sId != undefined) {
		var elem = $(sId);

		if(elem)
			elem.dispose();
	}
	if($("favorites")) oFavorites.display_list();
	return bReturn;
}
