//******************************************************************************************
//*		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);
} 

var scrollY;

function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}

function showBigViewer(html, width, height, bgOpac, boxColor, imgPath, onclose)
{
	
	//Cacher le scroll
	/*document.body.scrollTop  = 0;
	document.body.scrollLeft = 0;*/
	//document.body.style.overflow = "hidden";
	
	var scrollpos = getScrollXY();  
	var topCoord  = ((document.body.clientHeight/2)-(height/2)) + document.body.scrollTop;
	var leftCoord = (document.body.clientWidth/2)-(width/2);
	topCoord = 70+scrollpos[1];
	
	 
	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.clientHeight+100+"px";
	filler.style.zIndex  = "999996";
	filler.onclick = function(){
		hideBigViewer();
	};
	changeOpac(bgOpac, 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="'+imgPath+'forme-popup-big_04.gif"></td>' +
					      '    <td height="15" background="'+imgPath+'forme-popup-big_05.gif"><div style="position:relative; float:right; ">' +
					      '      <div id="fermer" style="position:absolute; background-image:url('+imgPath+'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="'+imgPath+'forme-popup-big_06.gif"></td>' +
					      '  </tr>' +
					      '  <tr>' +
					      '    <td width="10" background="'+imgPath+'forme-popup-big_07.gif"></td>' +
					      '    <td bgcolor="'+boxColor+'" valign="top" style="width:'+width+'px;height:'+height+'">'+html+'</td>' +	
					      '    <td width="11" background="'+imgPath+'forme-popup-big_09.gif"></td>' +
					      '  </tr>' +
					      '  <tr>' +
					      '    <td width="10" height="16" background="'+imgPath+'forme-popup-big_10.gif"></td>' +
					      '    <td height="16" background="'+imgPath+'forme-popup-big_11.gif"></td>' +
					      '    <td width="11" height="16" background="'+imgPath+'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 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 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 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];
		}
	}
}

/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
 
	if (qs == null)
		qs=location.search.substring(1,location.search.length)
 
	if (qs.length == 0) return
 
	// Turn <plus> back to <space>
	// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
 
	// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])
 
		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
 
		this.params[name] = value
	}
}
 
function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
 
	var value=this.params[key]
	if (value==null) value=default_;
 
	return value
}

/*So throw that in your page, and then call that sucker like this:

// Parse the current page's querystring
var qs = new Querystring()

And then just go right on ahead and populate some variables with some query string variables:

var v1 = qs2.get("name1")
var v3 = qs2.get("name3", "default value")*/
