/**
 *
 * @author	Benoit Asselin <benoit(at)agenceici(dot)com>
 * @version	javascript.js, 2010/02/04
 * @link	http://www.agenceici.com/
 *
 */


var windowOnload = '';
window.onload = function(){eval(windowOnload);};



/**
 * Convertir la transparence des images PNG sous Internet Explorer 6.0 et -
 * Ex: window.onload = function() { fixiePNG(); };
 * @param {String} p_path Chemin du fichier NONE.GIF (default: 'images/')
 */
function fixiePNG(p_path) {
	var msie = navigator.userAgent.match(/msie\s([\d\.]+)/i);
	if(msie && msie[1] <= '6.0') {
		p_path = p_path === undefined ? 'images/' : p_path;
		var png = /\.png$/i;
		var imgs = document.getElementsByTagName('img');
		for(var i = 0, l = imgs.length; i < l; i++) {
			if(png.test(imgs.item(i).src)) {
				imgs.item(i).style.width = imgs.item(i).offsetWidth;
				imgs.item(i).style.height = imgs.item(i).offsetHeight;
				imgs.item(i).style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgs.item(i).src + '\',sizingMethod=\'image\')';
				imgs.item(i).src = p_path + 'none.gif';
			}
		}
	}
}



/**
 * Mangeur de SPAM
 * @param {String} mt1 XXX_at_xxx_dot_xxx
 * @param {String} mt2 xxx_at_XXX_dot_xxx
 * @param {String} mt3 xxx_at_xxx_dot_XXX
 * @param {String} subject Objet du message
 * @return {Boolean} Toujours false
 */
function noSpam(mt1, mt2, mt3, subject) {
	var link = 'mailto:' + mt1 + '@' + mt2 + '.' + mt3;
	if(subject !== undefined) {link += '?subject=' + encodeURIComponent(subject);}
	window.location.href = link;
	return false;
}



/**
 * Ouvrir un popup
 * @param {String} p_href Lien a ouvrir
 * @return {Boolean} Toujours false
 */
function popup(p_href) {
	var v_w = 450;
	var v_h = 520;
	var v_l = (window.screen.width - v_w) / 2;
	var v_t = (window.screen.height - v_h) / 3;
	window.open(p_href, 'popup', 'scrollbars=no,status=no,toolbar=no,width='+v_w +',height='+v_h +',left='+v_l +',top='+v_t ).focus();
	return false;
}



/**
 * Classe permettant de deplacer un element en left et en top (Inspirer de mootools)
 * @param {Object} p_params
 *	{
 *		element: HTMLElement OR 'id',	Element ou IDenfifiant HTML
 *		property: String,		Propriete CSS a manipuler			Liste: 'width', 'height', 'left', 'top'..., 'margin'..., 'borderWidth'..., 'padding'..., 'fontSize', 'letterSpacing', 'lineHeight', 'opacity'...
 *		start: Number,			Point de depart ( default detection )
 *		end: Number,			Point d'arrivee
 *		inc: Number,			Incrementation
 *		transition: String,		Type d'animation ( default 'normal' )		Liste: 'linear', 'normal', 'pow', 'expo', 'circ', 'sine', 'back', 'bounce', 'elastic'
 *		ease: String,			Effet au debut et/ou a la fin ( default 'out')	Liste: 'in', 'out', 'inout'
 *		duration: Number,		Duree de l'animation en ms ( default 1000 )
 *		fps: Number,			Image par seconde ( default 50 )
 *		callback: Function,		Fonction de retour en fin d'animation ( default false )
 *		wait: Boolean			Attendre avant de demarrer ( default false )
 *	}
 * @constructor
 */
