//******************************************************************************************
//*		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)
{
	
	if ( Math.abs(parseInt(elem.style.height) - targetHeight ) < 5 )
		elem.style.height = targetHeight + 'px'	
	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)}, 55);
 	}
}

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 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);
} 



//**************************************
//** FCTS BerubeToutant
var scrollY;

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";
		
	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 = document.body.scrollHeight;
	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="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="hideBigViewer();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" style="width:'+width+'px;height:'+height+'">'+html+'</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>';					  
	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);
	frame.style.height = parseInt(height);
	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;}

	width = document.body.clientWidth - 100;
	height = document.body.clientHeight - 100;
	
	
	var topCoord  = ((document.body.clientHeight/2)-(height/2));//+document.body.scrollTop;
	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 = document.body.scrollHeight;
	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"></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,null);
	searchProps(container, "proprietes.php");
	
}

function autoSearch()
{
	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 srchBox = document.getElementById("srchBox");
		srchBox.innerHTML = '<div align="center" width="100%" style="height:103px;line-height:103px"><strong>Recherche en cours...</strong><div>';
		searchProps(container);
	}
}

function toggleArrow(id)
{
	var img = document.getElementById(id);
	
	if (img.src.indexOf('down') > 0)
		img.src	= 'images/arrow.gif';
	else
		img.src	= 'images/arrow-down.gif';
}

function newImage(arg) {
	if (document.images) {
		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];
		}
	}
}