var blocs = ["bloc_accueil", "bloc_fidelite", "bloc_services", "bloc_contact"];

function loadMagasin(nomMag,menuMag,typePage,channelName,channelId ) {
	var menu ="accueil";
	var pathname = window.location.pathname;
	var tab=pathname.split('/');
	if (tab.length==4){
		if (tab[3] != null) {
			menu = tab[3];	
		}
	}

	if (typePage!="template") {
		if ((tab.length==6)&& (window.location.pathname.indexOf("/portal/site/carrefour/menuitem.")!= -1)) {
			menuId= getParameter("vgnextoid",window.location.search);
			if (menuMag == "null") {
				if (channelName == "VOTRE MAGASIN") {
					menu ="accueil";
				} else {
					menu = menuId;
				}
			} else {
				menu =menuMag;
			}	
		}

		initMenu(nomMag,menu,channelId);
	}
}

/**
 * Methode permettant d'extraire un parametre de l'url 
 */
function getParameter(strParamName,strHref){
	var strReturn = "";
	var strQueryString = "";
	strHref = unescape(strHref); 
	if ( strHref.indexOf("?") > -1 ){ 
		strQueryString = strHref.substr(strHref.indexOf("?") + 1);
	}
	
	if (strQueryString != null && strQueryString != "") {
		var aQueryString = strQueryString.split("&"); 
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){ 
			var aParam = aQueryString[iParam].split("="); 
			
			if (aParam[0] == strParamName) {
				strReturn = aParam[1];
			} else {
				if (document.getElementById(aParam[0]) != null) {
					document.getElementById(aParam[0]).checked = aParam[1];
				}
			}	
		}
	}
	return unescape(strReturn);				
}

function initMenu(nomMag, item, channelId) {
	var bloc ="bloc_"+item;
	if ((window.location.pathname.indexOf("/magasin/"+nomMag) > -1) || (window.location.search.indexOf("id=") > -1)) {
		showMenu(bloc);
	} else {
		//Rubrique 
		var pathname = window.location.pathname;
		var tab=pathname.split('/');
		item =channelId;
	}
	if (document.getElementById(item)) {
		document.getElementById(item).className="str_menuItem";
	} else {
		//Pas de  menu item trouvé
		window.location="/magasin/trouver/";
	}
	
}

function getMenuMag(nomMag,item){
	var hostname =window.location.hostname;
	var pathname = "/magasin/"+nomMag;
	if(item =="accueil") {
		window.location=pathname;
	} else {
		window.location=pathname+"/"+item;
	}
}

//hide a div defined by id
var hideDiv = function(divid) {
	if (dojo.byId(divid) != null) {
		dojo.style(dojo.byId(divid), "display", "none");
	}
}

// show a div defined by id
var showDiv = function(divid) {
	if (dojo.byId(divid) != null) {
		dojo.style(dojo.byId(divid), "display", "block");
	}
}



function showMenu(bloc) {
	for(var i=0; i<blocs.length; i++) {
		if (bloc == blocs[i]) {
			showDiv(bloc);
		} else {
			hideDiv(blocs[i]);
		}
	}
}






// GESTION DES PICTOS

var pictos = 
{
	init:function() 
	{
		// afficher tip au rollover
		var picts = $$("#pictos a.picto");
		picts.each(function(item) 
		{
			item.addEvent("mouseover", pictos.showTip);
			item.addEvent("mouseout", pictos.hideTip);
			
			var tip = item.getPrevious();

			// copier le champ alt hors de la zone masqu�e
			var copy = tip.clone();
			document.body.appendChild(copy);
			copy.setStyles({
				position:"absolute",
				visibility:"hidden",
				left:0
			});
			item._tipRef = copy;

		});
		
		// cacher tips
		var popups = $$("#pictos div.pictoTip");
		popups.each(function(item)
		{
			item.setStyles({
				display:"none"
			});
		});
	},
	
	showTip:function(e)
	{
		var evt = new Event(e);
		var picto = $(evt.target);
		
		var tip = picto._tipRef;
		if (!tip) 
		{
			picto = picto.getParent();
			tip = picto._tipRef;
		}
		var dims = tip.getSize();
		var pos = picto.getPosition();
		
		tip.setStyles({
			left:(pos.x-55)+"px",
			top:(pos.y-dims.size.y)+"px",
			"z-index":2
		});
		tip.setOpacity(1);
	},
	
	hideTip:function(e)
	{
		var evt = new Event(e);
		var picto = $(evt.target);
		var tip = picto._tipRef;
		if (!tip) 
		{
			picto = picto.getParent();
			tip = picto._tipRef;
		}
		tip.setOpacity(0);
	}
	
}


