/**
 *
 * @author	Benoit Asselin <benoit(at)agenceici(dot)com>
 * @version	copy-paste.js, 2009/10/14
 * @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;
}



/**
 * Classe permettant de determiner la position reelle de l'element
 * @param {Node} p_elt Element a determiner
 * @param {Node} p_ref Element ou stopper la recheche
 * @constructor
 */
function ClassOffset(p_elt, p_ref) {
	/** @return {Number} Position Gauche @default 0 */
	this.left = 0;
	/** @return {Number} Position Haut @default 0 */
	this.top = 0;
	/** @return {Number} Largeur @default 0 */
	this.width = 0;
	/** @return {Number} Hauteur @default 0 */
	this.height = 0;

	/** @return {Node} Node de reference qui stop le comptage // doit etre un element offsetParent! @private */
	this.reference = (p_ref === undefined ? Doc.body() : p_ref);
	/** @return {Node} Node a retrouver dans this.reference @private */
	this.element = p_elt;

	this.getOffset();
}
/** Determiner les Offset @private */
ClassOffset.prototype.getOffset = function() {
	var v_node = this.element;
	while(v_node != null && v_node != this.reference) {
		this.left += v_node.offsetLeft;
		this.top += v_node.offsetTop;
		v_node = v_node.offsetParent;
	}
	this.width = this.element.offsetWidth;
	this.height = this.element.offsetHeight;
};



function ClassTravelImages(p_id) {
	this.body = document.getElementsByTagName('body').item(0);
	this.id = Doc.id(p_id);
	this.ul = this.id.getElementsByTagName('ul').item(0);
	this.li = this.ul.getElementsByTagName('li');
	
	this.width = {};
	this.width.ul = 0;						/* Largeur totale */
	this.width.li = [];						/* Largeur des images */
	
	this.left = 0;							/* Position sur la gauche */
	this.timeout = null;						/* Timer */
	this.running = false;						/* L'effet est en cours */
	this.speedPx = 5;						/* Vitesse en PX */
	this.speedMs = 30;						/* Vitesse des intervales */
	
	this.mouseX = {};						/* Position de la souris */
	this.mouseX.x = Math.round(this.body.offsetWidth / 2);		/* Position de la souris en X */
	this.mouseX._5 = 0;						/* Position centrale */
	this.mouseX.coef = 0;
	
	this.getMouseX();
	this.getWidth();
	this.setCss();
	this.makeNewLIs();
	this.setEvents();
	this.run();
};
ClassTravelImages.prototype = {
	getMouseX : function() {
		var v_offset = new ClassOffset(this.id);
		this.mouseX._5 = v_offset.left + (v_offset.width / 2);
		this.mouseX.coef = (this.mouseX._5 - this.mouseX.x) / this.mouseX._5;
		if(this.mouseX.coef < -1) {
			this.mouseX.coef = -1;
		} else if(this.mouseX.coef > 1) {
			this.mouseX.coef = 1;
		}
	},
	getWidth : function() {
		for(var v_i = 0, v_l = this.li.length; v_i < v_l; v_i++) {
			this.width.li[v_i] = this.li.item(v_i).getElementsByTagName('img').item(0).offsetWidth;
			this.width.ul += this.width.li[v_i];
		}
	},
	setCss : function() {
		this.ul.style.left = this.left + 'px';
		this.ul.style.width = this.width.ul + 'px';
		
		var v_left = 0;
		for(var v_i = 0, v_l = this.li.length; v_i < v_l; v_i++) {
			this.li.item(v_i).style.position = 'absolute';
			this.li.item(v_i).style.left = v_left + 'px';
			this.li.item(v_i).style.top = '0';
			this.li.item(v_i).style.width = this.width.li[v_i] + 'px';
			
			v_left += this.width.li[v_i];
		}
	},
	makeNewLIs : function() {
		var v_liLength = this.li.length;
		var v_clone = 2; /* Nombre de clones pour les LI */
		for(var v_c = 1; v_c <= v_clone; v_c++) {
			for(var v_i = 0; v_i < v_liLength; v_i++) {
				this.ul.appendChild(this.li.item(v_i).cloneNode(true));
			}
		}
		
		for(var v_i = v_liLength, v_l = this.li.length; v_i < v_l; v_i++) {
			var v_leftFix = Math.floor(v_i / v_liLength) * this.width.ul;
			this.li.item(v_i).style.left = (parseInt(this.li.item(v_i).style.left) + v_leftFix) + 'px';
		}
	},
	setEvents : function() {
		/*
		this.id.onmouseout = this.start._bindEvent(this);
		this.id.onmouseover = this.stop._bindEvent(this);
		*/
		this.body.onmousemove = this.mousemove._bindEvent(this);
	},
	run : function() {
		this.running = true;
		this.ul.style.left = this.left + 'px';
		
		this.left += Math.round(this.speedPx * this.mouseX.coef);
		if(this.left <= -this.width.ul) {
			this.left = 0;
		} else if(this.left >= 0) {
			this.left = -this.width.ul;
		}
		
		this.timeout = setTimeout(this.run._bind(this), this.speedMs);
	},
	/*
	start : function(p_event) {
		if(!this.running) {
			this.run();
		}
	},
	stop : function(p_event) {
		this.running = false;
		clearTimeout(this.timeout);
	},
	*/
	mousemove : function(p_event) {
		this.mouseX.x = FixEvent.pointerX(p_event);
		this.getMouseX();
	}
};



function restaurationCSS() {
	var v_top = ((document.body.offsetHeight / 2) -250);
	if(v_top < 0) { v_top = 0; }
	document.getElementById('restauration').style.top = v_top + 'px';
}


