
// ############################ Actualidad #####################################

var masPanelOpened = false;
var masStr;

function clickMasPanel(link, ocultarStr) {
    link.blur();
    if (masPanelOpened == true) {
        var effect = new Effect.SlideUp('indexForm:actualidadExtraDiv')
        link.innerHTML = masStr;
    } else {
        var effect = new Effect.SlideDown('indexForm:actualidadExtraDiv')
        masStr = link.innerHTML;
        link.innerHTML = ocultarStr;
    }
    masPanelOpened = !masPanelOpened;
}

// ############################### Menus #######################################

Menu = function(name, target, div, offsetX, offsetY, lapse, hideLapse) {
    if (mybox.getElement(target) == undefined)
        return;
    
    this.target = target;
    this.name = name;
    this.div = div;
    this.open = false;
    this.first = true;
    this.offsetX = offsetX;
    this.offsetY = offsetY;    
    this.lapse = lapse;
    this.hideLapse = hideLapse;
    this.over = false;
    this.divOver = false;
    this.hideInterval = null;
    
    var ctx = this;
    var overFt = function(e) {
        ctx.mouseOver.call(ctx, e);
    }
    mybox.addEvent(mybox.getElement(target), 'mouseover', overFt, true);    

    var outFt = function(e) {
        ctx.mouseOut.call(ctx, e);
    }    
    mybox.addEvent(mybox.getElement(target), 'mouseout', outFt, true);        
}

Menu.prototype = {
            
    mouseOut: function(e) {
        this.over = false;
    },
    
    mouseOver: function(e) {
        if (!this.over && !this.open) {
            this.over = true;
            var ctx = this;
            var ft = function() {
                ctx.show.call(ctx, e);
            };
            setTimeout(ft, this.lapse);
        }
    },
        
    show: function(e) {
        if (!this.over)
            return;
        
        var ctx = this;
        this.open = true;
        
        if (currentMenu != null && currentMenu != this) {
                currentMenu.hide();
        }                            
        currentMenu = this;
        var location = mybox.getLocation(mybox.getElement(this.target));
        var el = mybox.getElement(this.div);
        el.style.top = (location.y + this.offsetY) + 'px';
        el.style.left = (location.x + this.offsetX) + 'px';        
        Effect.Appear(this.div);

        if (this.hideInterval != null) {
            clearInterval(this.hideInterval);
        }
        var hideFt = function(e) {
            ctx.checkDivToHide.call(ctx);
        }    
        this.hideInterval = setInterval(hideFt, this.hideLapse);        
    },
    
    hide : function() {
        if (this.open == true) {
            Effect.Fade(this.div);
            this.open = false;
        }
        if (this.hideInterval != null) {
            clearInterval(this.hideInterval);
        }
        this.over = false;
    },   
    
    checkDivToHide : function() {
        var el = mybox.getElement(this.div);
        try {
            if (mouseLocation == null) {
            } else {
                if (!mybox.isOver(mouseLocation, el)) {
                    el = mybox.getElement(this.target);
                    if (!mybox.isOver(mouseLocation, el)) {
                        this.hide();
                    } 
                } 
            }
        } catch (e) {
            alert(e.description);
        }
    }
    
}

var currentMenu = null;
var mouseLocation = null;

function mouseFt(e) {        
    try {
        mouseLocation = mybox.getMouseLocation(e);
    } catch (ex) {
    }
}

mybox.addEvent(document, 'mousemove', mouseFt, true);


// ############################### Login #######################################

var loginDragWindow = null;

function login() {
    var div = mybox.getElement("login");
    div.style.display = 'block';
    mybox.centerInWindow(div);
    if (loginDragWindow == null)
        loginDragWindow = new mybox.DragElement('login');        
}

function cerrarLogin() {
    var div = mybox.getElement("login");
    div.style.display = 'none';
}

// ############################ Buscadores #####################################

function clickBuscar(e, buttonName) {
    try {
        var keycode;
        if (window.event) 
            keycode = window.event.keyCode;
        else if (e) 
            keycode = e.which;
        else 
            return true;

        if (keycode == 13) {
            var el = mybox.getElement(buttonName);
            if (el.click != undefined)
                el.click();
            else
                el.onclick();
            return false;
        } else
            return true;
   } catch (err) {
        alert(err);
   }
}

