$(document).ready(function(){
    
    /***
    * General 
    ***/
    
    //Menu Highlighting!
    var body = $("body").attr("id");
    $("#topMenu li#tab_"+body).addClass("tab-cur");
    $("#navigation li#tab_"+body).addClass("on");

    // Table Hover
    // Add tr hover class for ie6+
    $("table.tabbed tbody tr").hover(function(){
        $(this).addClass("hover");
    }, function(){
        $(this).removeClass("hover");
    });
    
    $("table tbody tr").each(function(){
        $(this).find("td:first:not(.first)").addClass("first");
        $(this).find("td:last:not(.end)").addClass("end");
    });
    $("table thead tr, table tr.head").each(function(){
        $(this).find("th:first:not(.first)").addClass("first");
        $(this).find("th:last:not(.end)").addClass("end");
    });

    // Handle horizontal menu dropdowns
    jQuery('#topMenu li').hover(
        function(){ jQuery('ul', this).css('display', 'block'); 
                    jQuery(this).addClass('current'); },
        function(){ jQuery('ul', this).css('display', 'none'); 
                    jQuery(this).removeClass('current'); 
    });
    
    // Forms & Labels
    // Hide overlay on focus
    $("input.text").focus(function(){
        $(this).prev("label.overlay").hide();
    });
   
    // Show overlay on blur
    $("input.text").blur(function(){
        if($(this).attr("value") == "") {
            $(this).prev("label.overlay").show();
        }
    });
    
    // Focus input on overlay click ;)
    $("label.overlay").click(function(){
        $(this).next().focus();
    });
    
    // Hide if input box filled
    $("input.text").each(function(){
        if($(this).attr("value") !== "")
           $(this).prev("label.overlay").hide();
    });

    // Feeds / Twitter
    $("#newsFeeds ul.tabs a").click(function(){
       $("#newsFeeds ul.tabs li").removeClass("on");
       $(this).parent().addClass("on");
       $("#twitter_div, #news_div").hide();
       $("#"+$(this).attr("id")+"_div").show();
       return false;
    });
       
    // Countdown Timer
    var today = new Date();
    var offset = (new Date().getTimezoneOffset() / 60) * -1;

    var gp1 = new Date("June 11, 2010 15:00:00 GMT+" + offset);
    var gp2 = new Date("June 16, 2010 17:00:00 GMT+" + offset);
    var gp3 = new Date("June 22, 2010 00:00:00 GMT+" + offset);
    var gp4 = new Date("June 26, 2010 00:00:00 GMT+" + offset);
    var gp5 = new Date("July 01, 2010 00:00:00 GMT+" + offset);
    var gp6 = new Date("July 05, 2010 00:00:00 GMT+" + offset);
    var end = new Date("July 12, 2010 00:00:00 GMT+" + offset);
    
    if(gp1>today) {
        var header = "Countdown to Kickoff";
        var date = gp1;
        var exp = "The game starts in:"
    } else if(gp2>today) {
        var header = "Game Periods";
        var date = gp2;
        var exp = "Game Period 2 starts in:";
    } else if(gp3>today) {
        var header = "Game Periods";
        var date = gp3;
        var exp = "Game Period 3 starts in:";
    } else if(gp4>today) {
        var header = "Game Periods";
        var date = gp4;
        var exp = "Game Period 4 starts in:";
    } else if(gp5>today) {
        var header = "Game Periods";
        var date = gp5;
        var exp = "Game Period 5 starts in:";
    } else if(gp6>today) {
        var header = "Game Periods";
        var date = gp6;
        var exp = "Game Period 6 starts in:";
    } else {
        var header = "Game Periods";
        var date = end;
        var exp = "Game ends in:";
    }
        
    $("#counter h2").text(header);
    $("#counter p").html(exp);
    
    $('#defaultCountdown').countdown({until: date}); 
    $('#removeCountdown').toggle(function(){ 
        $('#defaultCountdown').countdown('destroy'); 
    }, function(){ 
        $('#defaultCountdown').countdown({until: date, compact: true}); 
    });
    
  
    /***
    * Homepage 
    ***/
    
    if($("body").attr("id") == "Index") {
 
        //Image thingy
        $("#content ul.images li").hover(function(){
        
        // Reset z-index to 10
        $("#content ul.images li").css({
            zIndex: "10"
        });
            
        // Animate on hover
        $(this).css({
            width: "230px",
            height: "210px",
            marginTop: "-30px",
            marginLeft: "-30px",
            zIndex: "20"
        }).find("a").css({
            bottom: "10px"
        });
        }, function(){
            $(this).stop().css({
                width: "175px",
                height: "150px",
                marginTop: "0",
                marginLeft: "0"
            }).find("a").css({
                bottom: "-150px"
            });
        });
 
        // Groups
        
        $("#groupC, #groupD, #groupE, #groupF, #groupG, #groupH").hide();
        
        $("#groups a#tabAB").click(function(){
            $("#groups li").removeClass("on");
            $("#groupC, #groupD, #groupE, #groupF, #groupG, #groupH").hide();
            $("#groupA, #groupB").show();
            $(this).parent().addClass("on");
            return false;
        });
        
        $("#groups a#tabCD").click(function(){
            $("#groups li").removeClass("on");
            $("#groupA, #groupB, #groupE, #groupF, #groupG, #groupH").hide();
            $("#groupC, #groupD").show();
            $(this).parent().addClass("on");
            return false;
        });
        
        $("#groups a#tabEF").click(function(){
            $("#groups li").removeClass("on");
            $("#groupC, #groupD, #groupA, #groupB, #groupG, #groupH").hide();
            $("#groupE, #groupF").show();
            $(this).parent().addClass("on");
            return false;
        });
        
        $("#groups a#tabGH").click(function(){
            $("#groups li").removeClass("on");
            $("#groupC, #groupD, #groupE, #groupF, #groupA, #groupB").hide();
            $("#groupG, #groupH").show();
            $(this).parent().addClass("on");
            return false;
        });
        
    } 
    
    /***
    * Team 
    ***/
              
    else if($("body").attr("id") == "Team") {
       
        // Column Highlighting in 1 function + Sticky Clicking
        $("table.cols tr.head th").hover(function(){
            var _this = $(this).parent().parent();
            
            $(this).addClass("on");
            // Calculate Column Number
            $(_this).find("tr td:nth-child(" + (this.cellIndex + 1) + ")").addClass("on");
        
        }, function(){
            // Remove Highlighting
            $("table.cols tr.head th, table.cols tr td").removeClass("on");
            
        }).click(function(){
            var _this = $(this).parent().parent();
            // Sticky Highlighting
            $(_this).find("tr.head th, tr td").removeClass("sticky");
            $(this).addClass("sticky");
            $(_this).find("tr td:nth-child(" + (this.cellIndex + 1) + ")").addClass("sticky");
        });     
        
        // Points
        var totalpoints = parseInt($("#team_screen_container #team_screen_total .total span").text());
        var roundpoints = parseInt($("#team_screen_container #team_screen_month .total span").text());
        
        $("table.key tfoot td.total").text(totalpoints);
        
        // Team breakdown flags & sticky
        $("table.teamsold tr, table.teamlist tr").each(function(){
            $(this).find("td:last, th:last").addClass("sticky");
            $(this).find("td.nation").addClass("flag flag_"+($(this).find("td.nation").text()));
        });
        
        // Share to twitter
        $("#button_container ul li a.twitter").attr("href", "http://twitter.com/home?status=This round I scored "+ roundpoints + " points on "+ location.protocol+"//"+location.host);
        
    } else if($("body").attr("id") == "PlayerList") {
    
        // Flags
        $("table#playerlist tr").each(function(){
            $(this).find("td:nth-child(4)").addClass("flag flag_"+($(this).find("td:nth-child(4)").text()));
        });

    } else if ($("body").attr("id") == "ResultsAndFixtures") {
    
        $("#ResultsAndFixtures table.tabbed").each(function() {
            $(this).find("tr:first").addClass("head");
        });

        // Flags
        $("#ResultsAndFixtures table.tabbed tr:nth-child(2)").each(function() {
            $(this).addClass("on");
            var thisnation1 = $(this).find("td.first li div").attr("class").replace("team_", "");
            var thisnation2 = $(this).find("td.end li div").attr("class").replace("team_", "");

            $(this).find("td.first").addClass("flag flag_" + thisnation1);
            $(this).find("td.end").addClass("flag flag_" + thisnation2);

        });

        $("#ResultsAndFixtures table.tabbed tr:nth-child(3)").each(function() {
            $(this).remove();
        });

    }
   
    if(/msie|MSIE 6/.test(navigator.userAgent)) {
        DD_belatedPNG.fix('#team_screen_container li, #team_screen_container li a, a.button, #select fieldset, #select #available, #select #selectedTop, #select ul.tabs, #select #selectedPitch .teamInfo, #select ul.tabs, #rhs #login, .countdown_section');
    }
// End Doc.Rdy
});
