function $(id){return (typeof(id)=='string')?document.getElementById(id):id;}
function $getId(id){return (typeof(id)=='string')?document.getElementById(id):id;}

function hideAll(){
	if(current!='smenu_home'){
		$getId('menu_home').style.color='#253E5C';
		$getId('menu_home').parentNode.style.background='url(http://www.flash-consoles.com/img/menu_off.png) repeat-x';
	}
	if(current!='smenu_reparation'){
		$getId('menu_reparation').style.color='#253E5C';
		$getId('menu_reparation').parentNode.style.background='url(http://www.flash-consoles.com/img/menu_off.png) repeat-x';
	}
	$getId('smenu_reparation').style.display='none';
	if(current!='smenu_flashage'){
		$getId('menu_flashage').style.color='#253E5C';
		$getId('menu_flashage').parentNode.style.background='url(http://www.flash-consoles.com/img/menu_off.png) repeat-x';
	}
	$getId('smenu_flashage').style.display='none';
	if(current!='smenu_achat-vente'){
		$getId('menu_achat-vente').style.color='#253E5C';
		$getId('menu_achat-vente').parentNode.style.background='url(http://www.flash-consoles.com/img/menu_off.png) repeat-x';
	}
	$getId('smenu_achat-vente').style.display='none';
	if(current!='smenu_accessoires'){
		$getId('menu_accessoires').style.color='#253E5C';
		$getId('menu_accessoires').parentNode.style.background='url(http://www.flash-consoles.com/img/menu_off.png) repeat-x';
	}
	$getId('smenu_accessoires').style.display='none';
	if(current!='smenu_a-propos'){
		$getId('menu_a-propos').style.color='#253E5C';
		$getId('menu_a-propos').parentNode.style.background='url(http://www.flash-consoles.com/img/menu_off.png) repeat-x';
	}
	$getId('smenu_a-propos').style.display='none';
}
function show(id){
	hideAll();
	if($getId(id)){$getId(id).style.display='block';}
	if(id!=current){
		$getId(id.substring(1)).parentNode.style.background='#1F3E64';
		$getId(id.substring(1)).style.color='#FFFFFF';
	}
	$getId(current.substring(1)).parentNode.className=(id==current)?'menu':'menuoff';
}

/**************************************/

var autoSlideBlockMove=true;
var autoSlideBlockMoveCurrent=0;
var SlideBlockMoving=false;
function SlideBlock(go){
	if(go){ // APPEL DU MOUVEMENT
		go=go.substr(1, go.length);
		if(!SlideBlockMoving){ // On ne fait rien si un mouvement n'est pas terminé
			for(var i=0; i<4; i++){
				$getId('go'+i).src=(i==go)?'http://www.flash-consoles.com/img/go'+(i+1)+'over.gif':'http://www.flash-consoles.com/img/go'+(i+1)+'.gif';
			}
			autoSlideBlockMoveCurrent=go;
			SlideBlockMove(go); // Mouvement
		}
	}else{ // INITIALISATION
		var div=$getId('SlideBlock').getElementsByTagName('div');
		$getId('SlideBlock').style.position='relative'; // Nécessaire pour passer les contenus en absolute
		for(var i=0; i<div.length; i++){
			div[i].style.cssFloat='none'; // Transformation des blocs de contenu en absolute pour les mouvements
			div[i].style.position='absolute';
			div[i].style.textAlign='left';
			div[i].style.left=(i*$getId('SlideBlock').offsetWidth)+'px';
			div[i].style.width=$getId('SlideBlock').offsetWidth+'px';
		}
		setTimeout(function(){autoSlideBlock();}, 5000);
	}
}
function SlideBlockMove(go){
	SlideBlockMoving=true;
	var div=$getId('SlideBlock').getElementsByTagName('div');
	if(go<div.length){
		var distance=parseFloat(div[go].style.left.replace('px', '')); // Positive ou négative
		var decal=Math.round(Math.floor(distance/5*-1)); // Décalage + si distance - // Décalage - si distance +
		if(decal<0.1 && decal>0){ // La mouvement est ralentie sur la fin et peut mettre très longtemps à se terminer (0,00001 de décalage) donc on fixe une valeur supérieure assurant une fin plus rapide du mouvement
			decal=0.1;
		}else if(decal>(-0.1) && decal<0){ // Idem
			decal=-0.1;
		}
		if((decal>=0.1 && distance<0) || (decal<=0.1 && distance>0)){
			for(var i=0; i<div.length; i++){
				div[i].style.left=Math.round(parseFloat(div[i].style.left.replace('px', ''))+decal)+'px';
			}
			setTimeout(function(){SlideBlockMove(go);},10); // On boucle
		}else{ // Si le décalage arrondi est de 0, on s'assure de la bonne position des blocs en le forçant. Non visible par l'utilisateur.
			for(var i=0; i<div.length; i++){
				div[i].style.left=((go-i)*parseFloat($getId('SlideBlock').offsetWidth)*-1)+'px';
			}
			SlideBlockMoving=false; // Fin du mouvement
		}
	}
}
function autoSlideBlock(){
	if(autoSlideBlockMove){
		var next=(autoSlideBlockMoveCurrent*1+1);
		if(next==$getId('SlideBlock').getElementsByTagName('div').length){next=0;}
		SlideBlock('g'+next);
	}
	setTimeout(function(){autoSlideBlock();}, 6000);
}
function togglePP(){
	if($getId('buttonPP').src.substring($getId('buttonPP').src.lastIndexOf('/'))=='/pause.gif'){
		$getId('buttonPP').src='http://www.flash-consoles.com/img/play.gif';
		autoSlideBlockMove=false;
	}else{
		$getId('buttonPP').src='http://www.flash-consoles.com/img/pause.gif';
		autoSlideBlockMove=true;
	}
}

