inter = setInterval("timer()",17000);
control = 0;
function timer(){
    window.control++;
    if (window.control>3){
        window.control=1
    }
    eject(window.control);
}
function eject(control){
    switch (control){
        case 1:
            onClickInClass.firstac();
            break;
        case 2:
            onClickInClass.secondac();
            break;
        case 3:
            onClickInClass.lastac();
            break;
    }
}
var noEvent = {
    move : false,
    alerta:true,
    control : 1,
    the_title : null,
    x : 0,
    y : 0,
    ahora : function(){
        $('.botoncor').removeClass('active');
        $('.botonlar').removeClass('active');
        $('#ahora').addClass('active');
        var now = new Date();
        var hour=(now.getHours() * 60) * 4;
        $('ul#boxgrilla , #reloj, #boxprogramas, .horas' ).animate({
            scrollLeft:  hour+'px'
            }, 500 );
    }
}
//funciones que se basan en si la clase css existe en la estructura html
var onClickInClass = {
    regiback : function(){
        $('#warpregi').animate({
            scrollLeft: '-=240px'
        }, 500 );
    },
    regiforw : function(){/*grilla de programación*/
        $('#warpregi').animate({
            scrollLeft: '+=240px'
        }, 500 );
    },
    llamavlight : function(that){
        var num = that.id.split('_');
        $.get("/index.php",{
            obt:'video',
            embed:num[1],
            post:num[2]
            }, function(data){
            $('#content').before(data);
            $('.colornegro').css('height',$(document).height())
            $('.wblack').css('top',$(window).scrollTop())
            $("#vlight").show();
            $('.colornegro, #cerrarv').bind("click", function(){
                $("#vlight").remove();
            })
        }
        ,'html');
        return false;
    },
    llamavsingle : function(that){
        var num = that.id.split('_');
        $.get("/index.php",{
            obt:'videosingle',
            embed:num[1],
            post:num[2]
            }, function(data){
            $('#content').before(data);
            $('.colornegro').css('height',$(document).height())
            $('.wblack').css('top',$(window).scrollTop())
            $("#vlight").show();
            $('.colornegro, #cerrarv').bind("click", function(){
                $("#vlight").remove();
            })
        }
        ,'html');
        return false;
    },
    editar_reco : function(that){
        onClickInClass.recordatorio(that);
        $('#horario_recordatorio').css("left","250px").css("top","75px");
    },
    eliminar_reco : function(that){

        var id = that.id.split("_");
        $.get("/index.php",{
            obt:'delete',
            uid:id[2],
            pid:id[1]
            }, function(data){
            $(that).parent().parent().remove();
        },'json');
    },
    recordatorio : function(that){
        $.get("/index.php",{
            obt:'uid'
        }, function(data){
            if(!data.login){
                noEvent.alerta=false;
            }
            else{
                $(that).parent().parent().parent().append("<div class='helpdec centerleft  rounded'><h2>Necesitar registrarte</h2><p>Para programar recordatorios necesitas estar <a href='/registro/' title='Registrate en nuestra comunidad'>registrado</a>.</p><a class='cerrar' href='#' title='Cerrar ayuda'>[cerrar]</a></div>");
                $('.cerrar').live("click",function(e){
                    onClickInClass.cerrar(this); return false;
                });
                noEvent.alerta=true;
            }
        },'json');
        if(noEvent.alerta==false){
            var std = $(that).parent().parent().attr('class').split('_');
            $.get("/index.php",{
                obt:'fyd',
                hora: std[0],
                dia:std[1]
                }, function(data){
                $('#dia_cron').text(data.dia);
                $('#hora_cron').text(data.hora);
            },'json');
            var hyd = $(that).parent().parent().attr('class');
            canal = $(that).parent().prev().prev().prev().attr('id');
            if (that.id) $("#programa_id").val(that.id);
            if ($('.programa').text()!="") $("#programa").val($('.programa').text());
            $("#hyd").val(hyd);
            $("#canal_cron").val(canal);
            $('#horario_recordatorio').show();
        }
    },
    delfav : function(that){
        chn = that.id;
        ui= $("#"+chn).attr("class").split("uid_");
        $.get('/index.php', {
            push:'chnfav',
            channels: chn,
            uid: ui[1]
        }, function(e){
            if (e.action == "removed"){
                $("#"+that.id).parent().parent().remove();
            }
        }, 'json');
    },
    firstac : function (that){
        $(".firstac").next().slideDown()
        $("#headerDest li.secondac, #headerDest li.lastac").next().slideUp()
        $("#headerDest li.secondac, #headerDest li.lastac").removeClass("acti");
        $('ul#fondos li.first img').fadeIn(1500)
        $('ul#fondos li.second img, ul#fondos li.last img').fadeOut(1100);
        if ($(".firstac").hasClass("acti")){
        }
        else{
            $(".firstac").addClass("acti")
            $(".firstac").next().addClass("acti");
        }
        return false;
    },
    secondac : function (that){
        $(".secondac").next().slideDown()
        $("#headerDest li.firstac, #headerDest li.lastac").next().slideUp()
        $("#headerDest li.firstac, #headerDest li.lastac").removeClass("acti");
        $('ul#fondos li.second img').fadeIn(1500)
        $('ul#fondos li.first img, ul#fondos li.last img').fadeOut(1100);
        if ($(".secondac").hasClass("acti")){
        }
        else{
            $(".secondac").addClass("acti");
            $(".secondac").next().addClass("acti");
        }
        return false;
    },
    lastac : function (that){
        $(".lastac").next().slideDown()
        $("#headerDest li.firstac, #headerDest li.secondac").next().slideUp()
        $("#headerDest li.firstac, #headerDest li.secondac").removeClass("acti");
        $('ul#fondos li.last img').fadeIn(1500)
        $('ul#fondos li.first img, ul#fondos li.second img').fadeOut(1100);
        if ($(".lastac").hasClass("acti")){
        }
        else{
            $(".lastac").addClass("acti");
            $(".lastac").next().addClass("acti");
        }
        return false;
    },
    similar: function(){
        $('#similares').toggle();
    },
    cerrar : function(that){
        $(that).parent("div").hide();
    },
    cerrar_alert: function(that){
        $("#horario_recordatorio").hide();
    },
    help: function(){},
    user: function(that){
        $('#comunidad').hide()
        $('#login').show();
        $('#barra-vtr').css('background','url(/wp-content/themes/televisionvtr/img/menuvtrnew/header-log.png) no-repeat');
    },
    cancel: function(that){
        $('#comunidad').show()
        $('#login').hide();
        $('#barra-vtr').css('background','url(/wp-content/themes/televisionvtr/img/menuvtrnew/header-sb.png) no-repeat ');
    },
    eliminar_fan : function(that){
        fi= $(that).attr("id").split("_");
        $.get('/index.php', {
            del_fan:'true',
            hazte_fan: fi[1]
        }, function(e){
            if (e == "true"){
                $(that).parent().parent().remove();
            }
        }, 'json');
    },
    forw : function(){
        $('.wrap').animate({
            scrollLeft:'+=600px'
        }, 500 );
    },
    back :function(){
        $('.wrap').animate({
            scrollLeft:'-=600px'
        }, 500 );
    }
}
//funciones que se basan en si la clase css existe en la estructura html
var onClickInId = {
    programar_alerta : function(){
        var formu = $('#form_recordatorio').serialize();
        $.get("/index.php?"+formu,{
            push:'alarma'
        }, function(data){
            },'json');
        $('#horario_recordatorio p, #horario_recordatorio form').hide();
        $('#horario_recordatorio').append("<p class=\"torm\">Tu recordatorio ha sido programado exitosamente para la fecha seleccionada</p>");

        $('#horario_recordatorio').hide();
        $('.torm').remove();
        $('#horario_recordatorio p, #horario_recordatorio form').show();

    },
    vlightbox : function(){
        $('.colornegro').css('height',$(document).height())
        $('.wblack').css('top',$(window).scrollTop())
        $("#vlight").show();
        $('.colornegro').bind("click", function(){
            $("#vlight").hide();
        })
        return false;
    },
    cerrarv : function(){
        $("#vlight").hide();
        return false;
    },
    btnback : function(){
        $('#boxprogramas,.horas').animate({
            scrollLeft: '-=240px'
        }, 500 );
    },
    btnforw : function(){/*grilla de programación*/
        $('#boxprogramas,.horas').animate({
            scrollLeft: '+=240px'
        }, 500 );
    },
    btnup : function(){
        $('#canales,#boxprogramas').animate({
            scrollTop: '-=180px'
        }, 300 );
    },
    btndown : function(){
        $('#canales,#boxprogramas').animate({
            scrollTop: '+=180px'
        }, 300 );
    },
    etivod : function(){/*pestañas destacados home y portadas */
        $('.premium, .hd, .masvtr ul.cine, .bazuca').hide();
        $('.vod').show();
        if (!$('#etivod').hasClass("acti")){
            $('#etivod').addClass("acti");
            $('#etipremium').removeClass("acti");
            $('#etihd').removeClass("acti");
            $('#etibazuca').removeClass("acti");
            $('#eticine').removeClass("acti");
        }
    },
    etipremium :function(){/*pestañas destacados home y portadas */
        $('.vod, .hd, .masvtr ul.cine, .bazuca').hide();
        $('.premium').show();
        if (!$('#etipremium').hasClass("acti")){
            $('#etipremium').addClass("acti");
            $('#etivod').removeClass("acti");
            $('#etihd').removeClass("acti");
            $('#eticine').removeClass("acti");
        }
    },
    etihd : function(){/*pestañas destacados home y portadas */
        $('.vod, .premium, .masvtr ul.cine, .bazuca').hide();
        $('.hd').show();
        if (!$('#etihd').hasClass("acti")){
            $('#etihd').addClass("acti");
            $('#etipremium').removeClass("acti");
            $('#etibazuca').removeClass("acti");
            $('#etivod').removeClass("acti");
            $('#eticine').removeClass("acti");
        }
    },
    eticine :function(){/*pestañas destacados home y portadas */
        $('.vod, .hd, .premium, .bazuca').hide();
        $('.masvtr ul.cine').show();
        if (!$('#eticine').hasClass("acti")){
            $('#eticine').addClass("acti");
            $('#etipremium').removeClass("acti");
            $('#etibazuca').removeClass("acti");
            $('#etivod').removeClass("acti");
            $('#etihd').removeClass("acti");
        }
    },
    etibazuca :function(){/*pestañas destacados home y portadas */
        $('.vod, .hd, .premium,.masvtr ul.cine').hide();
        $('.bazuca').show();
        if (!$('#etibazuca').hasClass("acti")){
            $('#etibazuca').addClass("acti");
            $('#etipremium').removeClass("acti");
            $('#eticine').removeClass("acti");
            $('#etivod').removeClass("acti");
            $('#etihd').removeClass("acti");
        }
    },
    hazte_fan_btn:function(){ /*Hazte fan */
        var url_fan = $('#hazte_fan_btn').attr('href');
        $.getJSON(url_fan,function(data){
            var numero = data[0];
            var usuario = data[1];
            $('#numero_fans').replaceWith(numero);
            $("#lista_fans").prepend(usuario);
        });
        $("#hazte_fan_btn").remove();
        $("#foot").prepend('<span class="yafan">Ya eres fan</span>');
        return false;
    },
    loginbox : function(){
        noEvent.html =  $('.registro').clone();
        var codehtml = '<form id="loginform" name="loginform" action="http://televisionvtr.cl/wp-login.php" method="post"><label for="user_login">Nombre de usuario:</label> <input type="text" name="log" id="user_login"> <label for="user_pass">Contraseña:</label> <input type="password" name="pwd" id="user_pass"> <input type="submit" name="submit" value="Ingresar"> <input type="hidden" value="http://televisionvtr.cl/programacion/" name="redirect_to"/><input type="hidden" value="login" name="action"/><input type="hidden" value="1" name="testcookie"/> | <a href="/programacion" title="cancelar login" id="cancelogin">cancelar</a></form>';
        $('.registro').html(codehtml);
        $('#cancelogin').live("click",function(e){
            onClickInId.cancelogin(this); return false;
        });
    },
    cancelogin : function(){
        $('.registro').replaceWith( noEvent.html);
    },
    userid : function(){
        $('#userid').val("");
    },
    passid : function(){
        $('#passid').val("");
    }
}
var onRadioCheck = {
    cliente1 : function(){
        $("#product, #selreg,#selcom").show();
    },
    cliente2 : function(){
        $("#paquetes, #selreg,#selcom, #alacarta,#clight,#cfull,#product").hide();
    },
    producto1 : function (that){
        $("#clight, #paquetes,#alacarta").hide();
        $("#dbox_clight1,#dbox_clight2").attr("checked",false);
        $("#alacarta div.izq label, div.dere label").show();
        $("#cfull").show();
    },
    producto2 : function (that){
        $("#clight").show();
        $("#cfull, #paquetes,#alacarta").hide();
        $("#dbox_cfull1,#dbox_cfull2").attr("checked",false);
        $("#alacarta div.izq label, div.dere label").show();
    },
    producto3 : function (that){
        $("#clight,#cfull,#paquetes").hide();
        $("#alacarta").show();
        $("#alacarta div.izq label, div.dere label").hide();
        $("#alacarta #carta1,#carta2,#carta4").parent().show();
        $("#dbox_cfull1,#dbox_cfull2").attr("checked",false);
        $("#dbox_clight1,#dbox_clight2").attr("checked",false);

    },
    dbox_cfull1:function(that){
        $("#paquetes, #alacarta").hide();
    },
    dbox_cfull2:function(that){
        $("#paquetes, #alacarta").show();
    },
    dbox_clight1: function(that){
        $("#paquetes, #alacarta").hide();
    },
    dbox_clight2: function(that){
        $("#paquetes, #alacarta").show();
    }
}
var onChangeId = {
    region : function(){
        list="";
        $.get("/index.php",{
            obt:'comuna',
            comuna: $("#region").val()
            }, function(data){
            $.each(data, function(i, value){
                list += '<option valie="'+i+'">'+value+'</option>';
            })
            $("#comuna").html(list);
        }, 'json');
    },
    comuna: function(){
        
        var comuna_sel = $("#comuna option:selected").text();
        if (comuna_sel == "Tocopilla" || comuna_sel == "Vallenar" || comuna_sel == "El Salvador" || comuna_sel == "Illapel" || comuna_sel == "Aconcagua"|| comuna_sel == "San Antonio"|| comuna_sel == "La Ligua"|| comuna_sel == "San Fernando"|| comuna_sel == "Linares"|| comuna_sel == "San Carlos"|| comuna_sel == "Constitucion" || comuna_sel == "Parral"|| comuna_sel == "Cauquenes"|| comuna_sel == "Angol"|| comuna_sel == "Victoria"|| comuna_sel == "Ancud"|| comuna_sel == "Castro"|| comuna_sel == "Coyhaique"){
            $("#prod_3").attr({
                style : "display:none"
            });            
        }
        else
        {
            $("#prod_3").attr({
                style : "display:block"
            });
            
        }//else

        if (comuna_sel == "El Salvador" || comuna_sel == "Vallenar" || comuna_sel == "San Fernando" || comuna_sel == "San Antonio" || comuna_sel == "Cauquenes" || comuna_sel == "Castro" ){
            //set_top_box
            $("#set_top_box").attr({
                style : "display:block"
            });
            $("#set_top_box2").attr({
                style : "display:block"
            });
        }
        else
        {
            $("#set_top_box").attr({
                style : "display:none"
            });
            $("#set_top_box2").attr({
                style : "display:none"
            });
        }
    }
}
var onMouseEnterLeave = {
    accordian : function(that, status){
        if( status == "stop"){
            $(that).addClass("stop")
            clearInterval(inter)
        }
        else{
            $(that).removeClass("stop")
            inter = setInterval("timer()",15000);
        }
    },
    desplegable: function(that,status){
        if( status == "stop"){
            $('.desplegable li').show();
        }
        else{
            $('.desplegable li:gt(0)').hide();
        }
    },
    logoc: function(that, status){
        if(status=="stop"){
            noEvent.the_title = $(that).attr("title");
            $(that).attr("title","" );
            //  $("#the_driver").remove();
            var driver = '<div id="the_driver">'+noEvent.the_title+'</div>'
            $(that).parent().append(driver);
            $("#the_driver").css("display","block");
            $("#the_driver").css("color","#CCC");
            $("#the_driver").css("color","#CCC");
            $("#the_driver strong").css("color","#FFF");
            $("#the_driver").css("background","#646359");
            $("#the_driver").css("width","400px");
            $("#the_driver").css("heigth","80px");
            $("#the_driver").css("position","absolute");
            $("#the_driver").css("margin-top","-120px");
            $("#the_driver").css("margin-left","20px");
            $("#the_driver").css("z-index","100");
            $("#the_driver").css("padding","5px");
            $("#the_driver").css("border","1px solid #666");
            $("#the_driver").mouseenter(function(){
                $(".logoc").trigger('mouseenter');
            });
        }
        else{
            $(that).attr("title", noEvent.the_title);
            $("#the_driver").remove();
        }

    },
    help:function(that, status){
        if( status == "stop"){
            $(that).next().show();
        }
        else{
            $(that).next().hide();
        }
    }
}
$(document).ready(function(){
    $("img").lazyload({
    placeholder : "/wp-content/themes/televisionvtr/ajax/grey.gif",
        effect : "fadeIn"

});
    $(".firstac").addClass("acti");
    $(".firstac").next().addClass("acti");
    num_ul = $(".wrap ul").children().length;
    if (num_ul > 4){
        num_ul = num_ul * 78;
        $("#premium .wrap ul").width(num_ul);
    }
    $.each(onClickInClass, function (i) {
        if ($.isFunction(eval("onClickInClass."+i)) && $('*').hasClass(i)){
            $('.'+i).live("click", function(e){
                eval("onClickInClass."+i+"(this)");return false;
            });
        }
    });
    $.each(onClickInId, function (i) {
        if ($.isFunction(eval("onClickInId."+i)) && document.getElementById(i)){
            $('#'+i).live("click", function(e){
                eval("onClickInId."+i+"(this)");return false;
            });
        }
    });
    $.each(onRadioCheck, function (i) {
        if ($.isFunction(eval("onRadioCheck."+i)) && document.getElementById(i)){
            $('#'+i).live("click", function(e){
                eval("onRadioCheck."+i+"(this)");
            });
        }
    });
    $.each(onChangeId, function (i) {
        if ($.isFunction(eval("onChangeId."+i)) && document.getElementById(i)){
            $('#'+i).bind("change", function(e){
                eval("onChangeId."+i+"(this)");return false;
            });
        }
    });
    $.each(onMouseEnterLeave, function (i) {
        if ($.isFunction(eval("onMouseEnterLeave."+i)) && $('*').hasClass(i)){
            $('.'+i).bind("mouseenter", function(e){
                eval("onMouseEnterLeave."+i+"(this, 'stop')")
                }).bind("mouseleave", function(e){
                eval("onMouseEnterLeave."+i+"(this, 'resume')")
                });
        }
    });
    $("#boxprogramas ul li a").bind('click', function() {
        if(!$(this).parent().hasClass("fcomp")){
            return false;
        }
    });
    $('.helpdec').hide();
    $('.footer_contenidos td').each( function(i, value){
        $(value).removeAttr("valign").css("vertical-align","top");
    })
    $('a[rel*=external]').click( function() {
        window.open(this.href);return false;
    });
    $('.vod').show();
    noEvent.ahora();
    $('.desplegable li:gt(0)').hide();
    if ($('*').hasClass('.premium_ani')) $('.premium_ani').innerfade({
        speed: 'slow',
        timeout: 10000,
        type: 'sequence',
        containerheight: '480px'
    });
});
(function($) {
    $.fn.wait = function(option, options) {
        milli = 1000;
        if (option && (typeof option == 'function' || isNaN(option)) ) {
            options = option;
        } else if (option) {
            milli = option;
        }
        // set defaults
        var defaults = {
            msec: milli,
            onEnd: options
        },
        settings = $.extend({},defaults, options);

        if(typeof settings.onEnd == 'function') {
            this.each(function() {
                setTimeout(settings.onEnd, settings.msec);
            });
            return this;
        } else {
            return this.queue('fx',
                function() {
                    var self = this;
                    setTimeout(function() {
                        $.dequeue(self);
                    },settings.msec);
                });
        }
    }
})(jQuery);
/*max villegas*/
