
function js_getParentNode(obj,name,className){
	//return the first parent node with nodeName 'name' and className 
	var node=obj
	while (node) {
		if (node.nodeName.toUpperCase()==name.toUpperCase() && (className=='' || className==node.className)) return node;
		node=node.parentNode;
	}
	return false;
}
function js_getNextNode(obj,name,className){
	//return the next sibling node with nodeName 'name' and className 
	var node=obj.nextSibling;
	while (node) {
		if (node.nodeName.toUpperCase()==name.toUpperCase() && (className=='' || className==node.className)) return node;
		node=node.nextSibling;
	}
	return false;
}
function js_menuClick(obj){
	var nodes=obj.parentNode.childNodes;
	for (i=0;i<nodes.length;i++) {
		if (nodes[i].nodeName.toUpperCase()=='UL') nodes[i].style.display=(nodes[i].style.display=='none')?'':'none';
	}
	return false;
}
function js_alertNodePath(node) {
	var path="";
	while (node) {
		path=node.nodeName+" > "+path;
		node=node.parentNode;
	}
	alert(path);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

addLoadEvent( badac_init );

function badac_init() {
	if (obj=document.getElementById('menu_search')) js_setInputEvents(document.getElementById('menu_search'));

	if (obj=document.getElementById('slideshow')) {
		if (obj.getElementsByTagName('img').length>1) {
			imgs=obj.getElementsByTagName('img');
			for (i=1;i<imgs.length;i++) {
				js_changeOpac(imgs[i],0);
				imgs[i].setAttribute('opac','0');
				imgs[i].style.display='';
			}
			imgs[0].setAttribute('opac','100');
			setTimeout("js_slideShowFadingDown(0)",2000);
		}
	}

	try { 
		var pageTracker = _gat._getTracker("UA-435849-7"); 
		pageTracker._trackPageview(); 
	} catch(err) {}
}

function js_setInputEvents(obj) {
	//Set events to a 'input' element where when user click, clear the value, etc.. like a search on 
	obj.setAttribute('default',obj.getAttribute('value'));
	obj.className='inputWait';
	obj.onclick=function () {if (this.value==this.getAttribute('default')) {this.value="";this.className='inputType'}}
	obj.onblur=function () {if (this.value=="") {this.value=this.getAttribute('default');this.className='inputWait'}}
	obj.onchange=function () {if (this.value!=this.getAttribute('default')) {js_getParentNode(this,"form","").submit()}}
}
function js_slideShowFadingDown(index) {
	imgs=document.getElementById('slideshow').getElementsByTagName('img')
	opac=parseInt(imgs[index].getAttribute('opac'))		
	if (opac>0) {
		opac-=3;
		if (opac<0) opac=0;
		imgs[index].setAttribute('opac',opac);
		js_changeOpac(imgs[index],opac);
		setTimeout("js_slideShowFadingDown("+index+")",20);
	} else {
		imgs[index].style.zIndex='0';
		index=(index+1)%imgs.length;
		setTimeout("js_slideShowFadingUp("+index+")",100);
	}
}
function js_slideShowFadingUp(index) {
	imgs=document.getElementById('slideshow').getElementsByTagName('img')
	opac=parseInt(imgs[index].getAttribute('opac'))		
	imgs[index].style.zIndex='1';
	if (opac<100) {
		opac+=3;
		if (opac>100) opac=100;
		imgs[index].setAttribute('opac',opac);
		js_changeOpac(imgs[index],opac);
		setTimeout("js_slideShowFadingUp("+index+")",20);
	} else {
		setTimeout("js_slideShowFadingDown("+index+")",4000);
	}
}
function js_changeOpac(obj,opacity) { 
	obj.style.opacity = (opacity / 100); 
	obj.style.MozOpacity = (opacity / 100); 
	obj.style.KhtmlOpacity = (opacity / 100); 
	obj.style.filter = "alpha(opacity=" + opacity + ")";
}
function showhideSpan(s){
	var objBranch = document.getElementById(s).style;
	if(objBranch.display!="none")
		objBranch.display="none";
	else
		objBranch.display="";
	return (objBranch.display);
}
addLoadEvent (function() {if (fs=js_readCookie("fontSize")) document.getElementsByTagName('body')[0].style.fontSize=""+fs+"px";});
function js_changeFontSize(increment) {
	if(document.getElementsByTagName('body')[0].style.fontSize)
		fs=parseInt(document.getElementsByTagName('body')[0].style.fontSize);
	else
		fs=11;
	fs=fs+increment;
	if (fs<8) fs=8;
	if (fs>16) fs=16;
	js_createCookie("fontSize",fs,0);
	document.getElementsByTagName('body')[0].style.fontSize=""+fs+"px";
}
function js_createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function js_readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function js_eraseCookie(name) {
	createCookie(name,"",-1);
}

function js_db_theme_onclick(obj) {
	target=js_getNextNode(obj,"div","");
	if (target.style.display=='none') {
		target.style.display=''
		if (target.getElementsByTagName('table').length==0) ajax_functions('db_themes',obj.getAttribute('href'),target,'export=ajax');
	} else {
		target.style.display='none'
	}
}
function js_showhideNextNode(obj) {
	//obj must have a attribute 'node' which will be the main 'parent node'
	//if obj does not have an attribute 'childnode', the show/hide will address next node of the 'parent node' (nextSibling)
	//if obj has an attribute 'childnode', the show/hide will address the first childnode of the 'parent node'
	//if obj has a class name 'stats_down' or 'stats_up', there is an automatic switch
	//if obj
	if (obj.className=='stats_down') obj.className='stats_up'; else if (obj.className=='stats_up') obj.className='stats_down';
	if (node=js_getParentNode(obj,obj.getAttribute('node'),'')) {
		if (obj.getAttribute('childnode')) {
			nextnode=obj.getAttribute('childnode')
			node=node.firstChild
		} else {
			nextnode=obj.getAttribute('node')
		}
		while (node=node.nextSibling) {
			if(node.nodeName.toLowerCase()==nextnode) {if(node.nodeName=='TBODY2') js_slide(node); else node.style.display=(node.style.display=='none')?'':'none';break;}
		}
		if (obj.getAttribute('node')=='tr') {
			if (node.getElementsByTagName('img').length && node.getElementsByTagName('img')[0].getAttribute('location')) node.getElementsByTagName('img')[0].src=node.getElementsByTagName('img')[0].getAttribute('location');
		}
	}
}
function js_slide(obj){
	var display=obj.style.display;
	obj.setAttribute('C',50)
	obj.style.display='';
	obj.style.height='auto';
	//alert(obj.offsetHeight)
	obj.setAttribute('H',obj.offsetHeight)
	obj.style.overflow='hidden'
	if (display=='none') {
		obj.style.height='0px'; 
		obj.setAttribute('D',1)
	} else { 
		obj.style.height=obj.getAttribute('H')+'px'
		obj.setAttribute('D',-1)
	}
	obj.timer = setInterval(function(){js_sliding(obj)},10);
}
function js_sliding(obj) {
	var curH=parseInt(obj.style.height)
	C=parseInt(obj.getAttribute('C'))		//Count (max of iteration)
	H=parseInt(obj.getAttribute('H'))		//Max height
	D=parseInt(obj.getAttribute('D'))		//Direction
	obj.setAttribute('C',C-1)
	if (D==1)
		step=(H-curH)/10;
	else
		step=curH/10;
	step=Math.round(step+1)
	//alert(curH+' '+step+' '+(curH+(D*step))+'px')
	obj.style.height=(curH+(D*step))+'px'
	if (C<0) if (D==-1) {obj.style.height='0px';obj.style.display='none';clearInterval(obj.timer);} else {obj.style.height=H+'px';clearInterval(obj.timer);}
	if (D==-1 && curH<2) {obj.style.height='0px';obj.style.display='none';clearInterval(obj.timer);}
	if (D==1 && curH>H-2) {obj.style.height='auto';clearInterval(obj.timer);}
}
function js_showhideAllNodes(tagName,showhide) {
	var nodes=document.getElementsByTagName(tagName);
	for (i=0;i<nodes.length;i++) {
		if (nodes[i].getAttribute('node')) {
			if (nodes[i].className=='stats_down' || nodes[i].className=='stats_up') nodes[i].className=(showhide)?'stats_up':'stats_down';
			if (node=js_getParentNode(nodes[i],nodes[i].getAttribute('node'),'')) {
				if (nodes[i].getAttribute('childnode')) {
					nextnode=nodes[i].getAttribute('childnode')
					node=node.firstChild
				} else {
					nextnode=nodes[i].getAttribute('node')
				}
				while (node=node.nextSibling) {
					if(node.nodeName.toLowerCase()==nextnode) {node.style.display=(showhide)?'':'none';;break;}
				}
				if (nodes[i].getAttribute('node')=='tr') {
					if (node.getElementsByTagName('img').length && node.getElementsByTagName('img')[0].getAttribute('location')) node.getElementsByTagName('img')[0].src=node.getElementsByTagName('img')[0].getAttribute('location');
				}
			}
		}
	}
}
function is_debug() {
	return false;
}
function js_checkMouseOver(e,obj,flag) {
	// returns true if the mouseover/mouseout event comes from the element itself or not
	// flag=1 -> mouseover, flag=-1 -> mouseout
	if (!e) e=window.event;
	var target=0;
	if (e.relatedTarget) target=e.relatedTarget; else (flag==1)?target=e.toElement:target=e.fromElement;
	while (target) {
		if (target=obj) {alert("ok");return true;}
		target=target.parentNode;
	}
	alert("no");
	return false;
//	var t=(event)?event.relatedTarget:window.event.toElement;document.getElementById('debug').innerHTML+=''+t.nodeType+' onmouseout<br/>';
}

function ajax_functions(action,url,obj,args){
	var xmlHttp;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) { // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			result=xmlHttp.responseText;
			if (is_debug()) alert("result:"+result);
			switch (action) {
				case "dataset_list_option":
					obj.innerHTML=result;
					js=obj.getElementsByTagName('script');
					if (js.length) {
						for (i=0;i<js.length;i++) {eval(js[i].innerHTML);}
					}
					break;
				case "db_themes":
					if (node=document.getElementById('table_data')) {
						node.removeAttribute('id')		//remove id for Table1Sorter to be able to order new table
					}
					obj.innerHTML=result;
					var Table1Sorter = new TSorter;
					Table1Sorter.init('table_data');
					break;
				default:
					alert("unknown action in ajax_functions return ("+action+")")
					break;
			}
		}		
	}
	if (url.indexOf("?")>-1) url+="&action="+action+"&"+args; else url+="?action="+action+"&"+args;
	if (is_debug()) alert("url:"+url);
	switch (action) {
		case "example_post":
			params="sql="+args;
			xmlHttp.open("POST",url,true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(params);
			break;
		default:
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
			break;
	}
	return false;
}


