//******************************************************************************************
//*		Script 'utils.js'
//*-----------------------------------------------------------------------------------------
//*		Description  : 	Regroupement de fonction utils qui sont souvent utilis�s			
//*
//*		Programmeurs :	Christian Charest 	[CC]  
//*
//******************************************************************************************
var curLatest = 1
var timFadeInOut = -1;


// Trouve la position d'un element dans un container
// par default le container = boby
function findPos(obj) 
{
	var curleft = 0;
	var curtop  = 0;
		
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		
		while (obj == obj.offsetParent ) 
		{
			curleft += obj.offsetLeft;
			curtop  += obj.offsetTop;
		}
	}
	
	return [curleft,curtop];
}

// Retourne les coordonn�es de la souris dans la page html et non dans partit visible 
function getMousePos(event)
{
	var curX = event.clientX  + document.body.scrollLeft - 1 ;
	var curY = event.clientY  + document.body.scrollTop  - 1 ;
	
	return [curX,curY];
	
}

function putElemToLayer(elem,layer)
{
	var divs = document.getElementsByTagName('div')
	
	for (var i = 0; i < divs.length; i++)
	if (divs[i].style.position == "absolute" && divs[i].id!="" )
	{
		divs[i].style.zIndex = divs[i].style.zIndex - 1;
	}
	
	elem.style.zIndex = layer;
}	

//Change l`opacit�
function changeOpac(opacity, elem) 
{ 			
	elem.style.opacity = (opacity / 100); 
    elem.style.MozOpacity = (opacity / 100); 
    elem.style.KhtmlOpacity = (opacity / 100); 
    elem.style.filter = "alpha(opacity=" + opacity + ")"; 
}

//devine
function show(elem)
{
	elem.style.display = "";					
}	

function hide(elem)
{						
	elem.style.display = "none";
}

function expandCollapse(elemId, expanHeight )
{
	var elem = document.getElementById(elemId);
	
	//Collapse
	if (parseInt(elem.style.height) == expanHeight)
	{
		//elem.style.height  = "0px";
		resizeAnimate(elem,1);
	}
	//Expand
	else 
	{
		//elem.style.height = expanHeight+'px';
		//show(elem);
		resizeAnimate(elem,expanHeight);
		//elem.style.height = expanHeight+'px';
		//show(elem);
	}
}

function resizeAnimate(elem, targetHeight)
{
	elem.style.visibility = 'visible';
	if ( Math.abs(parseInt(elem.style.height) - targetHeight ) < 5 )
	{
		elem.style.height = targetHeight + 'px';
		if(targetHeight <= 1) {
			elem.style.visibility = 'hidden';	
		}
	}
	else if ( parseInt(elem.style.height) < targetHeight )
	{
		elem.style.height = parseInt(elem.style.height) + 5 + 'px';
		window.setTimeout( function(){resizeAnimate(elem,targetHeight)}, 5);
	}
	else if ( parseInt(elem.style.height) > targetHeight )
	{
		elem.style.height = parseInt(elem.style.height) - 5 + 'px';
		window.setTimeout( function(){resizeAnimate(elem,targetHeight)}, 5);
	}
		
}

function fadeInOut(targetOp, elem, speed, callback)
{
	if (typeof speed == "undefined"){speed = 5;}
	
 	var curOp = parseFloat(elem.style.opacity)*100;
 	if (isNaN(curOp))
 		curOp = 100;	
 	
 	if (Math.abs(curOp - targetOp) < speed)
 	{
 		changeOpac(targetOp, elem);	
 		timFadeInOut = -1; 	
 		
 		if (typeof callback != "undefined")
 			callback();
 	}
 	else
 	{
 		//alert(curOp);
	 	if (curOp < targetOp)
	 		changeOpac(curOp+speed, elem);
	 	else if (curOp > targetOp)
	 		changeOpac(curOp-speed, elem);
	 	timFadeInOut = setTimeout(function(){fadeInOut(targetOp, elem, speed, callback)}, 50);
 	}
}

