var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

// Document Ready!

$(document).ready(function(){
    headline_count = $("div.headline").size();
    $("div.headline:eq("+current_headline+")").css('top','5px');

    headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
    /*  $('.scrollup').hover(function() {
        clearInterval(headline_interval);
    }, function() {
        headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
        headline_rotate();
    });*/

    fadeContent();



}); //Termina Ready..


//Fuera de Ready..

function headline_rotate() {
    current_headline = (old_headline + 1) % headline_count;
    $("div.headline:eq(" + old_headline + ")").animate({
        top: -205
    },"slow", function() {
        $(this).css('top','210px');
    });
    $("div.headline:eq(" + current_headline + ")").show().animate({
        top: 5
    },"slow");
    old_headline = current_headline;
}

function fadeContent() {
    $("#sabiasque .sabias:hidden:first").fadeIn(500).delay(6000).fadeOut(500, function() {
        $(this).appendTo($(this).parent());
        fadeContent();
    });
}

//UI---------

$(function() {
    // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
    $( ".downloadp" ).live('click',function(){

        window.open($(this).attr('id'));
        return false;

    });


    $(".dpresentacion").submit(function(event) {

        event.preventDefault();
        if($("#clave_descarga").val()=='')
            alert('Contrase√±a Incorrecta');
        else
        {
            if($("#clave_descarga").val()==$(this).attr('id'))
                 window.open($("#url_pt").val());
             else
                  alert('Contrase√±a Incorrecta');
        }
    });


$( "#rdialog").click(function() {
        $( "#dialog-form" ).dialog( "open" );
    });
    $( "#dialog_event" ).dialog({
        bgiframe: true,
        autoOpen: false,
        height: 200,
        width: 600,
        modal: true
    });
    $( "#dialog-form" ).dialog({
        bgiframe: true,
        autoOpen: false,
        height: 420,
        width: 600,
        modal: true
    });
    $( "#dialog-confirm" ).dialog({
        bgiframe: true,
        autoOpen: false,
        height: 420,
        width: 600,
        modal: true
    });

    $( "#register_news" )
    .button()
    .click(function() {
        $( "#dialog-form" ).dialog( "open" );
    });


    $( "#registrog")
    .button()
    .click(function() {
        $( "#dialog-form" ).dialog( "open" );
    });


    $( "#registro_evento" )
    .button()
    .click(function() {
        $( "#dialog_event" ).dialog( "open" );
    });


    $('#username').live('change',function(){
        data = {
            username: $("#username").val()
        };
        $.post('index.php/valid_user/', data, function(data) {

            valid=data.valid;
        });

    });

    $('#a1').live('click',function(){

        $('#1').slideDown('slow');
        $('#2').slideUp('slow');
        $('#3').slideUp('slow');
        $('#4').slideUp('slow');
        $('#5').slideUp('slow');
        $('#main').slideUp('slow');

    });
    $('#a2').live('click',function(){

        $('#2').slideDown('slow');
        $('#1').slideUp('slow');
        $('#3').slideUp('slow');
        $('#4').slideUp('slow');
        $('#5').slideUp('slow');
        $('#main').slideUp('slow');


    });
    $('#a3').live('click',function(){

        $('#3').slideDown('slow');
        $('#2').slideUp('slow');
        $('#1').slideUp('slow');
        $('#4').slideUp('slow');
        $('#5').slideUp('slow');
        $('#main').slideUp('slow');


    });
    $('#a4').live('click',function(){

        $('#4').slideDown('slow');
        $('#2').slideUp('slow');
        $('#3').slideUp('slow');
        $('#1').slideUp('slow');
        $('#5').slideUp('slow');
        $('#main').slideUp('slow');


    });
    $('#a5').live('click',function(){

        $('#5').slideDown('slow');
        $('#2').slideUp('slow');
        $('#3').slideUp('slow');
        $('#4').slideUp('slow');
        $('#1').slideUp('slow');
        $('#main').slideUp('slow');


    });


    /* validator*/

    var validator = $("#comment_form").validate({
        rules: {
            name: "required",
            location:"required",
            comment:"required",
            captcha:"required",
            email: {
                required: true,
                email: true

            }

        },
        messages: {
            name: "Escribe tu nombre",
            location: "Escribe tu empresa",
            comment: "Escribe un comentario",
            captcha: "Escribe la palabra en la imagen",
            email: {
                required: "Escribe un email valido",
                minlength: "Escribe un email valido"

            }
        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") )
                error.appendTo ( element.next() );
            else
                error.appendTo( element.parent().next() );
        },
        // specifying a submitHandler prevents the default submit, good for the demo

        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set   as text for IE
            label.html(" ").addClass("checked");
        }
    });



    var validator = $("#comment_formbt").validate({
        rules: {
            name: "required",

            comment:"required",
            captcha:"required",
            email: {
                required: true,
                email: true

            }

        },
        messages: {
            name: "Escribe tu nombre",

            comment: "Escribe un comentario",
            captcha: "Escribe la palabra en la imagen",
            email: {
                required: "Escribe un email valido",
                minlength: "Escribe un email valido"

            }
        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") )
                error.appendTo ( element.next() );
            else
                error.appendTo( element.parent().next() );
        },
        // specifying a submitHandler prevents the default submit, good for the demo

        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set   as text for IE
            label.html(" ").addClass("checked");
        }
    });



    /*FORM DE REGISTRO*/
    var validator2 = $("#register_form").validate({
        rules: {
            rname: "required",
            rempresa:"required",
            rcaptcha:"required",
            rtelefono:"required",
            remail: {
                required: true,
                email: true,
                remote: 'http://softec.com.mx/index.php/emails'

            },
            rpassword: {
                required: true,
                minlength: 5
            },
            password_confirm: {
                required: true,
                minlength: 5,
                equalTo: "#rpassword"


            }
        },
        messages: {
            rname: "Escribe tu nombre",
            rempresa: "Escribe tu empresa",
            rcaptcha: "Escribe la palabra que ves arriba",
            rtelefono: "Escribe tu telefono",
            remail: {
                required: "Escribe un email valido",
                minlength: "Escribe un email valido"

            },
            rpassword: {
                required: "Escriba una contrase√±a",
                rangelength: jQuery.format("Escriba por lo menos 5 caracteres")
            },
            password_confirm: {
                required: "Repita su contrase√±a",
                minlength: jQuery.format("Escriba por lo menos 5 caracteres"),
                equalTo: "Escriba la misma contrase√±a"
            }
        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") )
                error.appendTo ( element.next() );
            else
                error.appendTo( element.parent().next() );
        },
        // specifying a submitHandler prevents the default submit, good for the demo
        submitHandler: function() {

            data = {
                username:$("#remail").val(),
                email: $("#remail").val(),
                screen_name: $("#rname").val(),
                password: $("#rpassword").val(),
                telefono:$("#rtelefono").val(),
                empresa:$("#rempresa").val(),
                ciudad:$("#rciudad").val()
            };
            $.post('http://softec.com.mx/index.php/softec_users/',data);
            $("#remail").attr('value','');
            $("#rname").attr('value','');
            $("#rpassword").attr('value','');
            $("#password_confirm").attr('value','');
            $("#rtelefono").attr('value','');
            $("#rempresa").attr('value','');
            $("#rciudad").attr('value','');


            $( "#dialog-form" ).dialog('close');

            $( "#dialog-confirm" ).dialog('open');

        },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set   as text for IE
            label.html(" ").addClass("checked");
        }
    });

    $('#register_form').each (function(){
        this.reset();
    });

    $('.close_form').live('click',function(){
        $("#remail").attr('value','');
        $("#rname").attr('value','');
        $("#rpassword").attr('value','');
        $("#password_confirm").attr('value','');
        $("#rtelefono").attr('value','');
        $("#rempresa").attr('value','');
        $("#rciudad").attr('value','');
        $("#rcaptcha").attr('value','')
        $( "#dialog-form" ).dialog('close');
    });
    $('.close_form2').live('click',function(){
        $( "#dialog-confirm" ).dialog('close');
    });



    /*
 * Aqui inicia el validador de el formulario de registro al evento
 *
 *
 */
    /*FORM DE REGISTRO*/
    var validator222= $("#r_evento").validate({
        rules: {
            evento_name: "required",
           
            evento_telefono:"required",
             evento_email: {
                required: true,
                email: true
                

            },

        },
        messages: {
            evento_name: "*",
            evento_email: {
                required: "*",
                minlength: "Escribe un email valido"

            },
            evento_telefono:"*"
        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") )
                error.appendTo ( element.next() );
            else
                error.appendTo( element.parent().next() );
        },
        // specifying a submitHandler prevents the default submit, good for the demo
        submitHandler: function() {

            data = {
                evento_email:$("#evento_email").val(),
                entry_id:$("#entry_id").val(),
                evento_name:$("#evento_name").val(),
                evento_telefono:$("#evento_telefono").val(),
                evento:$("#evento").val()
            };
           
            $.post('http://softec.com.mx/index.php/r_evento',data);
            $("#main_registro").slideUp('slow');
            $("#succes_evento").slideDown('slow');


        },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set   as text for IE
            label.html(" ").addClass("checked");
        }
    });


    $('.dncompleta').click(function(){

        window.open($(this).attr('id'));
        return false;
    });






    /*FORM DE REGISTRO*/
    var validator9 = $("#rlform").validate({
        rules: {
            rlnombre: "required",
            rlempresa:"required",

            rltelefono:"required",
            rlemail: {
                required: true,
                email: true,
                remote: 'http://softec.com.mx/index.php/rlemail'

            },
            rlpassword: {
                required: true,
                minlength: 5
            }

        },
        messages: {
            rlnombre: "*",
            rlempresa: "*",

            rltelefono: "*",
            rlemail: {
                required: "Invalido",
                minlength: "Invalido"
                
            },
            rlpassword: {
                required: "*",
                rangelength: jQuery.format("5 caracteres")
            }

        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") )
                error.appendTo ( element.next() );
            else
                error.appendTo( element.parent().next() );
        },
        // specifying a submitHandler prevents the default submit, good for the demo
        submitHandler: function() {

            data = {
                username:$("#rlemail").val(),
                email: $("#rlemail").val(),
                screen_name: $("#rlnombre").val(),
                password: $("#rlpassword").val(),
                telefono:$("#rltelefono").val(),
                empresa:$("#rlempresa").val()

            };
            $.post('http://softec.com.mx/index.php/softec_users/',data);
            $("#rlemail").attr('value','');
            $("#rlname").attr('value','');
            $("#rlpassword").attr('value','');

            $("#rltelefono").attr('value','');
            $("#rlempresa").attr('value','');


            $("#rldregistro").slideUp('slow');
            $("#rldsucces").slideDown('slow');

        },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set   as text for IE
            label.html(" ").addClass("checked");
        }
    });



    
    
    
