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


/**
 * Variable pour le window.onload
 * @return {String}
 */
var windowOnload = '';
windowOnload += 'fixiePNG();';
window.onload = function() { eval(windowOnload); };



var v_previous = '';
/**
 * Gestion des sous-menus
 * @param {String} p_id IDentifiant
 */
function smenu(p_id) {
	p_id = p_id === undefined ? '' : p_id;
	if(v_previous != p_id && v_previous) {
		Doc.id(v_previous).style.display = 'none';
	}
	v_previous = p_id;
	if(p_id) {
		Doc.id(p_id).style.display = 'block';
	}
}



/**
 * 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';
			}
		}
	}
}



/**
 * Classe permettant d'executer des fondus sur un lot d'images ( adaptee au PNG sans fond ).
 * !!! ATTENTION !!! Microsoft Internet Explorer X n'aime pas les PNG + filter:(opacity=XX) !!! ATTENTION !!!
 * @param {HTMLElement} p_elt Element a manipuler ( ou IDentifiant String )
 * @constructor
 */
function ClassFadeV2(p_elt) {
	/** @return {HTMLElement} Element a manipuler */
	this.id = typeof p_elt == 'object' ? p_elt : Doc.id(p_elt);
	/** @return {HTMLCollection} Les images */
	this.images = this.id.getElementsByTagName('img');
	/** @return {Number} Index de l'image a manipuler */
	this.imageFade1 = 0;
	/** @return {Number} Index de l'image a manipuler */
	this.imageFade2 = 1;
	/** @return {Number} Niveau d'opacite a atteindre pour le premier element */
	this.opacityLevel1 = 100;
	/** @return {Number} Niveau d'opacite a atteindre pour le second element */
	this.opacityLevel2 = 0;
	/** @return {Number} Nombre du pas pour les incrementations, min:1 || max:100 */
	this.opacityStep = 2;
	/** @return {Number} Temps avant le prochain fade en Ms, 0=random() || xxx=fixe */
	this.timeNext = 5000;
	/** @return {Number} Temps avant la prochaine etape en Ms */
	this.timeMs = 20;
	/** @return {Boolean} Premier passage */
	this.firstRun = true;

	// Controle
	if(this.images.length > 1) {
		this.initCss();
		this.fade();
	}
}
ClassFadeV2.prototype = {
	/**
	 * Initialisation des css
	 */
	initCss : function () {
		for(var v_i = 0, v_l = this.images.length; v_i < v_l; v_i++) {
			this.images.item(v_i).parentNode.style.zIndex = '32';
			this.images.item(v_i).style.opacity = '0';
			this.images.item(v_i).style.mozOpacity = '0';
			this.images.item(v_i).style.filter = 'alpha(opacity=0)';
		}
		this.images.item(this.imageFade1).parentNode.style.zIndex = '320';
		this.images.item(this.imageFade1).style.opacity = '1.00';
		this.images.item(this.imageFade1).style.mozOpacity = '1.00';
		this.images.item(this.imageFade1).style.filter = 'alpha(opacity=100)';
	},
	/**
	 * Appliquer l'opacite en CSS
	 */
	cssOpaticy : function() {
		// Disparition
		this.images.item(this.imageFade1).parentNode.style.zIndex = '32';
		this.images.item(this.imageFade1).style.opacity = (this.opacityLevel1 / 100);
		this.images.item(this.imageFade1).style.mozOpacity = (this.opacityLevel1 / 100);
		this.images.item(this.imageFade1).style.filter = 'alpha(opacity=' + this.opacityLevel1 + ')';

		// Apparation
		this.images.item(this.imageFade2).parentNode.style.zIndex = '320';
		this.images.item(this.imageFade2).style.opacity = (this.opacityLevel2 / 100);
		this.images.item(this.imageFade2).style.mozOpacity = (this.opacityLevel2 / 100);
		this.images.item(this.imageFade2).style.filter = 'alpha(opacity=' + this.opacityLevel2 + ')';
	},
	/**
	 * Determiner le prochain fondu
	 * @return {Number} Prochain en Ms
	 */
	nextFade : function() {
		return (this.timeNext > 1) ? (this.timeNext) : (Math.random() * 20000);
	},
	/**
	 * Boucle pour les fondus
	 */
	fade : function() {
		this.cssOpaticy();

		var v_ms;
		if(this.firstRun) {
			this.firstRun = false;
			v_ms = this.nextFade();

		} else if(this.opacityLevel1 <= 0 || this.opacityLevel2 > 100) {
			this.opacityLevel1 = 100;
			this.opacityLevel2 = 0;
			this.imageFade1 = this.imageFade2;
			this.imageFade2 = ((this.imageFade1 + 1) >= this.images.length) ? (0) : (this.imageFade1 + 1);
			v_ms = this.nextFade();

		} else {
			this.opacityLevel1 = this.opacityLevel1 - this.opacityStep;
			this.opacityLevel2 = this.opacityLevel2 + this.opacityStep;
			v_ms = this.timeMs;
		}
		window.setTimeout(this.fade._bind(this), v_ms);
	}
};



