function submenu(el, url)
{
	if(el != ""){
		//toggle submenu
		$$('div.ssmenu').each(function(element) {
		  //new Effect.toggle(element.id,'appear',{duration:.2});
		  if(element.id == 'ss'+el)
		  {
				/*['header','menuReflect','body'].each(function(e){
					Event.observe($(e), 'mouseover', function() {
						//console.log('mouse over'+new Date().getTime());
						$('ss'+el).hide();
						Event.stopObserving($(e), 'mouseover', null);
					});
				});*/
				
				//new Effect.Appear('ss'+el,{duration:0.3});
				
				//alert($('m'+e));
				
				if (navigator.userAgent.indexOf("MSIE") != -1) {
    				//alert($('m'+el).getDimensions());
					switch(el){
						case '6':
							$('ss'+el).setStyle({'marginLeft':'-'+($('m'+el).getDimensions().width+60)+'px'});
							break;
						case '7':
							$('ss'+el).setStyle({'marginLeft':'-'+($('m'+el).getDimensions().width+171)+'px'});
							break;
						default:
							$('ss'+el).setStyle({'marginLeft':'-'+$('m'+el).getDimensions().width+'px'});
							break;
					}
					
				}
				
				if($('ss'+el).style.display == '' || $('ss'+el).style.display == 'block')
				{
				//alert("block to none");
				new Effect.Fade('ss'+el,{duration:0.2});
				}else{
				//alert("none to block");
				new Effect.Appear('ss'+el,{duration:0.5, from:0.0, to:0.95});
				}
				
				//new Effect.toggle('ss'+el,'appear',{});
				
				if($('ss'+el).style.display == "none"){
				//add loader style to submenu
				$('ss'+el).addClassName('ssmenuLoader');
				
				// load submenu content
				new Ajax.Updater('ss'+el, url);
				
				//remove submenu loader style
				$('ss'+el).removeClassName('ssmenuLoader');
				}
		  }else
		  {
			  $(element.id).hide();
		  }
		});
	}else{
		$$('div.ssmenu').each(function(element){
			$(element.id).hide();
		});
	}
}

function submenu2(id, url)
{
	new Ajax.Request(url,
	{
		parameters:{cid:id},
		onSuccess:function(r){
			$('typP'+id).innerHTML = r.responseText;
			$('mm'+id).makePositioned();
			new Effect.Move ('mm'+id,{ x: -270, y: 0, mode: 'absolute'});
			/*new Effect.Parallel(
			[
			 new Effect.MoveBy('mm'+id, 0, -270,{ sync: true }),
			 ]
			);*/
			//console.log($('typP'+id).getDimensions().height);
			new Effect.Morph('mm'+id,{style:{height:$('typP'+id).getDimensions().height+20+'px'}});
		}
	});
	
}

function submenu3(id, idtp,dir)
{
	new Ajax.Request('/menu_accessoire.php?lang='+dir,
	{
		method : 'post',
		parameters:{ action:'menuM_Accessoire', id:id, idtp:idtp },
		onSuccess:function(r){
			$('acc'+id).innerHTML = r.responseText;
			$('mm'+id).makePositioned();
			new Effect.Move ('mm'+id,{ x: -270*2, y: 0, mode: 'absolute'});
			/*new Effect.Parallel(
			[
			 new Effect.MoveBy('mm'+id, 0, -270, { sync:true }),
			]);*/
			//console.log($('acc'+id).getDimensions().height);
			new Effect.Morph('mm'+id,{style:{height:$('acc'+id).getDimensions().height+20+'px'}});
		}
	});
	
}

