var urlEenviarLetra = "";

jQuery(document).ready(function(){ 
    jQuery('#cambioMoneda').live('click', function() {
        jQuery.ajax({
            url: jQuery("#moneda-actualizacion").attr("action"),
            type: "POST",
            data: jQuery("#moneda-actualizacion").serialize(),
            cache: false,
            success: function(html){
                jQuery("#ctr-selector-moneda").html(html);
            },
            beforeSend: function(){
                jQuery("#cargador").show();
            },
            complete: function(){
                jQuery("#cargador").hide();
            }
        });
        return false;
    });    
    setupEventos();
    setupEventosTematica();
});

function abrirVentantanaMonedas() {
    jQuery("#ventana-moneda").show();
}

function cerrarVentantanaMonedas() {
    jQuery("#ventana-moneda").hide();
}

function selccionarMoneda(monedaId) {
    jQuery(".precios div:not(.moneda-"+monedaId+")").hide();
    jQuery(".moneda-"+monedaId).show();
}

function actualizarTotal(texto) {
    jQuery("#total-hasta-ahora").html(texto);
}

 function setupEventos() {
    jQuery(".opcion").hover(
        function () {
            jQuery(this).find(".tooltip").show();
        },
        function () {
            jQuery(this).find(".tooltip").hide();
        }
    );

    jQuery(".opcion").bind('mousemove',function(e){
        var offset = jQuery(this).offset();
        var tooltip = jQuery(this).find(".tooltip");
        var alto = jQuery(this).innerHeight();

        tooltip.css({
            left: (e.pageX - offset.left - 20) + 'px',
            bottom: (10 + alto  - e.pageY + offset.top) + 'px'
        })
    });
}


/*
 * Temática
 */

function setupEventosTematica() {
     jQuery("#tabs").tabs({
        disabled: [1, 2, 3, 4, 5]
    });

     jQuery("#dialogo-tematica").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });

    jQuery(".tematica-opcion input").click(function () {
        jQuery("#tabs").tabs('option', 'disabled', [1, 2, 3, 4, 5]);
    })

    jQuery("#EnviarTematica").click(function(){
        var res = jQuery(".tematica-opcion input:radio[name^=data]:checked").val();
        if(res != undefined) {
            jQuery.ajax({
                url: jQuery("#tematica").attr("action"),
                type: "POST",
                data: jQuery("#tematica").serialize(),
                cache: false,
                success: function(html){
                    jQuery("#ctd-estilos").html(html);
                    jQuery("#tabs").tabs('enable', 1);
                    jQuery("#tabs").tabs('select', 1);
                },
                beforeSend: function(){
                    jQuery("#cargador").show();
                },
                complete: function(){
                    jQuery("#cargador").hide();
                }
            });
        }
        else {
            $("#dialogo-tematica").dialog( 'open' );
        }
        return false;
    });
}

/*
 * Estilos
 */

function setupEventosEstilos(destinoEnviar) {
    $("#tabs").tabs('option', 'disabled', [2, 3, 4, 5]);
    $(".estilo-opcion input, .pista-opcion input").click(function () {
        jQuery.ajax({
            url: jQuery("#estilo").attr("action"),
            type: "POST",
            data: jQuery("#estilo").serialize(),
            cache: false,
            success: function(html){
                jQuery("#ctd-estilos").html(html);
            },
            beforeSend: function(){
                jQuery("#cargador").show();
            },
            complete: function(){
                jQuery("#cargador").hide();
            }
        });
    })

    jQuery("#EnviarEstilo").click(function(){

        jQuery.ajax({
            url: destinoEnviar,
            type: "POST",
            data: jQuery("#estilo").serialize(),
            cache: false,
            success: function(html){
                jQuery("#ctd-letras").html(html);
                jQuery("#tabs").tabs('enable', 2);
                jQuery("#tabs").tabs('select', 2);
            },
            beforeSend: function(){
                document.getElementById("player-pista").SetVariable("player:jsStop", "");

                jQuery("#cargador").show();
            },
            complete: function(){
                jQuery("#cargador").hide();
            }
        });
        return false;
    });
}

