jQuery.noConflict();
var currentCssLeft = "50%";
var currentCssMarginLeft = "-750px";  
var maxSiteWidth = 1500 ;
var minSiteWidth = 1000 ; 
var fichePrettyPhoto = null;

var calculerLargeurAccueil = false;

jQuery(document).ready(function(){


	if (jQuery("#map_canvas:visible").length > 0){
		jQuery("#content_panel").css("height" , "100%");
	}
	
	PngIe6();
	
	// initialisation de la LIGHTBOX
	fichePrettyPhoto = jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		slideshow: false,
		close_btn_pos: "top",
		deeplinking: false,
		social_tools: false,
		show_title: false,
		show_description: false,
		theme: 'facebook' // pp_default / light_rounded / dark_rounded / light_square / dark_square / facebook
	});
	
	jQuery("#newsletter_btn").fancybox ({
		'title'			: false,
		'scrolling'		: 'no',
		'width'			: 350,
		'height'		: 190,
		'margin'		: 0,
		'padding'		: 5,
		'arrows'		: false,
		'closeClick'	: false
	});
	
	
	// RS >> si pas de promos afficher la pub du moment
	if (jQuery("#slider_promos").length == 0 || jQuery("#slider_promos .titre_avec_encart").length == 0)
	{
		jQuery("#pager_promos_prevnext").remove();
		jQuery("#encarts_promos_fiche").html("<div class='nom_adherent titre_gras'>En ce moment</div><img src='/sites/resources/images/pub_defaut.jpg' border='0' />");
	}
	else if (jQuery("#slider_promos .titre_avec_encart").length == 1)
	{
		jQuery("#pager_promos_prevnext").remove();
	}
	else
	{
		// slider encart promos
		encartPromosSlider = jQuery('#slider_promos').bxSlider({
			controls: false,
			randomStart: true,
			auto: true,
			pause: 5000,
			speed: 1000
		});
		jQuery('#promo_go_prev').click(function(){
			encartPromosSlider.goToPreviousSlide();
		    return false;
		});
		jQuery('#promo_go_next').click(function(){
			encartPromosSlider.goToNextSlide();
		    return false;
		});
		/*jQuery('#slider_promos').anythingSlider({
            width: 200,
            height: 250,
            autoPlay: true,
            resizeContents: false,
			buildArrows: false,
			buildNavigation: false,
			buildStartStop: false,
            easing: 'easeInOutExpo',
            delay: 5000,
            animationTime: 1000
		});*/

	}
	
	// si pas de pubs afficher la pub du moment
	if (jQuery("#slider_pubs").length == 0 || jQuery("#slider_pubs .pub_tpl").length == 0)
	{
		jQuery("#pager_pubs_prevnext").remove();
	}
	else if (jQuery("#slider_pubs .pub_tpl").length == 1)
	{
		jQuery("#pager_pubs_prevnext").remove();
	}
	else
	{
		// slider encart promos
		pubsSlider = jQuery('#slider_pubs').bxSlider({
			controls: false,
			randomStart: true,
			auto: true,
			pause: 5000,
			speed: 1000
		});
		jQuery('#pub_go_prev').click(function(){
			pubsSlider.goToPreviousSlide();
		    return false;
		});
		jQuery('#pub_go_next').click(function(){
			pubsSlider.goToNextSlide();
		    return false;
		});
		/*jQuery('#slider_pubs').anythingSlider({
			width: 200,
            height: 250,
            autoPlay: true,
            resizeContents: false,
			buildArrows: false,
			buildNavigation: false,
			buildStartStop: false,
            easing: 'easeInOutExpo',
            delay: 5000,
            animationTime: 1000
		});*/
	}
	
	// click sur l'encart promo
	/*jQuery(".encart_promo").click(function() {
		var idpromo = jQuery(this).attr("idpromo");
		jQuery("#lien_offre_"+idpromo).click();
	});*/
	
	
	//Changer le sujet par défaut du formulaire de contact suivant les pages
	var idDoc = jQuery("#objetSelect").attr("id_doc");

	if (idDoc == "1083")
		jQuery(".option2").attr("selected","selected");
	
	if (idDoc == "1141")
		jQuery(".option1").attr("selected","selected");
	
	if (idDoc == "1145")
		jQuery(".option3").attr("selected","selected");
	
	// modif RS >> inscription aux promos
	jQuery(".encart_promo .bt_inscription").click (function () {
		var docid = jQuery(this).attr("promo");
		var alias = jQuery(this).attr("alias");
		// rediriger vers la page de formulaire
		document.location.href="InscriptionPromotion.html?promo=" + docid + "&vitrine=" + alias;
	});
	
	// EXPAND ACTU DESCRIPTION
	jQuery(".summaryPost .bt_fulldesc").click(function () {		
    	var showType = jQuery(this).attr("type");
		var docid = jQuery(this).attr("article");
    	jQuery("#footer").css("visibility", "hidden");    	
    	
    	if (jQuery.browser.msie && jQuery.browser.version <= 8)
    	{
    		if (showType == "show")
    		{
    			jQuery("#plain_" + docid).show();
    			jQuery("#bt_show_article_"+docid).hide();
    			jQuery("#bt_hide_article_"+docid).show();
    		}
    		else
    		{
    			jQuery("#plain_" + docid).hide();
    			jQuery("#bt_show_article_"+docid).show();
    			jQuery("#bt_hide_article_"+docid).hide();
    		}
    		ResizeSiteHeight();
    	}
    	else
    	{
    		jQuery("#plain_" + docid).toggle('middle', function() {   		
	    		jQuery("#footer").css("visibility", "visible");
	    		
	    		// switcher le bouton show/hide
	    		if (showType == "show")
	    		{
	    			jQuery("#bt_show_article_"+docid).hide();
	    			jQuery("#bt_hide_article_"+docid).show();
	    		}
	    		else
	    		{
	    			jQuery("#bt_show_article_"+docid).show();
	    			jQuery("#bt_hide_article_"+docid).hide();
	    		}    
	    		ResizeSiteHeight();
    		});   
    	}
    });    
    // EXPAND ACTU DESCRIPTION FROM ID
	var nReq = location.search.substring(1,location.search.length);
    var MaValeur = nReq.substring(nReq.indexOf("=")+1, nReq.length);
    if (MaValeur != "" && MaValeur != null)
    {
	    jQuery("#plain_" + MaValeur).toggle('middle', function() { 
			jQuery("#bt_show_article_"+MaValeur).hide();
			jQuery("#bt_hide_article_"+MaValeur).show();			
			if(jQuery("#plain_" + MaValeur).length) { 
		   		jQuery('html,body').animate({scrollTop: (jQuery("#plain_" + MaValeur).offset().top - 20)},'slow');
			}
	   		ResizeSiteHeight();
	   	});
    }
    
	// RS >> centrer en hauteur les images dans les encarts promos
    jQuery(".encart_promo .img_actu a").each(function(i) {
    	var hauteur_zone = jQuery(this).outerHeight();
    	var imgObj = jQuery("img", this);
    	var hauteur_img = imgObj.outerHeight();
    	if (hauteur_img != 0)
    	{
    		var top = (hauteur_zone - hauteur_img) / 2;
    		imgObj.css("margin-top", top+"px");
    	}
    });
	// RS >> centrer en hauteur le titre dans les encarts promos
    jQuery(".encart_promo .titre").each(function(i) {
    	var hauteur_zone = jQuery(this).outerHeight();
    	var titreObj = jQuery("a", this);
    	var hauteur = titreObj.outerHeight();
    	if (hauteur != 0)
    	{
    		var top = (hauteur_zone - hauteur) / 2;
    		titreObj.css("margin-top", top+"px");
    	}
    });
    
    
    /* TOOL TIP CHAMPS DE RECHERCHE */	
 	jQuery("#recherche_input[title]").tooltip({
 		position: "bottom center",
		offset: [2, -5],
		effect: "fade",
		opacity: 0.8
 	});
    
    /* LANGUAGE TRANSLATION */
	jQuery.translate.ready(function(){		
		//alert("ready to translate");
		if (tradToLang != "")
		{
			//alert("traduction en "+tradToLang);
			jQuery('body').translate(tradToLang, {
				complete: function(){				
					DisplaySite();
			    }
			});
		}
		else
			DisplaySite();
	});
	

	// RS >> uniquement sur la page d'accueil
	if (document.location.pathname == "/" || document.location.pathname == "/Accueil.html")
	{
		/*** INTERSTICIEL ***/
		if(jQuery.browser.msie && jQuery.browser.version == 6)
		{
			// pas d'intersticiel sous IE6
			calculerLargeurAccueil = true;
		}
		else
		{		
			// RS >> tester si l'intersticiel a déjà été vu en cherchant si le cookie existe	
			var cookie_intersticiel = Get_Cookie("lva_intersticiel");
			if (cookie_intersticiel == null)
			{
				jQuery("#intersticiel").css("width", jQuery(window).width() + "px");
				jQuery("#intersticiel").css("height", jQuery(window).height() + "px");
				jQuery('#intersticiel').show();
				jQuery(".intersticiel_cancel_link").click(function(){
					jQuery('#intersticiel').hide();
					Set_Cookie("lva_intersticiel", "ok", 365);				
					calculerLargeurAccueil = true;
					ResizeSiteHeight();
					setTimeout("gmaps_initialize()", 200);				
				});
				ResizeSiteHeight();
				return;
			}
			else
				calculerLargeurAccueil = true;
		}
		/*** FIN INTERSTICIEL ***/
	}
	
	
    jQuery(window).bind('resize', function() {
    	ResizeSiteHeight();
	});
    // RS >> resize uniquement si on est pas sur la page d'accueil
    if ((document.location.pathname != "/" && document.location.pathname != "/Accueil.html") || calculerLargeurAccueil)
    	ResizeSiteHeight();
	/*
	 * 
	 * !!!! IMPORTANT !!!! NE RIEN METTRE A LA SUITE => METTRE LE NOUVEAU JS AU DESSUS DE L'INTERSTICIEL
	 *
	 */
    
});