function submenu_retour(id)
{
	//new Effect.Morph('mm'+id,{style:{height:$('sCat'+id).getDimensions().height+'px'}});
	var new_height = $('sCat'+id).getDimensions().height+'px';
	new Effect.Morph('mm'+id,
	{
		style:{
			height:new_height
		}
	});
	//$('mm'+id).makePositioned();
	new Effect.Move ('mm'+id,{ x: 0, y: 0, mode: 'absolute'});
	/*new Effect.Parallel(
	[
	 new Effect.MoveBy('mm'+id, 0, 270,{ sync: true }),
	 ]
	);*/
}
function submenu_retour2(id)
{
	new Effect.Morph('mm'+id,{style:{height:$('mtp'+id).getDimensions().height+20+'px'}});
	$('mm'+id).makePositioned();
	new Effect.Move ('mm'+id,{ x: -270, y: 0, mode: 'absolute'});
	/*new Effect.Parallel(
	[
	 new Effect.MoveBy('mm'+id, 0, 270,{ sync: true }),
	 ]
	);*/
}

	
function ScrollToTop()
{
	new Effect.ScrollTo('wrapper', {offset: -24});
}

function gotoPageCateg(url)
{
	if(url != "0")
		window.location.href = url;
}

function gotoPageMarque(url){
	if(url != "0")
		window.location.href = url;
}

function gotoPageCollection(url){
	if(url != "0")
		window.location.href = url;
}

function gotoPageCP(url){
	if(url != "0")
		window.location.href = url;
}

function gotoPage(id, url, dir)
{
	if(id > 0)
		window.location.href = url+"/"+dir+"/"+id;
}
function gotoPage3(url,id)
{
	if(id != "0")
		window.location.href = url+"/"+id;
}

function gotoPage2(id, url, dir)
{
	var i = id.split(",");
	//alert(url+"/modele/"+i[1]+"/"+dir+"/"+i[0]);
	if(id != "0")
		window.location.href = url+"/modele/"+i[1]+"/"+dir+"/"+i[0];
}

function show_typeproduit(id, url)
{
	$('tpSelect').innerHTML = "<option>Chargement en cours ...</option>";
	$('tpSelect').show();
	new Ajax.Request(url+'/script.php',
	{
		method:'post',
		parameters:{action:'tp', s:id},
		onSuccess:function(response){
			$('tpSelect').innerHTML = response.responseText;
		}
	});
}

function reloadTP(id, marque, url, cat){
	//alert(url+'script/'+cat+'/'+marque);
	var u = url+'script/'+cat+'/'+marque;
	$('tpSelect').innerHTML = "<option>Chargement en cours ...</option>";
	$('tpSelect').show();
	new Ajax.Request(u,
	{
		method:'post',
		parameters:{action:'tpm', id:id},
		onSuccess:function(response){
			$('tpSelect').innerHTML = response.responseText;
		}
	});
}

function hideOptField(){
	$$('div.fm-opt').each(function(e){
		$(e).toggle();
	});
}

function switch2Tabs(el){
	$$('div.pZoneBottom li').each(function(e){
		e.classNames();
		if(e.id == "tab_tab_0"){
			e.removeClassName('tabs0_current');
			e.addClassName('tabs0');
		}else{
			e.removeClassName('tabs1_current');
			e.addClassName('tabs1');
		}
	});
	
	$$('div.pZoneHsep .t').each(function(element){
		$(element.id).hide();
	});
	
	$(el).show();
	
	switch(el){
		case 'tab_0':
			$('tab_tab_0').addClassName('tabs0_current');
		break;
		case 'tab_1':
			$('tab_tab_1').addClassName('tabs1_current');
		break;
		case 'tab_2':
			$('tab_tab_2').addClassName('tabs1_current');
		break;
		case 'tab_3':
			$('tab_tab_3').addClassName('tabs1_current');
		break;
	}
}

function get_mbycat(value){
	$('marquePromo').innerHTML = "<option>Loading...</option>";
	new Ajax.Request('/script.php',{
		method:'post',
		parameters:{action:'mbycat', categorie:value},
		onSuccess:function(r){
			$('marquePromo').innerHTML = r.responseText;
		}
	});
}

