var myLightbox = null;
var currentCat = {htmlElement: null, niveau_id: null, niveau1_id: null};
var loadingTxt = null;
var currentAction = null;
var currentPage = 1;
var currentParam = {};

refreshMenu = function(categorie_id, currentNiveau, niveau1_id){
	if(($(categorie_id).className.indexOf('menu_on')!=-1)){
		if(currentNiveau==1){
			removeAllStylesOnMenu();
		}
		else if(currentNiveau==2){
			// Gestion du niveau courant
			$(categorie_id).className = $(categorie_id).className.replace(' menu_on', '');
			$(categorie_id).getElementsByTagName('A')[0].className = $(categorie_id).getElementsByTagName('A')[0].className.replace(' menu_on', '');
		}
	}
	else{
		removeAllStylesOnMenu();
		currentCat.htmlElement = $(categorie_id);
		currentCat.niveau_id = currentNiveau;
		currentCat.niveau1_id = niveau1_id;
	
		// Affichage du menu
		var catClass = $(categorie_id).className;
		if(catClass.indexOf('menu_on')==-1){
			// Gestion du niveau 1
			$(niveau1_id).className += ' menu_on';
			$(niveau1_id).getElementsByTagName('A')[0].className +=  ' menu_on';
	
			if(currentNiveau!=1){
				// Gestion du niveau courant
				$(categorie_id).className += ' menu_on';
				$(categorie_id).getElementsByTagName('A')[0].className +=  ' menu_on';
	
				// Gestion du niveau 2 s'il s'agit d'un niveau 3
				if(currentNiveau==3){
					$(categorie_id).parentNode.className += ' menu_on';
					$(categorie_id).parentNode.getElementsByTagName('A')[0].className +=  ' menu_on';
				}
			}
		}
	}
}

loadHome = function(){
	// removeAllStylesOnMenu();
	//loadMenu();
	// Chargement Ajax de l'accueil
	/*var objParam = new Object();
	setCurrentPage(1);
	objParam['page'] = currentPage;
	objParam['csaction'] = 'site/accueil_afficher';
	createAjax('contenu', objParam);*/
	window.location.replace('index.php');
}

var oldPlanche = null;
var inter = null;

intervalSelectMap = function(planche_id){
	if($('planche-'+planche_id)){
		if(oldPlanche){
			oldPlanche.className = oldPlanche.className.replace('menu_on', '');
		}
		oldPlanche = $('planche-'+planche_id);
		oldPlanche.className = oldPlanche.className+' menu_on';
		clearInterval(inter);
	}
}

loadMap = function(planche_id, planche_nom){
	loadMenuPlanche();
	inter = setInterval('intervalSelectMap('+planche_id+');', 100);
	// Chargement Ajax de l'accueil
	var objParam = new Object();
	setCurrentPage(1);
	objParam['page'] = currentPage;
	objParam['csaction'] = 'site/planche_afficher';
	objParam['planche_id'] = planche_id;
	objParam['planche_nom'] = planche_nom;
	createAjax('contenu', objParam);
}

loadLang = function(lang){
	// Chargement Ajax de l'accueil
	var objParam = new Object();
	objParam['csaction'] = currentAction;
	objParam['lang'] = lang;
	objParam['page'] = currentPage;
	objParam['justBody'] = 1;
	objParam[currentParam.clef] = currentParam.valeur;
	createAjax('body', objParam, true);
}

loadCatFromPlanche = function(str){
	$('recherche').value = str;
	loadSearch(1, true);
}

loadMenuPlanche = function(){
	if(!$('ch_menu').getElementsByTagName('IMG').length){
		var objParam = new Object();
		objParam['csaction'] = 'site/menu_planche_afficher';
		createAjax('ch_menu', objParam);
	}
}

loadMenu = function(){
	if($('ch_menu').getElementsByTagName('IMG').length){
		var objParam = new Object();
		objParam['csaction'] = 'site/menu_afficher';
		createAjax('ch_menu', objParam);
	}
}

loadCat = function(categorie_id, currentNiveau, niveau1_id, page){
	categorie_id = 'categorie-'+categorie_id;
	niveau1_id = 'categorie-'+niveau1_id;
	refreshMenu(categorie_id, currentNiveau, niveau1_id);

	categorie_id = 	categorie_id.replace('categorie-', '');
	// Chargement Ajax du catalogue
	var objParam = new Object();
	if(page){
		setCurrentPage(page);
		objParam['page'] = currentPage;
	}
	else{
		setCurrentPage(1);
		objParam['page'] = currentPage;
	}
	objParam['csaction'] = 'site/categorie_afficher';
	objParam['categorie_id'] = categorie_id;
	setCurrentParam('categorie_id', categorie_id);
	createAjax('contenu', objParam);
}