function tagValue(tag, elem, clear)
{
	if (typeof clear == "undefined"){clear = false;}
		
	elem.value = trim(elem.value);

	if (clear)
	{	
		if (elem.value == tag)
			elem.value = "";
	}
	else if (elem.value.length == 0)
			elem.value = tag;	
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function findElementById(id, fromElem)
{
	
	var nodes = fromElem.childNodes;
	for(var i=0; i<nodes.length;i++)
	{
		if (id == nodes[i].id)
			return nodes[i];
		else if (nodes[i].childNodes.length > 0)
		{
			var elem = findElementById(id, nodes[i]);
			if (elem != false)
				return elem;
		}
	}
	
	return false;
}

function findElementByAttr(block, attrName, attrValue)
{
	var nodes   = block.childNodes;
	var attrib;
	var text = ""; 
	for( var i=0; i<nodes.length;i++ )
	{
		attrib = nodes[i].getAttribute("rel");		
		var attrs = nodes[i].attributes;
	    
	    for(var j=attrs.length-1; i>=0; i--) 
        	text += attrs[j].name + "->" + attrs[j].value;
       
        		
		if (attrib != null && attrib == attrValue)
			return nodes[i];
		else if (nodes[i].childNodes.length > 0)
		{
			var elem = findElementByAttr(nodes[i], attrName, attrValue);
			if (elem != false)
				return elem;
		}
	}
	
	//alert(text);
	
	return false
	
}

function findElementsByClass(block, className, elems)
{
	
	var nodes   = block.childNodes;
	if ("undefined" == typeof elems)
		elems=new Array() 
	for( var i=0; i<nodes.length; i++ )
	{
		if (nodes[i].className)
		{
			if (nodes[i].className == className)				
				elems.push(nodes[i]);
			
		}
			
		
		if (nodes[i].childNodes.length > 0)
		{
			
			elems = findElementsByClass(nodes[i], className, elems);
			
		}
	}
	

	return elems;
	
}

function checkEnter(e, idAction)
{ 
	var characterCode;
	
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)	
		var elem = document.getElementById(idAction);
		elem.onclick();
		
		
		
		return false; 
	}
	else
	{
		return true 
	}

}

function pause(millis) 
{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); } 
	while(curDate-date < millis);
} 


function basename(path, suffix) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ash Searle (http://hexmen.com/blog/)
    // +   improved by: Lincoln Ramsay
    // *     example 1: basename('/www/site/home.htm', '.htm');
    // *     returns 1: 'home'
 
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}

//**************************************
//** FCTS BerubeToutant
var scrollY;
var root = "";

function hideBigViewer()
{
	var onTopViewer = document.getElementById("onTopViewer");

	if (onTopViewer != null)
	{
		while ( onTopViewer.childNodes.length >= 1 )
	    {
	        onTopViewer.removeChild( onTopViewer.firstChild );       
	    } 
	
		document.body.removeChild(onTopViewer);	
		document.body.style.overflow = "";
		
		if (typeof onTopViewer.userClose != "undefined")
			onTopViewer.userClose();
	}
	
}