function cancelIntro(e) {
    var keycode;
    try {
        if (window.event) 
            keycode = window.event.keyCode;
        else if (e) 
            keycode = e.which;
        else 
            return true;

        if (keycode == 13) {
            if (e.stopPropagation)
                e.stopPropagation();
            else
                e.cancelBubble = true;

            if (e.preventDefault)
                e.preventDefault();
            else
                e.returnValue = false;
        }
    } catch (err) {
        alert(err);
    } 
}

// ############################ Aviso Legal ####################################

function mostrarAvisoLegal() {
    var iframe = mybox.getElement('avisoLegalIframe');
    iframe.src = path + '/faces/jsf/avisoLegal.jsp';
    var div = mybox.getElement('avisoLegal');
    div.style.display = 'block';
    mybox.centerInWindow(div);        
}

function cerrarAvisoLegal() {
    var div = mybox.getElement('avisoLegal');
    div.style.display = 'none';
}

// ############################### Blink ######################################

Blink = function(name, class1, class2, max, speed) {
    this.name = name;
    this.class1 = class1;
    this.class2 = class2;
    this.contador = 0;
    this.max = max;    
    this.speed = speed;
}

Blink.prototype = {
    blink: function(e) {
        if (this.contador < this.max) {
            var className = this.class1;
            if (this.contador % 2 == 0) {
                className = this.class2;
            }
            mybox.getElement(this.name).className = className;
            this.contador = this.contador + 1;
            
            var ctx = this;
            var ft = function() {
                ctx.blink.call(ctx, e);
            };
            setTimeout(ft, this.speed);
        }
    }
}

// ############################### Pager ######################################

Pager = function(idsPaginas, textPager, currentPage) {
    this.idsPaginas = idsPaginas;
    this.textPager = textPager;
    this.currentPage = currentPage;
    this.pageCount = idsPaginas.length;
}

Pager.prototype.getCurrentPage = function() {
    return this.currentPage;
}

Pager.prototype.getMaxPage = function() {
    return this.pageCount;
}

Pager.prototype.next = function() {
    var page = this.getCurrentPage();
    if (page < this.pageCount) {
        var auxId = mybox.getElement(this.idsPaginas[page - 1]);
        var id = mybox.getElement(this.idsPaginas[page]);
        if (id != null && auxId != null) {
            auxId.style.display = "none";
            id.style.display = "block";
            this.currentPage = this.currentPage + 1;
            this.actualizarPage();
        } 
    }
}

Pager.prototype.back = function() {
    var page = this.getCurrentPage();
    if (page > 1) {
        var auxId = mybox.getElement(this.idsPaginas[page - 1]);
        var id = mybox.getElement(this.idsPaginas[page - 2]);
        if (id != null && auxId != null) {
            auxId.style.display = "none";
            id.style.display = "block";
            this.currentPage = this.currentPage - 1;
            this.actualizarPage();
        }
    }
}

Pager.prototype.activeCurrentPage = function() {
    var page = this.getCurrentPage();
    var id = mybox.getElement(this.idsPaginas[page-1]);
    if (id != null) {
        id.style.display = "block";
        this.actualizarPage();
    }
}

Pager.prototype.actualizarPage = function() {
    var texto = mybox.getElement(this.textPager);
    if (texto != null) {
        texto.innerHTML = this.getCurrentPage() + " / " + this.pageCount;
    }
}

// ############################ Historico #####################################


function mostrarPopupDestacado(mouseEvent, component) {
    var div = mybox.findJSFComponent('popupDst', component);
    var divMarcador = mybox.findJSFComponent('popupMarcador', component);
    var divShadow = mybox.findJSFComponent('popupShadow', component);
    if (div.style.display != 'block') {
        var p = mybox.getMouseLocation(mouseEvent);
        divMarcador.style.left = (p.x - 30) + 'px';
        divMarcador.style.top = (p.y + 13) + 'px';
        div.style.top = (p.y + 25) + 'px';
        var x = p.x;
        if (x > 600) {
            x = x - 200;
        } else {
            x = x - 50;
        }
        
        div.style.left = x + 'px';
        div.style.display = 'block';
        divMarcador.style.display = 'block';
        
        divShadow.style.height = div.style.height;
        
        mybox.cancelEvent(mouseEvent);
    }
}

function ocultarPopupDestacado(mouseEvent, component) {
    var div = mybox.findJSFComponent('popupDst', component);
    var m = mybox.findJSFComponent('popupMarcador', component);
    if (div.style.display != 'none') {
        div.style.display = 'none';
        m.style.display = 'none';
    }
}