function ClassCoreFX(p_params) {
	/** @returns {Object} Les parametres de l'utilisateur */
	this.params = p_params;

	/** @return {HTMLElement} Element a manipuler */
	this.elt = typeof this.params.element == 'object' ? this.params.element : Doc.id(this.params.element);

	/** @return {String} Propriete CSS */
	this.styleProperty = this.params.property;
	/** @return {Number} Valeur de depart */
	this.valueStart = this.params.start === undefined ? false : parseInt(this.params.start);
	/** @return {Number} Valeur d'arrive */
	this.valueEnd = this.params.end === undefined ? false : parseInt(this.params.end);
	/** @return {Number} Valeur a incrementer */
	this.valueInc = this.params.inc === undefined ? 0 : parseInt(this.params.inc);

	/** @return {String} Type de transition */
	this.fxTransition = this.params.transition === undefined ? 'normal' : this.params.transition;
	/** @return {String} Effet au debut et/ou a la fin */
	this.fxEase = this.params.ease === undefined ? 'out' : this.params.ease;
	/** @return {Number} Duree de l'effet */
	this.duration = this.params.duration === undefined ? 1000 : parseInt(this.params.duration);
	/** @return {Number} Image par seconde */
	this.fps = this.params.fps === undefined ? 50 : parseInt(this.params.fps);
	/** @return {Function} Fonction de retour */
	this.callback = (this.params.callback === undefined || this.params.callback === false) ? function(){} : this.params.callback;


	/** @return {Number} Temps de pause pour setTimeout */
	this.delay = Math.round(1000 / this.fps);
	/** @return {Number} Variable pour setTimeout */
	this.timeout = false;
	/** @return {String} Type de valeur du style	Liste: 'px', 'opacity', '' */
	this.styleType = 'px';
	/** @return {Number} Maintenant */
	this.time = 0;

	this._fixDbFX();
	this._detectStyleType();
	this._detectValueStart();
	if(!this.params.wait) {
		this.start();
	}
}
ClassCoreFX.prototype = {
	/**
	 * Date actuelle
	 * @return {Number}
	 */
	_dateNow : function() {
		return Date.now ? Date.now() : +new Date;
	},
	/**
	 * Bloquer la superposition de 2 effets identifiques
	 */
	_fixDbFX : function() {
		if(!this.elt._CoreFX) {this.elt._CoreFX = [];}
		if(!this.elt._CoreFX[this.styleProperty]) {
			this.elt._CoreFX[this.styleProperty] = this;
		} else {
			window.clearTimeout(this.elt._CoreFX[this.styleProperty].timeout);
			this.elt._CoreFX[this.styleProperty] = this;
		}
	},
	/**
	 * Detecter le type de valeur du style
	 */
	_detectStyleType : function() {
		switch(this.styleProperty) {
			case 'width' : case 'height' :
			case 'left' : case 'right' : case 'top' : case 'bottom':
			case 'margin' : case 'marginLeft' : case 'marginRight' : case 'marginTop' : case 'marginBottom' :
			case 'borderWidth' : case 'borderLeftWidth' : case 'borderRightWidth' : case 'borderTopWidth' : case 'borderBottomWidth' :
			case 'padding' : case 'paddingLeft' : case 'paddingRight' : case 'paddingTop' : case 'paddingBottom' :
			case 'fontSize' : case 'letterSpacing' : case 'lineHeight' :
				this.styleType = 'px';
				break;
			case 'opacity' :
				this.styleType = 'opacity';
				break;
			default :
			case 'zIndex' :
				this.styleType = '';
				break;
		}
	},
	/**
	 * Detecter la valeur du style
	 */
	_detectValueStart : function() {
		if(this.valueStart === false) {
			switch(this.styleProperty) {
				case 'opacity' :
					if(this.elt.style[this.styleProperty]) {
						this.valueStart = parseInt(this.elt.style[this.styleProperty] * 100);
					}
					break;
				default :
					if(this.elt.style[this.styleProperty]) {
						this.valueStart = parseInt(this.elt.style[this.styleProperty]);
					}
					break;
			}
			if(this.valueStart === false) {
				switch(this.styleProperty) {
					case 'width' :this.valueStart = this.elt.offsetWidth;break;
					case 'height' :this.valueStart = this.elt.offsetHeight;break;
					case 'left' :this.valueStart = this.elt.offsetLeft;break;
					case 'top' :this.valueStart = this.elt.offsetTop;break;
					default :this.valueStart = 0;break;
				}
			}
		}
	},
	/**
	 * Type de calcul pour les transitions
	 * @param {Number} p
	 * @param {Number} x ?
	 * @return {Number}
	 */
	_transition : function(p, x) {
		switch(this.fxTransition) {
			case 'linear' :
				return p;
				break;
			default :
			case 'normal' :
				return -(Math.cos(Math.PI * p) - 1) / 2;
				break;
			case 'pow' :
//				return Math.pow(p, x[0] || 6);
				return Math.pow(p, 6);
				break
			case 'expo' :
				return Math.pow(2, 8 * (p - 1));
				break;
			case 'circ' :
				return 1 - Math.sin(Math.acos(p));
				break;
			case 'sine' :
				return 1 - Math.sin((1 - p) * Math.PI / 2);
				break;
			case 'back' :
//				x = x[0] || 1.618;
				x = 1.618;
				return Math.pow(p, 2) * ((x + 1) * p - x);
				break;
			case 'bounce' :
				var value;
				for(var a = 0, b = 1; 1; a += b, b /= 2){
					if(p >= (7 - 4 * a) / 11){
						value = b * b - Math.pow((11 - 6 * a - 11 * p) / 4, 2);
						break;
					}
				}
				return value;
				break;
			case 'elastic' :
//				return Math.pow(2, 10 * --p) * Math.cos(20 * p * Math.PI * (x[0] || 1) / 3);
				return Math.pow(2, 10 * --p) * Math.cos(20 * p * Math.PI * (1) / 3);
				break;
		}
	},
	/**
	 * Effet au debut et/ou a la fin
	 * @param {Number} p
	 * @param {Number} x ?
	 * @return {Number}
	 */
	_ease : function(p, x) {
		switch(this.fxEase) {
			case 'in' :
				return this._transition(p, x);
				break;
			default :
			case 'out':
				return 1 - this._transition(1 - p, x);
				break;
			case 'inout' :
				return (p <= 0.5) ? this._transition(2 * p, x) / 2 : (2 - this._transition(2 * (1 - p), x)) / 2;
				break;
		}
	},
	/**
	 * Calcul de la value
	 * @param {Number} p_from Debut
	 * @param {Number} p_to Fin
	 * @param {Number} p_delta Delta
	 * @return {Number}
	 */
	_compute : function(p_from, p_to, p_delta){
		return (p_to - p_from) * p_delta + p_from;
	},
	/**
	 * Etape par etape ( boucle )
	 */
	_step : function() {
		var v_now = this._dateNow();
		if(v_now < this.time + this.duration) {
			var v_delta = (v_now - this.time) / this.duration;
			this.set(this._compute(this.valueStart, this.valueEnd, this._ease(v_delta)));

			this.timeout = window.setTimeout(this._step._bind(this), this.delay);

		} else {
			window.clearTimeout(this.timeout);
			this.timeout = false;
			this.set(this._compute(this.valueStart, this.valueEnd, 1));
			this.callback(this);
		}
	},
	/**
	 * Enregistrement de la valeur en CSS
	 * @param {Number} p_value
	 */
	set : function(p_value) {
		if(this.styleType == 'px') {
			this.elt.style[this.styleProperty] = p_value + 'px';
		} else if(this.styleType == 'opacity') {
			this.elt.style.opacity = (p_value / 100); // css3
			this.elt.style.mozOpacity = (p_value / 100); // moz
			this.elt.style.khtmlOpacity = (p_value / 100); // khtml
			this.elt.style.filter = 'alpha(opacity=' + p_value + ')'; // msie
		} else {
			this.elt.style[this.styleProperty] = p_value;
		}
	},
	/**
	 * Demarrage ou reprise
	 */
	start : function() {
		this.time = this._dateNow() - this.time;
		if(this.valueInc != 0) {
			if(this.valueEnd === false) {
				this.valueEnd = this.valueStart;
			}
			this.valueEnd += this.valueInc;
		}
		this._step();
	},
	/**
	 * Arret ou pause
	 */
	stop : function() {
		if(!this.timeout) { return; }
		this.time = this._dateNow() - this.time;
		window.clearTimeout(this.timeout);
		this.timeout = false;
	},
	/**
	 * Deplacer a X
	 * @param {Number} p_value
	 */
	move : function(p_value) {
		this.time = 0; // raz
		this.valueStart = false;
		this._detectValueStart()
		this.valueEnd = p_value;
		this.valueInc = 0;

		this.start();
	},
	/**
	 * Deplacer de X
	 * @param {Number} p_value
	 */
	translate : function(p_value) {
		this.time = 0; // raz
		this.valueStart = false;
		this._detectValueStart()
		this.valueInc = p_value;

		this.start();
	}
};



