/***************************************************************************
 *				javascript.js
 *				--------------
 *	Cree le		: 9 Octobre 2006
 *	Derniere modif.	: 4 Octobre 2007
 *	Auteur		: Asselin Benoit Developpement
 *	Email		: contact(a)ab-d.fr
 *
 ***************************************************************************/



function ClassTravel(v_id) {
	this.id = document.getElementById(v_id);
	this.node = this.id.firstChild;
	
	this.travelStep = 1;
	this.timeMs = 50;
	this.timePause = 100;
	this.MsIE = (navigator.appName == 'Microsoft Internet Explorer') ? true : false;
	
	this.prepare();
	this.travel();
}

ClassTravel.prototype = {
	prepare : function() {
		this.node.style.left = this.id.offsetWidth + 'px';
		this.node.style.visibility = 'visible';
	},
	travel : function() {
		var v_ms, v_left;
		if(parseInt(this.node.style.left) < (- this.node.offsetWidth)) {
			v_left = this.id.offsetWidth;
			v_ms = this.timePause;
		} else {
			v_left = parseInt(this.node.style.left) - this.travelStep;
			v_ms = this.timeMs;
		}
		
		this.node.style.left = v_left + 'px';
		setTimeout(this.travel._bind(this), v_ms);
	}
}



function ClassFade1(v_id, v_images) {
	this.id = v_id;				/* readonly - getElementById() */
	this.imagesUrl = v_images;		/* readonly - Array() */
	this.imagesLen = this.imagesUrl.length;	/* readonly - Integer */
	this.imageFade = 0;			/* readonly - Integer*/
	this.imagesBgcolor = '#FFF';		/* #FFFFFF */
	this.opacityStep = 10;			/* min: 1 // max: 100 */
	this.opacityLevel = 100;		/* 0 or 100 */
	this.timeNext = 0;			/* 0 = random // xxx = fix */
	this.timeMs = 20;			/* speed transition  */
	this.firstRun = true;			/* readonly - Boolean */
	this.MsIE = (navigator.appName == 'Microsoft Internet Explorer') ? true : false;
	
	this.loadImages();
	this.zIndexCss();
	this.fade();
}

ClassFade1.prototype = {
	addZero : function(nbr) {
		return (nbr < 10) ? ('0' + String(nbr)) : (String(nbr));
	},
	
	loadImages : function() {
		for (var v_i = 0; v_i < this.imagesLen; v_i++) {
			if (this.MsIE) {
				/* bug de chargement en boucle avec backgroundImage */
				var v_img = window.document.createElement('img');
				v_img.src = this.imagesUrl[v_i];
				Doc.id(this.id + this.addZero(v_i) ).appendChild(v_img);
			} else {
				var v_img = window.document.createElement('img');
				v_img.src = this.imagesUrl[v_i];
				Doc.id(this.id + this.addZero(v_i) ).appendChild(v_img);
/* img
				Doc.id(this.id + this.addZero(v_i) ).style.backgroundImage = 'url("' + this.imagesUrl[v_i] + '")';
				Doc.id(this.id + this.addZero(v_i) ).style.backgroundRepeat = 'no-repeat';
				Doc.id(this.id + this.addZero(v_i) ).style.backgroundPosition = 'center';
*/
			}
		}
	},
	opacityCss : function() {
		var v_id = this.id + this.addZero(this.imageFade);
		
		if (this.MsIE) {
			Doc.id(v_id).style.opacity = (this.opacityLevel / 100);
			Doc.id(v_id).style.filter = 'alpha(opacity=' + this.opacityLevel + ')';
		} else {
			v_css = '';
/* img			v_css += 'background: ' + this.imagesBgcolor + ' url("' + this.imagesUrl[this.imageFade] + '") no-repeat center; '; */
			v_css += '-moz-opacity:' + ( this.opacityLevel / 100) + '; opacity:' + (this.opacityLevel / 100) + '; ';
			v_css += 'z-index:' + this.imagesLen + '; ';
			Doc.id(v_id).setAttribute('style', v_css, 'false');
		}
	},
	zIndexCss : function () {
		var v_Fade = this.imageFade;
		for (var v_zIndex = (this.imagesLen); v_zIndex > 0; v_zIndex--) {
			var v_id = this.id + this.addZero(v_Fade);
			
			if (this.MsIE) {
				Doc.id(v_id).style.zIndex = v_zIndex;
				Doc.id(v_id).style.opacity = '1';
				Doc.id(v_id).style.filter = 'alpha(opacity=100)';
			} else {
				v_css = '';
/* img				v_css += 'background: ' + this.imagesBgcolor + ' url("' + this.imagesUrl[v_Fade] + '") no-repeat center; '; */
				v_css += '-moz-opacity:1; opacity:1; ';
				v_css += 'z-index:' + v_zIndex + '; ';
				
				Doc.id(v_id).setAttribute('style', v_css, 'false');
			}
			
			v_Fade = ((v_Fade + 1) >= this.imagesLen) ? (0) : (v_Fade + 1);
		}
	},
	nextFade : function() {
		var v_ms = (this.timeNext > 1) ? (this.timeNext) : (Math.random() * 20000);
		return v_ms;
	},
	fade : function() {
		this.opacityCss();
		
		var v_ms;
		if (this.firstRun) {
			this.firstRun = false;
			v_ms = this.nextFade();
			
		} else if (this.opacityLevel <= 0) {
			this.opacityLevel = 100;
				
			this.imageFade = ((this.imageFade + 1) >= this.imagesLen) ? (0) : (this.imageFade + 1);
			this.zIndexCss();
			
			v_ms = this.nextFade();
			
		} else {
			this.opacityLevel = this.opacityLevel - this.opacityStep;
			v_ms = this.timeMs;
		}
		
		setTimeout(this.fade._bind(this), v_ms);
	}
}