/* MISC */

function ScrollAncre()
{
	if(jQuery("#ancre_chambre").length) { 
   		jQuery('html,body').animate({scrollTop: (jQuery("#ancre").offset().top - 20)},'slow');
	}
}

function DisplaySite()
{
	jQuery("#loading").hide();
	jQuery("#page").css("visibility", "visible");
}

function PngIe6()
{
   if(jQuery.browser.msie && jQuery.browser.version == "6.0") {
    jQuery("#content").find('img[src*=".png"]').css("behavior", 'url("/manager/media/script/siteControl/png/pngbehavior.htc")');  
    }
}

function ResizeSiteHeight()
{ 
	// modif RS >> bug au resize : hauteur modifiée à l'infini
	var hauteur_browser = jQuery(window).height() - 200;
	var hauteur_content = jQuery("#content_panel").outerHeight() - 2;
	var hauteur_left = jQuery("#col_gauche_panel").outerHeight();
	var hauteur_right = jQuery("#separation_verte").outerHeight();
	
	//alert("browser="+hauteur_browser+" - content="+hauteur_content+" - left="+hauteur_left+" - right="+hauteur_right);

	var hauteur_max = Math.max(hauteur_browser, hauteur_left, hauteur_content, hauteur_right);

	jQuery("#col_gauche").css("height", hauteur_max +"px");
	jQuery("#content").css("height", hauteur_max +"px");
	jQuery("#separation_verte").css("height", hauteur_max + "px");
	jQuery("#footer").css("top", hauteur_max + 150 +"px");	
	jQuery("#footer").css("visibility", "visible");
	
	// RS >> si on est pas sur l'accueil relancer le calcul de la largeur
	if ((document.location.pathname != "/" && document.location.pathname != "/Accueil.html") || calculerLargeurAccueil)
	{
		ExtensibleWidth();
		
		if (document.location.pathname == "/" || document.location.pathname == "/Accueil.html")
			centerMap(default_center_coord, default_zoom);
	}
	
	if ((document.location.pathname == "/" || document.location.pathname == "/Accueil.html") && !calculerLargeurAccueil)
	{
		centerMap(default_center_coord, default_zoom);
	}
	
	// RS >> itinéraires
	ResizeZoneItineraire();
}


