//Global utils script

function switchMetaFiltersBox(id){
	mmf = 'mmf_'+id;
	mf = 'mf_'+id;
	if(id){
		currentState = document.getElementById(mf).className;
		if (currentState == 'mf_open'){
			document.getElementById(mf).className = 'mf_close';
			document.getElementById(mmf).className = 'normalb';
		}else{
			document.getElementById(mf).className = 'mf_open';
			document.getElementById(mmf).className = 'selectionb';
		}	
	}
}
function filterSwitch(){
	boxer = 'yellowBox';
	switcher = 'yellowSwitch';
	currentState = document.getElementById(switcher).className;
	if (currentState == 'openBox'){
		document.getElementById(switcher).className = 'closeBox';
		document.getElementById(boxer).style.display = 'block';
	}else{
		document.getElementById(switcher).className = 'openBox';
		document.getElementById(boxer).style.display = 'none';
	}
}
var last = null;
function switchEvents(id){
	if (last != null) {
		document.getElementById(last).style.display = 'none';
	}
	last = id;
	document.getElementById(id).style.display = 'block';
}
var lastTab = null;
var lastInBox = null;

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

	if (lastTab != null) {
		var lastTabElement = document.getElementById(lastTab);
		if (lastTabElement) { lastTabElement.style.color = '#333'; }
	}
	lastTab = TabId;
	var TabIdElement = document.getElementById(TabId);
	if (TabIdElement) { TabIdElement.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,_country){
	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_locationcountry'].value = _country;
// 	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;
	}
	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;
	}
}
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
		if (event && document.body) {
			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;
	}else{
		alert("Opération annulée!");
	}
}
//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;
	}
}

var lastOpenMenuId = false;

function openMenu(menu_id,xOpt){
	if(lastOpenMenuId == menu_id){
		closeMenu(lastOpenMenuId);
	}else{
		if(lastOpenMenuId){
			closeMenu(lastOpenMenuId);
		}
		document.getElementById(menu_id).style.left = (tempX + xOpt) + 'px';
		document.getElementById(menu_id).style.top = '31px';
		lastOpenMenuId = menu_id;
	}
}
function closeMenu(menu_id){
	document.getElementById(menu_id).style.top = '-1000px';
	lastOpenMenuId = false;
}

sdate = false;
edate = false;
DatesArray = false;
trigger = 'start';
passCounter = 0;
function setDatesArray(table){
	DatesArray= table;
}
function dateFormat(pdate){
	day = pdate.substr(6,2);
	month = pdate.substr(4,2);
	year = pdate.substr(0,4);
	return year+'-'+month+'-'+day
}
function DisplayPeriod(){
	if(trigger == 'end'){
		document.forms['fperiod']['sdate'].value = dateFormat(sdate);
		document.forms['fperiod']['edate'].value = dateFormat(sdate);
	}else{
		document.forms['fperiod']['sdate'].value = dateFormat(sdate);
		document.forms['fperiod']['edate'].value = dateFormat(edate);
	}
	javascript:document.forms['fperiod'].submit();
}
function setLastPeriod(){
	if(sdate){
		endp = DatesArray.findIndex(sdate) + 6;
		if(endp < DatesArray.length){
			setPeriod(DatesArray[endp]);
		}else{
			setPeriod(DatesArray[(DatesArray.length -1)]);
		}
	}
}
function setPeriod(pdate){
	if(!sdate & !edate){
		sdate = pdate;
		for(i=0; i < DatesArray.length ;i++){
			entity = 'cell'+DatesArray[i];
			id = DatesArray[i];
			if(id == sdate){
				document.getElementById(entity).className = 'in';
			}
			else{
				document.getElementById(entity).className = 'normal';
			}
		}
	}
	else if(sdate & !edate){
		if(sdate < pdate | sdate == pdate){
			edate = pdate
		}
		else if(sdate > pdate){
			edate = sdate;
			sdate = pdate;
		}
		for(i=0; i < DatesArray.length ;i++){
			entity = 'cell'+DatesArray[i];
			id = DatesArray[i];
			if(id < sdate | id > edate){
				document.getElementById(entity).className = 'normal';
			}
			else{
				document.getElementById(entity).className = 'in';
			}
		}
	}
	else if(sdate & pdate){
		if(trigger == 'start'){
			sdate = pdate;
			for(i=0; i < DatesArray.length ;i++){
				entity = 'cell'+DatesArray[i];
				id = DatesArray[i];
				if(id == sdate){
					document.getElementById(entity).className = 'in';
				}
				else{
					document.getElementById(entity).className = 'normal';
				}
			}
		}else{
			if(pdate < sdate){
				edate = sdate;
				sdate = pdate;
				
			}else{
				edate = pdate;
			}
			for(i=0; i < DatesArray.length ;i++){
				entity = 'cell'+DatesArray[i];
				id = DatesArray[i];
				if(id < sdate | id > edate){
					document.getElementById(entity).className = 'normal';
				}
				else{
					document.getElementById(entity).className = 'in';
				}
			}
		}

	}

	if (trigger == 'start'){
		trigger = 'end';
	}
	else if (trigger == 'end'){
		trigger = 'start';
		passCounter = passCounter + 1;
	}
	if(passCounter > 1){
		DisplayPeriod();
	}
}

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

function setFontSize(size){
  var expDate=  new Date();
  expDate.setDate(expDate.getDate()+300);
  document.cookie="fontSizePx"+ "=" + escape(size) + ";expires=" + expDate.toGMTString();
  setCookiesFontSize();
}
function setCookiesFontSize() {
  var val = getCookie('fontSizePx');
  if (val != '') {
    document.getElementById(fontElement).style.fontSize = val;
  }
}

function getCookie(myName){
  if (document.cookie.length > 0){
    c_start = document.cookie.indexOf(myName + "=");
    if (c_start!=-1) {
      c_start=c_start + myName.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return document.cookie.substring(c_start,c_end);
    }
  }
  return '';
}

function changeMetaLocation(aSelect) {
	var anOption = aSelect.options[aSelect.selectedIndex];
	if (anOption.value != "") {
		var oldlocation = location.href;
		var metaname = anOption.getAttribute('title');
		var append = '&' + metaname + '=' + anOption.value;
		var r1 = new RegExp('&' + metaname + '=\\d*&');
		var r2 = new RegExp('&' + metaname + '=\\d*$');
		if (oldlocation.indexOf(append) != -1) {
			append = '';
		}
		oldlocation = oldlocation.replace(r1, '&').replace(r2, '');
		location.href = oldlocation + append;
	}
}

function formSelectRedirect(aSelect) {
	if (aSelect.selectedIndex >= 0) {
		var url = aSelect.options[aSelect.selectedIndex].value;
		if (url != '') {
			window.location = url;
		}
	}
}

function switchTitleA(a) {
	var page_title = document.getElementById('page_title');
	if (page_title) { page_title.innerHTML = a.innerHTML; }
}

function toggleDivs(divShow, divHide) {
	document.getElementById(divHide).style.display = 'none';
	document.getElementById(divShow).style.display = 'block';
}

function dzSetCookie(cookieName, cookieValue) {
  var today = new Date();
  today.setFullYear(today.getFullYear()+1);
  var str = cookieName + '=' + encodeURIComponent(cookieValue) + ';path=/;expires='+today.toUTCString();
  document.cookie = str;
}
