/**********************************************************
OKINTERACTIF - LIBRAIRIE DE FONCTIONS JAVASCRIPTS
Some functions taken at Dustin Diaz:http://www.dustindiaz.com/top-ten-javascript/
Dernière modification : 7 Février 2007, Jean-Bernard Filion
**********************************************************/

//-- RUBRIQUES ----------------------------------------------------------------------------------------
//---- EVENTS
//---- WINDOW	
//---- ELEMENTS
//---- ARRAY
//---- CSS
//---- FORMS
//---- QUERYSTRING
//---- COOKIES
//---- TIMER
//---- FLASH


//-- EVENTS -------------------------------------------------------------------------------------------
	 function addEvent( obj, type, fn ) {
		if (obj.addEventListener) {
			obj.addEventListener( type, fn, false );
			EventCache.add(obj, type, fn);
		}
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent( "on"+type, obj[type+fn] );
			EventCache.add(obj, type, fn);
		}
		else {
			obj["on"+type] = obj["e"+type+fn];
		}
	}
		
	var EventCache = function(){
		var listEvents = [];
		return {
			listEvents : listEvents,
			add : function(node, sEventName, fHandler){
				listEvents.push(arguments);
			},
			flush : function(){
				var i, item;
				for(i = listEvents.length - 1; i >= 0; i--){
					item = listEvents[i];
					if(item[0].removeEventListener){
						item[0].removeEventListener(item[1], item[2], item[3]);
					};
					if(item[1].substring(0, 2) != "on"){
						item[1] = "on" + item[1];
					};
					if(item[0].detachEvent){
						item[0].detachEvent(item[1], item[2]);
					};
					item[0][item[1]] = null;
				};
			}
		};
	}();
	addEvent(window,'unload',EventCache.flush);
	
	function addLoadEvent(func) {
		var oldonload = window.onload;
		
		if (typeof window.onload != 'function') {
			window.onload = func;
		}
		else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
//-- WINDOW -------------------------------------------------------------------------------------------
	function redirect(URLStr) { location = URLStr; }
	
	function fermer_fenetre() { window.close(); }
	
	function openFullScreen(url){
		var fullWindow = "width="+screen.width+",height="+screen.height+",directories,location,menubar,resizable,left=0,top=0,screenX=0,screenY=0,scrollbars,titlebar,toolbar,status";
		newwin = window.open(url,'',fullWindow);
	}
	
	function openPopup(url,width,height){
		var left = 	(screen.width - width)/2;
		var top = 	(screen.height - height)/2;
		var popup = "width="+width+",height="+height+",left="+left+",top="+top+",scrollbars,menubar,titlebar,status";
		newwin = window.open(url,'Crédits',popup);
	}
	
	function openPrintableScreen(url){
		var fullWindow = "width="+screen.width+",height="+screen.height+",menubar,resizable,left=0,top=0,screenX=0,screenY=0,scrollbars,titlebar,status";
		newwin = window.open(url,'',fullWindow);
	}
//-- ELEMENTS -----------------------------------------------------------------------------------------
	function $() {
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
			if (arguments.length == 1)
				return element;
			elements.push(element);
		}
		return elements;
	}
	
	function getElementsByClass(searchClass,node,tag) {
		var classElements = new Array();
		if ( node == null ) node = document;
		if ( tag == null ) 	tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (i=0,j=0;i<elsLen;i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}
	
	function insertAfter(parent, node, referenceNode) {
		parent.insertBefore(node, referenceNode.nextSibling);
	}
	
	function _setAttribute(el,attr,value){
		var el = $(el);
		if(document.all)
			switch(attr){
				case 'class':
					attr = 'className'; break;
				case 'htmlFor':
					attr = 'for'; 		break;
			}
		el.setAttribute(attr,value);
	}
	function _getAttribute(el,attr){
		if(typeof el=='string') el = $(el);
		if(!document.all)
			switch(attr){
				case 'class':
					attr = 'className'; break;
				case 'htmlFor':
					attr = 'for'; 		break;
			}

		return el.getAttribute(attr);
	}	
//-- IMG ----------------------------------------------------------------------------------------------
	function changeSrc(id,src){
		if(!$(id)) return;
		$(id).src = src;
	}
	function generateRollOvers(){
		var c = 		'rollOverImg';
		var suffixes = 	new Object({over:'_over',up:'_up'});

		var imgs = 		getElementsByClass(c);
		var length = 	imgs.length;
		while(length--){
			s = 			imgs[length];
			ext =			s.src.substring(s.src.lastIndexOf('.'));
			prefix = 		s.src.substring(0,s.src.lastIndexOf('_'));
			s.onmouseover = new Function("this.src='"+prefix+suffixes.over+ext+"';");
			s.onmouseout = 	new Function("this.src='"+prefix+suffixes.up+ext+"';");
		}
	}
//-- ARRAY --------------------------------------------------------------------------------------------
	Array.prototype.inArray = function (value) {
		for (var i=0; i < this.length; i++) {
			if (this[i] === value) {
				return true;
			}
		}
		return false;
	}
//-- CSS ----------------------------------------------------------------------------------------------
	function toggle(div) {
		if(!$(div)) return;
		$(div).style.display = ($(div).style.display=='none' || $(div).style.display=='')
			? 'block' : 'none';
	}
	
	function setBgImage(div,img_src){
		if(!$(div)) return;
		var el_bg =				$(div);
		var bg = 				el_bg.style;
		bg.backgroundImage = 	'url('+img_src+')';
	}
	
	function change_bkg_color(element,bkg_color) { 
		if ($(element)) $(element).style.backgroundColor = bkg_color;
	}
//-- FORMS --------------------------------------------------------------------------------------------
	function checkUnique(group,id,check){
		var Group = getElementsByClass(group);
		var s;
		for(var i=0;i<Group.length;i++){
			s = Group[i];
			if(s==$(id)) continue;
			if(s.checked){
				s.checked = '';
				break;
			}
		}
		
		$(id).checked = (check) ? 'checked' : '';
	}
	
	function valider_courriel(unCourriel){
		var regEmail = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/;
 		return regEmail.test (unCourriel);
	}
//-- QUERYSTRING --------------------------------------------------------------------------------------
	function get_query_string_attribute(attr_name){
		var bol_replace = 	false;
		var qString = 		String(window.location);
		if(qString.indexOf('?')==-1) return '';
		var chaine = 		qString.substr(qString.indexOf('?')+1);
	
		(chaine.indexOf('&')>-1)
			?	value = chaine.substring(chaine.indexOf	(attr_name)+attr_name.length+1,chaine.indexOf('&',chaine.indexOf(attr_name)+attr_name.length))
			:	value = chaine.substring(chaine.indexOf(attr_name)+attr_name.length+1);
	
		while(!bol_replace){
			(value===value.replace('+',' ')) 
				? bol_replace = 	true
				: value = 			value.replace('+',' ');
		}
		
		return value;
	}
//-- COOKIES ------------------------------------------------------------------------------------------
	function getCookie( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}
		
	function setCookie( name, value, expires, path, domain, secure ) {
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+"="+escape( value ) +
			( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
			( ( path ) ? ";path=" + path : "" ) +
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
	}
		
	function deleteCookie( name, path, domain ) {
		if ( getCookie( name ) ) document.cookie = name + "=" +
				( ( path ) ? ";path=" + path : "") +
				( ( domain ) ? ";domain=" + domain : "" ) +
				";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
//-- TIMER --------------------------------------------------------------------------------------------
	var timer_secs;
	var timer_my_function;
	var timer_id = 			null;
	var timer_is_timer = 	false;
	var timer_delay = 		1000;
	
	function init_timer(seconds, fct){
		timer_my_function = fct;
		timer_secs = seconds;
		stop_timer();
		start_timer();
	}
	
	function stop_timer(){
		if(timer_is_timer) 		clearTimeout(timer_id);
		timer_is_timer = 		false;
	}
	
	function start_timer(){
		if (timer_secs==0){
			stop_timer();
			eval(timer_my_function+'()');
		}else{
			self.status = 		timer_secs;
			timer_secs--;
			timer_is_timer = 	true;
			timer_id = 			self.setTimeout("start_timer()", timer_delay);
		}
	}
//-- MENU ---------------------------------------------------------------------------------------------	
	var gMenuCourantVisible =	0;
	var gDureeDelai =			500;
	var gIdMinuterie =			null;
	var gTop =					138;
	var gWidthSite =			920;
	var gWidthbt =				114;
	var gDistanceAvantMenu =	5;
	var gDefilmentFirefox =		18;
	
	function cacherMenu(menu){
		if (gMenuCourantVisible!=menu && gMenuCourantVisible!=0)
			toggle(gMenuCourantVisible);
	}
	 
	function afficherMenu(menu,no_bt){
		var myWidth = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			myWidth = window.innerWidth;
			if(document.body.getHeight()>window.innerHeight){
				myWidth = myWidth-gDefilmentFirefox;
			}
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			myWidth = document.documentElement.clientWidth;
		}
		if (gMenuCourantVisible!=menu){
			$(menu).style.top = gTop + "px";
			$(menu).style.left = ((myWidth - gWidthSite)/2) + gDistanceAvantMenu + ( gWidthbt * no_bt ) - gWidthbt + "px";
			toggle(menu);
			gMenuCourantVisible = menu;
		}
	}
	 
	function stopTimerMenu(){
		clearTimeout(gIdMinuterie);
		gIdMinuterie = null;
	}
	 
	function timerMenu(){
		if (gIdMinuterie!=null){
			cacherMenu(0);
			gMenuCourantVisible = 	0;
			gIdMinuterie = 			null;
		}else gIdMinuterie = 		setTimeout("timerMenu()",gDureeDelai)
	}
//-- FLASH --------------------------------------------------------------------------------------------
	function resizeFlash(id){
		height = (!document.all) ? window.innerHeight : document.body.clientHeight;
		$(id).style.height = height+'px';
	}
	
	function alertFlash(msg){
		alert(msg);
	}
//-----------------------------------------------------------------------------------------------------
//-- FIN DU DOCUMENT ----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
