function $(id){return document.getElementById(id);}

// envent onload manager
function addLoadEvent(nomFunction) {
  var oldonload = window.onload;
  if(typeof window.onload != 'function') {
    window.onload = nomFunction;
  }else{
    window.onload = function(){
      if(oldonload){oldonload();}
      nomFunction();
    }
  }
}

function addEvent(obj, type, fn, objParent){
  if(obj.addEventListener){
    obj.addEventListener(type, function(event){
      return fn.call(obj,event,objParent);
    },false);
  }else if(obj.attachEvent){
    obj.attachEvent("on"+type, function(e){     
      if(!e) var e = window.event;
      return fn.call(obj, e, objParent);
    });
  }
}

function id_exist(id){
	if($(id)==null){return false;}else{return true;}
	}
	
function ie_check(){
		if(navigator.appName=="Microsoft Internet Explorer"){return true;}else{return false;}
		}


function mask(id_mask,id_container,z_index){
	div_create(id_mask,id_container);	
	add_style(id_mask,"position:fixed;width:100%;height:100%;z-index:"+z_index+";");
	addEvent($(id_mask),'click',function(){delete_mask(id_mask);});
	}
	
function delete_mask(id_mask){
	div_delete('popup');
	div_delete(id_mask);
	}
	
function div_create(id,id_container){ // création d'un div si nécéssaire (leave container empty, or set to "body" to use the body)
	if(id_exist(id)==false){ // si le div n'existe pas on le crée
		div_temp=document.createElement("div");
		div_temp.setAttribute('id',id);
		if(id_container!="" && id_container!="body"){ // crée le nouveau div dans le div container
			$(id_container).appendChild(div_temp);
			}else{document.body.appendChild(div_temp); // crée le nouveau div dans le body
				}
		return true;
		}else{return false;}
}

function div_delete(id){ // supprime un div
	if(id_exist(id)){
		tmp_del=$(id);
		tmp_del.parentNode.removeChild(tmp_del);
		tmp_del="";
		return true; // div supprimé
		}else{
			return false; // le div a supprimer n'existe pas
			}
	}
	
function ajax(url){
	var boolean=arguments[1];
	if(boolean==undefined){boolean=true;}
	if(create_XMLHTTP()==true){
		tmp_vars=url.split("?");
		if(boolean == true){
			ajax_object.onreadystatechange = function(){
				// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
				if(ajax_object.readyState == 4){
					if(ajax_object.status == 200){eval(ajax_object.responseText);}
				}			
			}
		}
		ajax_object.open("POST", tmp_vars[0], boolean);  // true=asynchrone / false=synchrone
		ajax_object.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
		ajax_object.setRequestHeader("Content-Encoding","UTF-8");
		if(url.indexOf("?")!=-1){ajax_object.send(tmp_vars[1]);} // si il y a des variables
		if(boolean == false){
			if(ajax_object.readyState == 4){
				if(ajax_object.status == 200){eval(ajax_object.responseText);}
			}
		}
	}else{
		alert("Your Browser doesn't support AJAX") // XMLHTTP non supporté ?
		}
}
	

function create_XMLHTTP(){
if(window.XMLHttpRequest){ajax_object = new XMLHttpRequest();return true;}  // Firefox    
	else{if(window.ActiveXObject){ajax_object = new ActiveXObject("Microsoft.XMLHTTP");return true;}else{return false;}
		}   
}

