
/******* Nouvelles fonctions : Cyril 24/03/09
 *
 * - hpo_menu_onlinkover    Action sur événement mouseover sur le lien ou bouton
 * - hpo_menu_onlinkeout    Action sur événement mouseout sur le lien ou bouton
 * - hpo_menu_onmenuover    Action sur événement mouseover sur le menu (toute la surface)
 * - hpo_menu_onmenuout     Action sur événement mouseout sur le menu (toute la surface)
 * - hpo_menu_openmenu      Ouverture du menu
 * - hpo_menu_closemenu     Fermeture du menu
 *
 */
/**
 *
 * Menu Object qui contient toutes les infos de gestion du menu et de ses items
 *
 * @params sLink Racine commune aux identifiant de type Link
 * @params sLayer Racine commune aux identifiant de type Layer
 * @params activeId Identifiant du menu actif au départ (sélectionné)
 * @params timeOut Timeout d'affichage du claque
 * @params closeLast  Fermer le dernier calque ouvert
 */
function hpo_menu_object(sLink, sLayer, activeId, selectedLink, timeOut, closeLast, isAjax) {
    this.sLink = sLink;
    this.sLayer = sLayer;
    this.activeId = activeId;
    this.selectedLink = selectedLink;
    this.timeOut = timeOut;
    this.closeLast = closeLast;
    this.isAjax = isAjax;
    this.ajaxPages = new Array();
    this.ajaxToogleLoaded = new Array();
    this.timers = new Array();
    this.skipEvents = false;
}

/**
 * Timer Objet qui contient les infos timer
 *
 */
function hpo_menu_timer(menuId, timerId, action) {
    this.menuId = menuId;   // id du menu (lien + calque)
    this.action = action;   // Action : open / close
    this.timerId = timerId; // Timer de fermeture ou d'ouverture
}

function hpo_menu_init(sLink, sLayer, activeId, selectedLink, timeOut, closeLast, isAjax) {
    return new hpo_menu_object(sLink, sLayer, activeId, selectedLink, timeOut, closeLast, isAjax);
}

hpo_menu_object.prototype.onLinkClick = function(menuId) {
    var savedId = this.activeId;
    
    if (this.activeId)
        this.closeMenu(this.activeId);
    
    if (menuId != savedId)
        this.openMenu(menuId);
}

hpo_menu_object.prototype.onLinkOver = function(menuId) {

	if ( this.isAjax && !this.ajaxToogleLoaded[menuId] )
		this.loadAjax(menuId);
	
    // 1 on prolonge le menu actif si il est ouvert
    for (var i = 0; i < this.timers.length; i++) {
        var timerObj = this.timers[i];
        clearTimeout(timerObj.timerId);
        
        if ((timerObj.menuId == this.activeId) && (timerObj.action == 'close')) {
            var thisObj = this;
            var timerId = setTimeout( function(){ thisObj.closeMenu(thisObj.activeId) }, this.timeOut);
            this.timers[i].timerId = timerId;
        }
        else
            this.timers.splice(i--, 1);
    }
    
    // 2 on créé le nouveau timer d'ouverture de menu et on le stocke !
    var thisObj = this;
    var timerId = setTimeout( function(){ thisObj.openMenu(menuId) }, this.timeOut);
    var timerObj = new hpo_menu_timer(menuId, timerId, 'open');
    this.timers.push(timerObj);
}

hpo_menu_object.prototype.onLinkOut = function (menuId){
    // 1 on supprime l'eventuel timer open du lien
    for (var i = 0; i < this.timers.length; i++) {
        var timerObj = this.timers[i];
        if ((timerObj.menuId == menuId) && (timerObj.action == 'open')) {
            clearTimeout(timerObj.timerId);
            this.timers.splice(i--, 1);
        }
    }
    
    // 2 on appelle la méthode de fermeture onLayerOut !
    this.onLayerOut(menuId);
}

hpo_menu_object.prototype.onLayerOver = function (menuId) {
    // on supprime tous les timers !
    for (var i = 0; i < this.timers.length; i++) {
        var timerObj = this.timers[i];

        clearTimeout(timerObj.timerId);
        this.timers.splice(i--, 1);
    }
}