/**
 * Popup FLV
 * @param {String} p_href Lien
 * @return {Boolean} False
 */
function openFLV(p_href) {
	var v_w = 320;
	var v_h = 260;
	var v_l = (window.screen.width - v_w) / 2;
	var v_t = (window.screen.height - v_h) / 3;
	window.open(p_href, 'popupPlayerFLV', 'scrollbars=no,status=no,toolbar=no,width='+v_w +',height='+v_h +',left='+v_l +',top='+v_t ).focus();
	return false;
}



/**
 * Popup SWF Home
 * @param {String} p_href Lien
 * @return {Boolean} False
 */
function openHomeSWF(p_href) {
	var v_w = 600;
	var v_h = 400;
	var v_l = (window.screen.width - v_w) / 2;
	var v_t = (window.screen.height - v_h) / 3;
	window.open(p_href, 'popupPlayerSWF', 'scrollbars=no,status=no,toolbar=no,width='+v_w +',height='+v_h +',left='+v_l +',top='+v_t ).focus();
	return false;
}



/**
 * Popup IMG
 * @param {String} p_href Lien
 * @param {Number} p_w Largueur
 * @param {Number} p_h Hauteur
 * @return {Boolean} False
 */
function openIMG(p_href, p_w, p_h) {
	var v_l = (window.screen.width - p_w) / 2;
	var v_t = (window.screen.height - p_h) / 3;
	window.open(p_href, 'popupImage', 'scrollbars=no,status=no,toolbar=no,width='+p_w +',height='+p_h +',left='+v_l +',top='+v_t ).focus();
	return false;
}



/**
 * xhtml-page-news-propose.html
 * @constructor
 */
var PageNewsPropose = {
	/**
	 * Validation du formulaire
	 * @return {Boolean}
	 */
	onSubmit : function() {
		if(!Doc.id('frm-news_title').value) {
			alert('Merci de saisir votre titre.');
			return false;
		} else if(!Doc.id('frm-news_text').value) {
			alert('Merci de saisir votre texte.');
			return false;
		}
		return true;
	}
};


/**
 * xhtml-page-contact.html
 * @constructor
 */
var PageContact = {
	/**
	 * Validation du formulaire
	 * @return {Boolean}
	 */
	onSubmit : function() {
		if(!Doc.id('frm-nom').value) {
			alert('Merci de saisir votre nom.');
			return false;
		} else if(!Doc.id('frm-prenom').value) {
			alert('Merci de saisir votre pr\xE9nom.');
			return false;
		} else if(!Doc.id('frm-telephone').value) {
			alert('Merci de saisir votre t\xE9l\xE9phone.');
			return false;
		} else if(!Doc.id('frm-email').value) {
			alert('Merci de saisir votre e-mail.');
			return false;
		} else if(!Doc.id('frm-message').value) {
			alert('Merci de saisir votre message.');
			return false;
		}
		return true;
	}
};