loadSearch = function(page, dontResetMenu){
	if(!dontResetMenu){
		removeAllStylesOnMenu();
	}
	// Chargement Ajax du catalogue
	var objParam = new Object();
	if(page){
		setCurrentPage(page);
		objParam['page'] = currentPage;
	}
	else{
		setCurrentPage(1);
		objParam['page'] = currentPage;
	}
	objParam['csaction'] = 'site/recherche_effectuer';
	objParam['recherche'] = $('recherche').value;
	setCurrentParam('recherche', $('recherche').value);
	createAjax('contenu', objParam);
}

loadAccount = function(){
	removeAllStylesOnMenu();
	var objParam = new Object();
	objParam['csaction'] = 'site/compte_afficher';
	createAjax('contenu', objParam);
}

loadContact = function(){
	/* removeAllStylesOnMenu();
	var objParam = new Object();
	objParam['csaction'] = 'site/contact_afficher';
	createAjax('contenu', objParam); */
	window.location.href = 'mailto:parts@chereau.com';
}

loadChereauMap = function(){
	removeAllStylesOnMenu();
	var objParam = new Object();
	objParam['csaction'] = 'site/chereau_map';
	createAjax('contenu', objParam);
}

loadCaracteristiques = function(url, produit_id, width, height){
	wOpen(url.replace('produit_id=produit_id', 'produit_id='+produit_id), width, height);
}

cSDisplayCarac = function(urlCarac, produit_id){
	cSDialog.cSDisplay('');
	var ajax = new CSAjax('cs_object_msg');
	ajax.onLoading = function(){
		$('cs_object_msg').innerHTML = loadingTxt;
	}
	urlCarac = urlCarac.replace('produit_id=produit_id', 'produit_id='+produit_id)+'&from_ajax=1';
	ajax.setRequestFile(urlCarac);
 	ajax.run();
}

loadPage = function(page){
	setCurrentPage(page);
	// Chargement Ajax du catalogue
	var objParam = new Object();
	objParam['csaction'] = currentAction;
	objParam['page'] = page;
	objParam[currentParam.clef] = currentParam.valeur;
	createAjax('contenu', objParam);
}

removeAllStylesOnMenu = function(){
	if($(currentCat.niveau1_id)){
		$(currentCat.niveau1_id).className = $(currentCat.niveau1_id).className.replace('menu_on', '');
	}
	var tableDivElements = $('ch_menu').getElementsByTagName('DIV')
	for(var i=0; i<tableDivElements.length; i++){
		tableDivElements[i].className = tableDivElements[i].className.replace('menu_on', '');
	}
	var tableAElements = $('ch_menu').getElementsByTagName('A')
	for(var i=0; i<tableAElements.length; i++){
		tableAElements[i].className = tableAElements[i].className.replace('menu_on', '');
	}
}

removeAllMenu = function(){
	$('ch_menu').innerHTML = '';
}

createAjax = function(idDiv, params, isBody){
	setCurrentAction(params['csaction']);
	var ajax = new CSAjax(idDiv);
// 	$(idDiv).style.height = $(idDiv).offsetHeight+'px';
	ajax.onLoading = function(){
		$(idDiv).innerHTML = loadingTxt;
	}
	ajax.onCompletion = function(){
// 		$(idDiv).style.height = 'auto';
		myLightbox.updateImageList();
		if($('eval')){
			eval($('eval').innerHTML);
			$('eval').id = 'id_'+(Math.random());
		}
	}
	for(var clef in params){
		ajax.setVar(clef, params[clef]);
	}
	ajax.setVar('PHPSESSID', document.getElementsByName('PHPSESSID')[0].value);
	if(!isBody){
		ajax.setVar('from_ajax', 1);
	}
	ajax.run();
}

setLoadingLibelle = function(str){
	loadingTxt = str;
}

setCurrentAction = function(str){
	currentAction = str;
}

setCurrentParam = function(clef, valeur){
	currentParam = {clef: clef, valeur: valeur};
}

setCurrentPage = function(page){
	currentPage = page;
}
