/* ver.: 10.10.2007 */

function addEvent( obj, type, fn )
{
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
	} 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] );
	}
}

function removeEvent( obj, type, fn ){
	if (obj.removeEventListener){
		obj.removeEventListener( type, fn, false );
	} else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}


//No more IE6 background flicker
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

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

function zebRowClick(){
	var area = forid(this.action);
	if(area.className.indexOf("bg_click") == 7){
			area.className = area.className.replace(/ bg_click/g, "")
		} else {
			area.className += " bg_click";
		}
}

function zebRowOver(){
	var area = forid(this.action);
			area.className += " bg_over";
}

function zebRowOut(){
	var area = forid(this.action);
			area.className = area.className.replace(/ bg_over/g, "");
}

function zebRow(){
	var tables = document.getElementsByTagName("table");
	
	for (var i=0;i<tables.length;i++) {
		if(tables[i].className == 'bordered') {
			
			var trs=tables[i].getElementsByTagName('tr');
			
			for(var j=0;j<trs.length;j++) {
				
				node = trs[j];
				if (node.nodeName=="TR"){
					node.id = "area" + i + "_" + j;
					node.action = node.id;
					node.onmouseover = zebRowOver;
					node.onmouseout = zebRowOut;
					node.onclick = zebRowClick;
				}
			
				if(j%2 == 0) {
					trs[j].className = "bg_one";
				} else {
					trs[j].className = "bg_two";
				}
			}
		}
	}
}

function confirm_passwd($field){
		if((forid($field + '_confirm').value) == (forid($field).value)){
			forid($field + '_confirm').style.background = "#80B51F";
			forid($field + '_confirm').title = "Confirmation correcte";
		} else {
			forid($field + '_confirm').style.background = "#F34C22";
			forid($field + '_confirm').title = "Confirmation incorrecte";
		}
}

function complexity_passwd($field){
		$paswd = forid($field).value;
		var req = new JSHttpRequest();
		req.onreadystatechange = function(){
		  if (req.readyState == 4) {
		  	switch(req.responseJS.row){
				case 1:
					$str = 'Mot de passe tres faible';
				break;
				
				case 2:
					$str = 'Mot de passe faible';
				break;
				
				case 3:
					$str = 'Mot de passe moyen';
				break;
				
				case 4:
					$str = 'Bon mot de passe';
				break;
				
				case 5:
					$str = 'Tres bon mot de passe';
				break;
			}
		  		var theDivNode = forid($field + '_complexity');
					theDivNode.innerHTML = '';
					theDivNode.appendChild(document.createTextNode($str));
				
				forid($field).style.background = 'url(images/system/lock' + req.responseJS.row + '.gif) no-repeat center right #FFF';
				forid($field).title = $str;
			}
		}
		
		req.caching = true;
		req.open('GET', 'inc/complexity_passwd.php', true);
		req.send({ paswd: $paswd });
}

function InsertCC($field){
	$ccode = forid($field + '_confirmcode').value;
	forid($field).value = $ccode;
}

function StartMiniGall(){
	var theImages = document.getElementsByTagName("img");
	
	for (var i=0;i<theImages.length;i++) {

		var theImage = theImages[i];
		if(theImage.className == 'pict' && theImage.id) {
			var theVars = theImage.id.split(".");
			var theParent = theImage.parentNode;
			
				theImage.id = theVars[0] + theVars[2] + "-" + i + "img";
				
			var theInputPrev = document.createElement("input");
				theInputPrev.type = "hidden";
				theInputPrev.id =  theImage.id.replace("img", "") + "prev";
				theInputPrev.value = theVars[3];

			var theInputNext = document.createElement("input");
				theInputNext.type = "hidden";
				theInputNext.id = theImage.id.replace("img", "") + "next";
				theInputNext.value = 1;

			var theNavUl = document.createElement("ul");
				theNavUl.className = "minigalcontrol";
				theNavUl.id = theImage.id.replace("img", "") + "control";

			var theNavUlLiPrev = document.createElement("li");

			var theNavUlLiPrevA = document.createElement("a");
				theNavUlLiPrevA.title = "Prev";
				theNavUlLiPrevA.action_act = "prev";
				theNavUlLiPrevA.id = "control" + theImage.id.replace("img", "") + "prev";
				theNavUlLiPrevA.className = "imglistprev";
				theNavUlLiPrevA.action_field = theVars[1];
				theNavUlLiPrevA.action_id = theImage.id.replace("img", "");
				theNavUlLiPrevA.action_dataname = theVars[0];
				theNavUlLiPrevA.onclick = ListMinigal;
				theNavUlLiPrevA.onmouseover = MinigalHover;
				theNavUlLiPrevA.onmouseout = MinigalOut;
				
				theNavUlLiPrev.appendChild(theNavUlLiPrevA);

			var theNavUlLiNext = document.createElement("li");
			var theNavUlLiNextA = document.createElement("a");
				theNavUlLiNextA.title = "Next";
				theNavUlLiNextA.action_act = "next";
				theNavUlLiNextA.id = "control" + theImage.id.replace("img", "") + "next";
				theNavUlLiNextA.className = "imglistnext";
				theNavUlLiNextA.action_act = "next";
				theNavUlLiNextA.action_field = theVars[1];
				theNavUlLiNextA.action_id = theImage.id.replace("img", "");
				theNavUlLiNextA.action_dataname = theVars[0];
				theNavUlLiNextA.onclick = ListMinigal;
				theNavUlLiNextA.onmouseover = MinigalHover;
				theNavUlLiNextA.onmouseout = MinigalOut;
				
				theNavUlLiNext.appendChild(theNavUlLiNextA);
				
				theNavUl.appendChild(theNavUlLiPrev);
				theNavUl.appendChild(theNavUlLiNext);

			var theClock = document.createElement("div");
				theClock.id = theImage.id.replace("img", "") + "clock";
				theClock.className = "clock";
				theClock.style.display = "none";

				theParent.insertBefore(theClock, theImage);
				theParent.insertBefore(theNavUl, theImage);
				theParent.appendChild(theInputPrev);
				theParent.appendChild(theInputNext);
		}
	}
}

