$(document).ready(function() {
	
	/*
	* Choix de la version
	*/
	var version = 'v1';
	
	/*
	* Code commun
	*/
	execVersion(version);
//	if(version === 'v2') inc('v2', 'Css/', 'css');
	getOnglets();
	var listForm = '.CentralLarge > .formations > ul.list > li';
	if($(listForm).children().length > 0) orderRand(listForm, true);
	var listEcoles = '.CentralLarge > #ecoles > ul > li';
	if($(listEcoles).children().length > 0) orderRand(listEcoles, false);
	
	/*
	* Utilisation du javascript car invalide par le correcteur w3c pour l'instant
	*/
	$(listForm + ':nth-child(4n+4)').addClass('right');
	$(listForm + ':nth-child(-1n+4)').addClass('top');
	$(listForm + ':nth-last-child(-n+4)').addClass('bott');
	$(listForm + ':nth-child(8n+5), '+listForm + ':nth-child(8n+6), '+listForm + ':nth-child(8n+7), '+listForm + ':nth-child(8n+8)').addClass('two');
	$(listForm + '[rel=ll]').addClass('ll');
/*	
	$('div.liste-formation li:nth-child(3n+3)').addClass('right');
	$('div.liste-formation li:nth-child(-1n+3)').addClass('top');
	$('div.liste-formation li:nth-last-child(-n+3)').addClass('bott');
	$('div.liste-formation li:nth-child(6n+4)').addClass('two');
	$('div.liste-formation li:nth-child(6n+5)').addClass('two');
	$('div.liste-formation li:nth-child(6n+6)').addClass('two');
	
	$('.depliant').each(function(){
		var nbChild = $(this).find('div.liste-formation li').length;
		var reste = nbChild % 3;
		if(reste === 1) { $(this).find('div.liste-formation li:eq('+(nbChild-3)+')').removeClass('bott'); $(this).find('div.liste-formation li:eq('+(nbChild-2)+')').removeClass('bott'); }
		else if(reste === 2) { $(this).find('div.liste-formation li:eq('+(nbChild-3)+')').removeClass('bott'); }
	});
*/	
	$('#ecoles li:nth-child(3n+3)').addClass('right');
	$('#ecoles li:nth-child(-1n+3)').addClass('top');
	$('#ecoles li:nth-last-child(-n+2)').addClass('bott');
	$('#ecoles li:nth-child(6n+4), #ecoles li:nth-child(6n+5), #ecoles li:nth-child(6n+6)').addClass('two');
});

/*
* Code perso
*/
function execVersion(version)
{
	if(version == 'v1')
	{
//		$('img').each(function() { var href=$(this).attr('src'); href=href.replace("v2", "Pages"); $(this).attr('src', href); });
//		$('.formations img').each(function(){ var href=$(this).attr('src'); href=href.replace("png", "jpg"); $(this).attr('src', href); $(this).attr('width', 80); $(this).attr('height', 80); });
//		$('div.options-formations li:nth-child(4n+4)').addClass('right');
//		$('div.options-formations li:nth-last-child(-n+4)').addClass('bott');
//		$('html').css('background', 'url("/Content/www.formations-distance.com/Images/HtmlBackground.png") repeat-x 0 0 #FFF');
		oldVersion = 'v2';
	}
	else if(version == 'v2')
	{
		
		$('h1 img, .options-formations img').each(function() { var href=$(this).attr('src'); href=href.replace("Pages", "v2"); $(this).attr('src', href); });
//		$('.formations img').each(function(){ var href=$(this).attr('src'); href=href.replace("jpg", "png"); $(this).attr('src', href); $(this).attr('width', 76); $(this).attr('height', 76); });
//		$('div.options-formations li:nth-child(3n+3)').addClass('right');
//		$('div.options-formations li:nth-last-child(-n+3)').addClass('bott');
//		$('html').css('background', 'url("/Content/www.formations-distance.com/Images/v2/htmlBackground.png") repeat-x left top #aee1f4');
		oldVersion = 'v1';
	}
	

	var ua = navigator.userAgent;
	var nav = (ua.indexOf('MSIE') !== -1) ? ' ie' : '';
	addClassToBody = (typeof addClassToBody == 'undefined') ? '' : ' ' +addClassToBody;
	$('body').removeClass(oldVersion).addClass(version + nav + addClassToBody);

}

/*
* Selection de l'onglet
*/
function getOnglets()
{
	switch(ongle)
	{
		case 'formations' : 
			$('ul.jd_menu li:eq(0)').addClass('select');
			$('div.Ligne1 div.backHome a').addClass('active');
			break;
			
		case 'ecole':
			$('ul.jd_menu li:eq(1)').addClass('select');
			break;
			
		case 'formdis':
			$('ul.jd_menu li:eq(2)').addClass('select');
			break;
		
		default:
			break;
	}
	$('.arrowOnglet').addClass(ongle);
}