function ClassFade2(p_id) {
	this.id = Doc.id(p_id);				/* <div> */
	this.lis = this.id.getElementsByTagName('li');	/* <li> */
	this.lisLength = this.lis.length;		/* Integer */
	this.lisFade = new Array();			/* _ClassFade2 */
	this.liFade = 0;				/* Integer */
	
	this.timeFade = 100				/* ms */
	this.timeImage = 3000;				/* ms */
	
	this.restart();
}

ClassFade2.prototype = {
	getLisAllTrue : function() {
		for(v_i = 0; v_i < this.lisLength; v_i++) {
			if(this.lisFade[v_i] == false) {
				return false;
			}
		}
		return true;
	},
	getOneLI : function() {
		while(!this.getLisAllTrue()) {
			var v_rand = Math.floor(Math.random() * (this.lisLength + 1));
			if(this.lisFade[v_rand] == false) {
				return v_rand;
			}
		}
		return -1; /* finish! */
	},
	fade : function() {
		if(this.lisFade[this.liFade] && this.lisFade[this.liFade].running == true) {
			/* wait... */
			setTimeout(this.fade._bind(this), this.timeFade);
		} else {
			this.liFade = this.getOneLI();
			if(this.liFade != -1) {
				this.lisFade[this.liFade] = new _ClassFade2(this.lis[this.liFade]);
				setTimeout(this.fade._bind(this), this.timeFade);
			} else {
				/* finish! */
				this.restart();
			}
		}
	},
	restart : function() {
		for(v_i = 0; v_i < this.lisLength; v_i++) {
			this.lisFade[v_i] = false;
		}
		
		setTimeout(this.fade._bind(this), this.timeImage);
	}
}

function _ClassFade2(p_element) {
	this.element = p_element;				/* <li> */
	this.images = this.element.getElementsByTagName('a');	/* <a style="background-image: ;"> */
	this.imageFade = null;					/* .item()	-->>	z-index: MAX; */
	this.imageNext = null;					/* .item()	-->>	z-index: MAX-1; */
	
	this.opacityStep = 5;					/* min: 1 // max: 100 */
	this.opacityLevel = 100;				/* 0 or 100 */
	this.timeMs = 20;					/* speed transition  */
	this.running = true;					/* boolean */
	
	this.findFadeAndNext();
	this.fade();
}