function showBigViewer(html, width, height, onclose)
{

	//Cacher le scroll
	//document.body.scrollTop  = 0;
	//document.body.scrollLeft = 0;
	//document.body.style.overflow = "hidden";
	
	if (window.innerWidth) {
		var topCoord = (window.pageYOffset || document.documentElement.scrollTop || 0) + (window.innerHeight-height)/2 - 5;
		var leftCoord = (window.innerWidth/2)-(width/2);
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		var topCoord = (window.pageYOffset || document.documentElement.scrollTop || 0) + (document.documentElement.clientHeight-height)/2 - 5;
		var leftCoord = (document.documentElement.clientWidth/2)-(width/2);
	}
	else if (document.body) {
		var topCoord = (window.pageYOffset || document.documentElement.scrollTop || 0) + (document.body.clientHeight-height)/2 - 5;
		var leftCoord = (document.body.clientWidth/2)-(width/2);
	}
	
	//var topCoord  = ((document.body.clientHeight/2)-(height/2)) - 10;//+document.body.scrollTop;
	//var leftCoord = (document.body.clientWidth/2)-(width/2);
	 
	onTopViewer = document.createElement("DIV");
	onTopViewer.id = "onTopViewer";
	onTopViewer.userClose = onclose;
	document.body.appendChild(onTopViewer);
		
	filler = document.createElement("DIV");					
	filler.style.backgroundColor = "black";
	filler.style.position = "absolute";
	filler.style.top    = "0px";
	filler.style.left   = "0px";
	filler.style.width  = "100%";
	filler.style.height = (window.innerHeight + window.scrollMaxY || document.body.scrollHeight || document.body.offsetHeight || 0)+'px';
	filler.style.zIndex  = "999996";
	changeOpac(60, filler)
	onTopViewer.appendChild(filler);
	
	container = document.createElement("DIV");
	container.style.position = "absolute";
	container.style.top    = topCoord+"px";
	container.style.left   = leftCoord+"px";
	container.style.zIndex  = "999998";
	container.style.color = "white";
	
	container.innerHTML = '<table border="0" cellpadding="0" cellspacing="0">' +
					      '  <tr>' +
					      '    <td width="10" height="15" background="'+tplDir+'images/forme-popup-big_04.gif"></td>' +
					      '    <td height="15" background="'+tplDir+'images/forme-popup-big_05.gif"><div style="position:relative; float:right; ">' +
					      '      <div id="fermer" style="position:absolute; background-image:url('+tplDir+'images/forme-popup-big_02.gif); background-repeat: no-repeat; top:-20px; height:21px; width: 70px; left:-70px; cursor:pointer" onclick="hideBigViewer();return false;"></div>' +
					      '    </div> </td>' +
					      '    <td width="11" height="15" background="'+tplDir+'images/forme-popup-big_06.gif"></td>' +
					      '  </tr>' +
					      '  <tr>' +
					      '    <td width="10" background="'+tplDir+'images/forme-popup-big_07.gif"></td>' +
					      '    <td bgcolor="#f3f1f0" valign="top" style="width:'+width+'px;height:'+height+'px">'+html+'</td>' +	
					      '    <td width="11" background="'+tplDir+'images/forme-popup-big_09.gif"></td>' +
					      '  </tr>' +
					      '  <tr>' +
					      '    <td width="10" height="16" background="'+tplDir+'images/forme-popup-big_10.gif"></td>' +
					      '    <td height="16" background="'+tplDir+'images/forme-popup-big_11.gif"></td>' +
					      '    <td width="11" height="16" background="'+tplDir+'images/forme-popup-big_12.gif"></td>' +
					      '  </tr>' +
					      '</table>';					  
	onTopViewer.appendChild(container);
						
	frame = document.createElement("iframe");
	frame.style.position = "absolute";
	frame.style.top    = container.style.top;
	frame.style.left   = container.style.left;
	frame.style.width  = parseInt(width)+'px';
	frame.style.height = parseInt(height)+'px';
	frame.style.zIndex  = "999997"
	onTopViewer.appendChild(frame);
	
	//scrollY = (document.all)?document.body.scrollTop:window.pageYOffset;
}

function hideBigMap()
{
	var onTopMap = document.getElementById("onTopMap");
	if (onTopMap != null)
	{
		while ( onTopMap.childNodes.length >= 1 )
	    {
	        onTopMap.removeChild( onTopMap.firstChild );       
	    } 
	
		document.body.removeChild(onTopMap);	
		document.body.style.overflow = "";
		//window.scrollTo(0, scrollY);
		var curPt = map.getCenter();
		var curZm = map.getZoom();
		mId = mId - 1;
		activateMap(mId);
		map.setCenter(curPt, curZm);
		map.addOverlay(selMarker);
	}
	
}

