$(document).ready(function() {
    //alert($('td#ticket' + '43').text());
    
    var login_focused;
    var password_focused;
    
    $(window).load(function() {
        $("#id_btn_login").focus();
    });

    $("input#roof_login").focus(function() {
        login_focused = true;
    });
    
    $("input#roof_login").blur(function() {
        login_focused = false;
    });
    
    $("input#roof_password").focus(function() {
        password_focused = true;
    });
    
    $("input#roof_password").blur(function() {
        password_focused = false;
    });

    $.timer(10, function() {
        checkLogin(login_focused, password_focused);
    });
    
    
    $("div#tickets table.can_buy td.free").hover(
        function() {
            $(this).css("cursor", "pointer");
            //alert($(this).attr("id"));
        },
        function() {
            $(this).css("cursor", "default");
            //alert($(this).attr("id"));
        }
    );
    
    $("div#tickets table.can_buy td.free").click(
        function() {
            if(confirm('You buy a ticket ' + jQuery.trim($(this).text()))) {
                buy_ticket($(this).text());
            }
        }
    );

    $("input#id_btn_login").hover(
        function() {
            $(this).attr("class", "btn_login_hover");
            //alert($(this).attr("id"));
        },
        function() {
            $(this).attr("class", "btn_login");
            //alert($(this).attr("id"));
        }
    );
    
    $("input#id_btn_logout").hover(
        function() {
            $(this).attr("class", "btn_logout_hover");
            //alert($(this).attr("id"));
        },
        function() {
            $(this).attr("class", "btn_logout");
            //alert($(this).attr("id"));
        }
    );
    
    $("input.btn_save").hover(
        function() {
            $(this).attr("class", "btn_save_hover");
            //alert($(this).attr("id"));
        },
        function() {
            $(this).attr("class", "btn_save");
            //alert($(this).attr("id"));
        }
    );
    
    $("input.btn_cashout").hover(
        function() {
            $(this).attr("class", "btn_cashout_hover");
            //alert($(this).attr("id"));
        },
        function() {
            $(this).attr("class", "btn_cashout");
            //alert($(this).attr("id"));
        }
    );
    
    $("a#remember_me").click(function() {
        var checkbox = $(this).parent("p").children("input");
        if(checkbox.attr("checked")) {
            checkbox.attr("checked", "")
        }else {
            checkbox.attr("checked", "checked")
        }
        return false;
    });
    
    $("input#reg_username").blur(function() {
        validate_username($(this).attr("value"));
    });
    
    $("input#reg_password").blur(function() {
        validate_password($(this).attr("value"));
    });
    
    $("input#reg_password_repeat").blur(function() {
        validate_password_match($("input#reg_password").attr("value"), $(this).attr("value"));
    });
    
    $("input#reg_email").blur(function() {
        validate_email($(this).attr("value"));
    });
    
    $("span.tooltip").tooltip({
        top: 5,
        left: 15,
        delay: 0,
        fade: 20,
        showURL: false
        //bodyHandler: function() { 
            //return "aaa"; 
        //}
    });
    
    $('span.time_counter').secCountdown();
    
});