function setupPlayerPistas(audioMp3) {
    var fPista = {
        mp3: audioMp3,
        autoplay: '0',
        loop: '1',
        bgcolor1: '888888',
        bgcolor2: '666666',
        buttonovercolor: 'E5E5E5',
        slidercolor1: 'ffffff',
        slidercolor2: 'cccccc',
        sliderovercolor: 'E5E5E5',
        textcolor: '0',
        showvolume: '1',
        showslider: '1',
        buttoncolor: 'ffffff',
        width: '176'

    };
    var pPista = {
        quality: "high",
        scale: "noscale",
        allowFullScreen: "false",
        salign: "tl",
        menu: "false",
        base: dir_raiz+'swf',
        wmode: 'transparent'
    };

    swfobject.embedSWF(dir_raiz+'swf/player_mp3_maxi.swf', "player-pista", "176", "20", "9.0.124", dir_raiz+'swf/playerProductInstall.swf', fPista, pPista);
}

/*
 * Letras
 */

function setupEventosLetras() {
    $("#tabs").tabs('option', 'disabled', [3, 4, 5]);
    $(".letra-opcion input").click(function () {
        jQuery.ajax({
            url: jQuery("#letra").attr("action"),
            type: "POST",
            data: jQuery("#letra").serialize(),
            cache: false,
            success: function(html){
                jQuery("#ctd-letras").html(html);
            },
            beforeSend: function(){
                jQuery("#cargador").show();
            },
            complete: function(){
                jQuery("#cargador").hide();
            }
        });
    });

    jQuery("#EnviarLetraPreDis").click(function(){
        var valido = true;
        jQuery(".letra-campos input").each(function(){
            if($(this).val() == '') {
                valido = false;
            }
        });
        if(valido) {
            enviarLetra();
        }
        else {
            $("#dialogo-letra-predis").dialog( 'open' );
        }

        return false;
    });

    jQuery("#EnviarLetraPers").click(function(){
        var valido = true;
        if(jQuery('#PedidoLetraPersonal').val() == '') {
            valido = false;
        }

        if(valido) {
           enviarLetra();
        }
        else {
            $("#dialogo-letra-person").dialog( 'open' );
        }

        return false;
    });

    $("#dialogo-letra-predis, #dialogo-letra-person").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });
}

function enviarLetra() {
    jQuery.ajax({
        url: urlEenviarLetra,
        type: "POST",
        data: jQuery("#letra").serialize(),
        cache: false,
        success: function(html){
            jQuery("#ctd-cantantes").html(html);
            jQuery("#tabs").tabs('enable', 3);
            jQuery("#tabs").tabs('select', 3);
        },
        beforeSend: function(){
            jQuery("#cargador").show();
        },
        complete: function(){
            jQuery("#cargador").hide();
        }
    });
}

/*
 * Cantantes
 */

function setupEventosCantantes(destinoEnviar) {
    $("#tabs").tabs('option', 'disabled', [4, 5]);

    jQuery(".cantante-opcion input, #PedidoCoros").click(function () {
        $("#tabs").tabs('option', 'disabled', [4, 5]);
    })

    jQuery("#EnviarCantante").click(function(){
        jQuery.ajax({
            url: destinoEnviar,
            type: "POST",
            data: jQuery("#cantante").serialize(),
            cache: false,
            success: function(html){
                jQuery("#ctd-tapas").html(html);
                jQuery("#tabs").tabs('enable', 4);
                jQuery("#tabs").tabs('select', 4);
            },
            beforeSend: function(){
                document.getElementById("player-cantante").SetVariable("player:jsStop", "");
                jQuery("#cargador").show();
            },
            complete: function(){
                jQuery("#cargador").hide();
            }
        });
        return false;
    });

    $(".cantante-opcion input").click(function () {
        jQuery.ajax({
            url: jQuery("#cantante").attr("action"),
            type: "POST",
            data: jQuery("#cantante").serialize(),
            cache: false,
            success: function(html){
                jQuery("#ctd-cantantes").html(html);
            },
            beforeSend: function(){
                jQuery("#cargador").show();
            },
            complete: function(){
                jQuery("#cargador").hide();
            }
        });
    })
}

function setupPlayerCantantes(audioMp3) {
    var fCantante = {
        mp3: audioMp3,
        autoplay: '0',
        loop: '1',
        bgcolor1: '888888',
        bgcolor2: '666666',
        buttonovercolor: 'E5E5E5',
        slidercolor1: 'ffffff',
        slidercolor2: 'cccccc',
        sliderovercolor: 'E5E5E5',
        textcolor: '0',
        showvolume: '1',
        showslider: '1',
        buttoncolor: 'ffffff',
        width: '176'

    };
    var pCantante = {
        quality: "high",
        scale: "noscale",
        allowFullScreen: "false",
        salign: "tl",
        menu: "false",
        base: dir_raiz+'swf',
        wmode: 'transparent'
    };

    swfobject.embedSWF(dir_raiz+'swf/player_mp3_maxi.swf', "player-cantante", "176", "20", "9.0.124", dir_raiz+'swf/playerProductInstall.swf', fCantante, pCantante);
}