hpo_menu_object.prototype.onLayerOut = function (menuId) {
    // on créé le nouveau timer de fermeture pour le menu actif si il existe
    if ((this.closeLast == 1) && (this.activeId != 0) && (!this.skipEvents)) {
        var thisObj = this;
        var timerId = setTimeout( function(){ thisObj.closeMenu(thisObj.activeId) }, this.timeOut);
        var timerObj = new hpo_menu_timer(this.activeId, timerId, 'close');
        this.timers.push(timerObj);
    }
}

/*** Spécifique ie : gestion onFocus sur le select*/
hpo_menu_object.prototype.onFocus = function () {  
    this.skipEvents = true;
}

/*** Spécifique ie : gestion onLeave sur le select*/
hpo_menu_object.prototype.onBlur = function () {   
    this.skipEvents = false;         
}
            
/*** Spécifique ie : gestion onLeave sur le select*/
hpo_menu_object.prototype.onChange = function () {
    this.skipEvents = false;
    event.srcElement.blur();       
}
            
hpo_menu_object.prototype.openMenu = function (menuId) {
	if ( this.isAjax && !this.ajaxToogleLoaded[menuId] )
		this.loadAjax(menuId);
		
    var linkObj = document.getElementById(this.sLink + menuId);             // Le nouveau calque
    var layerObj = document.getElementById(this.sLayer + menuId);
    
    // On supprime l'entrée du tableau hpo_menu_objects
    this.cleanTimers(menuId, 'open');
    
    linkObj.className = 'over';
    // Parcours des enfants et modification de la propriété display
    //this.display(linkObj, linkObj.parentNode, 'block');
    layerObj.style.display = 'block';
    
    // Si il existe un menu actif <> de celui qu'on désire ouvrir, on le supprime !
    if ((this.activeId > 0) && (this.activeId != menuId))
        this.closeMenu(this.activeId);
    // Si on a un selectedLink...
    else if ((this.selectedLink > 0) && (this.selectedLink != menuId))
        document.getElementById(this.sLink + this.selectedLink).className = '';
    
    // On enregistre le nouveau calque actif
    this.activeId = menuId;
    
    // Si on a du ie !!! On parcours les éléments du calque pour ajouter aux selects la gestion du onmouseover et onmouseout !
    if (typeof document.selection != "undefined") {
        var objs = layerObj.getElementsByTagName("SELECT");
        var thisObj = this;
        
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].attachEvent) {
                // Le select
                objs[i].attachEvent("onfocus", function(){ thisObj.onFocus() });
                objs[i].attachEvent("onblur", function(){ thisObj.onBlur() });
                objs[i].attachEvent("onchange", function(){ thisObj.onChange() });
            }
        }
    }
}

hpo_menu_object.prototype.closeMenu = function (menuId) {
    if (menuId == 0)
        return;
    
    var linkObj = document.getElementById(this.sLink + menuId);
    var layerObj = document.getElementById(this.sLayer + menuId);
    
    // On supprime l'entrée du tableau hpo_menu_objects
    this.cleanTimers(menuId, 'close');
    
    // On gère la sélection du lien...
    if (this.selectedLink > 0)
        document.getElementById(this.sLink + this.selectedLink).className = 'over';
    
    if (menuId != this.selectedLink)
        linkObj.className = '';
    
    // Parcours des enfants et modification de la propriété display
    //this.display(linkObj, linkObj.parentNode, 'none');
    if ( layerObj )
    	layerObj.style.display = 'none';
    
    // Il n'y a plus de calque actif
    this.activeId = 0;
    
    // Si on a du ie !!! On parcours les éléments du calque pour ajouter aux selects la gestion du onmouseover et onmouseout !
    if (typeof document.selection != "undefined" && layerObj) {
        var objs = layerObj.getElementsByTagName("SELECT");
        var thisObj = this;
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].detachEvent) {
                // Le select
                objs[i].detachEvent("onfocus", function(){ thisObj.onFocus() });
                objs[i].detachEvent("onblur", function(){ thisObj.onBlur() });
                objs[i].detachEvent("onchange", function(){ thisObj.onChange() });
            }
        }
    }
}

