//Global utils script

function openMenu(id){
	document.getElementById(id).style.display = "block";
}
function closeMenu(id){
        //browser sniff
	//alert(navigator.appName);
        if (navigator.appName == "Microsoft Internet Explorer") {
            document.getElementById(id).onmouseleave = function() {
                document.getElementById(id).style.display = "none";
            };
        } else {
		document.getElementById(id).style.display = "none";
        }
}
function switchMapTitle(id){
	if(id > 0){
		var numId = (id * 75);
		document.getElementById("mapTitle").style.backgroundPosition="0px -"+ numId +"px";
	}else{
		document.getElementById("mapTitle").style.backgroundPosition="0px 0px";
	}
	
	
}

function switchImgSrc(img){
	document.getElementById("mtlimg").src=img;
}
var last = null;
var lastTab = 'tab_0';
var lastInBox = 'inBox_0';

function switchAdminView(position){
	boxer = 'adminBox_' + position;
	switcher = 'switch_' + position;
	currentState = document.getElementById(switcher).className;
	if (currentState == 'switchOn'){
		document.getElementById(switcher).className = 'switchOff';
		document.getElementById(boxer).style.display = 'block';
	}else{
		document.getElementById(switcher).className = 'switchOn';
		document.getElementById(boxer).style.display = 'none';
	}

}

function switchEvents(id){
	if (last != null) {
		document.getElementById(last).style.display = 'none';
	}
	last = id;
	document.getElementById(id).style.display = 'block';
}

function switchTab(inBoxId,TabId){
	if (lastInBox != null && lastInBox != inBoxId) {
		var elementInBox = document.getElementById(lastInBox);
		if (elementInBox) {
			elementInBox.style.display = 'none';
		}
	}
	lastInBox = inBoxId;
	var inBox = document.getElementById(inBoxId);
	if (inBox) { inBox.style.display = 'block'; }

	if (lastTab != null && lastTab != TabId) {
		var elementTab = document.getElementById(lastTab);
		if (elementTab) {
			elementTab.style.color = '#333';
		}
	}

	lastTab = TabId;
	var tab = document.getElementById(TabId);
	if (tab) { tab.style.color = '#a00'; }
}
function hideID(id){
	document.getElementById(id).style.display = 'none';
}
function showID(id){
	document.getElementById(id).style.display = 'block';
}

function insertEventAdress(_name,_street,_locality,_region,_pcode,_info){
	document.forms['event']['__DIREventData_locationname'].value = _name;
	document.forms['event']['__DIREventData_locationstreet'].value = _street;
	document.forms['event']['__DIREventData_locationlocality'].value = _locality;
	document.forms['event']['__DIREventData_locationregion'].value = _region;
	document.forms['event']['__DIREventData_locationpcode'].value = _pcode;
// 	document.forms['event']['__DIREventData_locationinfo'].value = _info;
}
function insertEventContact(_name,_phone,_email){
	document.forms['event']['__DIREventData_contactname'].value = _name;
	document.forms['event']['__DIREventData_contacttel'].value = _phone;
	document.forms['event']['__DIREventData_contactemail'].value = _email;
// 	document.forms['event']['__DIREventData_contactinfo'].value = _info;
}
var startTime = new Array(0,0,0);
var endTime = new Array(0,0,0);
var timerLimits = new Array(23,59,59);

var setStart = 0;
var setEnd = 0;

function setStartTime(hrs,min,sec){
	if(setStart == 0){
		startTime[0] = hrs;
		startTime[1] = min;
		startTime[2] = sec;
		setStart = 1;
	}
}

