/*------------------------- BASE FUNCTIONS -------------------------*/

Object.prototype.getTags = function(t){return this.getElementsByTagName(t);}
Object.prototype.getStyle = function(){return this.style;}
Object.prototype.switchDisplay = function() {if( !this.style.display || this.style.display == 'none' ) this.display = 'block'; else this.display = 'none';}
Object.prototype.reset = function(){this.value='';}

/*------------------------- WINDOWS ONLOAD FUNCTION ---------------------*/

function Window_Load (){
	Popup = new Popups();
	
	if(window.addEventListener){
		window.addEventListener("keyup", Window_KeyUp, false);
	}else if(window.attachEvent){
		document.body.attachEvent("onkeyup", Window_KeyUp);
	} 
}
function Window_KeyUp(e){
	var keynum;if(window.event){keynum=e.keyCode;}else if(e.which){keynum=e.which};
	if(keynum==27&&Popup)Popup.Close();
}
window.onload = Window_Load;

/*------------------------- SPECIFICS FUNCTIONS -------------------------*/

// Gestion des rollOvers du block service de gauche
function servOver( obj, over ) {
	var currentImg = obj.getElementsByTagName('img')[0];
	
	var endLength,imgSuffix;
	if(over){
		endLength = 4;
		imgSuffix = "l.gif";
	}else{
		endLength = 5;
		imgSuffix = ".gif";
	} 
	var imgName = currentImg.src.substring(pageURL.length+5, currentImg.src.length-endLength);
	
	currentImg.src = pageURL + "ximg/" + imgName + imgSuffix;
}

function flagOver( flagID ) {

	var flagLayer = document.getElementById('lang_select_layer');
	
	if(flagID) flagLayer.className = flagID;
	else flagLayer.className = current_lang;

}

/*------------------------ VARIABLES POUR LA POPUP ------------------------*/

var nb_btn = 5; //Nombre de boutons de la toolbar
var nb_line = 0; //Nombre de ligne du block quick command

var Popup = null;
function Popups(){
	this.object = document.getElementById("alpha_layer");
	this.toolbar = document.getElementById("toolBar");
	this.toolbarButtons = this.toolbar.getElementsByTagName("li");
	
	this.Popups = new Array();
	this.Popups.push(document.getElementById("mailing_popUp_layer"));
	this.Popups.push(document.getElementById("lostPass_popUp_layer"));
	this.Popups.push(document.getElementById("addToCart_popUp_layer"));
	this.Popups.push(document.getElementById("quickCommand_popUp_layer"));
	this.CurrentContent = null;
}
Popups.prototype.Close = function (){
	this.object.style.display = "none";
}
Popups.prototype.MaskAll = function (){
	for(var i=0; i<this.Popups.length; i++){ this.Popups[i].style.display = "none"; }
	var bts=this.toolbarButtons;
	for(var i=0; i<bts.length; i++){
		if(bts[i].id != "") bts[i].style.display = "none";
	}
}
Popups.prototype.DisplayContent = function (ID){
	this.MaskAll();
	for(var i=0; i<this.Popups.length; i++){
		if(this.Popups[i].id == ID){
			this.Popups[i].style.display = "block";
			this.CurrentContent = this.Popups[i];
		}
	}
	for(var i=0; i<this.toolbarButtons.length; i++){
		if(this.toolbarButtons[i].id == ID.replace("_layer", "_btn"))
			this.toolbarButtons[i].style.display = "block";
	}
	this.object.style.display = "block";
}

Popups.prototype.Mailing = function(){
	this.DisplayContent("mailing_popUp_layer");
}
Popups.prototype.LostPassword = function(){
	this.DisplayContent("lostPass_popUp_layer");
}

Popups.prototype.AddToCart = function(){
	this.DisplayContent("addToCart_popUp_layer");
}

Popups.prototype.QuickCommand = function(){
	this.DisplayContent("quickCommand_popUp_layer");
	$('toolBar').style.marginTop = "0px";
}
Popups.prototype.QuickCommandAddLine = function(){
	var LineLayer = document.getElementById("lines_layer");
	var FieldSet = LineLayer.getElementsByTagName("fieldset")[0];
	var NbLines = (LineLayer.getElementsByTagName("input").length / 2) + 1;
	
	FieldSet.innerHTML += '<input type="text" value="" id="code_commande' + NbLines + '" name="code_commande' + NbLines + '" class="cde">';
	FieldSet.innerHTML += '<input class="qte" type="text" value="" id="qte' + NbLines + '" name="qte' + NbLines + '"><br />';
	
	var marge = $('toolBar').style.marginTop;
	//alert("marge1 = " + marge);
	marge = marge.substring(0, marge.length - 2);
	//alert("marge2 = " + marge);
	marge = parseInt(marge) + 22;
	//alert("marge3 = " + marge);
	var marge = marge + "px";
	//alert("marge4 = " + marge);
	$('toolBar').style.marginTop = marge;
}
function check_str(value,msg_error){
	if(value == ""){
		alert(msg_error);
		return 1;
	}
	return 0;
}

