//Activar efecto de menu secundario
$("#mnuSecundario").delegate('a','mouseover mouseleave', function(e) {
    if (e.type == 'mouseover') {
      //Se guarda el color inicial
        if ($(this).attr("rel") == "") {
            $(this).attr("rel", $(this).css("color"));
        }

        //Animacion
        $(this).stop()
        .css({
            "color" : $(this).attr("rel")
            })
        .animate({
            "color": "#aa5500"
            ,"padding-left" : 12

        }, 200);
        //Añadir clase que contiene el background
        $(this).parent().addClass("hoverItem");
    }
    else {
      //Remover clase que contiene el background
        $(this).parent().removeClass("hoverItem");

        //Animacion
        $(this).stop()
        .animate({
            "color": $(this).attr("rel")
            ,"padding-left" : 3
        }, 180);
    }
});

function aparecerPanel(idPanel, retraso){
    $("#" + idPanel + " div").each(function(){
        $(this).fadeIn(retraso);
    });
}

function desvanecerPanel(idPanel, retraso){
    $("#" + idPanel + " div").each(function(){
        $(this).fadeOut(retraso);
    });
}

//Para cambiar de panel sin cargar de nuevo la pagina
function panelVisibleSinRecarga(idPanel){
    var retraso = 500;
    if( $(".panelAlternadorActivo").attr("id") == idPanel)
        return;

    $(".panelAlternadorActivo").removeClass("panelAlternadorActivo").addClass("panelAlternador");
    $("#"+idPanel).removeClass("panelAlternador").addClass("panelAlternadorActivo");

    $(".panelAlternador").fadeOut(retraso, function(){
        setTimeout(function(){
            $(".panelAlternador h1:first").show();
            $("#headerMaestro").stop().css({"color":"white"});
            $("#headerMaestro").html($(".panelAlternadorActivo h1:first").html());
            $(".panelAlternadorActivo h1:first").hide();
            $(".panelAlternadorActivo").fadeIn(retraso, function(){
                $("#headerMaestro").animate({"color" : "yellow"}, 1200);
            });
        }, 500);
    });
}

//Obtiene la pagina, definida en urlPagina, y la muestra en el objetivo
function mostrarPaginaAjax(urlPagina, objetivo){
        $.get(urlPagina, function(data) {
            var retraso = 500;
            var objDiv = '#' + objetivo;
            $(objDiv).fadeOut(retraso);
            setTimeout(function(){
              $(objDiv).html(data);
              $("#headerMaestro").html($(objDiv + " h1:first").html());
              $(objDiv + " h1:first").remove();
              $("#headerMaestro").stop().css({"color":"white"});
              $("#headerMaestro").animate({"color" : "yellow"}, 1200);
              $("button, input:submit, input:reset", objDiv ).button();
              $(objDiv).fadeIn(retraso);
            }, retraso);
        });
    }

//Para activar el resaltado de lineas en las tablas
function activarHighlightFilas(idTabla){

    $("#" + idTabla + " tbody").delegate('td','mouseover mouseleave', function(e) {
        if (e.type == 'mouseover') {
          $(this).parent().addClass("hoverCelda");
        }
        else {
          $(this).parent().removeClass("hoverCelda");
        }
    });
}

//Limitar caracteres textarea
function limitChars(textid, limit, infodiv)
{
    var text = $('#'+textid).val();
    var textlength = text.length;
    if(textlength > limit)
    {
        $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
        $('#'+textid).val(text.substr(0,limit));
        return false;
    }
    else
    {
        $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
        return true;
    }
}