function ListMinigal(){
	var theField = this.action_field;
	var theId = this.action_id;
	var theAction = this.action_act;
	var theDataname = this.action_dataname;
	
	var theClock = forid(theId + "clock");
		theClock.style.display = "block";
	
	var req = new JSHttpRequest();
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				forid(theId + "prev").value = req.responseJS.row["prev"];
				forid(theId + "next").value = req.responseJS.row["next"];
				
				forid(theId + 'img').src = req.responseJS.row["src"];
				forid(theId + 'img').title = forid(theId + "img").src;
				
				//description
				//if(req.responseJS.row['desc']) forid('gal_' + $id + '_desc').innerHTML = req.responseJS.row['desc'];
	
				forid(theId + 'img').onload=function(){
				  theClock.style.display = "none";
				}
			}
		}
		
		req.caching = true;
		req.open("GET", "inc/listminigal.php", true);
		req.send({ field: theField, id: theId.replace(theDataname, "").split("-")[0], position: forid(theId + theAction).value, dataname: theDataname });
	
	//Google stat
	urchinTracker('/gallery/' + theField + '/' + theId + '/' + theAction + '/' + theDataname);
}

function MinigalHover(){
	var Who = this.action_act;
	var WhoId = this.action_id;
	
	forid("control" + WhoId + Who).className = "imglist" + Who + "hover";
}

function MinigalOut(){
	var Who = this.action_act;
	var WhoId = this.action_id;
	
	forid("control" + WhoId + Who).className = forid("control" + WhoId + Who).className.replace("hover", "");
}

function openInNewWindow() {
	// Change "_blank" to something like "newWindow" to load all links in the same new window
    var newWindow = window.open(this.getAttribute('href'), '_blank');
    newWindow.focus();
    return false;
}