function js_mapShowHide() {
	nodes=document.getElementById('listObjects').getElementsByTagName('h5');
	//alert(nodes[0].innerHTML)
	if (nodes.length>1 && nodes[1].getAttribute('name')=='maps') {
		nodes[1].parentNode.removeChild(nodes[1]);
	} else {
		div=document.getElementById('listObjects').getElementsByTagName('div');
		newmap=document.getElementById('maps').getElementsByTagName('h5')[0].cloneNode(1);
		inputs=document.getElementById("region").getElementsByTagName('input');
		imgs=newmap.getElementsByTagName('img');
		//alert(imgs.length)
		for (i=0;i<inputs.length;i++) if (inputs[i].checked) {
			id=inputs[i].getAttribute('name').split('_');
			id=id[1];
			for (j=0;j<imgs.length;j++) if (imgs[j].hasAttribute('id') && imgs[j].getAttribute('id').substr(0,id.length)==id) {imgs[j].style.display='';imgs[j].setAttribute('selected','1');}
			
		}
		if (div.length) maps=div[0].parentNode.insertBefore(newmap,div[0]);
	}
}

function js_map(obj,event) {
	var node=document.getElementById(obj.getAttribute('imgid'));
	if (!node) return(false);
	if (node) {
		selected=node.getAttribute("selected")=="1";
		if (obj.getAttribute('imgid')!='0_autres-regions') {
			node.style.display='';
			switch(event) {
				case 0:		//Click
					if (selected) {
						js_changeOpac(node,50);
						node.setAttribute('selected',0);
					} else {
						js_changeOpac(node,100);
						node.setAttribute('selected',1);
					}
					js_mapSelect(node,selected)
					break;
				case 1:		//MouseOver
					if (!selected) js_changeOpac(node,50);
					break;
				case -1:	//MouseOut
					if (!selected) js_changeOpac(node,0);
					break;
			}
		}
		if (event==1 && node.getAttribute('text') && node.getAttribute('text')!="" && document.getElementById('map_text')) {
			document.getElementById('map_text').innerHTML=node.getAttribute('text');
			document.getElementById('map_text').style.display="";
			document.getElementById('map_text').style.marginLeft=node.getAttribute('text_x')+'px';
			document.getElementById('map_text').style.marginTop=node.getAttribute('text_y')+'px';			
		}
		if (event==-1 && document.getElementById('map_text')) {
			document.getElementById('map_text').style.display="none";
		}
	} else {
		alert(obj.getAttribute('imgid')+" does not exist")
	}
}
function js_mapSelect(node,flag) {
	if (node.getAttribute("location")) document.location=node.getAttribute("location");
}