_ClassFade2.prototype = {
	findFadeAndNext : function() {
		var v_length = this.images.length;
		for(var v_i = 0; v_i < v_length; v_i++) {
			if(this.images[v_i].style.zIndex == v_length) {
				this.imageFade = v_i;
			}
			if(this.images[v_i].style.zIndex == (v_length - 1)) {
				this.imageNext = v_i;
			}
		}
	},
	css : function() {
		var v_node = this.images[this.imageFade];
		var v_css = '';
		v_css += 'background-image:' + v_node.style.backgroundImage + ';';	/* readonly */
		v_css += 'z-index:' + v_node.style.zIndex + ';';			/* readonly */
		
		v_css += 'filter:alpha(opacity=' + this.opacityLevel + ');';		/* mie */
		v_css += '-moz-opacity:' + (this.opacityLevel / 100) + ';';		/* moz */
		v_css += 'opacity:' + (this.opacityLevel / 100) + ';';			/* css */
		v_node.style.cssText = v_css;
	},
	fade : function() {
		this.css();
		this.opacityLevel -= this.opacityStep;
		
		if(this.opacityLevel < 0) {
			this.opacityLevel = 0;
			this.css();
			this.finish();
		} else {
			setTimeout(this.fade._bind(this), this.timeMs);
		}
	},
	finish : function() {
		var v_length = this.images.length;
		
		/* rebuild: z-index */
		var v_i = this.imageNext;
		for(var v_zIndex = v_length; v_zIndex >= 1; v_zIndex--) {
			var v_node = this.images[v_i];
			var v_css = '';
			v_css += 'background-image:' + v_node.style.backgroundImage + ';';
			v_css += 'z-index:' + v_zIndex + ';';
			v_node.style.cssText = v_css;
			
			v_i = (v_i >= (v_length - 1)) ? (0) : (v_i + 1);
		}
		
		this.running = false;
	}
}


var v_kitchenFade = false;
function kitchenClick(p_mode) {
	var v_id = Doc.id('kitchen-images');
	
	if(v_kitchenFade && v_kitchenFade.running == true) {
		/* setTimeout( function(){kitchenClick(p_mode);} , 100); */
	} else {
		v_kitchenFade = new _ClassFade2(v_id);
	}
}



function openMe(v_href, v_h, v_w, v_scroll) {
	var v_scroll = (!v_scroll) ? 'yes' : v_scroll;
	var v_h = (!v_h) ? 540 : v_h;
	var v_t = (screen.height - v_h) / 3;
	var v_w = (!v_w) ? 540 : v_w;
	var v_l = (screen.width - v_w) / 2;
	window.open(v_href, '', 'scrollbars='+v_scroll +',status=no,toolbar=no,width='+v_w +',height='+v_h +',left='+v_l +',top='+v_t ).focus;
	return false;
}

function noSpam(mt1, mt2, mt3) {
	var link = 'mailto:' + mt1 + '@' + mt2 + '.' + mt3;
	location.href = link;
	return false;
}



var v_product_visibility = '';

function displayProduct(v_href, v_id) {
	if (v_href) {
		openMe(v_href);
	}
	if (Doc.id(v_product_visibility)) {
		Doc.id(v_product_visibility).style.display = 'none';
	}
	v_product_visibility = v_id;
	if (Doc.id(v_product_visibility)) {
		Doc.id(v_product_visibility).style.display = 'inline';
	}
	return false;
}



function displayDiv(id) {
	Doc.id(id).style.display = Doc.id(id).style.display == 'block' ? 'none' : 'block';
}


var v_menu_key = '';
function menu(key, mode) {
	var v_a = (v_menu_key != '') ? ('sous-menu-'+v_menu_key+'-a') : ('');
	var v_ul = (v_menu_key != '') ? ('sous-menu-'+v_menu_key+'-ul') : ('');
	
	if (v_menu_key != '') {
		if (Doc.id(v_a) && Doc.id(v_a).className != 'active') { Doc.id(v_a).className = ''; }
		if (Doc.id(v_ul)) { Doc.id(v_ul).style.display = 'none'; }
	}
	
	v_menu_key = key;
	
	if (v_menu_key != '') {
		v_a = (v_menu_key != '') ? ('sous-menu-'+v_menu_key+'-a') : ('');
		v_ul = (v_menu_key != '') ? ('sous-menu-'+v_menu_key+'-ul') : ('');
		if (Doc.id(v_a) && Doc.id(v_a).className != 'active') { Doc.id(v_a).className = 'active-js'; }
		if (Doc.id(v_ul)) { Doc.id(v_ul).style.display = mode ? 'block' : 'none'; }
	}
}