function popup_content(left,top,width,content,mask){ // affiche une popup (top, left, width sans les "px" !

	if(ie_check()==true){
		HTML="<div style='width:"+width+"px;'><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td width='8' height='8' valign='top'><img src='img/structure/c1.png' width='8' height='8' /></td><td height='8' bgcolor='#FFFFFF'><img src='img/structure/c5.png' width='8' height='8' /></td><td width='8'  height='8' valign='top'><img src='img/structure/c2.png' width='8' height='8' /></td></tr><tr><td bgcolor='#FFFFFF'></td><td bgcolor='#FFFFFF' style='padding:15px;'>"+content+"</td><td bgcolor='#FFFFFF'>&nbsp;</td></tr><tr><td width='8' height='8' valign='bottom'><img src='img/structure/c4.png' width='8' height='8' /></td><td bgcolor='#FFFFFF'  height='8'></td><td width='8'  height='8' valign='bottom'><img src='img/structure/c3.png' width='8' height='8' /></td></tr></table></div>";
	}else{
		HTML="<div style='width:"+width+"px;-moz-border-radius:8px;-webkit-border-radius:8px;background-color:#FFFFFF;padding:15px;'>"+content+"</div>"
		}
		
		mask_height=findPosY("footer_zone")+155;
		
		if(ie_check()==true){
			page_height=document.documentElement.clientHeight;
			}else{
				page_height=window.innerHeight
				}
		
		if(page_height >mask_height){mask_height=page_height;}
		
		top=f_scrollTop()+150;
		left=(f_clientWidth()-width) /2 ;
	
		document.getElementById("abs_popup").innerHTML=HTML;
		document.getElementById("abs_popup").style.top=top+"px";
		document.getElementById("abs_popup").style.left=left+"px";
		document.getElementById("abs_popup").style.width=width+"px";
		document.getElementById("abs_mask").style.height=mask_height+"px";
		if(mask==1){document.getElementById("abs_mask").style.display="inline";}
		document.getElementById("abs_popup").style.display="inline";

	}
	
// récupère la position en left
function findPosX(id) { 
		var obj=document.getElementById(id)
		var curleft = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				curleft += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		} else if (obj.x) {curleft += obj.x;}
		return curleft;
	}


// récupère la position en top
function findPosY(id) { 
	var obj=document.getElementById(id)
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {curtop += obj.y;}
	return curtop;
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}



function abs_hide_popup(){ // masque la popup
	document.getElementById("abs_mask").style.display="none";
	document.getElementById("abs_popup").style.display="none";
	document.getElementById("abs_popup").innerHTML="";
	}


// Hover bt
function hover(id){
	if($(id).className!="selected"){
		if($(id).src.indexOf("_a.gif")!=-1){
			$(id).src=$(id).src.replace("_a.gif","_b.gif");
			}else{
				$(id).src=$(id).src.replace("_b.gif","_a.gif");
				}
		}
	}
	

function set_menu(id){
	for(i=0;i<50;i++){
		if(id_exist("bt_"+i)){
			$("bt_"+i).className="unselected";
			$("bt_"+i).src=$("bt_"+i).src.replace("_b.gif","_a.gif");
			}
		}
	$(id).className="selected";
	$(id).src=$(id).src.replace("_a.gif","_b.gif");
	}


function init_events(){
	addEvent($('bt_1'),'mouseover',function(){hover("bt_1");},this);
	addEvent($('bt_2'),'mouseover',function(){hover("bt_2");},this);
	addEvent($('bt_3'),'mouseover',function(){hover("bt_3");},this);
	addEvent($('bt_4'),'mouseover',function(){hover("bt_4");},this);
	addEvent($('bt_5'),'mouseover',function(){hover("bt_5");},this);
	
	addEvent($('bt_3'),'mouseout',function(){hover("bt_3");},this);
	addEvent($('bt_1'),'mouseout',function(){hover("bt_1");},this);
	addEvent($('bt_2'),'mouseout',function(){hover("bt_2");},this);
	addEvent($('bt_4'),'mouseout',function(){hover("bt_4");},this);
	addEvent($('bt_5'),'mouseout',function(){hover("bt_5");},this);
	
	addEvent($('bt_1'),'click',function(){set_menu("bt_1");},this);
	addEvent($('bt_2'),'click',function(){set_menu("bt_2");},this);
	addEvent($('bt_3'),'click',function(){set_menu("bt_3");},this);
	addEvent($('bt_4'),'click',function(){set_menu("bt_4");},this);
	addEvent($('bt_5'),'click',function(){set_menu("bt_5");},this);
	
	addEvent($('abs_mask'),'click',function(){abs_hide_popup();},this);

	}

// onload
addLoadEvent(init_events);