
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_getPreviousNode(obj,name,className){
	//return the previous sibling node with nodeName 'name' and className 
	var node=obj.previousSibling;
	while (node) {
		if (node.nodeName.toUpperCase()==name.toUpperCase() && (className=='' || className==node.className)) return node;
		node=node.previousSibling;
	}
	return false;
}
function js_isChild(child,parent){
	//Return true if 'child' is a child node of 'parent'
	//PAS POSSIBLE?? A VOIR
	var node=child;
	while(node) {
		if(node===parent) {alert(true);return true;}
		node=node.parentNode;
	}
	alert(false);
	return false;
}
function js_menuClick(obj){
	var node=js_getParentNode(obj,"div","");
	
	if (node && node.getAttribute("auto_open_child")=="yes" && node.getAttribute("mode")!="edit") {		// Auto open next child if enabled
		as=obj.parentNode.getElementsByTagName('a');
		for (i=1;i<as.length;i++) {
			if (as[i].getAttribute("location")) {
				document.location=as[i].getAttribute("location");
				return false;
			}
		}
	}
	if (node && node.getAttribute("modal")=="yes") {				//If modal mode, close all other menus
		//Removing the "selected" class on all a and put selected on obj node
		as=node.getElementsByTagName('a');
		for (i=0;i<as.length;i++) if (as[i].className=="selected") {as[i].className=""}
		obj.className="selected";
		uls=node.getElementsByTagName('ul');
		for (i=0;i<uls.length;i++) if (uls[i].parentNode.nodeName!="DIV") {uls[i].style.display='none';}
		parentN=obj;
		while(parentN) {
			if (parentN.nodeName=="UL") {
				parentN.style.display='';
				if (node && node.getAttribute("parent_selected")=="yes") parentN.className="selected";
			}
			parentN=parentN.parentNode
		}
	}
	
	obj.className="selected";
		
	var nodes=obj.parentNode.childNodes;		//Click on the A -> parent node is LI
	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_getNodeInfo(node) {
	var info="";
	info+="nodeName: "+node.nodeName+"\n";
	
	pathNode=node;
	pathText="";
	while (pathNode) {
		pathText=pathNode.nodeName+" > "+pathText;
		pathNode=pathNode.parentNode;
	}
	info+="nodePath: "+pathText+"\n";
	
	info+="nodeAttributes:\n";
	for (i=0;i<node.attributes.length;i++)
		info+="  '"+node.attributes[i].name+"' = '"+node.attributes[i].value+"'\n";
	return info;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

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_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 js_changeFontSize(increment) {
	if(document.getElementsByTagName('body')[0].style.fontSize)
		fs=parseInt(document.getElementsByTagName('body')[0].style.fontSize);
	else
		fs=11;
	fs=fs+increment;
	document.getElementsByTagName('body')[0].style.fontSize=""+fs+"px";
}

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].hasAttribute('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 js_checkMouseOver(e,obj,flag) {
	//return false;
	// 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;
	if (e.relatedTarget) target=e.relatedTarget; else target=e.toElement;
	alert('obj='+js_getNodeInfo(obj)+', target='+js_getNodeInfo(target))
	while (target) {
		if (target==obj) {return false;}		//The target was 'inside' the obj -> is a child -> not a real mouseout
		target=target.parentNode;
	}
	return false;
//	var t=(event)?event.relatedTarget:window.event.toElement;document.getElementById('debug').innerHTML+=''+t.nodeType+' onmouseout<br/>';
}

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_adminMenuSave(obj,action) {
	//After a preview, all 'a onclick' have been replaced to post the form
	xml=document.getElementById('cmsWebmenus');
	form=document.getElementById('cmsPostForm');
	if (obj.nodeName=='A') {	//If we clicked on a 'A' (within the menu)
		form.setAttribute('action',obj.getAttribute('href'))
	} else	{					//Else, we click on a 'INPUT' in the 'Edit menu', get the href from: current_menu OR first menu with a content
		var href="";
		if (current_menu && action!="reset_menu") {
			node=current_menu.parentNode.getElementsByTagName('div')[0];
			if (node.getAttribute('admin_file') && node.getAttribute('admin_file')!='') {href=node.getAttribute('admin_file');}
			if (node.getAttribute('admin_cid') && node.getAttribute('admin_cid')!='') {href="/content.php?cid="+node.getAttribute('admin_cid');}
		} 
		if (href=="") {
			nodes=xml.getElementsByTagName('div');
			for (i=0;i<nodes.length;i++) {
				if (nodes[i].getAttribute('admin_file') && nodes[i].getAttribute('admin_file')!='') {href=nodes[i].getAttribute('admin_file');i=nodes.length}
				if (nodes[i].getAttribute('admin_cid') && nodes[i].getAttribute('admin_cid')!='') {href="/content.php?cid="+nodes[i].getAttribute('admin_cid');i=nodes.length}
			}
		}
		if (href=="") {
			alert("Error: all menus are empty");
			return false;
		} else {
			form.setAttribute('action',href)
		}
	}
	inputs=form.getElementsByTagName('input');
	switch(action) {
		case "preview_menu":			//preview from the menu
		case "preview_content":			//preview from the content edition (edit.php)
			document.getElementById('cmsPostFunction').setAttribute('value',action);
			tmp=js_adminMenuParse(xml);
			document.getElementById('cmsPostWebmenus').setAttribute('value',tmp);
			preview=window.open("","preview","width=1110,height=680,scrollbars=yes,resize=yes");
			form.setAttribute('target','preview');
			form.submit();
			break;
		case "save_menu":			//save
			document.getElementById('cmsPostFunction').setAttribute('value','save')
			tmp=js_adminMenuParse(xml);
			document.getElementById('cmsPostWebmenus').setAttribute('value',tmp)
			if (form.getAttribute('target')) form.removeAttribute('target');
			form.submit();
			break;
		case "reset_menu":			//reset
			document.location=href;
			break;
	}
}

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":
				if (nodes[i].getElementsByTagName('li').length) {
					var newnode=document.createElement('menus');
					if (nodes[i].getElementsByTagName('div').length) {
						div_node=nodes[i].getElementsByTagName('div')[0];
						for (var j=0;j<div_node.attributes.length;j++) {
							if (div_node.attributes[j].nodeName.indexOf("admin_")==0) newnode.setAttribute(div_node.attributes[j].nodeName.replace("admin_",""),div_node.attributes[j].nodeValue)
						}
					}
					newnode.innerHTML=js_adminMenuParse(nodes[i])
					result.appendChild(newnode);
				}
				break;
			case "LI":
				var newnode=document.createElement('menu');
				if (nodes[i].getElementsByTagName('div').length) {
					div_node=nodes[i].getElementsByTagName('div')[0];
					for (var j=0;j<div_node.attributes.length;j++) {
						if (div_node.attributes[j].nodeName.indexOf("admin_")==0) newnode.setAttribute(div_node.attributes[j].nodeName.replace("admin_",""),div_node.attributes[j].nodeValue)
					}
				}
				newnode.innerHTML=js_adminMenuParse(nodes[i])
				result.appendChild(newnode);
				break;
		}
	}
	return DOMtoString(result);
	//return result.innerHTML;
}