/**************************************/

function catalog1(id){
	$getId('catalog_xbox').style.display='none';
	$getId('catalog_ps3').style.display='none';
	$getId('catalog_wii').style.display='none';
	$getId('catalog_ds').style.display='none';
	$getId('catalog_'+id).style.display='block';
	switch(id){
		case 'xbox': $getId('cursor1').style.left='55px'; break;
		case 'ps3': $getId('cursor1').style.left='205px'; break;
		case 'wii': $getId('cursor1').style.left='355px'; break;
		case 'ds': $getId('cursor1').style.left='505px'; break;
	}
}
function catalog2(id){
	$getId('catalog_psp').style.display='none';
	$getId('catalog_achat').style.display='none';
	$getId('catalog_tel').style.display='none';
	$getId('catalog_pc').style.display='none';
	$getId('catalog_'+id).style.display='block';
	switch(id){
		case 'psp': $getId('cursor2').style.left='55px'; break;
		case 'achat': $getId('cursor2').style.left='205px'; break;
		case 'tel': $getId('cursor2').style.left='355px'; break;
		case 'pc': $getId('cursor2').style.left='505px'; break;
	}
}

/**************************************/

var Ex, Ey, ContentInfo, initialize=0, maxOpa=100;

function MoveToolTip(layerName, FromTop, FromLeft){
	$getId(layerName).style.top = (FromTop-30)+'px';
	$getId(layerName).style.left = (FromLeft+15)+'px';
}

function ReplaceContent(layerName){
	document.getElementById(layerName).innerHTML = ContentInfo;
}

function Activate(){initialize=1; $getId('ToolTip').style.display='block'; zoom('ToolTip', 0, 1);}
function deActivate(){initialize=0; zoom('ToolTip', maxOpa, 0);}

function register_position(){
	Ex = 0;
	Ey = 0;
	document.onmousemove = launch;
} 

