$(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");
    });

    // Menu
    $('#topMenu li').hover(function() {
        $('ul', this).show();
        $(this).addClass('hover');
    }, function() {
        $('ul', this).hide();
        $(this).removeClass('hover');
    });

    // 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 gw1 = new Date("August 6, 2010 19:45:00 GMT+" + offset);
    var gw2 = new Date("September 01, 2010 00:00:00 GMT+" + offset);
    var gw3 = new Date("October 01, 2010 00:00:00 GMT+" + offset);
    var gw4 = new Date("November 01, 2010 00:00:00 GMT+" + offset);
    var gw5 = new Date("December 01, 2010 00:00:00 GMT+" + offset);
    var gw6 = new Date("January 01, 2011 00:00:00 GMT+" + offset);
    var gw7 = new Date("February 01, 2011 00:00:00 GMT+" + offset);
    var gw8 = new Date("March 01, 2011 00:00:00 GMT+" + offset);
    var gw9 = new Date("April 01, 2011 00:00:00 GMT+" + offset);
    var end = new Date("May 08, 2011 00:00:00 GMT+" + offset);

    if (gw1 > today) {
        var header = "Countdown to Kick-off";
        var date = gw1;
        var exp = "The season starts in:"
    } else if (gw2 > today) {
        var header = "Transfer Periods";
        var date = gw2;
        var exp = "Month 1 Open, closes in:";
    } else if (gw3 > today) {
        var header = "Transfer Periods";
        var date = gw3;
        var exp = "Month 2 Open, closes in:";
    } else if (gw4 > today) {
        var header = "Transfer Periods";
        var date = gw4;
        var exp = "Month 3 Open, closes in:";
    } else if (gw5 > today) {
        var header = "Transfer Periods";
        var date = gw5;
        var exp = "Month 4 Open, closes in:";
    } else if (gw6 > today) {
        var header = "Transfer Periods";
        var date = gw6;
        var exp = "Month 5 Open, closes in:";
    } else if (gw7 > today) {
        var header = "Transfer Periods";
        var date = gw7;
        var exp = "Month 6 Open, closes in:";
    } else if (gw8 > today) {
        var header = "Transfer Periods";
        var date = gw8;
        var exp = "Month 7 Open, closes in:";
    } else if (gw9 > today) {
        var header = "Transfer Periods";
        var date = gw9;
        var exp = "Month 8 Open, closes in:";
    } else {
        var header = "Transfer Periods";
        var date = end;
        var exp = "Month 9 Open, closes 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 });
    });


    $("#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") {

        $('ul.images-list').kwicks({ max: 300, spacing: 10, sticky: true, duration: 200, easing: 'easeOutQuad' });

        $('ul.images-list li a').click(function() {
            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");

            var _this = $(this).find("td.club").text();
            $(this).find("td.club").html("<span class='flag flag_" + _this + "'>" + _this + "</span>");

        });

        // Share to twitter
        $("#share_container ul li a.twitter").attr("href", "http://twitter.com/home?status=This week I scored " + roundpoints + " points on " + location.protocol + "//" + location.host);

        // Facebook Connect Show Points
        $("#button_container a.facebook").click(function() {
            var message = "This week I scored: " + roundpoints + " points on Texaco Fantasy Football 10/11";
            var actionLinks = [{ "text": "Texaco Fantasy Football 10/11", "href": "http://fantasy.football-league.co.uk"}];

            FB.ensureInit(function() {
                FB.Connect.showPermissionDialog("offline_access, publish_stream", function(perms) {
                    if (!perms) {
                        return false;
                    } else {
                        FB.Connect.streamPublish(message, null, actionLinks);
                    }
                });
            });

            return false;
        });

        // Facebook Connect Show Team
        $("#button_container a.facebookteam").click(function() {
            var team = "";

            $("#team_screen_container #team_screen_status li a").each(function() {
                team += $(this).text().replace(/\s+$/, '');
                team += ", ";
            });

            var teammessage = "My Texaco Fantasy Football 10/11 team is: " + team + "what's yours?";
            var actionLinks = [{ "text": "Texaco Fantasy Football 10/11", "href": "http://fantasy.football-league.co.uk"}];

            FB.ensureInit(function() {
                FB.Connect.showPermissionDialog("offline_access, publish_stream", function(perms) {
                    if (!perms) {
                        return false;
                    } else {
                        FB.Connect.streamPublish(teammessage, null, actionLinks);
                    }
                });
            });
            return false;
        });


    }

    // End Doc.Rdy
});