/*function switchClassName(i){
	if(i == 0){
		$('tab_tab_'+i).removeClassName('tabs0_current');
		$('tab_tab_'+i).addClassName('tabs0');
	}else{
		$('tab_tab_'+i).removeClassName('tabs1_current');
		$('tab_tab_'+i).addClassName('tabs1');
	}
	
}*/


/*
* carroussel produit
*/
var carrousel_i;
var carrousel_limit;
var carrousel_height;
var carrousel_page;

function initCarroussel(limit, height, count){
	carrousel_i = 0;
	carrousel_limit = limit;
	carrousel_height = -height;
	carrousel_page = Math.floor((count-1)/limit);
}

function moveNext(){
	if(carrousel_i < carrousel_page){
		new Effect.Move ('pCarrousel',{ x: 0, y: carrousel_height * (carrousel_i+1), mode: 'absolute'});
		carrousel_i++;
	}
}
function movePrevious(){
	if(carrousel_page > 0 && carrousel_i > 0){
		new Effect.Move ('pCarrousel',{ x: 0, y: carrousel_height * (carrousel_i-1), mode: 'absolute'});
		carrousel_i--;
	}
}
/*
* carroussel produit
*/

function showProductDetails(){
	var new_height = 350;
	var overflow_ = 'scroll';
	//var new_text = 'moins';
	//var class = 'moins';
	if($('pdetails').getHeight() == 350){
		new_height = 18;
		overflow_ = 'hidden';
		//new_text = 'plus';
		//var class = 'plus';
		$('pdetails').style.overflowY = 'hidden';
	}else{
		$('pdetails').style.overflowY = 'scroll';
	}
	new Effect.Morph('pdetails',{
		style:{
			height:new_height+'px'
		}
	});
	$$('div.descriptionAbsolute div a').each(function(e){
		//e.innerHTML = new_text;
		$(e).toggleClassName('plus');
		e.innerHTML = "<span style='color:#0033FF; font-weight:bold; font-size:1em' id='txtmoins'>&nbsp;&nbsp; informations</span>";
	})
}


// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

/*
* accessoirePopup
*/
function accessoirePopup(tp){
	
	$('tp').value = tp;
	var pageSize = getPageSize();
	$('access_marque').innerHTML = '<option>Loading ...</option>';
	new Ajax.Request('../../../../script',
	{
		method:'post',
		parameters:{action:'mbytp', tp:tp},
		onSuccess:function(response){
			$('access_marque').innerHTML = response.responseText;
		}
	});
	
	$('drakBackground').style.width = "100%";//pageSize[2]+'px';
	$('drakBackground').style.height = pageSize[1]+'px';
	$('accesoirePopup').style.left = "50%";//(pageSize[2]/2)-($('accesoirePopup').getWidth()/2)+'px';
	$('accesoirePopup').style.top = (pageSize[3]/2)-($('accesoirePopup').getHeight()/2)+'px';
	$('accesoirePopup').style.marginLeft = -($('accesoirePopup').getWidth()/2)+"px";
	
	//$('accesoirePopup').show();
	//$('drakBackground').show();
	
	//new Effect.Appear('accesoirePopup',{duration:0.5});
	//new Effect.Appear('drakBackground',{duration:0.5,to:0.8});
	
	new Effect.Parallel(
    [ 
	 new Effect.Appear('accesoirePopup',{sync: true }), 
     new Effect.Appear('drakBackground',{sync: true, to: 0.7, duration:0.4} ) 
	],
    { duration: 0.8 }
	);
}

function close_accessoirePopup(){
	//$('accesoirePopup').hide();
	//$('drakBackground').hide();
	
	//new Effect.Fade('accesoirePopup',{duration:0.5});
	//new Effect.Fade('drakBackground',{duration:0.5,to:0.0});
	
	new Effect.Parallel(
    [ 
	 new Effect.Fade('accesoirePopup',{sync: true }), 
     new Effect.Fade('drakBackground',{sync: true, to: 0.0} ) 
	],
    { duration: 0.5 }
	);
}

