jQuery(document).ready( function () {
	// Tableaux des données XML
	var tabId = new Array();
	var tabNomPays = new Array();
	var tabNbFeuille = new Array();
	var tabPosX = new Array();
	var tabPosY = new Array();
	var tabTitre = new Array();
	var tabUrl_Image = new Array();
	var tabDescription = new Array();
	var tabLien = new Array();
	
	// Variables dev
	var isOpen = false;
	var lang = jQ("html").attr('lang');
	var chemin = '/skin/frontend/pdt/default/';
	var isFirstClick = true;
	
	// Récupération des données XML
	jQ.ajax({
		type: "GET",
		url: "/carte.xml",
		dataType: (jQ.browser.msie) ? "text" : "xml",
		success: function(data) { 
			var xml;
			 if (typeof data == "string") {
			   xml = new ActiveXObject("Microsoft.XMLDOM");
			   xml.async = false;
			   xml.loadXML(data);
			 } else {
			   xml = data;
			 }

			jQ(xml).find('pays').each(function(i){
				tabId[i] = jQ(this).attr('id');
				jQ(this).find('carte').each(function(){
					if (lang == jQ(this).attr('lang')) {
						tabNomPays[i] = jQ(this).find('nomPays').text();
						tabNbFeuille[i] = jQ(this).find('nbFeuille').text();
						tabPosX[i] = jQ(this).find('posX').text();
						tabPosY[i] = jQ(this).find('posY').text();
					}
				});
				jQ(this).find('texte').each(function(){
					if (lang == jQ(this).attr('lang')) {
						tabTitre[i] = jQ(this).find('titre').text();
						tabUrl_Image[i] = jQ(this).find('url_image').text();
						tabDescription[i] = jQ(this).find('description').text();
						tabLien[i] = jQ(this).find('lien').text();
					}
				});
			});
			
			setTimeout(afficherDonnées,3000);
			setTimeout(afficherCarteInfo,3000);
		}
	}); 	
	

	// Création de l'animation
	jQ(".map .repere").live("click",function() {
		var id = jQ(this).attr('id');
		if(isOpen == false) {
			isOpen = true;
			modifInfo(id);
			jQ(".map").animate({
				marginLeft: "-=173px"
			}, 500);
			jQ(".descriptionMap").animate({
				marginLeft: "-=173px"
			}, 500);
			jQ(".titreCarte").animate({
				marginLeft: "-=346px"
			}, 500);
		}
		else {
			jQ(".descriptionMap").stop().animate({
				opacity: 0
			}, 0);
			jQ(".descriptionMap .infoBtnVoir").stop().animate({
				opacity: 0
			}, 0);
			modifInfo(id);
			jQ(".descriptionMap").stop().animate({
				opacity: 1
			}, 500);
			jQ(".descriptionMap .infoBtnVoir").stop().animate({
				opacity: 1
			}, 500);
		}
		
		if(isFirstClick == true) {
			jQ("#carteInfo").animate({
				marginLeft: "-=200px"
			},500);
			isFirstClick = false;
		}

		jQ(".repere").children("pre").removeClass("nomPays_hover").addClass("nomPays").attr("style","");
		jQ(".repere").children(".flecheHover").removeClass("flecheHover").addClass("fleche");
		jQ(this).children("pre").addClass("nomPays_hover").removeClass("nomPays");
		jQ(this).children(".fleche").addClass("flecheHover").removeClass("fleche");
	});
	
	jQ(".descriptionMap .infoClose a").live("click",function() {
		jQ(".map").animate({
			marginLeft: "+=173px"
		}, 500);
		jQ(".descriptionMap").animate({
			marginLeft: "+=173px"
		}, 500);
		jQ(".titreCarte").animate({
			marginLeft: "+=346px"
		}, 500);
		jQ(".repere").children("pre").removeClass("nomPays_hover").removeClass("nomPays").addClass("nomPays");
		jQ(".repere").children(".flecheHover").removeClass("flecheHover").addClass("fleche");
		isOpen = false;
	});
	
	function afficherDonnées() {
		
		for(i=0;i<tabId.length;i++) {
			var maChaine = '<a class="repere" id="'+tabId[i]+'" style="margin:'+tabPosY[i]+'px '+tabPosX[i]+'px;"><pre class="nomPays">'+tabNomPays[i]+'</pre><br /><br /><div class="fleche"></div><img class="pngFix" src="'+chemin+'images/'+tabNbFeuille[i]+'_feuilles.png" alt="" /><div class="clear"></div></a>';
			jQ(".map").append(maChaine); 
			
		}
		jQ(".repere").each(function(i) {
			effetApparition(this,i)	
		});
	}
	
	function modifInfo(id) {
		jQ(".descriptionMap .infoTitre").text(tabTitre[id]);
		jQ(".descriptionMap .infoImg img").attr('src',chemin+tabUrl_Image[id]);
		jQ(".descriptionMap .infoDescription").text(tabDescription[id]);
		jQ(".descriptionMap .infoBtnVoir a").attr('href',tabLien[id]);
	}
	
	// Gestion du hover pour bug IE6
	jQ(".repere").hover(function() {
		jQ(this).children(".nomPays").css({background:"#754520",color:"#ffffff"});
		jQ(this).children(".fleche").addClass("flecheHover");
		
	},function() {
		jQ(this).children(".nomPays").css({background:"#ffffff",color:"#6d6800"});
		jQ(this).children(".fleche").removeClass("flecheHover");
	});	
	
	function effetApparition(obj,i) {
		var time = 500 * i;
		var wait = setTimeout(function(time) {
			var margeInit = jQ(obj).css("marginLeft");
			margeAnim = "-=" + margeInit;
			jQ(obj).css("marginLeft","700px");
			jQ(obj).css("visibility","visible");
			jQ(obj).animate({
				marginLeft: margeInit
			}, 500, 'easeOutBack');
		}, time);
	}
	
	// Affichage de la carteInfo 
	function afficherCarteInfo() {
		jQ("#carteInfo").animate({
			marginLeft: "-=200px"
		},4000).animate({
			marginLeft: "+=405px"
		},500,'easeOutBack');
	}
});
