var _thisIndex = "";

function mycarousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        _thisIndex = $.jcarousel.intval($(this).attr("rel"));
        carousel.scroll(_thisIndex);
        return false;
    });
}

function mycarousel_itemFirstOutCallback() {
    $(".jcarousel-control dt").removeClass("on");
    $(".jcarousel-control a[rel='" + _thisIndex + "']").parent().addClass("on");
}

function mycarousel_itemVisibleInCallback(carousel, object, index) {
    _thisIndex = index;
    $(".jcarousel-control dt").removeClass("on");
    $(".jcarousel-control a[rel='" + _thisIndex + "']").parent().addClass("on");
}

$(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();
    });

    /***
    * Homepage 
    ***/

    if ($("body").attr("id") == "Index") {

        $("#slider").jcarousel({
            scroll: 1,
            visible: 1,
            auto: 12, /* Time (seconds) between slide */
            animation: 400,
            wrap: "last",
            initCallback: mycarousel_initCallback,
            itemFirstOutCallback: { onBeforeAnimation: mycarousel_itemFirstOutCallback },
            itemVisibleInCallback: { onAfterAnimation: mycarousel_itemVisibleInCallback },
            buttonNextHTML: null,
            buttonPrevHTML: null
        });

    }

    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 li div").attr("class", "flag flag_" + thisnation1);
            $(this).find("td.end li div").attr("class", "flag flag_" + thisnation2);

        });

        $("#ResultsAndFixtures table.tabbed tr:nth-child(3)").each(function() {
            $(this).remove();
        });

    }

    /***
    * 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_week .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>");

        });

        // Facebook Connect Show Points
        $("#button_container a.facebook").click(function() {
            var message = "This week I scored: " + roundpoints + " points on Sky Sports Fantasy Football";
            var actionLinks = [{ "text": "Sky Sports Fantasy Football", "href": "http://fantasyfootball.skysports.com"}];

            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;
        });

        // Team Screen
        $("#team_buttons a").click(function() {

            $("#team_buttons li").removeClass("on");
            var pitchWidth = $('div#teamScreen').width();

            if ($(this).attr("id") == "week_tab")
                $("#team_screen_container").stop().animate({ left: "0" }, 500, "easeInQuad");
            else if ($(this).attr("id") == "month_tab")
                $("#team_screen_container").stop().animate({ left: ("-" + (1 * (0 + pitchWidth))) }, 500, "easeInQuad");
            else if ($(this).attr("id") == "total_tab")
                $("#team_screen_container").stop().animate({ left: ("-" + (2 * (0 + pitchWidth))) }, 500, "easeInQuad");
            else if ($(this).attr("id") == "status_tab")
                $("#team_screen_container").stop().animate({ left: ("-" + (3 * (0 + pitchWidth))) }, 500, "easeInQuad");


            $(this).parent().addClass("on");

            return false;
        });

    }
/*
    if (/msie|MSIE 6/.test(navigator.userAgent)) {
 
    }*/

    // End Doc.Rdy
});



