jQuery(function(){
    jQuery(document).pngFix();
    if ( $(".contenido-default").length > 0 ) {
// contenido_default_script();
}
//alert($('#agenda_iframe').contents().find("#nav1").html()); //.css("background-color","#F6F6F6");
});


function contenido_default_script()
{
  
    cajaTitulo=$(".caja_titulo").height();
    galeria=0;
    descripcion=0;
    comentarios=0;

    if ( $(".galeria").length > 0 ) galeria= $(".galeria").innerHeight();
    if ( $("#descripcion").length > 0 )
    {
        //descripcion= $("#descripcion").outerHeight();
        $("#descripcion").css("top", cajaTitulo+galeria+"px");
    }
    if ( $(".comentarios").length > 0 )
    {
        comentarios= $(".comentarios").outerHeight(options)
        $(".comentarios").css("top", cajaTitulo+galeria+descripcion+"px");
    }
    if ( $(".detalles").length > 0 )
    {
        $(".detalles").css("height", cajaTitulo+galeria+descripcion+comentarios+20+"px");
        $(".detalles").css("top", cajaTitulo+galeria+"px");
    }

    alert($("#descripcion").innerHeight());

    $("#contenido").css("height", cajaTitulo+galeria+descripcion+comentarios+20+"px");
}

var timerGaleria=false;
var urlImagenes=new Array();
var tituloImagenes=new Array();
var webroot=false;
var nImagen=-1;

function cambiarImagen(nVista)
{
    //alert(lista[nVista]);
    lista=urlImagenes;
    titulos=tituloImagenes;

    nVista=nVista%lista.length;
    nImagen=nVista;
    $("#galeria").prepend('<img id="imagen_galeria_new" style="" class="recurso" src="'+lista[nVista]+'" alt="'+titulos[nVista]+'"/>');
    //$("#boton-galeria-"+nVista).addClass("bola-vista-previa-seleccionada");
    $("#imagen_galeria_new").hide();
    $("#imagen_galeria").animate({
        "opacity": "toggle"
    },400,callbackcambiarImagen);
    $("#imagen_galeria_new").animate({
        "opacity": "toggle"
    },400);
    for(i=0;i<lista.length;i++)
    {
        if(i==nVista)
        {
            $("#galeria_boton_"+i).attr("src",webroot+"/res/bola-seleccionada.png");
        }
        else
        {
            $("#galeria_boton_"+i).attr("src",webroot+"/res/bola.png");
        }
    }


    if(lista.length>1){
        $(document).stopTime();
        $(document).oneTime(20000,"timer_galeria", function(){
            cambiarImagen(nImagen+1);
        });
    }
    
}

function callbackcambiarImagen()
{
    $("#imagen_galeria").remove();
    $("#imagen_galeria_new").attr("id","imagen_galeria");
}

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;
};

jQuery.fn.toXML = function () {
    var out = '';
    if (this.length > 0) {
        if (typeof XMLSerializer == 'function' ||
            typeof XMLSerializer == 'object')
            {
            var xs = new XMLSerializer();
            this.each(function() {
                out += xs.serializeToString(this);
            });
        } else if (this[0].xml !== undefined) {
            this.each(function() {
                out += this.xml;
            });
        } else {
    // TODO: Manually serialize DOM here,
    // for browsers that support neither
    // of two methods above.
    }
    }
    return out;
};

$.validator.addMethod(
    "nif_nie",
    isNIE,
    "Su NIE, CIF ó NIF no es válido"
    );


function isNIE(a) {
    a=a.toUpperCase();
    var temp=a;
    var cadenadni="TRWAGMYFPDXBNJZSQVHLCKE";

    if (temp!==''){
        //si no tiene un formato valido devuelve error
        if ((!/^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$/.test(temp) && !/^[T]{1}[A-Z0-9]{8}$/.test(temp)) && !/^[0-9]{8}[A-Z]{1}$/.test(temp)){
            return false;
        }
        //comprobacion de NIFs estandar
        if (/^[0-9]{8}[A-Z]{1}$/.test(temp))
        {
            posicion = a.substring(8,0) % 23;
            letra = cadenadni.charAt(posicion);
            var letradni=temp.charAt(8);
            if (letra == letradni)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //algoritmo para comprobacion de codigos tipo CIF
        suma = parseInt(a.charAt(2))+parseInt(a.charAt(4))+parseInt(a.charAt(6));
        for (i = 1; i < 8; i += 2)
        {
            temp1 = 2 * parseInt(a.charAt(i));
            temp1 += '';
            temp1 = temp1.substring(0,1);
            temp2 = 2 * parseInt(a.charAt(i));
            temp2 += '';
            temp2 = temp2.substring(1,2);
            if (temp2 == '')
            {
                temp2 = '0';
            }

            suma += (parseInt(temp1) + parseInt(temp2));
        }
        suma += '';
        n = 10 - parseInt(suma.substring(suma.length-1, suma.length));

        //comprobacion de NIFs especiales (se calculan como CIFs)
        if (/^[KLM]{1}/.test(temp))
        {
            if (a.charAt(8) == String.fromCharCode(64 + n))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //comprobacion de CIFs
        if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(temp))
        {
            temp = n + '';
            if (a.charAt(8) == String.fromCharCode(64 + n) || a.charAt(8) == parseInt(temp.substring(temp.length-1, temp.length)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //comprobacion de NIEs
        //T
        if (/^[T]{1}/.test(temp))
        {
            if (a.charAt(8) == /^[T]{1}[A-Z0-9]{8}$/.test(temp))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //XYZ
        if (/^[XYZ]{1}/.test(temp))
        {
            pos = str_replace(['X', 'Y', 'Z'], ['0','1','2'], temp).substring(0, 8) % 23;
            if (a.charAt(8) == cadenadni.substring(pos, pos + 1))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    return false;
}

function str_replace(search, replace, subject) {
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
    return sa ? s : s[0];
}

function hacerLogout(idioma)
{
    $.post("../x-cliente.php", {
        accion:"logout",
        idiomaPagina:idioma
    },
    function(data){
        if(data.exit_id==1)
        {
            location.href=data.redireccion;
        }
        else
        {

    }

    }, "json");
}
/*
function abrirModal(ht,w,h)
{
    if(ht!="")
        $('#basic-modal-content').html(ht);
    $('#basic-modal-content').modal();
    $('#simplemodal-container').css("width", w)
    $('#simplemodal-container').css("height", h)
}
*/

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();
    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();
}