function showBigMap(callback)
{
	if("undefined" == typeof callback){callback = false;}
	
	if (window.innerWidth) {
		var width=window.innerWidth - 100;
		var height=window.innerHeight - 100;
		var topCoord = (window.pageYOffset || document.documentElement.scrollTop || 0) + (window.innerHeight-height)/2 - 5;
		var leftCoord = (window.innerWidth/2)-(width/2);
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		var width=document.documentElement.clientWidth - 100;
		var height=document.documentElement.clientHeight - 100;
		var topCoord = (window.pageYOffset || document.documentElement.scrollTop || 0) + (document.documentElement.clientHeight-height)/2 - 5;
		var leftCoord = (document.documentElement.clientWidth/2)-(width/2);
	}
	else if (document.body) {
		var width=document.body.clientWidth - 100;
		var height=document.body.clientHeight - 100;
		var topCoord = (window.pageYOffset || document.documentElement.scrollTop || 0) + (document.body.clientHeight-height)/2 - 5;
		var leftCoord = (document.body.clientWidth/2)-(width/2);
	}
	
	onTopMap = document.createElement("DIV");
	onTopMap.id = "onTopMap";
	document.body.appendChild(onTopMap);
	
	filler = document.createElement("DIV");					
	filler.style.backgroundColor = "black";
	filler.style.position = "absolute";
	filler.style.top    = "0px";
	filler.style.left   = "0px";
	filler.style.width  = "100%";
	filler.style.height = (window.innerHeight + window.scrollMaxY || document.body.scrollHeight || document.body.offsetHeight || 0)+'px';
	filler.style.zIndex  = "999996";
	filler.id = "bigMap";
	changeOpac(60, filler)
	onTopMap.appendChild(filler);
	
	container = document.createElement("DIV");
	container.style.position = "absolute";
	container.style.top    = topCoord+"px";;
	container.style.left   = leftCoord+"px";
	container.style.zIndex  = "999998";
	container.style.color = "white";	
	container.innerHTML = '<table border="0" cellpadding="0" cellspacing="0">' +
					      '  <tr>' +
					      '    <td width="10" height="15" background="images/forme-popup-big_04.gif"></td>' +
					      '    <td height="15" background="images/forme-popup-big_05.gif"><div style="position:relative; float:right; ">' +
					      '      <div id="fermer" style="position:absolute; background-image:url(images/forme-popup-big_02.gif); background-repeat: no-repeat; top:-20px; height:21px; width: 70px; left:-70px; cursor:pointer" onclick="hideBigMap();return false;"></div>' +
					      '    </div> </td>' +
					      '    <td width="11" height="15" background="images/forme-popup-big_06.gif"></td>' +
					      '  </tr>' +
					      '  <tr>' +
					      '    <td width="10" background="images/forme-popup-big_07.gif"></td>' +
					      '    <td bgcolor="#f3f1f0" valign="top"><div id="bmTitle" style="width:100%;display:none;margin-bottom:5px"></div><div id="bmDirContainer" style="display:none;overflow:auto;vertical-align:top;float:left;"><div id="bmDirections" style="background-color:white;height:100%;"></div></div><div style="height:'+height+'px;width:'+width+'px;float:left;overflow:hidden" id="map2"></div></td>' +	
					      '    <td width="11" background="images/forme-popup-big_09.gif"></td>' +
					      '  </tr>' +
					      '  <tr>' +
					      '    <td width="10" height="16" background="images/forme-popup-big_10.gif"></td>' +
					      '    <td height="16" background="images/forme-popup-big_11.gif"></td>' +
					      '    <td width="11" height="16" background="images/forme-popup-big_12.gif"></td>' +
					      '  </tr>' +
					      '</table>';					  
	onTopMap.appendChild(container);
						
	frame = document.createElement("iframe");
	frame.style.position = "absolute";
	frame.style.top    = container.style.top;
	frame.style.left   = container.style.left;
	frame.style.width  = parseInt(width+18);
	frame.style.height = parseInt(height+28);
	frame.style.zIndex  = "999997"
	onTopMap.appendChild(frame);	
	
	//center on current map pos
	var curPt = map.getCenter();
	var curZm = map.getZoom();
	var fctToExec;

	fctToExec = function(){			
														
							if (callback != false)
								callback();
							else
							{
								//addAllMarkers();
								markerMan.addMarkers(markers, 7, 17);
								markerMan.refresh();
								
								map.addOverlay(selMarker);
								//selectMarker();
								map.setCenter(curPt, curZm);
							}
							map.enableScrollWheelZoom();
				}	
	
	loadGMap(fctToExec, "map2");
	//document.body.scrollTop  = 0;
	//document.body.scrollLeft = 0;
	//document.body.style.overflow = "hidden";
	//scrollY = (document.all)?document.body.scrollTop:window.pageYOffset;
} 

