/****************************************************************************************************************
	global vars
****************************************************************************************************************/

openedTabs 			= [];	// tabs
openedAccordions 	= [];	// accordions

/****************************************************************************************************************
	PNG hack for IE
****************************************************************************************************************/

function initPNG(){
	if(($.browser.msie) && ($.browser.version == "6.0")){
		$("img.pngfix[@src$=.png]").each(function(){
			$(this).css('filter','progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + $(this).attr("src") + '",sizingMethod="image")');
			$(this).attr("src","/App_Themes/ImagesRefonte/spacer.gif");
		});
	}
}

/****************************************************************************************************************
	Is there any default variables set ?
****************************************************************************************************************/

function checkDefault(defaultVar, containers, contents){
	
	// defaultVar = default values of the opened vars [array] (tabs or accordions contents)
	// example =  var openedAccordions = [3,2];
	// containers = class of the containers
	// contents = class of the contents
	// if not exists or not matching the number of tabs/accordions systems in the page, (re-)initializing it

	if ((typeof(defaultVar)=='undefined')  || ($(containers).size() != defaultVar.length) ){
		defaultVar = new Array($(containers).size());
		for (var i=0; i<defaultVar.length; i++){ defaultVar[i] = 1; }
	}
	
	// if exists, testing each value to be a number and neither bigger than the contents'size nor negative
	else{
		for (var i=0; i<defaultVar.length; i++){ 
			if (isNaN(parseInt(defaultVar[i]))) defaultVar[i] = 1;
			else {
				mySize = $(containers+':eq('+i+') '+contents).size();
				if (defaultVar[i] > mySize) defaultVar[i] = parseInt(mySize);
				else if (defaultVar[i] < 0) defaultVar[i] = 1;
					else defaultVar[i] = parseInt(defaultVar[i]);
			}
		}
	}

	return(defaultVar);
}

/****************************************************************************************************************
	nav2 rollover script
****************************************************************************************************************/

function initNav2(){
	
	/* init */
	$('#header, #nav2Roll').addClass('JSenabled');
	$('#nav2Roll, #nav2RollContent div').hide();
	
	var isHover = false;
	
	/* rollOver*/
	$('#nav2 li a').hover(function(){
		
		$('#nav2RollContent div').hide();
		$('#nav2Roll').show();
		$('#'+$(this).parent().attr('id')+'Content, #'+$(this).parent().attr('id')+'Content *').show();
		
		$('#nav2 a').removeClass("hover");
		$(this).addClass("hover");
				
		if ($(this).parent().attr('id') == 'jourApresJour') $('#nav2RollContent, #nav2RollBtm').addClass('jaj');
		else $('#nav2RollContent, #nav2RollBtm').removeClass('jaj');
		
	},function(e){
  		
		// if cursor is above the nav hide the nav2Roll
		if (e.pageY < 110) {
			$('#nav2 a').removeClass("hover");
			$('#nav2Roll').hide();
		}
		// else if it is on the nav2Roll
		else{
			$('#nav2Roll').hover(function(){
				// don't do anything !
			},function(){
				// hide the stuff on roll out
				$('#nav2 a').removeClass("hover");
				$(this).hide();
	
			});
		}
	});
}

/****************************************************************************************************************
	Collapse main script
****************************************************************************************************************/

function showContent(button){
	button.children("span").addClass('opened');
	button.children("span").removeClass('closed');
	/* AMO : Blur is obsolete and exit textbox focus on keypress */
	//button.blur();
	button.next('div.collapseContent').show();
    /* EXP-LMA : Cookie value = true */
    createCookie('listservices', 'true', 365);
}

function hideContent(button){
	button.children("span").addClass('closed');
	button.children("span").removeClass('opened');
	/* AMO : Blur is obsolete and exit textbox focus on keypress */
	//button.blur();
	button.next('div.collapseContent').hide();
    /* EXP-LMA : Cookie value = false */
    createCookie('listservices', 'false', 365);
}

function initCollapse(){
	
	/* init */	
	/* EXP-LMA : Ajout du collapse/extends dans le cookie */
    if(readCookie('listservices') == 'false') 
    {
        $('.collapseContent').removeClass('defaultOpened');
        $('.collapseLink').removeClass('defaultOpened');
    }

    $('.collapseLink.defaultOpened').children('span').addClass('opened');
    $('.collapseLink.defaultOpened').children('span').removeClass('closed');
    $('.collapseLink.defaultOpened').next('div.collapseContent').show();


    $('.collapseContent').not('.defaultOpened').hide();
    $('.collapseLink').not('.defaultOpened').children('span').removeClass('opened');
    $('.collapseLink').not('.defaultOpened').children('span').addClass('closed');

	$('.collapseLink').each(function(i){
		
		/* first opened or not ? */
		if ($(this).attr('class').indexOf('defaultOpened') != -1) var defaultOpened = true;
		else var defaultOpened = false;	

		/* behaviour */
		$(this).toggle(function(){
								
			if (defaultOpened) hideContent($(this));
			else showContent($(this));
			
		},function(){
			
			if (defaultOpened) showContent($(this));
			else hideContent($(this));
			
		});
	});
}

/****************************************************************************************************************
	Tabs script
****************************************************************************************************************/

function initTabs(){

	// openedTabs = default values of the opened tabs [array]
	// if not exists or not matching the number of tabs systems in the page, (re-)initializing it

	/******************* A SUPPRIMER IDEALEMENT */

	if ((typeof(openedTabs)=='undefined')  || ($('.tabs').size() != openedTabs.length) ){
		openedTabs = new Array($('.tabs').size());
		for (var i=0; i<openedTabs.length; i++){ openedTabs[i] = 1; }
	}
	// if exists, testing each value to be a number
	else{
		for (var i=0; i<openedTabs.length; i++){ 
			if (isNaN(parseInt(openedTabs[i]))) openedTabs[i] = 1;
			else openedTabs[i] = parseInt(openedTabs[i]);
		}
	}
	
	/******************* /A SUPPRIMER IDEALEMENT */
	
	// ça en dessous, ça marche mais il faut retoucher le DOM comme sur "hp-multiple" en rajoutant des tabsContainer, donc pour la livraison on oublie
	// openedTabs = checkDefault(openedTabs, '.tabsContainer', '.tab'); 
	
	// Hiding the titles
	$('.tab h3').addClass('forPrintOnly');
	
	$('.tabs').each(function(i){
		
		var myTabsCollection = $(this).attr('id');
		
		// opening first tab and first tab content (default settings)
		$('#'+myTabsCollection+' li:eq('+(openedTabs[i]-1)+')').addClass('on');
		$('#'+myTabsCollection+'-container div').hide();
		$('#'+myTabsCollection+'-container div:eq('+(openedTabs[i]-1)+')').show();
		
		// onclick
		$('#'+myTabsCollection+' li a').click(function(){
			
			// tabs
			$('#'+myTabsCollection+' li').removeClass('on');
			$(this).parent().addClass('on');
			
			// content
			$('#'+myTabsCollection+'-container div').hide();
			$($(this).attr('href')).show();
			
			/* AMO : Blur is obsolete and exit textbox focus on keypress */
			//$(this).blur();
			return false;
			
		});		
	});
}
/****************************************************************************************************************
	Carousel script
****************************************************************************************************************/

function initCarousel(){
	
	// Showing and highlighting the buttons
	$('.carouselNavigation').show();
	$('.carouselItems li.first a').addClass('on');

	$('.carouselItems li a, .carouselNavigation li a').click(function(){
		
		var myCarousel = $(this).attr('href').split('-')[0];
		
		// buttons highlight
		$(myCarousel+" .carouselItems li a").removeClass("on");
		$(".carouselItems li a[href$=" + $(this).attr('href') + "]").addClass("on");
		
		$(myCarousel+' .carouselContent').hide();
		$($(this).attr('href')).show();
		
		// 'prev' button
		if ($($(this).attr('href')).prev('div').length > 0) $(myCarousel+' .carouselNavigation .prev a').attr('href','#'+$($(this).attr('href')).prev('div').attr('id'));
		else $(myCarousel+' .carouselNavigation .prev a').attr('href', '#'+$(myCarousel+' .carouselContainer div:last').attr('id'));

		// 'next' button
		if ($($(this).attr('href')).next('div').length > 0)	$(myCarousel+' .carouselNavigation .next a').attr('href','#'+$($(this).attr('href')).next('div').attr('id'));
		else $(myCarousel+' .carouselNavigation .next a').attr('href', '#'+$(myCarousel+' .carouselContainer div:first').attr('id'));
		
		/* AMO : Blur is obsolete and exit textbox focus on keypress */
		//$(this).blur();
		return false;

	});
	
}

/****************************************************************************************************************
	Accordion script
****************************************************************************************************************/

function hideAccordionContent(myAccordionId){

	$('#'+myAccordionId+' .accordionButton').removeClass('on');
	$('#'+myAccordionId+' .accordionContent').hide();
	
}

function showAccordionContent(myAccordionId, accordionContentToOpen){

	// Hidin' tha all stuff
	hideAccordionContent(myAccordionId);
	
	// Openin' the good one
	if ((typeof(accordionContentToOpen)=='undefined')){ accordionContentToOpen = 1; }
	$('#'+myAccordionId+'-'+accordionContentToOpen).show();
	
	// Addin' tha class y'all
	var myLink = '#'+myAccordionId+' a[href="#'+myAccordionId+'-'+accordionContentToOpen+'"]';
	$(myLink).addClass('on');
}

function initAccordion(){
	
	// openedAccordions = default values of the opened accordions [array]
	// if not exists or not matching the number of tabs systems in the page, (re-)initializing it

	openedAccordions = checkDefault(openedAccordions, '.accordionContainer', '.accordionContent');
	
	$('.accordionContainer').each(function(i){

		var myAccordionId = $(this).attr('id');

		// default settings
		showAccordionContent(myAccordionId,openedAccordions[i]);

		// onclick
		$('#'+myAccordionId+' .accordionButton').click(function(){
			
			if ($(this).hasClass("on")){ hideAccordionContent(myAccordionId); }
			else { showAccordionContent(myAccordionId,$(this).attr('href').split(myAccordionId+'-')[1]); }
			
			/* AMO : Blur is obsolete and exit textbox focus on keypress */
			//$(this).blur();
			//return false;
			
		});
	});	
}

/****************************************************************************************************************
	Stylesheet Switch using cookies to keep changes
****************************************************************************************************************/

/* cookie functions */

function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+escape("style="+value+";")+expires+"; path=/";
}

