var msie=document.all;
var mozi=document.getElementById&&!document.all;
var g_urlImg = g_webroot + "img/";
var mvt = null;
var map;   // pour l'objet GMap2
var gloTextItems = new Array();
var gloIdxCur = 0;
var gloIdxRequete = 0;
var vitesse = 35;

function defilantDecaler() {
	//~ if (vitesse > 30) {
		//~ vitesse = vitesse - 2;
	//~ }
	var pas = 1;
	var elt2 = document.getElementById("mediapoPelImg2");
	var x2 = parseInt(elt2.style.left);
	if (x2 == "") { x2 = 0; }
	x2 = x2 - pas;

	var elt = document.getElementById("mediapoPelImg");
	var x = parseInt(elt.style.left);
	var larg = $("#mediapoPelImg").width();
	x = x - pas;
	if (x < 0 && -x > larg) {
		x = x + 2 * larg;
	}
	else
	if (x2 < 0 && -x2 > larg) {
		x2 = x2 + 2 * larg;
	}
	
	elt.style.left = x + "px";
	elt2.style.left = x2 + "px";
	
	mvt = setTimeout(defilantDecaler, vitesse);
}

function gmaps_centrerRecherche() {
	var pointCentre = new GLatLng(44.1, 3.88);   // Languedoc-roussillon
	map.setCenter(pointCentre, 7);
}

function gmaps_charger() {
    if (GBrowserIsCompatible()) {
        gmaps_message("info", "Chargement de la carte en cours...")
        map = new GMap2(document.getElementById("map"));
		map.setMapType(G_HYBRID_MAP);
        map.addControl(new GLargeMapControl3D());
        map.addControl(new GMenuMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT));
        //map.addControl(new GScaleControl());
		gmaps_centrerRecherche();
		
        // Les marqueurs
        gmaps_creerMarqueurs();
    }
}

function gmaps_creerMarqueurInfo(point, html, tyeuId) {
	
    var baseIcon = new GIcon();
    baseIcon.iconSize = new GSize(28,28);
    //baseIcon.shadowSize=new GSize(20,22);
    baseIcon.iconAnchor=new GPoint(14,14);
    baseIcon.infoWindowAnchor=new GPoint(14,14);
    //var MonIcone = new GIcon(baseIcon, g_webroot + 'img/gmap_puce_verte.png', null, g_webroot + 'img/gmaps/marqueur-ombre.png');
	if (tyeuId == 'TYEUGest') {
		var MonIcone = new GIcon(baseIcon, g_webroot + 'img/gmap_puce_verte2.png');
	} else {
		var MonIcone = new GIcon(baseIcon, g_webroot + 'img/gmap_puce_grise2.png');
	}
    //alert("marqueur:" + html);
    //var marqueur = new GMarker(point, MonIcone);
    var marqueur = new GMarker(point, MonIcone);
    GEvent.addListener(
        marqueur,
        "click",
        function () {
            marqueur.openInfoWindowHtml(html);
        }
    );
    return marqueur;
}

var gmaps_creerMarqueurs = function() {
	
	if (!map.getCenter()) {
		// La carte n'est pas encore centrée, temporiser
		setTimeout(gmaps_creerMarqueurs, 500);
		return false;
	}
	
	$.post(	g_webroot + "ajax_gmapsMarqueurs.php",
			{idx: gloIdxRequete, page: g_page},
			function (r) {
				//alert(r);
				if (r != "" && r != "ERREUR") {
					gloTextItems = r.split("|");
					gmaps_creerMarqueursRecursif();
				} else {
					gmaps_message("effacer", "");
				}
			}
	);
	
    if (gloIdxRequete == 0) {
	    // Effacer les marqueurs existants
	    map.clearOverlays();
    }

    return true;
}