function launch(e){
	//e=(window.event)?window.event:e;
	e=(!e)?window.event:e;
	Ex = e.pageX || e.clientX;
	Ey = e.pageY || e.clientY;
	if('opacity' in $getId('ToolTip').style){
		Ex += window.pageXOffset;
	    Ey += 0;
	}else{
		Ex += document.body.scrollLeft;
	    Ey += (document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop;
	}
	MoveToolTip('ToolTip', Ey, Ex);
}

function EnterContent(layerName, TTitle, TContent){
ContentInfo = '<table border="0" width="100%" cellspacing="0" cellpadding="0" class="table_ToolTip">'+
'<tr><td width="100%" align="center" class="td_ToolTiptitle"><font class="ToolTiptitle">&nbsp;'+TTitle+'</font></td></tr>'+
'</tr><tr><td width="100%" class="td_ToolTipcontent"><font class="ToolTipcontent">'+TContent+'</font></td></tr>'+
'</table>';
	ReplaceContent(layerName);
}

function zoom(e, i, j){
	if(j==0){
		i=i-20;
	}else{
		i=i+20;
	}
	if('opacity' in $getId(e).style){
		$getId(e).style.opacity=i/100;
	}else{
		$getId(e).style.filter='Alpha(opacity='+i+')';
	}
	if(i==0){$getId(e).style.display='none';}
	if(i!=0 && i!=maxOpa && initialize==j){setTimeout(function(){zoom(e, i ,j);}, 0);}
}

/**************************************/

function getXhr(){
	var xhr = null;
	if(window.XMLHttpRequest){ // Firefox et autres
		xhr = new XMLHttpRequest();
	}else if(window.ActiveXObject){ // Internet Explorer
		try{xhr = new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){try{xhr = new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}
	}else{ // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas la méthode asynchrone de Javascript.\nVeuillez le mettre à jour ou optez pour un logiciel de navigation plus courant\ntel que Internet Explorer ou Firefox.\nReportez vous à la rubrique \"Aide\" pour plus d'information.");
		xhr = false;
	}
	return xhr;
}

function addProduct(){
	var data='id='+document.Ajout.id.value+'&q='+document.Ajout.q.value;
	var xhr=getXhr();
	xhr.open('POST', '/panier_update.php?BreakIEcache='+Math.round(Math.random()*10000), true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(data);
	xhr.onreadystatechange = function(){
		if(xhr.readyState==4 && xhr.status==200){
			if(xhr.responseText!='ok'){
				alert(xhr.responseText);
			}else{
				alert('Ajouté au panier !');
				goPanier();
			}
		}
	}
}
function actFromPanier(f){
	var form1=eval('document.'+f);
	if(form1.s.checked!=false){
		return deleteProduct(f);
	}else{
		return editQuantity(f);
	}
}
function editQuantity(f){
	var form1=eval('document.'+f);
	var data='id='+form1.id.value+'&q2='+form1.q2.value;
	var xhr=getXhr();
	xhr.open('POST', '/panier_update.php?BreakIEcache='+Math.round(Math.random()*10000), true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(data);
	xhr.onreadystatechange = function(){
		if(xhr.readyState==4 && xhr.status==200){
			if(xhr.responseText!='ok1' && xhr.responseText!='ok2'){
				alert(xhr.responseText);
				totalPanier();
			}else if(xhr.responseText=='ok1'){
				alert('Produit retiré de votre panier !');
				window.location='/panier.php';
			}else if(xhr.responseText=='ok2'){
				alert('Quantité modifiée !');
				totalPanier();
			}
		}
	}
}
function deleteProduct(f){
	var form1=eval('document.'+f);
	var data='s=1&id='+form1.id.value;
	var xhr=getXhr();
	xhr.open('POST', '/panier_update.php?BreakIEcache='+Math.round(Math.random()*10000), true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(data);
	xhr.onreadystatechange = function(){
		if(xhr.readyState==4 && xhr.status==200){
			if(xhr.responseText!='ok'){
				alert(xhr.responseText);
				totalPanier();
			}else{
				alert('Produit retiré de votre panier !');
				window.location='/panier.php';
			}
		}
	}
}
function totalPanier(){
	var data='get=total';
	var xhr=getXhr();
	xhr.open('POST', '/panier_update.php?BreakIEcache='+Math.round(Math.random()*10000), true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(data);
	xhr.onreadystatechange = function(){
		if(xhr.readyState==4 && xhr.status==200){
			$getId('total').innerHTML='<b>'+xhr.responseText+' €</b>';
		}
	}
}
function goPanier(){
	var data='get=sum';
	var xhr=getXhr();
	xhr.open('POST', '/panier_update.php?BreakIEcache='+Math.round(Math.random()*10000), true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(data);
	xhr.onreadystatechange = function(){
		if(xhr.readyState==4 && xhr.status==200){
			var plur=(xhr.responseText>1)?'s':'';
			$getId('goPanier').innerHTML=xhr.responseText+' article'+plur;
		}
	}
}
/**************************************/

function isNumberKey(evt){  
	var charCode=(evt.which)?evt.which:event.keyCode;
	return (charCode>31 && (charCode<48 || charCode>57))?false:true;
}
/**************************************/

function scroll_Top(acceleration, time){
    acceleration=acceleration || 0.1;
    time=time || 16;
    var dx=0;
    var dy=0;
    var bx=0;
    var by=0;
    var wx=0;
    var wy=0;
    if(document.documentElement){
        dx=document.documentElement.scrollLeft || 0;
        dy=document.documentElement.scrollTop || 0;
    }
    if(document.body){
        bx=document.body.scrollLeft || 0;
        by=document.body.scrollTop || 0;
    }
    var wx=window.scrollX || 0;
    var wy=window.scrollY || 0;
    var x=Math.max(wx, Math.max(bx, dx));
    var y=Math.max(wy, Math.max(by, dy));
    var speed=1+acceleration;
    window.scrollTo(Math.floor(x/speed), Math.floor(y/speed));
    if(x>0 || y>0){
        var invokeFunction="scroll_Top("+acceleration+", "+time+")";
        window.setTimeout(invokeFunction, time);
    }
}