/**
 * Navigation du menu
 * @constructor
 */
var Menu = {
	/** @return {Number} Menu visible actuellement */
	menuActive : 0,
	/** @return {Number} Nombre d'element dans le menu */
	menuLength : 0,
	/** @return {HTMLElement} Petit menu */
	menu1UL : null,
	/** @return {HTMLElement} Grand menu */
	menu2UL : null,
	/** @return {Number} Largueur d'un bloc */
	menu1offsetWidth : 0,
	/** @return {Number} Largueur d'un bloc */
	menu2offsetWidth : 0,
	/** @return {ClassCoreFX} Gestion du deplacement du petit menu */
	menu1move : null,
	/** @return {ClassCoreFX} Gestion du deplacement du grand menu */
	menu2move : null,
	/** @return {Boolean} Menu tourne */
	menuRunning : false,
	/**
	 * Chargement du menu
	 * @param {Number} p_menuActive Menu actif
	 */
	onload : function(p_menuActive) {
		this.menuActive = Number(p_menuActive);
		this.menuLength = parseInt(Doc.id('menu-1').getElementsByTagName('li').length / 3); // normalement 6

		this.menu1UL = Doc.id('menu-1').getElementsByTagName('ul').item(0);
		this.menu1offsetWidth = this.menu1UL.getElementsByTagName('a').item(0).offsetWidth;
		this.menu1move = new ClassCoreFX({
			element: this.menu1UL,
			property: 'left',
			transition: 'normal',
			duration: 1000,
			wait: true
			});
		this.menu1move.set(((this.menuActive - 0) * -this.menu1offsetWidth));

		this.menu2UL = Doc.id('menu-2').getElementsByTagName('ul').item(0);
		this.menu2offsetWidth = this.menu2UL.getElementsByTagName('a').item(0).offsetWidth;
		this.menu2move = new ClassCoreFX({
			element: this.menu2UL,
			property: 'left',
			transition: 'normal',
			duration: 1000,
			wait: true
			});
		this.menu2move.set(((this.menuActive - 1) * -this.menu2offsetWidth));
	},
	/**
	 * Click souris
	 * @param {HTMLElement} p_elt Provenant du A declenchant le clic
	 */
	click : function(p_elt) {
		if(!this.menuRunning) {
			this.menuRunning = true;
			var v_result = p_elt.parentNode.className.match(/ico\-(\d+)\s+pos\-(\d+)/i);
			if(v_result) {
				var v_incMenu = Number(v_result[1]);
				var v_posMenu = Number(v_result[2]);

				this.menu1move.move((v_incMenu - 0 + (this.menuLength * (v_posMenu - 1))) * -this.menu1offsetWidth);
				this.menu2move.move((v_incMenu - 1 + (this.menuLength * (v_posMenu - 1))) * -this.menu2offsetWidth);
			}
			
			this.menu1move.callback = function(){
				Menu.menuRunning = false; // fix."Reculer d'une page"
				window.location.href = p_elt.href;
				};
			
		}
		return false;
	}
};



