/**
 * Latz v0.36 béta
 * http://latz.laligatz.com
 *
 * @author Alexis MINEAUD <laligatz@laligatz.com>
 * @copyright Copyright © 2005-2006, Alexis MINEAUD
 * @license xxx
*/

/////////////////////
// Insertions balises [g][/g] etc...
/////////////////////
function addText(form, instext) {
    var mess = form.texte;
	//IE support
	if (document.selection) {
		mess.focus();
		sel = document.selection.createRange();
		sel.text = instext;
		form.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (mess.selectionStart || mess.selectionStart == "0") {
		var startPos = mess.selectionStart;
		var endPos = mess.selectionEnd;
		var chaine = mess.value;

		mess.value = chaine.substring(0, startPos) + instext + chaine.substring(endPos, chaine.length);

		mess.selectionStart = startPos + instext.length;
		mess.selectionEnd = endPos + instext.length;
		mess.focus();
	} else {
		mess.value += instext;
		mess.focus();
	}
}


/////////////////////
// Confirmations
/////////////////////
function confirmation() {
	x = window.confirm("Etes-vous sûr(e) ?");
	return x;
}


//////////////////////
// Popup
//////////////////////
function popup(page, largeur, hauteur, options) {
	var top=(screen.height-hauteur)/5;
	var left=(screen.width-largeur)/2;
	window.open(page,'',"top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}


//////////////////////
// Popup centré
//////////////////////
function popupCentre(page, largeur, hauteur, options) {
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open(page,'',"top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}


/////////////////////
// Sélect automatique
/////////////////////
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


/////////////////////
// Passe un élément en display : none à la volée sur un lien
/////////////////////
props_aff=new Array;
function aff_masq(id) {
	if(document.getElementById(id).style.display!='none'){
		props_aff[id]=document.getElementById(id).style.display;
		document.getElementById(id).style.display='none';
	}else{
		if(props_aff[id] & props_aff[id]!='none')
			document.getElementById(id).style.display=props_aff[id];
		else
			document.getElementById(id).style.display='block';	
	}
}


/**
 * Change le paramètre style d'une balise html.
 * Exemple : onClick="changeVisible(mon_bouton_radio.checked, 'name_div', 'block', 'none');"
 */
function changeVisible(input, id, select, nonselect) {
	if (input) {
		element = findObj(id);
		if (element.style.display != select)  element.style.display = select;
	} else {
		element = findObj(id);
		if (element.style.display != nonselect)  element.style.display = nonselect;
	}
}


/**
 * Trouve un objet dans la page.
 */
var memo_obj = new Array();
function findObj(n) { 
	var p,i,x;

	// Voir si on n'a pas deja memoriser cet element		
	if (memo_obj[n]) {
		return memo_obj[n];
	}
	
	d = document; 
	if((p = n.indexOf("?"))>0 && parent.frames.length) {
		d = parent.frames[n.substring(p+1)].document; 
		n = n.substring(0,p);
	}
	if(!(x = d[n]) && d.all) {
		x = d.all[n]; 
	}
	for (i = 0; !x && i<d.forms.length; i++) {
		x = d.forms[i][n];
	}
	for(i=0; !x && d.layers && i<d.layers.length; i++) x = findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x = document.getElementById(n); 
	
	// Memoriser l'element
	memo_obj[n] = x;
	
	return x;
}

/**
 * Lien onclick pour les zones
 */
function goUrl(url){
	window.location.replace(url)
}


/**
 * Lancement Jquery
 */
$(document).ready(function () {
	var options1 = {
		easing: 'easeInOutCirc',
		stageW: 372,
		stageH: 250,
		nlabel: 'suivant',
		plabel: 'précédent',
		oflabel: '/',
		blabel: 'agrandir',
		clabel: "Cliquer sur l'image pour fermer",
		llabel: 'Ouvrir la fiche du produit',
		smallImgScriptPath: '/root/scripts/resize_image.php?max=372&contraindre=largeur&coupe=1&hauteurCoupe=250&img=/upload/catalogue/produits/pics',
		largeImgScriptPath: '/root/upload/catalogue/produits/pics'
	}
	
	$("#cadreAccueil").hide();
	$('#slideShow').popeye(options1);
	
	$(".glossymenu a").hover(function(){
		/*alert(arrayMenuImg[this.search.slice(4)]);*/
		return false;
	});
	
	$(".reloadSlideShow").click(function(){
		//var main = $('#main');
		var slideShow = $('#slideShow');
		
		var id=($(this).attr("id"));
		$.ajax({
			type: "GET",
			url: "/catalogue/appel_ajax_menu.html",
			data: "id="+id,
			dataType : "xml",
			error:function(msg, string){
				alert( "Error !: " + string );
			},
			success:function(data){
				// appel ajax pour récupérer les produits
				var arrayImg = new Array();
				var arrayAlt = new Array();
				var arrayLink = new Array();
				var arrayTitle = new Array();
				var produits = data.getElementsByTagName('rubrique');
				var nbProduits = produits[0].childNodes.length;
				// enregistrement des produits dans les tableaux js
				for (i=0; i<nbProduits; i++) {
					arrayTitle[i] = produits[0].getElementsByTagName("libelle")[i].firstChild.nodeValue;
					arrayImg[i] = produits[0].getElementsByTagName("img")[i].firstChild.nodeValue;
					arrayLink[i] = '/catalogue/produit.html?idProduit='+produits[0].getElementsByTagName("produit")[i].getAttribute("id");
					arrayAlt[i] = '';
				}
				// fermeture du Slide Show
				slideShow.slideToggle('show', function(){
					// regénération du DOM aprés la fermeture
					$('#slideShow .popeye-stagewrap').remove();
					$('#slideShow .popeye-tools-wrap').remove();
					//slideShow.remove();

					// create html nodes
					//var rwSlideShow	= $('<div id="slideShow"/>');
					var rwList		= $('<ul class="rwImg"/>');
										
					// build DOM tree
					//main.append(rwSlideShow);
					slideShow.append(rwList);
					for (i=0; i<nbProduits; i++) {
						rwList.append('<li><a href="'+arrayLink[i]+'" title="'+arrayTitle[i]+'"><img src="/'+arrayImg[i]+'" alt="'+arrayAlt[i]+'"></a></li>');
					}
					slideShow.popeye(options1);
				});
				
				// ouverture du nouveau Slide Show
				slideShow.slideToggle();
			}
		});
		
		return false;
	});
	
	$("input[type=checkbox]#mainCategorie").click(function(){
		if ($(this).attr("checked") == true) {
			$("#cadreAccueil").show("slow");
		} else {
			$("#cadreAccueil").hide("slow");
		}
	});
});

ddaccordion.init({
	headerclass:		"submenuheader",//Shared CSS class name of headers group
	contentclass:		"submenu",		//Shared CSS class name of contents group
	revealtype:			"click",		//Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover
	mouseoverdelay:		200,			//if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev:		true,			//Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded:	[],				//index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen:		false,			//Specify whether at least one header should be open always (so never all headers closed)
	animatedefault:		false,			//Should contents open by default be animated into view?
	persiststate:		true,			//persist state of opened contents within browser session?
	toggleclass:		["", ""],		//Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml:			["prefix","<img src='/root/img/puce_menu_close.gif' class='statusicon' />", "<img src='/root/img/puce_menu_open.gif' class='statusicon' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed:		"fast",			//speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){	//custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
});
