/*-------------------------------------------------------------------
"Léa aime les fleurs" Javascript functions
---------------------------------------------------------------------
20061115  - ESPACE DYNAMIQUE - ADH V1.00
---------------------------------------------------------------------*/
// Global variables
var d = document;

// Slide variables
var iDelayImg = 3500; // 3s
var iDelay = 50;
var iInc = 50;
var FirstSlide, SecondSlide;
var MaxSlide;
var Slide;
var Sty0, StyI0, Sty1, StyI1;
var xSize = 338; // image size
var ySize = 338;
var bSlideRun = true;
var bSlideInProcess = false;
var NbImg = 0;
// Slide button
var LStartStop = '35px';
var RStartStop = '315px';
// ChangeWay
var LeftMenuPos = 376; // see CSS file
var LeftContentPos = 354; // -22px
var LeftPosL = '220px';
var LeftPosR = '0px';
// Tip
var iTipDelay = 50;
var TipHeight = 50;
var TipTop = 284;
var TipIncH = 10;
/*-------------------------------------------------------------------
20061115
---------------------------------------------------------------------*/
function Init (P) {
	// Slide function
	aImg = Array();
	Id = 'img' + NbImg;
	if (d.getElementById(Id)) {
		Sty0 = d.getElementById('ImgD' + 0).style;
		Sty1 = d.getElementById('ImgD' + 1).style;
		StyI0 = d.getElementById('ImgI' + 0).style;
		while(d.getElementById(Id)) {	
			aImg[NbImg] = new Image(xSize, ySize);

			aImg[NbImg].src = d.getElementById(Id).value;
			NbImg++;
			Id = 'img' + NbImg;
		}
		// clear title and alt img attributs
		o = d.getElementById('ImgI0');
		o.title = o.alt = '';
		FirstSlide = 0;
		MaxSlide = NbImg;
		if (NbImg > 1) {
			window.setTimeout(Slide, iDelayImg);
			var pc = d.getElementById('PictCmd');
			pc.style.visibility = 'visible';
			pc.title = 'Infos produit';
		}
	}
	// flip-flop menu
	var sc = d.getElementById('Content').style;
	var sm = d.getElementById('Menu').style;
	var cw = d.getElementById('ChangeWay');
	// ChangeWay button
	if (CookiesEnabled()) {
		cw.style.visibility = 'visible';
		var Menu = GetCookie('LeftMenu');
		var Content = GetCookie('LeftContent');
		if (!((Menu == '') && (Content == ''))) { // only if cookies filled <> 0
			sc.left = ((Content == '0') ? Content : LeftContentPos) + 'px';
			sm.left = ((Menu == '0') ? Menu : LeftMenuPos) + 'px';
			if (Menu > Content) {
				cw.style.backgroundImage = 'url("img/BpToL.gif")';
				cw.title = 'Menu à gauche';
				cw.style.left = LeftPosL;
//				if (NbImg > 1) pc.style.left = LStartStop;
			}
			else {
				cw.style.backgroundImage = 'url("img/BpToR.gif")';
				cw.title = 'Menu à droite';
				cw.style.left = LeftPosR;
//				if (NbImg > 1) pc.style.left = RStartStop;
			}
		}
	}
	sc.visibility = 'visible';
	sm.visibility = 'visible';
}	
/*-------------------------------------------------------------------
20060531
---------------------------------------------------------------------*/
function Slide () {
	if (!bSlideRun) {
		return;
	}
	if (bSlideInProcess) return;
	bSlideInProcess = true;
	SecondSlide = FirstSlide + 1;
	if (SecondSlide >= MaxSlide) SecondSlide = 0;
	Sty0.width = xSize + 'px';
	StyI0.left = '0';
	Sty1.width = '0';
	Sty1.left = xSize + 'px';
	d.getElementById('ImgI' + 1).src = aImg[SecondSlide].src;
	window.setTimeout(MoveSlide, iDelay);
}
/*-------------------------------------------------------------------
20060531
---------------------------------------------------------------------*/
function MoveSlide () {
	var iWidth0 = parseInt(Sty0.width);
	var iLeft0 = parseInt(StyI0.left);
	var iWidth1 = parseInt(Sty1.width);
	var iLeft1 = parseInt(Sty1.left);
	if (iWidth0 > iInc) {
		Sty0.width = (iWidth0 - iInc) + 'px';
		StyI0.left = (iLeft0 - iInc) + 'px';
		Sty1.width = (iWidth1 + iInc) + 'px';
		Sty1.left = (iLeft1 - iInc) + 'px';
		window.setTimeout(MoveSlide, iDelay);
	}
	else {
		Sty0.width = '0';
		Sty1.left = '0';
		Sty1.width = xSize + 'px';
		window.setTimeout(EndMove, iDelayImg);
	}
}
/*-------------------------------------------------------------------
20060531
---------------------------------------------------------------------*/
function EndMove() {
	d.getElementById('ImgI' + 0).src = d.getElementById('ImgI' + 1).src;
	Sty0.width = xSize + 'px';
	StyI0.left = '0';
	FirstSlide++;
	if (FirstSlide >= MaxSlide) FirstSlide = 0;
	bSlideInProcess = false;
	window.setTimeout(Slide, iDelay);
}
/*-------------------------------------------------------------------
20050912
---------------------------------------------------------------------*/
function TestFields() {
	if (
		(d.getElementsByName("eMail")[0].value == "")
		) {
		alert("Merci de saisir une adresse email");
		return false;
	}
	var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,4}$/
	if (reg.exec(d.getElementsByName("eMail")[0].value) == null) {
		alert("Merci de saisir une adresse email valide");
		return false;
	}
	var txtMsg = "Vous souhaitez m'envoyer ce message : ";
	var txtTmp;
	txtMsg += " :\n\n";
	txtMsg += "Nom : " + d.getElementsByName('Name')[0].value + " - ";
	txtMsg += "Téléphone : " + d.getElementsByName('Phone')[0].value + " - ";
	txtMsg += "E-mail : " + d.getElementsByName('eMail')[0].value;
	txtMsg += "\n\n";

	txtTmp = d.getElementsByName('Message')[0].value;
	txtMsg += "Message :\n" + txtTmp + "\n";
	//
	txtMsg += "\nEst-ce correct ?\n";
	if (confirm(txtMsg)) {
		alert("Merci de nous avoir contacté !");
		return true;
	}
	return false;
}
/*-------------------------------------------------------------------
20061115
---------------------------------------------------------------------*/
function ChangeWay() {
	var LeftMenu, LeftContent;
	var OffsetContent = d.getElementById('Content').offsetLeft;
	var OffsetMenu = d.getElementById('Menu').offsetLeft;
	var cw = d.getElementById('ChangeWay');
	var pc = d.getElementById('PictCmd');
	if (OffsetMenu > OffsetContent) { // menu to right
		LeftContent = OffsetMenu - 22;
		LeftMenu = OffsetContent;
		cw.style.backgroundImage = 'url("img/BpToR.gif")';
		cw.style.left = LeftPosR;
		cw.title = 'Menu à droite';
//		if (NbImg > 1) pc.style.left = RStartStop;
	}
	else {
		LeftContent = OffsetMenu;
		LeftMenu = OffsetContent + 22;
		cw.style.backgroundImage = 'url("img/BpToL.gif")';
		cw.style.left = LeftPosL;
		cw.title = 'Menu à gauche';
//		if (NbImg > 1) pc.style.left = LStartStop;
	}
	d.getElementById('Content').style.left = LeftContent + 'px';
	d.getElementById('Menu').style.left = LeftMenu + 'px';
	SetCookie('LeftMenu', LeftMenu, 10960); // expire : 1 week
	SetCookie('LeftContent', LeftContent, 10960);
}
/*-------------------------------------------------------------------
20061115
---------------------------------------------------------------------*/
function CookiesEnabled () {
/**
	var bOk = (navigator.cookieEnabled) ? true : false;
	if (typeof navigator.cookieEnabled == "undefined" && !bOk) {
		d.cookie = 'TestCookie';
		bOk = (d.cookie.indexOf('TestCookie') != -1) ? true : false;
	}
**/
	SetCookie('TestCookie', 0, 0);
	bOk = (d.cookie.indexOf('TestCookie') != -1) ? true : false;
	return bOk;
}
/*-------------------------------------------------------------------
20061115
Expire in minutes
---------------------------------------------------------------------*/
function SetCookie (Name, Value, Expire)
{
	var strCookie = Name + '=' + Value;
	if (Expire != 0) {
		var Today = new Date();
		var DateExpire = new Date();
		DateExpire.setTime(Today.getTime() + (Expire * 60 * 1000));
		strCookie += ';expires=' + DateExpire.toGMTString() ;
	}
	d.cookie = strCookie;
}
/*-------------------------------------------------------------------
20061115
cookie form : "name1=value1;name2=value2;etc."
---------------------------------------------------------------------*/
function GetCookie(Name)
{
	var strCookie = d.cookie;
	aCookie = strCookie.split(';');
	for (var i in aCookie) {
		if (aCookie[i].indexOf(Name) != -1) {
			aRes = aCookie[i].split('=');
			return aRes[1];
		}
	}
	return ''; // not found
}
/*-------------------------------------------------------------------
20061116
---------------------------------------------------------------------*/
function PictCmd() {
	var pc = d.getElementById('PictCmd');
	if (bSlideRun) {
		pc.style.backgroundImage = 'url("img/BpStart.gif")';
		pc.title = 'Autres produits';
		bSlideRun = false;
		ShowTip(true);
	}
	else {
		pc.style.backgroundImage = 'url("img/BpStop.gif")';	
		pc.title = 'Infos produit';
		bSlideRun = true;
		ShowTip(false);
		Slide();
	}
}
/*-------------------------------------------------------------------
20061116
---------------------------------------------------------------------*/
function ShowTip(bShow) {
	if (bShow) {
		Id = 'XImg' + ((SecondSlide == undefined) ? FirstSlide : SecondSlide);
		if (d.getElementById(Id)) {
			txt = d.getElementById(Id).value;
			var a = txt.split("\n");
			if (a.length > 1) {
				txt = '';
				for (i = 0; i < a.length - 1; i++)
					txt += a[i] + '<br />';
				txt += a[i];
			}
			var o = d.getElementById('TipText');
			o.innerHTML = txt;
			o.style.top = '334px';
			o.style.height = '0px';
			o.style.visibility = 'visible';
			window.setTimeout(MoveTip, iTipDelay);
		}
	}
	else {
		d.getElementById('TipText').style.visibility = 'hidden';
	}
}
/*-------------------------------------------------------------------
20061116
---------------------------------------------------------------------*/
function MoveTip() {
	var s = d.getElementById('TipText').style;
	var Top = parseInt(s.top);
	var Height = parseInt(s.height);
	Height += TipIncH;
	if (Height < TipHeight) {
		Top -= TipIncH;
		window.setTimeout(MoveTip,  iTipDelay);
	}
	else {
		Height = TipHeight;
		Top = TipTop;
	} 
	s.height = Height + 'px';
	s.top = Top + 'px';
}