/*
 * Tapas
 */

function setupEventosTapas() {
    $("#tabs").tabs('option', 'disabled', [5]);

    jQuery(".tapa-opcion input").click(function () {
        $("#tabs").tabs('option', 'disabled', [5]);
    })

    jQuery("#dialogo-tapa").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });

    jQuery("#EnviarTapa").click(function(){
        var res = jQuery(".tapa-opcion input:radio[name^=data]:checked").val();
        if(res != undefined) {
            jQuery.ajax({
                url: jQuery("#tapa").attr("action"),
                type: "POST",
                data: jQuery("#tapa").serialize(),
                cache: false,
                success: function(html){
                    jQuery("#ctd-checkout").html(html);
                    jQuery("#tabs").tabs('enable', 5);
                    jQuery("#tabs").tabs('select', 5);
                },
                beforeSend: function(){
                    jQuery("#cargador").show();
                },
                complete: function(){
                    jQuery("#cargador").hide();
                }
            });
        }
        else {
            $("#dialogo-tapa").dialog( 'open' );
        }

        return false;
    });
}

/*
 * Checkout
 */

function setupEventosCktUser() {
    jQuery("#dialogo-aceptacion").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });

    jQuery("#PedidoMediosdepagoId").change(function () {
        if(jQuery("#PedidoMediosdepagoId").val() != '0') {
            jQuery.ajax({
                url: jQuery("#mediodepago").attr("action"),
                type: "POST",
                data: jQuery("#mediodepago").serialize(),
                cache: false,
                success: function(html){
                    jQuery("#ctd-checkout").html(html);
                },
                beforeSend: function(){
                    jQuery("#cargador").show();
                },
                complete: function(){
                    jQuery("#cargador").hide();
                }
            });
        }

    });

    jQuery("#EnviarAceptacion").click(function(){
        var valorAcep = jQuery('input#tyc:checkbox:checked').val();        
        
        if(valorAcep != undefined) {
            jQuery.ajax({
                url: jQuery("#aceptacion").attr("action"),
                type: "POST",
                data: jQuery("#aceptacion").serialize(),
                cache: false,
                success: function(html){
                    jQuery("#ctd-checkout").html(html);
                },
                beforeSend: function(){
                    jQuery("#cargador").show();
                },
                complete: function(){
                    jQuery("#cargador").hide();
                }
            });
        }
        else {
            $("#dialogo-aceptacion").dialog( 'open' );
        }
        return false;
    });
}

function setupEventosCkt(destinoEnviar) {
    jQuery("#ir-registro a").click(function(){
        jQuery.ajax({
            url: destinoEnviar,
            type: "GET",
            cache: false,
            success: function(html){
                jQuery("#ctd-checkout").html(html);
            },
            beforeSend: function(){
                jQuery("#cargador").show();
            },
            complete: function(){
                jQuery("#cargador").hide();
            }
        });

        return false;
    });

    jQuery("#login").click(function(){
        jQuery.ajax({
            url: jQuery("#f-login").attr("action"),
            type: "POST",
            data: jQuery("#f-login").serialize(),
            cache: false,
            success: function(html){
                jQuery("#ctd-checkout").html(html);
            },
            beforeSend: function(){
                jQuery("#cargador").show();
            },
            complete: function(){
                jQuery("#cargador").hide();
            }
        });

        return false;
    });
    jQuery("#dialogo-recordar").dialog({
        bgiframe: true,
        autoOpen: false,
	width: 385,
	height: 145,
        modal: true,
        buttons: {
            'Enviar': function() {
		var destino = jQuery("#formRecordar").attr('action');
                var datos = jQuery("#formRecordar").serialize();

                jQuery.ajax({
                    url: destino,
                    type: "POST",
                    data: datos,
                    cache: false,
                    success: function(html){
                        jQuery("#ctd-recordar").html(html);
                    },
                    beforeSend: function(){
                        jQuery("#cargador").show();
                    },
                    complete: function(){
                        jQuery("#cargador").hide();
                    }
                });
	    },
	    'Cancelar': function() {
		$(this).dialog('close');
	    }
        }
    });
}

function recordarPass() {
    $("#dialogo-recordar").dialog( 'open' );
}