/*
* panier
*/

function switch_cartTabs(element){
	init_cartTabs();
	var sep_droite = "url(/images/panier/onglets/separateur_onglets-droite.png)";
	var sep_gauche = "url(/images/panier/onglets/separateur_onglets-gauche.png)"
	switch(element){
		case 't1':
			$('t1').setStyle({'background-position':'left top'});
			$('sep1').setStyle({'background-position':'left top','background-image':sep_droite});
		break;
		case 't2':
			$('t2').setStyle({'background-position':'left top'});
			$('sep1').setStyle({'background-position':'left top','background-image':sep_gauche});
			$('sep2').setStyle({'background-position':'left top','background-image':sep_droite});
		break;
		case 't3':
			$('t3').setStyle({'background-position':'left top'});
			$('sep2').setStyle({'background-position':'left top','background-image':sep_gauche});
			$('sep3').setStyle({'background-position':'left top','background-image':sep_droite});
		break;
		case 't4':
			$('t4').setStyle({'background-position':'left top'});
			$('sep3').setStyle({'background-position':'left top','background-image':sep_gauche});
			$('sep4').setStyle({'background-position':'left top','background-image':sep_droite});
		break;
		case 't5':
			$('t5').setStyle({'background-position':'left top'});
			$('sep4').setStyle({'background-position':'left top','background-image':sep_gauche});
		break;
	}
}

function init_cartTabs(){
	for(var i=1; i<=5; i++){
		$('t'+i).setStyle({'background-position':'left bottom'});
	}
	for(var j=1; j<=4; j++){
		$('sep'+j).setStyle({'background-position':'left bottom','background-image':'url(/images/panier/onglets/separateur_onglets-gauche.png)'});
	}
}


/*
* popup commande
*/
function showPopupCommande(url){
	var pageSize = getPageSize();
	new Insertion.Top('wrapper', "<div id='drakBG2' class='drakBG2'></div>");
	$('drakBG2').setStyle({
		'width':'100%',
		'height':pageSize[1]+'px'
	});
	
	var cContent = "<div id='popupC' class='popup' style='width:710px; height:530px; z-index:1001; padding:5px;'>";
		cContent += "<div style='float:left; clear:both; width:700px; text-align:right;'><a href='#' onclick='hidePopupCommande();return false;'>Close</a></div>";
		cContent += "<div style='float:left; clear:both; width:700px;'>";
		cContent += "<iframe src='"+url+"' allowtransparency='true' frameborder='0' width='700px' height='500px'></iframe>";
		cContent += "</div>";
		cContent += "</div>";
		
	new Insertion.Top('wrapper', cContent);
	/*$('popupC').setStyle({
		'left':(pageSize[2]/2)-($('popupC').getWidth()/2)+'px',
		'top':(pageSize[3]/2)-($('popupC').getHeight()/2)+'px'
	});*/
	$('popupC').setStyle({
		'left':'50%',
		'top':'200px',
		'marginLeft':'-'+($('popupC').getWidth()/2)+'px'
	});
	
	new Effect.Parallel(
    [ 
	 new Effect.Appear('popupC',{sync: true }), 
     new Effect.Appear('drakBG2',{sync: true, to: 0.7, duration:0.4} ) 
	],
    { duration: 0.8 }
	);
	
	//create window.resize listener
	/*Event.observe(window, 'resize', function(){
		var pageSize = getPageSize();
		$('drakBG2').setStyle({
			'width':'100%',
			'height':pageSize[1]+'px'
		});
		$('popupC').setStyle({
			'left':(pageSize[2]/2)-($('popupC').getWidth()/2)+'px',
			'top':(pageSize[3]/2)-($('popupC').getHeight()/2)+'px'
		});
	});*/
}
function hidePopupCommande(){
	$('drakBG2').remove();
	$('popupC').remove();
	Event.stopObserving(window, 'resize', null);
}

