$(document).ready(function() {

    /***
    * General 
    ***/

    //Menu Highlighting!
    var body = $("body").attr("id");
    $("#topMenu li#tab_" + body).addClass("tab-cur");
    $("#logged ul 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 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") {

        $("#slider").jcarousel({
            scroll: 1,
            visible: 1,
            wrap: "circular",
            auto: 8,
            animation: 500,
            itemVisibleInCallback: { onBeforeAnimation: mycarousel_itemVisibleInCallback },
            itemVisibleOutCallback: { onAfterAnimation: mycarousel_itemVisibleOutCallback },
            buttonNextHTML: '<a href="#" class="next">Next</a>',
            buttonPrevHTML: null
        });

        $(".next").click(function() {
            return false;
        });


        // 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;
        });


        //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"
            });
        });

    }

    /***
    * Select 
    ***/

    else if ($("body").attr("id") == "new_select") {

        // Position Tabs
        $("#select #availablePos a").click(function() {
            $("#select #availablePos li").removeClass("on");
            $(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()));
        });


    }
    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();
        });

    } 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()));
        });

    }
    
    var IE6 = false/*@cc_on || @_jscript_version < 5.7@*/;

    if (IE6) {
        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
});

var mycarousel_itemList = [
    { url: 'http://imageseasynet.fantasyleague.com/fast/worldcup.2010/schools/images/intro1.jpg', link: 'Register.aspx', target: '_self' },
    { url: 'http://imageseasynet.fantasyleague.com/fast/worldcup.2010/schools/images/intro2.jpg', link: 'Register.aspx', target: '_self' },
    { url: 'http://imageseasynet.fantasyleague.com/fast/worldcup.2010/schools/images/intro3.jpg', link: 'Register.aspx', target: '_self' }
];

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};

function mycarousel_getItemHTML(item) {
    return '<a target="' + item.target + '" href="' + item.link + '"><img src="' + item.url + '" /></a>';

};