hpo_menu_object.prototype.cleanTimers = function (id, sType) {
    for (var i = 0; i < this.timers.length; i++) {
        var timerObj = this.timers[i];
        
        if ((this.timers[i].menuId == id) && (this.timers[i].action == sType)) {
            this.timers.splice(i--, 1);
            break;
        }
    }
}

hpo_menu_object.prototype.loadAjax = function(menuId)
{
	var pageUrl = this.ajaxPages[(menuId - 1)];
	if ( pageUrl )
	{
		document.getElementById(this.sLayer + menuId).className = 'loading';
		var thisObj = this;
		pageXhr = this.getFile(pageUrl, 'GET');
		pageXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
		pageXhr.onreadystatechange = function() 
		{
			if(pageXhr.readyState == 4) 
			{
				document.getElementById(thisObj.sLayer + menuId).innerHTML = pageXhr.responseText;
				document.getElementById(thisObj.sLayer + menuId).className = '';
				thisObj.ajaxToogleLoaded[menuId] = 1;
			}
		}
		pageXhr.send(null); 
	}
}

hpo_menu_object.prototype.getXhr = function()
{
	if(window.XMLHttpRequest) 
		xhr = new XMLHttpRequest();
	else if(window.ActiveXObject) 
		try
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	else
	{
		alert("Votre navigateur n'est pas compatible avec XMLHttpRequest");
		return false;
	}
	
	return xhr;
}

hpo_menu_object.prototype.getFile = function(fichier, protocole, asyncr)
{
	if(arguments.length == 3)
		async = asyncr;
	else
		async = true;
		
	// Récupération XMLHttpRequest selon navigateur
	if(!(xhr = this.getXhr()))
		return false;
		
	//********* Pour contrer le cache IE *******/
	d = new Date();
	
	reg = new RegExp("[?]");
	if(!fichier.match(reg))
		fichier = fichier + '?' + d.getTime();
	else
		fichier = fichier + '&' + d.getTime();
	//*******************************************/
		
	xhr.open(protocole, fichier, async);
	
	return xhr;
}

function hpo_menu_preloadImgs(imgs) {
    var doc = document;
    if (doc.images) { 
        if (!doc.hpo_menu_Images)
            doc.hpo_menu_Images = new Array();
        
        var i, j = doc.hpo_menu_Images.length, args = hpo_menu_preloadImgs.arguments;
        
        for(i = 0; i < args.length; i++)
            if (args[i].indexOf("#") != 0){
                doc.hpo_menu_Images[j]=new Image;
                doc.hpo_menu_Images[j++].src = args[i];
            }
    }
}
/*
hpo_menu_object.prototype.display = function (activeObjId, parent, s) {
    if (parent == null)
        return;
    
    var nodes = parent.childNodes;
    
    for(var i = 0; i < nodes.length; i++) {
        if ((nodes[i] != activeObjId) && (nodes[i].nodeType != 3)) {
            if ((nodes[i].style != undefined) && (nodes[i].style.display != undefined))
                nodes[i].style.display = s;
            this.display(activeObjId, nodes[i], s);
        }
    }
} 
*/
/*function extractParams(s) {
    var r = new RegExp("[ ,;-]+", "g");
    
    var s_ = s.substring(s.indexOf("[",0) + 1, s.indexOf("]",0));
    return s_.split(r)
}*/




function preloaderImage(){
    imageObjn = new Image();
    imageObjo = new Image();

    // Démarrer le préchargement
    for(i=1; i<=6; i++){
        imageObjn.src="/fileadmin/site_corporate/templates/skin/default/imgs/menu/bkg_item"+ i +"_n.gif";
        imageObjo.src="/fileadmin/site_corporate/templates/skin/default/imgs/menu/bkg_item"+ i +"_s.gif";
    }
}