function ExtensibleWidth()
{
	var browserWidth = jQuery(window).width();

	
	if ( browserWidth > maxSiteWidth ) {
	
		jQuery('#page').css("left", currentCssLeft);
		jQuery('#page').css("width", maxSiteWidth);
		jQuery('#page').css("margin-left", currentCssMarginLeft);
		jQuery('body, html').css("overflow-x","hidden");
		
		jQuery("#bandeau").css("width", maxSiteWidth - 198+ "px");
		
		jQuery("#bandeau_mot_cle").css("width", maxSiteWidth - 750 + "px");
		
		jQuery("#menu").css("width", maxSiteWidth - 198 + "px");
		
		jQuery("#recherche").css("width", maxSiteWidth - 198 + "px");
		
		jQuery("#content_panel").css("width", maxSiteWidth - 409 + "px");
	
		jQuery("#content_panel").css("width", maxSiteWidth - 409 + "px");
	
		jQuery("#content").css("width", maxSiteWidth - 409 + "px");
		
		jQuery("#col_droite").css("left", maxSiteWidth - 202 + "px");
		
		jQuery("#separation_verte").css("left", maxSiteWidth - 207 + "px");
		
		
	}
	
	if ( browserWidth < maxSiteWidth && browserWidth > minSiteWidth ) {
		
		jQuery("#page").css("left", "0px");
		jQuery("#page").css("marginLeft", "0px");
		jQuery("#page").css("width", browserWidth + "px");

		jQuery("#bandeau").css("width", browserWidth - 198+ "px");
		
		jQuery("#bandeau_mot_cle").css("width", browserWidth - 750 + "px");
		
		jQuery("#menu").css("width", browserWidth - 198 + "px");
		
		jQuery("#recherche").css("width", browserWidth - 198 + "px");
		
		jQuery("#content_panel").css("width", browserWidth - 409 + "px");
	
		jQuery("#content_panel").css("width", browserWidth - 409 + "px");
	
		jQuery("#content").css("width", browserWidth - 409 + "px");
		
		jQuery("#col_droite").css("left", browserWidth - 202 + "px");
		
		jQuery("#separation_verte").css("left", browserWidth - 207 + "px");
		
	}
	
	if ( browserWidth < minSiteWidth ) {
		jQuery('#page').css("left", "0px");
		jQuery('#page').css("margin-left", "0px");
		jQuery('body, html').css("overflow","auto");
	}

}