function check_form(form,newClient){
				
	var tmp_form = document.getElementById(form);
	var error = 0;

	if(newClient) error+=check_str(tmp_form.username.value,label_error_champ + " " + label_User);
	error+=check_str(tmp_form.nom.value,label_error_champ + " " + label_Nom);
	error+=check_str(tmp_form.prenom.value,label_error_champ + " " + label_Prenom);
	error+=check_str(tmp_form.email.value,label_error_champ + " " + label_Email);
	error+=check_str(tmp_form.adresse1.value,label_error_champ + " " + label_Adresse);
	error+=check_str(tmp_form.cp.value,label_error_champ + " " + label_CP);
	error+=check_str(tmp_form.ville.value,label_error_champ + " " + label_Ville);
	error+=check_str(tmp_form.tel.value,label_error_champ + " " + label_Tel);
	
	if(error) void(0);
	else tmp_form.submit();

}

/*---------- GALLERY ----------*/
	var galleryId = 'BBGal_layer'; /* change this to the ID of the gallery list */
	var	gallery; /* this will be the object reference to the list later on */
	var galleryImages; /* array that will hold all child elements of the list */
	var currentImage; /* keeps track of which image should currently be showing */
	var previousImage;
	var preInitTimer;
	preInit();

/* functions */

function preInit() {

	if ((document.getElementById)&&(gallery=document.getElementById(galleryId))) {
		gallery.style.visibility = "hidden";
		if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		preInitTimer = setTimeout("preInit()",2);
	}
}

function fader(imageNumber,opacity) {
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	var obj=galleryImages[imageNumber];
	if (obj.style) {
		if (obj.style.MozOpacity!=null) {  
			/* Mozilla's pre-CSS3 proprietary rule */
			obj.style.MozOpacity = (opacity/100) - .001;
		} else if (obj.style.opacity!=null) {
			/* CSS3 compatible */
			obj.style.opacity = (opacity/100) - .001;
		} else if (obj.style.filter!=null) {
			/* IE's proprietary filter */
			obj.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}

function fadeInit() {
	if (document.getElementById) {
		preInit(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit first */
		galleryImages = new Array;
		var node = gallery.firstChild;
		/* instead of using childNodes (which also gets empty nodes and messes up the script later)
		we do it the old-fashioned way and loop through the first child and its siblings */
		while (node) {
			if (node.nodeType==1) {
				galleryImages.push(node);
			}
			node = node.nextSibling;
		}
		for(i=0;i<galleryImages.length;i++) {
			/* loop through all these child nodes and set up their styles */
			galleryImages[i].style.position='absolute';
			galleryImages[i].style.top=0;
			galleryImages[i].style.zIndex=0;
			/* set their opacity to transparent */
			fader(i,0);
		}
		/* make the list visible again */
		gallery.style.visibility = 'visible';
		/* initialise a few parameters to get the cycle going */
		currentImage=0;
		previousImage=galleryImages.length-1;
		opacity=100;
		fader(currentImage,100);
		/* start the whole crossfade process after a second's pause */
		window.setTimeout("crossfade(100)", 1000);
	}
}

function crossfade(opacity) {
		if (opacity < 100) {
			/* current image not faded up fully yet...so increase its opacity */
			fader(currentImage,opacity);
			/* fader(previousImage,100-opacity); */
			opacity += 10;
			window.setTimeout("crossfade("+opacity+")", 30);
		} else {
			/* make the previous image - which is now covered by the current one fully - transparent */
			fader(previousImage,0);
			/* current image is now previous image, as we advance in the list of images */
			previousImage=currentImage;
			currentImage+=1;
			if (currentImage>=galleryImages.length) {
				/* start over from first image if we cycled through all images in the list */
				currentImage=0;
			}
			/* make sure the current image is on top of the previous one */
			galleryImages[previousImage].style.zIndex = 0;
			galleryImages[currentImage].style.zIndex = 100;
			/* and start the crossfade after a second's pause */
			opacity=0;
			window.setTimeout("crossfade("+opacity+")", 1000);
		}
		
}
function addEvent(elm, evType, fn, useCapture) {
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
} 

addEvent(window,'load',fadeInit)