/* specific functions */

function js_slideshow(obj,flag) {
	if (t=document.getElementById('cmsSlideshow')) {
		nodes=document.getElementById('cmsSlidelist').getElementsByTagName('div');
		for (i=0;i<nodes.length;i++) nodes[i].setAttribute("style","background-color:");
		switch (flag) {
			case 1:		// Click on a thumb in the bottom
				obj.setAttribute("style","background-color:#b60006");
				t.style.display='';
				if (!document.getElementById('cmsSlideshowImgId')) {
					t.innerHTML="<img src='/images/slideshow_close.png' class='rrSlideshowClose' onclick='js_slideshow(this,3)'><a href='#'><img id='cmsSlideshowImgId' border='0' src='"+obj.getAttribute("src")+"' onclick='js_slideshow(this,2);return(false);'></a>";
				} else {
					document.getElementById('cmsSlideshowImgId').src=obj.getAttribute("src");
				}
				break;
			case 2:		// Click on an image in the page
				found=false;
				for (i=1;i<nodes.length;i++) {
					if (nodes[i].getAttribute("src")==obj.getAttribute("src")) {
						obj.src=nodes[i-1].getAttribute("src");
						nodes[i-1].setAttribute("style","background-color:#b60006");
						found=true;
					}
				}
				if (!found) js_slideshow(obj,3);
				break;
			case 3:
				t.style.display='none';
				break;
			case 4:				// Onmouseover on big image
				//alert(this.width)
				break;
			case 5:				// Onmouseout on big image
				break;
		}
	}
}

js_homeCount=-1;
function js_homeSlide() {
	nodes=document.getElementById('cmsMenu').getElementsByTagName('div')[0].getElementsByTagName('img');
	if (js_homeCount>-1) {
		for (i=0;i<nodes.length;i++) nodes[i].style.display=(js_homeCount==i)?"block":"none";
	}
	js_homeCount++;
	if (js_homeCount>=nodes.length) js_homeCount=0;
	setTimeout("js_homeSlide()",3000);
}