function afficherAdresse()
{
	// RS >> si on est pas sur l'accueil rediriger dessus
	var pathname = document.location.pathname;
	if (pathname.indexOf("Accueil") == -1 && pathname != "/")
	{
		document.location.href = "/Accueil.html?view=Adresse";
		return;
	}
	
	// sinon changer la vue en mode adresses
	if (jQuery("#map_canvas").is(":visible"))
	{
		jQuery("#zone_itineraire").hide();
		jQuery("#map_canvas").hide();
		jQuery("#address_canvas").show();
		jQuery("#content_panel").css("height" , "auto");
	}
	
	jQuery("#btn_plan a").removeClass("btn_plan_select");
	jQuery("#btn_adresses a").addClass("btn_adresses_select");

	ResizeSiteHeight();
}

function afficherCarte(idboutique, adresse, photo, alias)
{	
	var redirect = false;
	if (alias != undefined)
	{
		// RS >> obligé de faire plus simple avec api v2
		document.location.href = "/Accueil.html?vitrine="+alias;
	}
	else
		redirect = true;
		
	
	// RS >> si on est pas sur l'accueil rediriger dessus
	var pathname = document.location.pathname;
	if ((pathname.indexOf("Accueil") == -1  && pathname != "/") || redirect)
	{
		document.location.href = "/Accueil.html";
		return;
	}
	
	/*
	// sinon changer la vue en mode carte
	if (jQuery("#map_canvas").not(":visible"))
	{
		if (jQuery("#map_directions div").length)
			jQuery("#zone_itineraire").show();
		jQuery("#address_canvas").hide();
		jQuery("#content_panel").css("height" , "100%");
		jQuery("#map_canvas").show();
		
	}*/

	/*
	if (latlng != undefined && latlng != null)
	{
		centerMap(default_center_coord, default_zoom);
	}

	if (idboutique != null && arrayMarker[idboutique] != null && content != null)
	{
		var infowindow = new google.maps.InfoWindow({
		    content:"<div onClick='afficherFiche(\""+alias+"\")' style='cursor:pointer;'>" +
			    		"<div style='margin-bottom:5px'>" + 
			    			"<img src='" + photo + "' style='max-height:130px; max-width:200px;'/>" + 
				    	"</div>" +
				    	"<div>" +
				    	 	"<b>" + arrayMarker[idboutique].getTitle() + "</b><br/>" + adresse +
				    	"</div>"+
			    	"<div/>"
		});

		infowindow.open(map, arrayMarker[idboutique]);

		jQuery("body").click(function () { 
			infowindow.close();
		});
	}
	
	jQuery("#btn_plan a").addClass("btn_plan_select");
	jQuery("#btn_adresses a").removeClass("btn_adresses_select");

	ResizeSiteHeight();*/
}

function AfficherAide()
{
	var lien = 'http://api.dmcloud.net/player/embed/4dc67a14f325e1357c0001f2/4edf9a56f325e1071f001616?auth=1638698044-0-m4nwb8cv-6c66f76cd1db00327e663b5dd1cf0ca9&preset=4dc6b246f325e135770001dd&autoplay=1';
	AfficherVideoBox(lien);
}

function AfficherVideoBox(LienDmCloud)
{
	jQuery.fancybox.open(LienDmCloud, {
		'type'			: 'iframe',
		'href'			: LienDmCloud,
		'scrolling'		: 'no',
		'height'		: 480,
		'width'			: 848,
		'margin'		: [0, 20, 0, 10],
		'padding'		: 10,
		'title'			: false,
		'beforeShow'	: function() {
			if (encartPromosSlider != null)
				encartPromosSlider.stopShow();
			if (pubsSlider != null)
				pubsSlider.stopShow();
		},
		'beforeClose'	: function() {
			if (encartPromosSlider != null)
				encartPromosSlider.startShow();
			if (pubsSlider != null)
				pubsSlider.startShow();
		}
	});
}

function rawurlencode(str)
{
	// URL-encodes string
	str = (str + '').toString();
	// PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following
	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
}