/*
Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var strWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\bnewblank\b/.exec(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				//objWarningText = document.createElement("em");
				//strWarningText = document.createTextNode(strNewWindowAlert);
				//objWarningText.appendChild(strWarningText);
				//link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

//From Yandex
document.onkeydown = NavigateThrough;
function NavigateThrough (event) {
	if (!document.getElementById) return;

	if (window.event) event = window.event;

	if (event.ctrlKey){
		var link = null;
		var href = null;
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null){
			case 0x25:
				link = document.getElementById ('nextlink');
				break;
			case 0x27:
				link = document.getElementById ('prevlink');
				break;
			case 0x26:
				link = document.getElementById ('uplink');
				break;
			case 0x28:
				link = document.getElementById ('downlink');
				break;
			case 0x24:
				href = '/';
				break;
		}

		if (link && link.href) document.location = link.href;
		if (href) document.location = href;
	}			
}

function PollActivate(){
		var fields = document.getElementsByTagName('input');
		for (var i=0; i<fields.length; i++){
			var field = fields[i];
			if(field.type == "radio" || field.type == "button"){
				field.onclick = PollGetResult;
			}
		}
}

function PollGetResult(){
		var polldata = "";
		
		var fields = document.getElementsByTagName('input');
		var pollurl = forid('polllocation');
		var pollid = forid("id_poll");
		
		for (var i=0; i<fields.length; i++){
			var field = fields[i];
			if(field.type == "radio" || field.type == "checkbox"){
				if(field.checked){
					polldata += field.value + "|";
				}
			}
		}
		
		var req = new JSHttpRequest();
		req.onreadystatechange = function(){
		  if (req.readyState == 4){
					forid("polldata").innerHTML = req.responseJS.row;
			}
		}
		
		forid("polldata").innerHTML = "<p class=\"clock\">&nbsp;</p>";
				
		req.caching = true;
		req.open('GET', '/inc/poll.php', true);
		req.send({ data: polldata, url: pollurl.value, id: pollid.value });
}

function SwitchTab(){
	var idtab = this.action.replace("tab", "");
	var theDiv = document.getElementById("div" + idtab);
	var theTabControl = document.getElementById(this.action);
	var idtabarr = this.action.replace("tab", "").split("_");
	
	var theActiveDiv = document.getElementsByTagName('div')[idtabarr[0]];
	
	for (var a=0; a<theActiveDiv.childNodes.length; a++) {
		var data = theActiveDiv.childNodes[a];
		
		if((data.nodeName=="DIV") && (data.className=="tabscontrol")){
				var theTabDiv = data.childNodes[a];
				for (var b=0; b<theTabDiv.childNodes.length; b++) {
					var theTabDivLi = theTabDiv.childNodes[b];
								if(theTabDivLi.nodeName=="LI"){
									theTabDivLi.className = theTabDivLi.className.replace("active", "");
								}
				}
		}	
		else if(data.nodeName=="DIV"){			
			data.className = data.className.replace("show", "");
		}
	}
	
	theDiv.className += "show";
	theTabControl.className += "active";
}

function getTabs() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Find all tabarea
		var theDivs = document.getElementsByTagName('div');
		for (var i=0; i<theDivs.length; i++) {
			var theDiv = theDivs[i];
			if (/\btabs\b/.exec(theDiv.className)) {
				for (var a=0; a<theDiv.childNodes.length; a++) {
					var theTabDiv = theDiv.childNodes[a];
						if((theTabDiv.nodeName=="DIV") && (theTabDiv.className=="tabscontrol")){
							for (var b=0; b<theTabDiv.childNodes.length; b++) {
								var theTabDivUl = theTabDiv.childNodes[b];
								if(theTabDivUl.nodeName=="UL"){
										for (var c=0; c<theTabDivUl.childNodes.length; c++) {
											var theTabDivLi = theTabDivUl.childNodes[c];
											if(theTabDivLi.nodeName=="LI"){
													theTabDivLi.id = "tab" + i + "_" + c;
													theTabDivLi.title = theTabDivLi.id;
													theTabDivLi.action = theTabDivLi.id;
													theTabDivLi.onclick = SwitchTab;
													
													for (var d=0; d<theDiv.childNodes.length; d++) {
														var theDivData = theDiv.childNodes[d];
														if((theDivData.nodeName=="DIV") && (theDivData.className!="tabscontrol") && (!theDivData.id)){
															theDivData.id = "div" + i + "_" + c;
															if(c==1){
																theDivData.className = "show";
																theTabDivLi.className = "active";
															}
															break;
														}
													}
													
											}
										}
								}
							}
						}
				}
			}
		}
	}
}

/* 

	Sitemap Styler v0.1
	written by Alen Grakalic, provided by Css Globe (cssglobe.com)
	visit http://cssglobe.com/lab/sitemap_styler/
	
*/

this.sitemapstyler = function(){
	var sitemap = document.getElementById("sitemap")
	if(sitemap){
		
		this.listItem = function(li){
			if(li.getElementsByTagName("ul").length > 0){
				var ul = li.getElementsByTagName("ul")[0];
					ul.style.display = "none";
				
				var span = document.createElement("span");
				
				span.className = "collapsed";
				span.onclick = function(){
					ul.style.display = (ul.style.display == "none") ? "block" : "none";
					this.className = (ul.style.display == "none") ? "collapsed" : "expanded";
				};
				
				li.appendChild(span);
			};
		};
		
		var items = sitemap.getElementsByTagName("li");
		for(var i=0;i<items.length;i++){
			listItem(items[i]);
		};
		
	};	
};

addEvent(window, 'load', getNewWindowLinks);
addEvent(window, 'load', zebRow);
addEvent(window, 'load', StartMiniGall);
addEvent(window, 'load', PollActivate);
addEvent(window, 'load', getTabs);
addEvent(window, 'load', sitemapstyler);
//addEvent(window, 'load', enableTooltips);