function toggleAllCommande(){
	
	var height;
	if($('commandeUL').getDimensions().height == '80'){
		height = '400px';
		$('commandeUL').setStyle({'overflow-y':'scroll'});
	}else{
		height = '80px';
		$('commandeUL').setStyle({'overflow-y':'hidden'});
	}
	
	new Effect.Morph('commandeUL',{
  		style:{
    		'height':height
  		}
  	});
}

/*
* send data to swf file
*/
	  
function FlashMovie(movieName) {
	//var isIE = navigator.appName.indexOf("Microsoft") != -1;
	//return (isIE) ? window[movieName] : document[movieName];
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}
	
function addTocart(nom, ref, id, prix, url, lens, taille, couleur){
	FlashMovie("sotester").asFunc(nom, ref, id, prix, url, lens, taille, couleur);
	
}

function getCart(){
	FlashMovie("fcart").getCart();
}

function clearCart(id){
	if(id != "all"){
		$('p_'+id).remove();
		cartArray.splice(id,1);
	}else{
		$$('tr.cartItem').each(function(e){
			$(e).remove();
		});
		cartArray = [];
	}
	FlashMovie("sotester").clearCart(id);
	calculateTotal();
}


var cartArray = new Array();
function getJsCart(data){
	cartArray = data;
	generateCart();
}

function generateCart(){
	
	//alert("cart length : " + cartArray.length);
	var data =  "";
	if(cartArray.length > 0){
		for(var i=0; i<cartArray.length; i++){
			data +='<tr id="p_'+i+'" class="cartItem">';
			data +='<td align="center" class="th_empty"><input type="hidden" name="idproduit[]" id="idproduit_'+i+'" value="'+cartArray[i].idproduit+'" /><a href="#" onclick="clearCart('+i+');return false;"><img src="/images/panier/delete.png" border="0" /></a></td>';
			data +='<td class="th_center2"><a href="'+cartArray[i].url+'">'+cartArray[i].nom+'';
			if(cartArray[i].couleur != undefined){
				data += ' '+cartArray[i].couleur+' ';
			}
			if(cartArray[i].lens.length > 0){
				data += '/ ';
				for(var j=0; j<cartArray[i].lens.length; j++){
					data += ' '+cartArray[i].lens[j];
					if(j>0){
						data += ', ';
					}
				}
			}
			if(cartArray[i].taille != undefined){
				data += '/ '+(cartArray[i].taille)+' ';
			}
			data += '</a><br/>Ref : '+cartArray[i].ref+'';
			data += '</td>';
			data +='<td align="center" class="th_center2"><input name="qte[]" id="qte_'+i+'" type="text" value="'+cartArray[i].quantite+'" size="4"  onblur="checkQte('+i+')" /></td>';
			data +='<td align="center" class="th_center2" id="prix_'+i+'">'+parseFloat(cartArray[i].prix) * cartArray[i].quantite+' &euro;</td>';
			data +='</tr>';
		}
	}else{
		data +='<tr id="p_'+i+'" class="cartItem">';
		data +='<td align="center" class="th_empty">&nbsp;</td>';
		data +='<td class="th_center2">&nbsp;</td>';
		data +='<td align="center" class="th_center2">&nbsp;</td>';
		data +='<td align="center" class="th_center2" id="prix_'+i+'">&nbsp;</td>';
		data +='</tr>';
	}
	$('genCart').update(data);
	calculateTotal();
	
	//onkeyup="reCalculateQte(this.value,'+cartArray[i].prix+','+i+')"
}

function reCalculateQte(value, price, index){
	$('prix_'+index).innerHTML = price*value + " €";
	FlashMovie("sotester").updateQte(cartArray[index].idproduit,value);
	calculateTotal();
}