/*
* Fonction include
*/
function inc(i, t, ex)
{
	var src = "www.formations" + '-' + "distance.com";
	switch(ex)
	{
		case 'css' :
			$('head').append('<link href="' + '/Content/' + src + '/' + t + i + '.css' + '" rel="stylesheet" type="text/css" />');
			break;
		
		case 'js' :
			$('head').append('<script type="text/javascript" src="' + '/Content/' + src + '/' + t + i + '.js' + '"></script>');
			break;
		
		case 'shared-js':
			$('head').append('<script type="text/javascript" src="' + '/Content/' + 'Shared' + '/' + t + i + '.js' + '"></script>');
			break;

		case 'shared-css':
			$('head').append('<script type="text/javascript" src="' + '/Content/' + 'Shared' + '/' + t + i + '.css' + '"></script>');
			break;

		default :
			break;
	}
}

/*
* Affiche une liste donnee, de facon aleatoire
*/
function orderRand(list, duGateau)
{
	var nbChild = $(list).length;
	var gateau = (duGateau === true) ? mangerGateau('ordreformations') : null;
	var ord = new Array();
	var tmpList = $.makeArray($(list).removeAttr('class'));
	var newList = new Array();
	var rand = 0;
	
	/*
	* On verifie qu'un gateau n'est pas deja present
	*/
	if(gateau == null)
	{
		for (i = 0; i < nbChild; i++)
		{
			do
			{
				if(i !== (nbChild-1))
				{
					rand = Math.floor(Math.random()*nbChild); // On genere un nombre compris entre 0 et le nombre d'elements
				}
				else
				{
					for(j = 0; j <= nbChild; j++) { if($.inArray(j, ord) === -1) { rand = j; break; } } // Pour le dernier element, on mets le dernier chiffre qui n'est pas insere, le dernier chiffre peux ne JAMAIS sortir
				}
			}
			while($.inArray(rand, ord) !== -1); // Tant que le chiffre trouve est existant
			ord[i] = rand;
		}
		
		if(duGateau === true) faireGateau('ordreformations', ord.join('|')); // On enregistre le tout pour la duree de la session
	}
	else
	{
		ord = gateau.split('|');
	}
		
	// On recupere la liste dans un tableau
	for (i = 0; i < nbChild; i++)
	{
		newList[i] = tmpList[ord[i]];
	}
	$(newList).appendTo($(list).parent());
}

/*
* Lis les gateaux
*/
function mangerGateau(nom) {
    var arg = nom + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
	
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return chercheGateau(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) {
            break;
        }
    }
    return null;
}

/*
* Va chercher le gateau
*/
function chercheGateau(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

/*
* Cree un gateau
*/
function faireGateau(nom, valeur) {
    var argv = faireGateau.arguments;
    var argc = faireGateau.arguments.length;
    var expires = argc > 2 ? argv[2] : null;
    var path = argc > 3 ? argv[3] : null;
    var domain = argc > 4 ? argv[4] : null;
    var secure = argc > 5 ? argv[5] : false;
    var expdate = new Date;
    var dateExp = expdate + 2678400000;
    document.cookie = nom + ("=" + valeur + "; expires=" + dateExp + (path == null ? "" : "; path=" + path) + (domain == null ? "" : "; domain=" + domain) + (secure == true ? "; secure" : ""));
}

/*
* Gere l'accordeon verticale
*/
function depliant(elt)
{
	var listElt = $('.depliant');
	var url = document.location.href;
	var opt = url.split('#');
	var diez = null;
	
	if(typeof elt == 'undefined')
	{
		$(listElt).children('.contenu').hide();
		if(opt.length > 1) { if($(listElt).siblings('#'+opt[1]).length === 1) diez = opt[1]; }
		
		$(listElt).each(function(i) {
			if(diez !== null)
			{
				if($(this).attr('id') === diez)
				{
					$(this).children('.contenu').show();
					$(this).addClass('hover');
				}
			}
			else
			{
				if(i === 0)
				{
					$(this).children('.contenu').show();
					$(this).addClass('hover');
				} else return false;
			}
		});
	}
	else if(elt == 'all')
	{
		if($('body').hasClass('ie')) 
			$(listElt).addClass('hover').children('.contenu').show();
		else
			$(listElt).addClass('hover').children('.contenu').slideDown();
	}
	else if(elt == 'none')
	{
		if($('body').hasClass('ie')) 
			$(listElt).removeClass('hover').children('.contenu').hide();
		else
			$(listElt).removeClass('hover').children('.contenu').slideUp();
	}
	else
	{
		if($('#'+elt).hasClass('hover')) return false;
		if($('body').hasClass('ie')) 
		{
			$(listElt).removeClass('hover').children('.contenu').hide();
			$(listElt).siblings('#'+elt).addClass('hover').children('.contenu').show();
		}
		else
		{
			$(listElt).removeClass('hover').children('.contenu').slideUp();
			$(listElt).siblings('#'+elt).addClass('hover').children('.contenu').slideDown();
		}
	}
}


