// GESTION DES VIGNETTES

var vignettes = 
{
	currentPopup:null,
	currentIndex:-1,
	items:[],
	coords:[],
	popups:[],
	
	init:function() 
	{
		// vignettes
		$$("#prod_popup li").each(function(item)
		{
			if(item.id == 'evenement') {
			
			   vignettes.items.push(item);
			   vignettes.coords.push(item.getPosition());
			
			   // capturer clics
			   var links = item.getElementsByTagName("a");
			   for(var i=0; i<links.length; i++)
			   {
				     var item = $(links[i]);
				   		
				     if(!item.onclick) {				   
				     	item.__index = vignettes.items.length-1;
				     	item.addEvent("click", vignettes.showPopup);
				     }
				     
			   };
		  }
		});
		// cacher popups
		var popups = $$("#prod_popup div.vignettes_popup");
		popups.each(function(item)
		{
			vignettes.popups.push(item);
			item.setStyles({
				display:"none"
			});
		});
		
		// cacher bas popups
		$$("#prod_popup div.vignettes_popup_bottom").each(function(item) 
		{
			item.setStyle("display", "none");
		});
	},
	
	showPopup:function(e)
	{
		var evt = new Event(e);
		evt.preventDefault();
		if (vignettes.currentPopup != null) return false;
		
		var link = $(evt.target).getParent();
		vignettes.arrangePopup(link.__index);
		return false;
	},
	
	arrangePopup:function(index)
	{
		// cacher popup visible
		if (vignettes.currentIndex >= 0) vignettes.popups[vignettes.currentIndex].setStyle("display", "none");
		
		if (index == undefined) index = 0;
		else if (index >= vignettes.popups.length) index = vignettes.popups.length-1;
		else if (index < 0) index = 0;
		vignettes.currentIndex = index;
		
		// repositionner les vignettes
		var select = vignettes.items[index];
		if (!select) 
		{
			vignettes.retour();
			return;
		}
		vignettes.items.each(function(item) { if (item != select) item.setStyle("display", "none"); });
		
		// on cache aussi le catalogue
		$$("#prod_popup #catalogue").setStyle("display", "none");
		
		select.setStyles({
			display:"block",
			position:"absolute",
			top:0,
			left:0
		});
		
		// afficher le popup
		$("prod_popup_nav").setStyle("display", "block");
		vignettes.popups[index].setStyle("display", "block");
	},
	
	retour:function()
	{
		if (vignettes.currentIndex < 0) return;
		// cacher le popup
		vignettes.popups[vignettes.currentIndex].setStyle("display", "none");
		$("prod_popup_nav").setStyle("display", "none");
		vignettes.currentIndex = -1;
		
		// repositionner les vignettes
		vignettes.items.each(function(item) {
			item.setStyles({
				display:"inline",
				position:"",
				left:"",
				top:""
			});
			item.setOpacity(1); 
		});
		
		// On repositionne aussi le catalogue
		$$("#prod_popup #catalogue").setStyles({display:"inline", position:"", left:"", top:""});
		$$("#prod_popup #catalogue").setOpacity(1);
	},
	
	precedent:function()
	{
		if (vignettes.currentIndex > 0) vignettes.arrangePopup(vignettes.currentIndex-1);
		else vignettes.arrangePopup(vignettes.popups.length-1);
	},
	
	suivant:function()
	{
		if (vignettes.currentIndex < vignettes.popups.length-1) vignettes.arrangePopup(vignettes.currentIndex+1);
		else vignettes.arrangePopup(0);
	}
	
}

// initialisation
window.addEvent("domready", pictos.init);
window.addEvent("domready", vignettes.init);

