$(document).ready(function(){						
	// liste de langues à faire évoluer
	var _int = new LObject("Internationnal","_int");
	var _fr = new LObject("Français","_fr");
	var _en = new LObject("English","_en");
	var _us = new LObject("American","_en");
	var _de = new LObject("Deutsch","_de");
	var _es = new LObject("Español","_es");
	var _jp = new LObject("日本語","_ja");
	var _kr = new LObject("한국어", "_ko");
	var _nl = new LObject("Nederlands", "_nl");
	//var _ch = new LObject("Suisse", "_ch");
	//var _it = new LObject("Italien", "_it");

	
	// Liste de pays // PARAMETRER LA LISTE DES PAYS ET LEUR LANGUE AFFECTEE PAR ICI ::
	var countryList = new Array();
		//countryList.push(new Country("_International [MASTER]_","_int", new Array(_int)));
		//countryList.push(new Country("Suisse","_ch", new Array(_ch,_it)));
		countryList.push(new Country("France","_fr", new Array(_fr)));
		countryList.push(new Country("Belgique","_be", new Array(_fr,_nl)));
		//countryList.push(new Country("United States of America","_us", new Array(_us)));
		countryList.push(new Country("Deutschland","_de", new Array(_de)));
		countryList.push(new Country("United Kingdom","_gb", new Array(_en)));
		countryList.push(new Country("España","_es", new Array(_es)));
		countryList.push(new Country("日本","_jp", new Array(_jp)));
		countryList.push(new Country("한국","_kr", new Array(_kr)));
		//countryList.push(new Country("Zona de América Latina","_zal", new Array(_es)));
		countryList.push(new Country("Australia","_aop", new Array(_en)));
		countryList.push(new Country("New Zealand","_aop", new Array(_en)));
		countryList.push(new Country("South Africa","_aop", new Array(_en)));
		countryList.push(new Country("India","_aop", new Array(_en)));
		countryList.push(new Country("Philippines","_aop", new Array(_en)));
	countryList.sortOn("_libelle", Array.CASEINSENSITIVE );
	
	var optionsInit = $("#country").html();	
	
	// construction de la liste des pays
	var options = "";
	for ( var i= 0; i < countryList.length; i++ ) {
		options += "<option value=\""+countryList[i]._code+"\">"+countryList[i]._libelle+"</option>";
	}
	$("#country").html(optionsInit + options);
	$("#country option:first").attr("selected","selected");
	// changement de pays
	$("#country").change(function() {
			$("#country option").each(function (i) {
					if ( $(this).attr("selected") ) {
						if ( i > 0) {
								// on lance une langue
								//$("#validBtn").css("visibility","visible");
									// création de la liste de langues
									var langOpt = "";
									var langList = countryList[i-1]._lang;
									langList.sortOn("_libelle", Array.CASEINSENSITIVE );
									for ( var j=0; j < langList.length; j++) {
										langOpt += "<option value=\""+langList[j]._code+"\">"+langList[j]._libelle+"</option>";;
									}
									$("#lang").removeAttr("disabled");
									$("#lang").html(optionsInit + langOpt);
									$("#lang option:first").attr("selected","selected");
									$("#validBtn").css("visibility","hidden");

						} else {
							// on ne choisit aucun pays (1ère option)
								$("#validBtn").css("visibility","hidden");
								$("#lang").attr("disabled","disabled");
						}
					}
			});
	});
	
	// le bouton valider ne doit apparaitre que si une langue est choisie
	$("#lang").change(function() {
		$("#lang option").each(function (i) {
			if ( $(this).attr("selected") ) {
				if ( i > 0) {
					$("#validBtn").css("visibility","visible");
				} else {
					$("#validBtn").css("visibility","hidden");
				}
			}
		});
	});

	// pseudo objet de langue
	function LObject(libelle,code) {
		this._libelle = libelle.substring(0,1).toUpperCase() + libelle.substring(1,libelle.length);
		this._code = code;
	}
	// un pseudo objet pays
	/**
	* libelle : String le nom du pays qui sera affiché
	* code : String le code qui sera passé comme valeur à la combo
	* langList : un tableau de LObject (un pays peut avoir plusieurs langues)
	*/
	function Country(libelle, code, langList) {
		this._libelle = libelle.substring(0,1).toUpperCase() + libelle.substring(1,libelle.length);
		this._code = code;
		this._lang = langList;
	}

	// validation du formulaire
	/*$("#validBtn").click(function() {
							  alert($("form").attr("action"));
		$("form").attr("action","/e-academy.aspx");
		$("form").attr("method","get");
		alert($("form").attr("action") + " // " + $("form").attr("method"));
		$("form").submit();
	});*/
  

//onmouseover="bouton.src = 'img/home/bouton_on.gif';" onmouseout="bouton.src = 'img/home/bouton_off.gif';" 

});

	// clique sur le bouton valider
	function redirect() {
    var country = $("#country option:selected").val().substr(1);
    
		location.href = "http://"+(country=="gb"?"uk":country)+".kerastase-eacademy.com/e-academy.aspx?lang="+$("#lang option:selected").val()+"&country="+ $("#country option:selected").val();
		//location.href ="http://"+$("#country option:selected").val().substr(1)+".kerastase-eacademy.com?l="+$("#lang option:selected").val().substr(1);
	} 



