Texto = {};
if (/^\/en/.test(document.location.pathname)) {
    Texto['error_mail_vacio'] = "Fill your email...";
    Texto['error_mail_dif'] = "The email address doesn't match";
    Texto['informar'] = 'Notify payment';
    Texto['comprar'] = 'Buy';
    Texto['datos'] = 'Information';
} else {
    Texto['error_mail_vacio'] = "Completá tu mail...";
    Texto['error_mail_dif'] = "El mail no coincide";
    Texto['informar'] = 'Informar pago';
    Texto['comprar'] = 'Comprar';
    Texto['datos'] = 'Datos';
}

Modos = {};

Modos.ptr = {};

Modos.by_id = function(id) {
    for (var i in MODOS) {
        if (MODOS[i].id == id) {
	    return MODOS[i];
	};
    };
};
Compra = {};
Compra.data = {};
Compra.dialogo = function(titulo, url) {
    Compra.data.modo = Modos.ptr.id;
    cc = $('#confirmarcompra');
    cc.autodialog(titulo).loading().load("rapid/" + url, Compra.data, function() {
        $('#confirmarcompra .cancel').click(function() {
            cc.dialog("destroy");
        });
        $("#confirmarcompra button").button();
        cc.submit(Compra.enviar);
    });
    cc.attr({target: '_blank', method: 'post', action: 'rapid/envio'});
};

Compra.click = function(e) {
    Compra.data.rapids = $("#slider").slider("value");
    Compra.dialogo(isbanco(Modos.ptr.id) ? Texto['informar'] : Texto['comprar'], "form_compra");
};

Compra.datos = function() {
    Compra.data.rapids = 0;
    Compra.dialogo(Texto['datos'], "datos");
};

Compra.enviar = function() {
    var cc = $("#confirmarcompra");
    
    //verificar
    var mail = $("#mail1").val();
    if (!(mail.length > 0 && /@/.test(mail))) {
        alert(Texto['error_mail_vacio']);
        return false;
    }
    if ($('#mail1').val() != $('#mail2').val()) {
        alert(Texto['error_mail_dif']);
        return false;
    }
    
    if (Modos.ptr.tipo == 'datos') {
        jQuery.extend(Compra.data, cc.getformdata())
        cc.loading().load('/rapid/envio', Compra.data, undefined, function() {
            $('#confirmarcompra .cancel').click(function() {cc.dialog("destroy")})
        })
        return false;
    } else {
        for (key in Compra.data) {
            cc.append($("<input type='hidden'>").attr({name: key, value: Compra.data[key]}))
        }
        setTimeout(function(){
            $('#confirmarcompra').loading().load("rapid/gracias", undefined, function() {
                $('#confirmarcompra .cancel').click(function() {cc.dialog("destroy")})
            })
        }, 100);
        return true
    }
    Compra.data = {}
}

function modo(x) {
    Modos.ptr = Modos.by_id(x)
    $('#nombremodopago').text(Modos.ptr.nombre)
    $('#imagenmodopago').attr('src', "/imagenes/logos/" + Modos.ptr.codename + ".jpg");
    $("#tabs .selected").removeClass("selected")
    $("#tab_"+x).addClass("selected").find("a").blur()
    $('#mostrardatos').css('display', isbanco(x) ? 'inline' : 'none');
    $('#comprar').button("option", "label", Texto[isbanco(x) ? 'informar' : 'comprar'].toUpperCase());
    try{
        onslide($("#slider").slider("value"))
    }
    catch(e) {}
    //$('table.precioshoriz div.precio button').text(Texto[isbanco(x) ? 'boton_informar' : 'boton_comprar'])

    /*$('table.precioshoriz div.precio').each(function() {
        name = $(this).attr("name");
        valor = $(this).children(".valor");
        valor.children('.moneda').text(Modos.ptr.moneda);
        pesos = Modos.ptr.precios[name]
        valor.children('.pesos').text(Math.floor(pesos));
        l = (Math.floor(pesos) + "").length
        if (l > 4) {
            valor.css("font-size", "28px");
            valor.children('.moneda').css("font-size", "13px");
            valor.children('.cent').css("font-size", "13px");
            valor.children('.pesos').css("font-size", "0.8em");
        } else {
            valor.css("font-size", "");
            valor.children().css("font-size", "")
        }
        //valor.children('.cent').text(Math.floor((pesos - Math.floor(pesos)) * 100));
    })*/
}

function isbanco(x) {
    return (x ? Modos.by_id(x) : Modos.ptr).tipo == 'datos';
}

function onslide(value) {
    $("#rapids").val(value);
    precio = " " + Math.round(Modos.ptr.precios.rapids100 * value) / 100;
    if (precio.indexOf(".") == -1) precio += ".00";
    if (precio.match(/\.[0-9]$/)) precio += "0";
    $("#plata").text(Modos.ptr.moneda + " " + precio);
}

$(function() {
    $("#comprar").button().click(Compra.click);
    $("#mostrardatos").button().click(Compra.datos);
    modo(3);
    var slider = $("#slider").slider({
        min: 100, max: 10000,
        step: 100,
        value: 500,
        slide: function(event, ui) {onslide(ui.value)}
    })
    $("#rapids").keyup(function() { /* MUY MOLESTO */
        var intval = parseInt($(this).val());
        if (!isNaN(intval)) {
            slider.slider("value", intval);
        }
        var curval = slider.slider("value");
        if ($(this).val() != curval) {
            $(this).val(curval);
        }
    })
    onslide(slider.slider("value"));
    $("#toggle_ventajas").click(function() {$("#ventajas").slideToggle();});
    $("#toggle_promocion").click(function() {$("#promocion").slideToggle();});
});

function formcargado() {
    $('#tipocompra input').click(function() {
        $('#idrapid').css("display", ($("#tipocompra input:checked").val() == 'extender') ? "block" : "none");
    });
}

jQuery.fn.loading = function() {
    return this.each(function(){
        this.innerHTML = "<img src='/imagenes/loading.gif' alt='' id='loading' />"
    });
}
jQuery.fn.autodialog = function(titulo) {
    this.dialog({
        title: titulo, resizable: false,
        width: 500, height: 450 /*,
        modal: true,
        overlay: {opacity: 0.5, background: "black", display: "none"}
        */
    }
    ).dialog("open").show().css({overflow: 'auto'})
    //$(".ui-dialog-overlay").fadeIn("slow")
    return this;
}

jQuery.fn.getformdata = function(filter) {
    var postdata = {};
    this.find(filter || "input[type!='submit'], textarea").each(function() {
        postdata[$(this).attr("name")] = $(this).val();
    })
    return postdata;
}