function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==" ") c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) {
			var cookieStyle= ((c.substring(nameEQ.length,c.length)).split("style%3D")[1]).split("%3B")[0];
			return cookieStyle;
		}
	}
	return null;
}

function GetCookie() {
	var myCookie = document.cookie;
	return myCookie;
}

function eraseCookie(name){
	createCookie(name,"",-1);
}

/* update */

function updateSkin(styleName){

	$("link[rel*='style'][title]").each(function(i) {
		this.disabled = true;
		if (this.getAttribute("title") == styleName) this.disabled = false;
	})
	createCookie("planetVerbaudet", styleName, 365);
	
}

/* init */

function initStyleSwitch(){
	
	if((typeof(nuit)=='undefined') || !(nuit)){
	
		/* cookie creation */
		var c = readCookie("planetVerbaudet");
		if (c) updateSkin(c);
		
		
		/* buttons */
		$(".styleSwitch").click(function()
		{

		    updateSkin(this.getAttribute("rel"));

		    /* AMO : Blur is obsolete and exit textbox focus on keypress */
		    //this.blur();

		    try
		    {
		        //We are testing the value containsWebTV
		        if (containsWebTV == true)
		        {
		            window.location.reload();
		            return false;
		        }
		        else
		        {
		            return false;
		        }
		    } catch (e)
		    {
		        //If containsWebTV is undefined, then we don't reload the page
		        return false;
		    }

		});
	}
}

/****************************************************************************************************************
	onLoad
****************************************************************************************************************/

$(document).ready(function()
{
	initStyleSwitch();
	//initPNG();
	initNav2();
	initCollapse();
	initTabs();
	initCarousel();
	initAccordion();
});

