$(document).ready(function(){

    calculoPosicionamientoCapas();

    $('.jqmWindow').jqm({
        toTop: true
    });



    $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav

    $("ul.topnav li span").click(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function() {
            }, function(){
                $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
            });

    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        $(this).addClass("menuhover"); //On hover over, add class "subhover"
    }, function(){	//On Hover Out
        $(this).removeClass("menuhover"); //On hover out, remove class "subhover"
    });


});

function calculoPosicionamientoCapas()
{
    $("#contenido").css("height",null);
    if($("#central").length>0)
    {
        idCentral="central";
        min=400;
    }
    else  if($("#central_home").length>0)
    {
        idCentral="central_home";
        min=0;
    }

    h2=$("#contenido").height();

   // alert(" h2:"+h2);

    if(h2<min)
    {
        h2=min;
        $("#contenido").height(h2);
    }



    h1=$("#lat_iz").height();

    if(h2>h1)
        altura=h2;
    else if(h1>h2)
        altura=h1;

    $("#"+idCentral).height(altura);

    //alert("document: "+ $(document).height());
    hw=$(window).height();
    hb1=$("#body_fila1").height();
    hb2=$("#body_fila2").height();
    if(hb1+hb2<hw)
    {
        $("#body_fila1").height(hb1+(hw-hb1-hb2));
    }
}

function mostrarMensaje(m,t)
{
    $("#mensaje_box").text(m);
    setTimeout('$("#mensaje_box").text("");',3000);
}











function abrirModal(tipo,ht,w,h)
{
    ventana=$("#ventana-"+tipo+"-jqmodal");
    contenido_ventana=$("#ventana-"+tipo+"-jqmodal-contenido");
    if(ht!="")
        contenido_ventana.html(ht);
    $("#ventana-"+tipo+"-jqmodal-contenido > label").css("display","block");
    $("#ventana-"+tipo+"-jqmodal-contenido").show();
    if(ht!=null)ventana.jqmShow();
    ventana.css("width", w);
    ventana.css("height", "auto");
    ventana.css("margin-left", -w/2);

}

function abrirModalAlerta(ht)
{
    abrirModal("alerta",ht,350,100);
}

function abrirModalInformacion(ht)
{
    abrirModal("info",ht,350,100);
}

function abrirModalValidacionFormulario(ht)
{
    abrirModal("alerta",ht,350,100);
}

function abrirVentana(url)
{
    window.open(url,url,"");
}

function trim(str)
{
    str = str.replace(/^\s*|\s*$/g,"");
    return str;
}

function ejecutarCerrar()
{
    close();
}


function mostrarValor(sufijo)
{
    capa="#valores-"+sufijo;
    enlace="#valores-titulo-"+sufijo;
    if(!$(capa).hasClass("item-valor-visible"))
    {
        vid= $(".item-valor-visible").attr("id");
        eid= $(".enlace-valores-seleccionado").attr("id");
        //alert(eid);
        if(eid!=null)
            $("#"+eid).removeClass("enlace-valores-seleccionado");
        $("#"+vid).fadeOut(80);
        $(capa).fadeIn(100, function() {
            calculoPosicionamientoCapas();
        });
        $(enlace).addClass("enlace-valores-seleccionado");
        $(capa).addClass("item-valor-visible");
        
        $("#"+vid).addClass("item-valor");
        $("#"+vid).removeClass("item-valor-visible");


            
    }
}


function mostrarSubcontenido(sufijo)
{
    capa="#subcontenido-"+sufijo;
    enlace="#subcontenido-titulo-"+sufijo;
    if(!$(capa).hasClass("item-subcontenido-visible"))
    {
        vid= $(".item-subcontenido-visible").attr("id");
        eid= $(".enlace-subcontenido-seleccionado").attr("id");
        //alert(eid);
        if(eid!=null)
            $("#"+eid).removeClass("enlace-subcontenido-seleccionado");
        $("#"+vid).fadeOut(80);
        $(capa).fadeIn(100, function() {
            calculoPosicionamientoCapas();
        });
        $(enlace).addClass("enlace-subcontenido-seleccionado");
        $(capa).addClass("item-subcontenido-visible");
        
        $("#"+vid).addClass("item-subcontenido");
        $("#"+vid).removeClass("item-subcontenido-visible");


            
    }
}



/*******************************************/

jQuery.fn.formToArray = function(semantic){
    var a = [];
    var q = semantic ? ':input' : 'input,textarea,select,button';

    jQuery(q, this).each(function() {
        var n = this.name;
        var t = this.type;
        //alert(n +" "+t);
        if ( !n || this.disabled || t == 'reset' ||
            (t == 'checkbox' || t == 'radio') && !this.checked ||
            (t == 'submit' || t == 'image' || t == 'button') && this.form && this.form.clk != this ||
            this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
            return;

        if (t == 'image' && this.form.clk_x != undefined)
            return a.push(
            {
                name: n+'_x',
                value: this.form.clk_x
            },

            {
                name: n+'_y',
                value: this.form.clk_y
            }
            );
        if (t == 'select-multiple') {
            jQuery('option:selected', this).each( function() {
                a.push({
                    name: n,
                    value: this.value
                });
            });
            return;
        }
        a.push({
            name: n,
            value: this.value
        });
    });
    return a;
};