function sendLSrchQuery()
{
	var container = document.getElementById("srchFields");
	getInfos("proprietes.php?action=showSrchPannel&value=1",null,function(rAct){
		if (rAct.readyState == 4 && rAct.status == 200)	
			searchProps(container, "proprietes.php");
	});
	
	
}

function changeSelectOptions(selectObj, newOptions) {
	for (var i=0; i<selectObj.options.length; i++) {
		if (i < newOptions.length) {
			selectObj.options[i]=new Option(newOptions[i], newOptions[i], false, false)
		} else {
			selectObj.options[i]=null	
		}
	}
}

function setSelectValue(selectObj, value) 
{
	for (var i=0; i<selectObj.options.length; i++) {
		if (selectObj.options[i].value == value)	{
			  selectObj.selectedIndex = i;
		}
	}	
}

function autoSearch(text)
{
	var container = document.getElementById("srchFields");			
	var pSrch = document.getElementById("srchPannel");

	if (pSrch != null)
	{
		show(pSrch); 
		getInfos("?action=showSrchPannel&value=1",null,null);
		
		//cacher le block de la map
		var bMap = document.getElementById("blockMap");	
		bMap.style.display = "none";

		var detailsProp = document.getElementById("detailsProp");	
		detailsProp.style.display = "none";	
		
		var menuProp = document.getElementById('menu-proprietes');
		if(menuProp) {
			menuProp.style.display = 'block';	
		}
		
		var srchBox = document.getElementById("srchBox");
		srchBox.innerHTML = '<div align="center" width="100%" style="height:103px;line-height:103px"><strong>'+text+'...</strong><div>';
		searchProps(container);
	}
}

function toggleArrow(id, templateDir)
{
	var img = document.getElementById(id);
	
	if (img.src.indexOf('dessus') > 0)
		img.src	= templateDir+'images/recherche-fleche.png';
	else
		img.src	= templateDir+'images/recherche-fleche-dessus.png';
}

function toggleBackgroundColor(id, color, time)
{
	var element = document.getElementById(id);
	
	if (element.style.backgroundColor == '') {
		element.style.backgroundColor = color;
	} else {
		window.setTimeout( function(){ element.style.backgroundColor = '' }, time);
	}
}

function newImage(arg) {
	if (document.images) {
		var rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function checkClientConnect(rAct)			
{
	if (rAct.readyState == 4 && rAct.status == 200)
	{
		//alert('test');
		
		//alert(rAct.responseText);
		var infoBox = document.getElementById('uConnectInfo');
		var results = rAct.responseText;
		if (results.indexOf('success') != -1)			
				location.href="accesClient.php";
		else
		{
			infoBox.innerHTML = results;
		}
	}
}