function isFunction(a) {
    return typeof a == 'function';
}

if (!isFunction(Array.prototype.push)) {
    Array.prototype.push = function (obj) {        
        this[this.length] = obj;
        return this.length;
    };
}

var OnLoadFunctions = new Array();
var OnLoadFunctionsEx = new Array();
function AddOnLoadFunction(OnLoadFunction, OnLoadFunctionEx)
{
    OnLoadFunctions.push(OnLoadFunction);
    OnLoadFunctionsEx.push(OnLoadFunctionEx);
}

window.onload = OnBodyLoad;
function OnBodyLoad()
{
	for (var i = 0; i < OnLoadFunctions.length; i++)
    {
        var cfunction = OnLoadFunctions[i];
        var cfunctionex = OnLoadFunctionsEx[i];
        eval("if (window." + cfunction + ") { " + cfunctionex + "; }");
    }
}

function GetEventObject( evt )
{
	if( !evt && window.event )
	{
		evt = window.event;
	}

    if (evt)
	{
		if( evt.srcElement )
			return evt.srcElement;
		else
			return evt.target;
	}
	else
		return null;
}

function getRealLeft(imgElem)
{
	xPos = eval(imgElem).offsetLeft;
	tempEl = eval(imgElem).offsetParent;
  	while (tempEl != null)
	{
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
  	
    if(navigator.platform == "MacPPC" && navigator.userAgent.indexOf("MSIE") != -1)
    {           
        //Dans IE pour Mac, il faut additionner la marge du body.
        xPos += parseInt(document.body.currentStyle.marginLeft);       
    }
      	
	return xPos;
}

function getRealTop(imgElem)
 {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while ( tempEl != null )
	{
        //Note: On a pas besoin de passer par ici pour calculer la position avec
        //      opera car offsetTop contient déjà la bonne position.  On a pas besoin
        //      de l'additionnner aux positions de ses éléments parent.
  		yPos += tempEl.offsetTop;
  		
  		if(navigator.userAgent.indexOf("Opera") != -1 && yPos != 0)
  		    tempEl = null;
  		else
  		    tempEl = tempEl.offsetParent;	
  	}
  	
    if( (navigator.platform == "MacPPC" && navigator.userAgent.indexOf("MSIE") != -1) )
    {           
        //Dans IE pour Mac, il faut additionner la marge du body.
        yPos += parseInt(document.body.currentStyle.marginTop);       
    }
              	
	return yPos;
}

function getSubMenuTop(imgElem)
 {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while ( tempEl != null && (!tempEl.id || tempEl.id != "menu"))
	{
        //Note: On a pas besoin de passer par ici pour calculer la position avec
        //      opera car offsetTop contient déjà la bonne position.  On a pas besoin
        //      de l'additionnner aux positions de ses éléments parent.
  		yPos += tempEl.offsetTop;
  		
  		if(navigator.userAgent.indexOf("Opera") != -1 && yPos != 0)
  		    tempEl = null;
  		else
  		    tempEl = tempEl.offsetParent;	
  	}
  	
    if( (navigator.platform == "MacPPC" && navigator.userAgent.indexOf("MSIE") != -1) )
    {           
        //Dans IE pour Mac, il faut additionner la marge du body.
        yPos += parseInt(document.body.currentStyle.marginTop);       
    }
              	
	return yPos;
}


/* Agrandir les images de produits et de réalisations */
function ShowLargePicture()
{
    var imgLarge = document.getElementById("imgLarge");
    if (imgLarge)
    {
        imgLarge.style.display = "block";
    }
}

function HideLargePicture()
{
    var imgLarge = document.getElementById("imgLarge");
    if (imgLarge)
    {
        imgLarge.style.display = "none";
    }
}

AddOnLoadFunction("SetupOnKeyDownHandler", "SetupOnKeyDownHandler()");

function SetupOnKeyDownHandler()
{    
    var bodys = document.getElementsByTagName("body");
    if (bodys && bodys.length > 0)
    {
        bodys[0].onkeydown = OnBodyKeyDown;
    }
}

function OnBodyKeyDown( evt )
{
    var keyCode = -1;
    var ctl = GetEventObject(evt);
    
    if (evt && evt.keyCode)
    {
        keyCode = evt.keyCode;
    }
    else if (event && event.keyCode)
    {
        keyCode = event.keyCode;
    }
    
    if (keyCode == 13 && ctl && ctl.id && ctl.value != "")
    {
        switch (ctl.id)
        {
            case "uc_ucBloqueTrouverDetaillant_txtCodePostal":
            case "uc_element_bloque_trouver_detaillant_txtCodePostal":
                var fsubmitdealer = document.getElementById("uc_ucBloqueTrouverDetaillant_ibTrouverDetaillant");
                if (!fsubmitdealer)
                {
                    fsubmitdealer = document.getElementById("uc_element_bloque_trouver_detaillant_ibTrouverDetaillant");
                }
                if (fsubmitdealer)
                {
                    if (evt)
                    {
                        evt.returnValue = false;
                        evt.cancel = true;
                    }
                    fsubmitdealer.click();
                    return false;
                }
                break;
            case "uc_element_en_tete_txtSearch":
                var fsubmitsearch = document.getElementById("uc_element_en_tete_ibRecherche");
                if (fsubmitsearch)
                {
                    if (evt)
                    {
                        evt.returnValue = false;
                        evt.cancel = true;
                    }
                    fsubmitsearch.click();
                    return false;
                }
                break;
            case "uc_txtRecherche":
                var fsubmitnewsearch = document.getElementById("uc_btnRecherche");
                if (fsubmitnewsearch)
                {
                    if (evt)
                    {
                        evt.returnValue = false;
                        evt.cancel = true;
                    }
                    fsubmitnewsearch.click();
                    return false;
                }
                break;
            default:
                return true;
                break;
        }
    }
    
    return true;
}

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}