function calculateTotal(){
	var total = 0;
	for(var i=0; i<cartArray.length; i++){
		total += cartArray[i].prix * $F('qte_'+i);
	}
	$('totalCart').innerHTML = total.toFixed(2) + " &euro;";
}

function addCoupon(id,prix_coupon){
	FlashMovie("sotester").prixcoupon(id,prix_coupon);
	calculateTotal();
}


function checkQte(index){
	if($F('qte_'+index) == 0  || $F('qte_'+index) > 99){
		$('qte_'+index).value = 1;
		reCalculateQte(1,cartArray[index].prix,index);
	}else{
		reCalculateQte($F('qte_'+index),cartArray[index].prix,index);
	}
	
}


function moveToNextStep(current,to,jump){
	if(jump){
		$('etape'+current).hide();
		$('etape'+(to+1)).show();
		switch_cartTabs('t'+(to+1));
	}else{
		$('etape'+current).hide();
		$('etape'+to).show();
		switch_cartTabs('t'+to);
	}
	
	
}

// product details (declinaison)

function showPDetails(lang, id, t){
	//$('detailsProduct').update('loading ...');
	//$('detailsProduct2').show();
	
	var currentX = $('detailsProduct2').getStyle('left');
	if(currentX == '0px'){
		new Effect.Move('detailsProduct2',
		{
			x:350,
			duration:0.5, 
			afterFinish:function(){
				$('detailsProduct2').innerHTML = '';
				showPDetails(lang, id, t);
			}
		});
	}else{
		$('detailsProduct2').addClassName('ploading');
		new Ajax.Request('/detailsp/declinaison_details.php?lang='+lang,
		{
			parameters:{action:'showPDetails', idproduit:id, tp:t},
			onSuccess:function(response){
				$('detailsProduct2').removeClassName('ploading');
				$('detailsProduct2').innerHTML = response.responseText;
			}
		});	
		$('dtp1M').setStyle({width:'352px'});
		new Effect.Move('detailsProduct2',{x:-350, duration:0.5});
	}
	
}
function hidePDetails(){
	
	new Effect.Parallel(
		[ new Effect.Move('detailsProduct2',{x:350, sync:true, duration:1.0})],
		{ afterFinish:function(){
			$('detailsProduct2').innerHTML = '';
			$('dtp1M').setStyle({width:'0px'});
			//$('detailsProduct2').hide();
		}}
	);
	
}

// validate search field
function validateSearchField(field,error,defaultText){
	var len = $F(field).length;
	if((len < 3) || ($F(field) == defaultText)){
		alert(error);
		return false;
	}else{
		return true;
	}
}

//show hide tooltip
var tooltip = {
	show:function(element){
		if($(element)){
			if(!$(element).visible()){
				$(element).show();
				//new Effect.Appear(element, {duration:0.5});
			}
		}
	},
	hide:function(element){
		if($(element)){
			if($(element).visible()){
				$(element).hide();
				//new Effect.Fade(element, {duration:0.5});
			}
		}
	}
}

// comboBox
var comboBox = {
	element:null,
	show:function(element){
		var nextElement = $(element).next();
		nextElement.toggle();
		this.draw();
		this.element = $(element);
		this.observe(element);
	},
	draw:function(){
		$$('.comboBoxList').each(function(e){
			var dimensions = e.getDimensions();		
			if(dimensions.width < 164)
				e.setStyle({width:'164px'});
			if(dimensions.height > 250)
				e.setStyle({height:'250px',overflow:'auto'});
			
		});
	},
	observe:function(element){
		Event.observe(window, 'click', function(event){
			var currentElement = Event.element(event);
			// alert("currentElement.id = " +  currentElement.tagName);
			//console.log("currentElement.id = " +  currentElement.tagName);
			//alert(currentElement.tagName)
			if(currentElement.id != element.id && currentElement.tagName != 'UL'){
				$(element.id).next().hide();
				//Event.stop(event);
			}
		});
	}
}