// ci-dessous, juste pour le fun, un proto pour permettre le classement d'un tableau d'objet, utilisé pour la liste de langues et de pays.


Array.CASEINSENSITIVE = 1;
Array.DESCENDING = 2 ;
Array.NUMERIC = 16 ;
Array.RETURNINDEXEDARRAY = 8 ;
Array.UNIQUESORT = 4 ;
 
 
Array.prototype.sortOn = function ( propName , options ) {
 
	var owner = this ;
 
	var sortFunction = function(o1, o2) {
		
		var v1 = (o1[propName] != undefined) ? o1[propName].valueOf() : "" ;
		var v2 = (o2[propName] != undefined) ? o2[propName].valueOf() : "" ;
	   
		function noCase() {
			if (typeof(v1) == "string" || v1 instanceof String) {
				v1 = v1.toLowerCase() ;
			}
	   		if (typeof(v2) == "string" || v2 instanceof String) {
				v2 = v2.toLowerCase() ;
			}
		}
	   	
		function numeric() {
			v1 = Number(v1) ;
			v2 = Number(v2) ;
			v1 = isNaN(v1) ? 0 : v1 ;
			v2 = isNaN(v2) ? 0 : v2 ;
		}
		
		function reverse() {
			var tmp = v1 ;
			v1 = v2 ;
			v2 = tmp ;
		}
		
		switch (options) {
			
			case Array.CASEINSENSITIVE :
			case Array.CASEINSENSITIVE | Array.RETURNINDEXEDARRAY :
				noCase() ;
				break ;
			
			case Array.NUMERIC :
			case Array.NUMERIC | Array.RETURNINDEXEDARRAY :
	   			numeric() ;
				break ;
			
			case Array.DESCENDING :
			case Array.DESCENDING | Array.RETURNINDEXEDARRAY :
				reverse() ;
				break ;
			
			case Array.CASEINSENSITIVE | Array.DESCENDING :
			case Array.CASEINSENSITIVE | Array.DESCENDING | Array.RETURNINDEXEDARRAY :
				noCase() ;
				reverse() ;
				break ;
				
			case Array.NUMERIC | Array.DESCENDING :
			case Array.NUMERIC | Array.DESCENDING | Array.RETURNINDEXEDARRAY :
				numeric() ;
				reverse() ;
				break ;
			
			case Array.UNIQUESORT :
			
				if (v1 == v2) return ;
				break 
			
			
		}
	   
		if (v1 < v2) return -1 ;
		else if (v1 > v2) return 1 ;
		else return 0 ; 
	   
	}
	
	switch (options) {
		case Array.RETURNINDEXEDARRAY : 
		case Array.RETURNINDEXEDARRAY | Array.NUMERIC :
		case Array.RETURNINDEXEDARRAY | Array.CASEINSENSITIVE :
		case Array.RETURNINDEXEDARRAY | Array.NUMERIC | Array.DESCENDING :
		case Array.RETURNINDEXEDARRAY | Array.CASEINSENSITIVE | Array.DESCENDING :
			
			var tmp = [].concat(this) ;
			tmp.sort(sortFunction) ;
			var result = [] ;
			var l = this.length ;
			for (var i = 0; i < l; i++) {
				var index = tmp.indexOf(this[i]) ;
				result.push(index) ;
			}
			return result  ;
		default :
			return this.sort(sortFunction) ;
	}
	
}