$( "#cerrar_succes" ).button().click(function() {
$( "#dialog_event" ).dialog( "close" );
    });
    $( "#close_event1" ).button().click(function() {
        $( "#dialog_event" ).dialog( "close" );
    });
$( "#registrar_evento" ).button();
    $('#promociones').liteAccordion({
        onActivate : function() {
            this.find('figcaption').fadeOut();
        },
        slideCallback : function() {
            this.find('figcaption').fadeIn();
        },
        autoPlay : true,
        pauseOnHover : true,
        theme : 'dark',
        rounded : true,
        containerWidth : 860,           // width of accordion (px)
        containerHeight : 420,          // height of accordion (px)
        headerWidth : 48,               // width of tabs (px)
        enumerateSlides : true
    }).find('figcaption:first').show();



    sfHover = function() {
        var sfEls = document.getElementById("menu").getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover=function() {
                this.className+=" sfhover";
            }
            sfEls[i].onmouseout=function() {
                this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
            }
        }
    }
    if (window.attachEvent) window.attachEvent("onload", sfHover);





    $('#aindsoft').live('click',function(){

        $('#main').slideUp('slow');
        $('#indfinan').slideUp('slow');
        $('#indburs').slideUp('slow');
        $('#indhipo').slideUp('slow');
        $('#indsoft').slideDown('slow');
        
    });
        $('#aindfinan').live('click',function(){

        $('#main').slideUp('slow');
        $('#indfinan').slideDown('slow');
        $('#indburs').slideUp('slow');
        $('#indhipo').slideUp('slow');
        $('#indsoft').slideUp('slow');
        
    });
    $('#aindburs').live('click',function(){

        $('#main').slideUp('slow');
        $('#indfinan').slideUp('slow');
        $('#indburs').slideDown('slow');
        $('#indhipo').slideUp('slow');
        $('#indsoft').slideUp('slow');
        
    });
     $('#aindhipo').live('click',function(){

        $('#main').slideUp('slow');
        $('#indfinan').slideUp('slow');
        $('#indburs').slideUp('slow');
        $('#indhipo').slideDown('slow');
        $('#indsoft').slideUp('slow');
        
    });
    
    
    
     $('.url_promocion').live('click',function(){
        window.open($(this).attr('id'));
        return false;
     });
$( "#mapa-dialog" ).dialog({
    bgiframe: true,
    autoOpen: false,
    height: 550,
    width: 850,
    modal: true
});

$( ".r_mapa" )
    .button()
    .click(function() {
        $( "#mapa-dialog" ).dialog( "open" );
    });



$( "#shoppingcart" ).dialog({
    bgiframe: true,
    autoOpen: false,
    height: 250,
    width: 400,
    modal: true
});

$( ".addshop" )
    
    .click(function() {
        $( "#shoppingcart" ).dialog( "open" );
    });

$("#selectproductos").msDropDown();
$( "#contacto" )
    .button()
    .click(function() {
        window.open("http://softec.com.mx/index.php/contacto");
        return false;
    });

$( ".closemap" ).click(function() {
        $( "#mapa-dialog" ).dialog( "close" );
    });

});