var gmaps_creerMarqueursRecursif = function() {
    var item = gloTextItems[gloIdxCur].split(":");
    id = item[0];
    lat = item[1];
    lng = item[2];
    tyeuId = item[3];
    html = item[4].replace(/#2PTS#/g, ":");
    
    // Créer le marqueur
    map.addOverlay(gmaps_creerMarqueurInfo(new GLatLng(lat, lng), html, tyeuId));
    
    gloIdxCur++;
    
    // Récursif
    if (gloIdxCur < gloTextItems.length) {
    	setTimeout(gmaps_creerMarqueursRecursif, 5);
    } else {
    	gloIdxRequete++;
    	gloIdxCur = 0;
    	gmaps_creerMarqueurs();
    }
}

function gmaps_message(type, msg) {
    var elt = document.getElementById("mapInfo");
	/*
    if (type == "info") {
        elt.style.display = "block";
    } else if (type == 'effacer') {
        elt.style.display = "none";
    }
    elt.innerHTML = msg;
	*/
}

function mediapoInit() {
	var elt = document.getElementById('mediapoPelImg');
	//elt.style.position = "relative";
	elt.style.left = "0px";
	
	// init de la largeur de la pelicule en fonction de largeur des logos
	var larg = 0;
	$("#mediapoPelImg > .defilantItem").each(function (i, e) {
		larg += $(e).innerWidth();
	});
	//alert(larg);
	$("#mediapoPelImg").width(larg);
	var elt2 = document.getElementById("mediapoPelImg2");
	//elt2.style.position = "relative";
	elt2.style.left = larg + "px";
	//alert(elt2.style.left);
	
	//$("#mediapoPelImg2").html($("#mediapoPelImg").html());
	$("#mediapoPelImg2").width(larg);
	
	$(".defilantItem > img").each(function (i,e) {
		var site = $(e).attr("siteinternet");
		if (site != "") {
			$(e).wrap('<a href="http://' + site + '" target="_blank"></a>');
		}
	});
	
	setTimeout(defilantDecaler, 1000);
}

function actionDetailToggle() {
	if ($(this).hasClass("actionBtnDetailOn")) {
		$(this).siblings(".actionTexte").fadeOut("normal");
	} else {
		$(this).siblings(".actionTexte").fadeIn("slow");
	}
	$(this).toggleClass("actionBtnDetailOn").toggleClass("actionBtnDetail");
}

function reponseAffPrec() {
	var n = $(this).attr("numero") - 1;
	if (n > 0) {
		$(".reponse[numero=" + n + "]").effect("slide", {}, "slow", reponseAffPrec);
	}
}

function menuMouseover() {
	var $prec = $(this).prev();
	if ($(this).hasClass($(this).attr("ssrub") + "-off")) {
		$prec.css("background", "url(" + g_urlImg + $prec.attr("ssrub") + "_roll_next.png) no-repeat 0 0");
		$(this).css("background", "url(" + g_urlImg + $(this).attr("ssrub") + "_roll.png) no-repeat 0 0");
	}
}

function menuMouseout() {
	var $prec = $(this).prev();
	if ($(this).hasClass($(this).attr("ssrub") + "-off")) {
		if ($prec.hasClass($prec.attr("ssrub") + "-off")) {
			$prec.css("background", "url(" + g_urlImg + $prec.attr("ssrub") + "_off.png) no-repeat 0 0");
		} else {
			$prec.css("background", "url(" + g_urlImg + $prec.attr("ssrub") + "_on.png) no-repeat 0 0");
		}
		$(this).css("background", "url(" + g_urlImg + $(this).attr("ssrub") + "_off.png) no-repeat 0 0");
	}
}

$(document).ready(function(){
	$("#menu1 > a").mouseover(menuMouseover).mouseout(menuMouseout);
	
	if ($("#mediapoPelImg").length) {
		mediapoInit();
	}
	
	// charger la carte google map si nécessaire
	if ($("#map").length) {
		gmaps_charger();
	}
	
	// quiz : afficher réponses
	for (var n=4 ; n>0 ; n--) {
		var $e = $(".reponse[numero=" + n + "]");
		if ($e.find(".libReponse").text() != "") {
			//~ $e.slideDown("slow", reponseAffPrec);
			$e.effect("slide", {}, "slow", reponseAffPrec);
			break;
		}
	}
	
	// init actions quiz
	$(".reponse").wrapInner('<a href="javascript:void(0);"></a>');
	$(".reponse > a").click(function () {
		var ok = $(this).parents(".reponse:first").attr("ok");
		$(".reponse").eq(ok-1).addClass("ok");
		$(".reponse").each(function (i,e) {
			if (i != (ok-1)) {
				$(e).find(".libReponse").fadeTo("normal", 0.5);
			}
		});
		$("p.notice").hide();
		if (ok == $(this).parents(".reponse:first").attr("numero")) {
			$("#reponseKO").hide();
			$("#reponseOK").show();
		} else {
			$("#reponseOK").hide();
			$("#reponseKO").show();
		}
		$(".commentaire").fadeIn("slow", function() { $(".illustrationReponse").show(); });
	});
	
	// init btn sur actions concrètes
	$("a.actionBtnDetail").click(actionDetailToggle);
	
});