function setEndTime(hrs,min,sec){
	if(setEnd == 0){
		endTime[0] = hrs;
		endTime[1] = min;
		endTime[2] = sec;
		setEnd = 1;
	}
}
function incTimer(timer,part){
	if(timer == 'end'){
		if(endTime[part] == timerLimits[part]){
			endTime[part] = 0;
		}else{
			endTime[part] = endTime[part] + 1;
		}
	}
	if(timer == 'start'){
		if(startTime[part] == timerLimits[part]){
			startTime[part] = 0;
		}else{
			startTime[part] = startTime[part] + 1;
		}
	}
}
function decTimer(timer,part){
	if(timer == 'end'){
		if(endTime[part] == 0){
			endTime[part] = timerLimits[part];
		}else{
			endTime[part] = endTime[part] - 1;
		}
	}
	if(timer == 'start'){
		if(startTime[part] == 0){
			startTime[part] = timerLimits[part];
		}else{
			startTime[part] = startTime[part] - 1;
		}
	}
}
function setElementTimer(formulaire,element,timer){
	if(timer == 'end'){
		hrs = endTime[0];
		if(hrs < 10){
			hrs='0'+hrs;
		}
		min = endTime[1];
		if(min < 10){
			min='0'+min;
		}
		sec = endTime[2];
		if(sec < 10){
			sec='0'+sec;
		}
		timerString = hrs+':'+min+':'+sec;
		document.forms[formulaire][element].value = timerString;
		document.forms[formulaire]['label_'+element].value = timerString;
	}
	if(timer == 'start'){
		hrs = startTime[0];
		if(hrs < 10){
			hrs='0'+hrs;
		}
		min = startTime[1];
		if(min < 10){
			min='0'+min;
		}
		sec = startTime[2];
		if(sec < 10){
			sec='0'+sec;
		}
		timerString = hrs+':'+min+':'+sec;
		document.forms[formulaire][element].value = timerString;
		document.forms[formulaire]['label_'+element].value = timerString;
	}
}
var NS =false;
var IE = false;

if (window.ActiveXObject){ //ActiveX
	IE=true;
}
if(window.XMLHttpRequest){ //GEKO
	NS=true;
}


if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;
var xp = 0;
var yp = 0;
function getMouseXY(e) {

	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX+ document.body.scrollLeft;
		tempY = event.clientY+ document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}
	return true;
}

function moveOut(id){
    var object = document.getElementById(id).style;
	object.top = '-1000px';
	object.left ='-1000px';

}
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;;
        }

    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }

}
//Action check! ask realy act?
function didYouMean(url,what){
	var userSaid = confirm(what)
	if(userSaid == true){
		window.location=url;
		alert("L'opération s'est terminée avec succès!");
	}else{
		alert("Opération annulée!");
	}
}

function confirmRedirect(url,what){
	var userSaid = confirm(what)
	if(userSaid == true){
		window.location=url;
	}
}
//checkSwitch Functions
function checkSwitch(conteneur,element,formulaire){
	if(document.forms[formulaire][element].checked  ==  true ){
		document.getElementById(conteneur).style.display = 'block';
	}else{
		document.getElementById(conteneur).style.display = 'none';
	}
}
//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function showPopUpNote(id){
	document.getElementById(id).style.top = tempY+-40+'px';
	document.getElementById(id).style.left = tempX+30+'px';
	opacity(id, 0, 90, 1000);
	
}

function hidePopUpNote(id){
	opacity(id, 90, 0, 500);
	setTimeout("moveOut('"+id+"')",100);
}



var map = null;
var geocoder = null;

function showAddress(address,sname) {
	if (GBrowserIsCompatible()) {
		geocoder = new GClientGeocoder();
		if (geocoder) {
			geocoder.getLatLng(
				address,
				function(point) {
					if (!point) {
						alert("Google map ne peut pas trouver l´adresse de "+sname+" ! "+address+".");
					} else {
						map = new GMap2(document.getElementById("google_map"));
						map.addControl(new GSmallMapControl());
						map.addControl(new GMapTypeControl());
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						marker.openInfoWindowHtml("<strong>"+sname+"</strong><br/>"+address);
					}
				}
        		);
		}
	}
}
function getCenter(){
	if(IE){
		return document.body.offsetWidth;
	}
	if(NS){
		return window.innerWidth;
	}
}



Array.prototype.findIndex = function(value){
	var ctr = "";
	for (var i=0; i < this.length; i++) {
		if (this[i] == value) {
		return i;
		}
	}
	return ctr;
};

function directoryRedirect(a) {
	if (a.hasAttribute('redirect')) {
		a.href = a.getAttribute('redirect');
	}
	return true;
}