function js_eventHandler() {
	var e=window.event;
	var target=(window.event)?e.srcElement:e.target;
	document.getElementById('debug').innerHTML+=''+target.nodeName+' onmouseover<br/>';
	this.nextSibling.style.display=''
}
function js_adminCheckbox(obj) {
	document.getElementById('admin_menu_form').style.display=(obj.checked)?'':'none';
	nodes=document.getElementById('badacMenu').getElementsByTagName('a');
	for (i=0;i<nodes.length;i++) {
		if (nodes[i].className=='admin_menu_button') nodes[i].style.display=(obj.checked)?'':'none';
	}
}
function js_adminMenuSave(obj,flag) {
	//Flag: -1: reset, 1: preview, 2: test, 3:save
	//After a preview or all 'testing', all 'p onclick' have been replaced to post the form
	//if (flag==-1) {document.location=document.location;return false}		//Reset
	xml=document.getElementById('badacWebmenus');
	if(document.all) {alert("Use Firefox for this feature");return false}		//IE doen not behave correctly with innerHTML -> disabled!
	form=document.getElementById('admin_menu_form');
	if (obj.nodeName=='P') {		//If we clicked on a 'P' (within the menu)
		form.setAttribute('action',obj.getAttribute('location'))
	} else	{					//Else, we click on a 'INPUT' in the 'Edit menu', get the first menu with a 'file' as location
		var location="";
		nodes=xml.getElementsByTagName('div');
		//for (i=0;i<nodes.length;i++) alert(nodes[i].innerHTML)
		for (i=0;i<nodes.length;i++) if (nodes[i].getAttribute('admin_file') && nodes[i].getAttribute('admin_file')!='') {location=nodes[i].getAttribute('admin_file');i=nodes.length}
		if (location=="") {
			alert("Error: all menus are empty, please use FireFox for this to work");
			return false;
		} else {
			form.setAttribute('action',location)
		}
	}
	inputs=form.getElementsByTagName('input');
	switch(flag) {
		case 1:			//preview
			inputs[0].setAttribute('value','preview')
			tmp=js_adminMenuParse(xml);
			inputs[1].setAttribute('value',tmp)
			preview=window.open("","preview","width=980,height=680,scrolls=yes");
			form.setAttribute('target','preview');
			form.submit();
			break;
		case 2:			//testing
			inputs[0].setAttribute('value','testing')
			form.setAttribute('target','');
			form.submit();
			break;
		case 3:			//save
			inputs[0].setAttribute('value','save')
			tmp=js_adminMenuParse(xml);
			inputs[1].setAttribute('value',tmp)
			if (form.getAttribute('target')) form.removeAttribute('target');
			form.submit();
			break;
		case -1:			//reset
			document.location=form.getAttribute('action');
			break;
	}
	//var form2=document.createElement("form2")
	//form2.appendChild(form.cloneNode(true))
	//alert(form2.innerHTML)
}
function js_adminMenuParse(node) {
	var nodes=node.childNodes
	var result=document.createElement('tmp');
	for (var i=0;i<nodes.length;i++) {
		switch (nodes[i].nodeName) {
			case "UL":
				var newnode=document.createElement('menus');
				if (nodes[i].getAttribute('class')) newnode.setAttribute('class',nodes[i].getAttribute('class'))
				newnode.innerHTML=js_adminMenuParse(nodes[i])
				result.appendChild(newnode);
				break;
			case "LI":
				var newnode=document.createElement('menu');
				anode=nodes[i].getElementsByTagName('div')[0];
				for (var j=0;j<anode.attributes.length;j++) {
					if (anode.attributes[j].nodeName.indexOf("admin_")==0) newnode.setAttribute(anode.attributes[j].nodeName.replace("admin_",""),anode.attributes[j].nodeValue)
				}
				newnode.innerHTML=js_adminMenuParse(nodes[i])
				result.appendChild(newnode);
				break;
		}
	}
	return result.innerHTML;
}
function ieInnerHTML(zz) {
	//var zz = obj.innerHTML,
	z = zz.match(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/g);
	if (z){
		for (var i=0;i<z.length;i++){
			var y, zSaved = z[i];
			z[i] = z[i].replace(/(<?\w+)|(<\/?\w+)\s/,function(a){return a.toLowerCase();});
			y = z[i].match(/\=\w+[?\s+|?>]/g);
			if (y){
				for (var j=0;j<y.length;j++){
					z[i] = z[i].replace(y[j],y[j].replace(/\=(\w+)([?\s+|?>])/g,'="$1"$2'));
				}
			}
			zz = zz.replace(zSaved,z[i]);
		}
	}
	return zz;
}

function js_adminShowDico() {
	var objs = document.getElementsByTagName("A");
	for (var i=0; i<objs.length; i++) {
		if (objs[i].className=='dicotag') {
			objs[i].style.display='';
			objs[i].innerHTML="<img src='/images/db/admin.png' border='0' align='top'>";
		}
	}
	if (document.getElementById('dicoUnreachable')) document.getElementById('dicoUnreachable').style.display='';
}

function js_windowPrint() {
	// If iframe from sigma.badac.ch then resize it first and set it back after...
	if (node=document.getElementById('database2')) node.style.height='800px';
	window.print();
	if (node=document.getElementById('database2')) setTimeout("resize_iframe();",2000);
}
//Manage google-analytics:
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); 
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); 