/**
 * Gestion de menu qui bouge zoliment
 * @param {HTMLElement} ... Menu a manipuler ( ou IDentifiant String )
 * @param {HTMLElement} ... et les Elements dans le menu a manipuler ( ou IDentifiant String )
 * @constructor
 */
function ClassMenu() {
	/** @return {HTMLElement} Menu UL de base */
	this.menu = null;
	/** @return {Array} de HTMLElement a agiter */
	this.elementsHTML = [];
	/** @return {Array} des ID text */
	this.elementsID = [];
	/** @return {Array} de ClassCoreFX pour redimensionner l'element */
	this.resizes = [];
	/** @return {Array} de Number des taille du maximal de menus */
	this.elementsOffsetHeight = [];
	/** @return {Number} Element visible @default -1 */
	this.elementVisible = -1;
	/** @return {Boolean} Avec le click hors zone */
	this.withClickOut = false;

	this.init(arguments);
}
ClassMenu.prototype = {
	/**
	 * Determiner les tailles et initialiser les styles
	 * @param {Object} p_arguments Reference arguments
	 */
	init : function(p_arguments) {
		this.menu = typeof p_arguments[0] == 'object' ? p_arguments[0] : Doc.id(p_arguments[0]);
		for(var i = 1, l = p_arguments.length; i < l; i++) {
			this.elementsHTML[i] = typeof p_arguments[i] == 'object' ? p_arguments[i] : Doc.id(p_arguments[i]);
			this.elementsID[this.elementsHTML[i].id] = i;
			this.elementsOffsetHeight[i] = this.elementsHTML[i].offsetHeight;
			this.resizes[i] = new ClassCoreFX({
				element: this.elementsHTML[i],
				property: 'height',
				start: 0,
//				end: this.elementsOffsetHeight[i],
				transition: 'bounce',
				duration: 1000,
				wait: true
				});
			// Magnier
			this.resizes[i].set(0);
			// /Magnier

			this.elementsHTML[i].style.visibility = 'visible';

			// Magnier
			this.elementsHTML[i].style.position = 'relative';
			// /Magnier
		}
	},
	/**
	 * Click dans le menu
	 * @param {String} p_id IDenfiant menu a afficher
	 * @return {Boolean} false
	 */
	click : function(p_id) {
		if(this.elementVisible >= 0) {
			if(this.elementsHTML[this.elementVisible].id == p_id) {
				p_id = undefined;
			}
			this.resizes[this.elementVisible].move(0);

			// Magnier
			Doc.id(this.elementsHTML[this.elementVisible].id + '-close').style.display = 'none';
			// /Magnier

			this.elementVisible = -1;
		}
		if(p_id !== undefined) {
			this.elementVisible = this.elementsID[p_id];
			this.resizes[this.elementVisible].move(this.elementsOffsetHeight[this.elementVisible]);
			if(this.withClickOut) {
				Doc.body().onclick = this.clickOut._bindEvent(this);
//				Doc.body().onmouseout = this.clickOut._bindEvent(this);
			}

			// Magnier
			Doc.id(this.elementsHTML[this.elementVisible].id + '-close').style.display = 'block';
			// /Magnier

		} else {
			if(this.withClickOut) {
				Doc.body().onclick = null;
//				Doc.body().onmouseover = null;
			}
		}
		return false;
	},
	/**
	 * Click hors zone
	 * @param {MouseEvent} p_event
	 * @return {void} null
	 */
	clickOut : function(p_event) {
		var v_node = FixEvent.element(p_event);
		while(v_node) {
			if(v_node == this.menu) { return; }
			v_node = v_node.parentNode;
		}
		this.click();
	}
};



/**
 * Creation d'une bulle
 * @constructor
 */
var Infobulle = {
	bull : null,
	build : function(p_event, p_msg) {
		if(this.bull) {this.destroy();}

		var v_xhtml = '';
		v_xhtml += '<img src="' + Globals.path + 'images/none.gif" alt="" />';
		v_xhtml += '<span>' + p_msg + '</span>';

		this.bull = document.createElement('div');
		this.bull.className = 'infobulle';
		this.bull.style.left = FixEvent.pointerX(p_event) + 'px';
		this.bull.style.top = FixEvent.pointerY(p_event) + 'px';
		this.bull.innerHTML = v_xhtml;
		Doc.body().appendChild(this.bull);
	},
	move : function(p_event) {
		if(this.bull) {
			this.bull.style.left = FixEvent.pointerX(p_event) + 'px';
			this.bull.style.top = FixEvent.pointerY(p_event) + 'px';
		}
	},
	destroy : function() {
		if(this.bull) {
			Doc.body().removeChild(this.bull);
		}
		this.bull = null;
	}
};



