C4.Gamebar = {};
C4.Gamebar.pulseTimer;
C4.Gamebar.width = 332;
C4.Gamebar.disableControls = false;
C4.Gamebar.pulsatingButtons = function () {
    $(".launchNow").animate({
        opacity: 0.5
    }, 500).animate({
        opacity: 1
    }, 500);
    $(".livescore").animate({
        opacity: 0.5
    }, 500).animate({
        opacity: 1
    }, 500);
    C4.Gamebar.setPulseTimer();
};
C4.Gamebar.setPulseTimer = function () {
    clearTimeout(C4.Gamebar.pulseTimer);
    var menuTimer = setTimeout("C4.Gamebar.pulsatingButtons()", 2000);
};
$(document).ready(function () {
    C4.Gamebar.pulsatingButtons();
    $(".carouselWrap").addClass("pngbg");
});
C4.Gamebar.Initialise = function () {
    C4.Gamebar.$gamebar = $(".carouselWrap");
    C4.Gamebar.$gamebar.find(".prev").mousedown(C4.Gamebar.Prev);
    C4.Gamebar.$gamebar.find(".next").mousedown(C4.Gamebar.Next);
    var $carousel = C4.Gamebar.$gamebar.find(".carousel");
    C4.Gamebar.$roundslist = $carousel.children();
    C4.Gamebar.ModuleId = $("div[id$='Gamebar_carousel']").attr("moduleId");
    var $rounds = C4.Gamebar.$roundslist.children();
    C4.Gamebar.rounds = $rounds.length;
    for (var i = 0; i < $rounds.length; i++) {
        if ($rounds.get(i).className == "current") {
            C4.Gamebar.currentround = i;
            break;
        }
    }
    if (C4.Gamebar.currentround > 0 || C4.Gamebar.$roundslist.find("li:first-child").attr("PrevRoundId") && parseInt(C4.Gamebar.$roundslist.find("li:first-child").attr("PrevRoundId")) > 0) {
        $(".carouselWrap a.prev").css("display", "block");
    }
    if ($("li[id$='CurrentListItem']").attr("NextRoundId") > 0) {
        $(".carouselWrap a.next").css("display", "block");
    }
    C4.Gamebar.$roundslist.css({
        left: -(C4.Gamebar.currentround * C4.Gamebar.width) + "px",
        width: $rounds.length * C4.Gamebar.width + "px",
        display: "block"
    });
    C4.Gamebar.addCarouselEffects();
    C4.Gamebar.findTeamNames();
};
C4.Gamebar.Prev = function () {
    if (!C4.Gamebar.disableControls) {
        if (C4.Gamebar.currentround > 0) {
            C4.Gamebar.currentround--;
            C4.Gamebar.$roundslist.animate({
                left: -(C4.Gamebar.currentround * C4.Gamebar.width)
            }, 500);
        }
        else {
            var prevRound = C4.Gamebar.$roundslist.find("li:first-child").attr("PrevRoundId");
            C4.Gamebar.disableControls = true;
            $(".carouselWrap a.prev").css({
                'background-image': "URL(/portals/0/images/css/gamebar/gameBar-loader2.gif)",
                'background-position': "0px 30px"
            });
            if (prevRound) {
                $.get("/datafeeder/feed.aspx", {
                    format: "barexml",
                    feed_SportFixtureGetRoundGamebar: "req0",
                    param_req0_roundId: prevRound,
                    param_req0_moduleId: C4.Gamebar.ModuleId
                }, C4.Gamebar.PrevLoaded);
            }
        }
        if (C4.Gamebar.currentround === 0 && (!C4.Gamebar.$roundslist.find("li:first-child").attr("PrevRoundId") || parseInt(C4.Gamebar.$roundslist.find("li:first-child").attr("PrevRoundId")) < 0)) {
            $(".carouselWrap a.prev").css("display", "none");
        }
        $(".carouselWrap a.next").css("display", "block");
    }
};
C4.Gamebar.PrevLoaded = function (result, status) {
    if (status == "success") {
        C4.Gamebar.$roundslist.prepend(result);
        C4.Gamebar.rounds++;
        C4.Gamebar.$roundslist.css({
            left: -((C4.Gamebar.currentround + 1) * C4.Gamebar.width) + "px",
            width: C4.Gamebar.rounds * C4.Gamebar.width + "px"
        });
        C4.Gamebar.addCarouselEffects();
        C4.Gamebar.findTeamNames();
        C4.Gamebar.$roundslist.animate({
            left: -(C4.Gamebar.currentround * C4.Gamebar.width)
        }, 500);
        $(".carouselWrap a.prev").removeAttr("style");
        var prevRound = C4.Gamebar.$roundslist.find("li:first-child").attr("PrevRoundId");
        if (!prevRound) {
            $(".carouselWrap a.prev").css("display", "none");
        }
    }
    C4.Gamebar.disableControls = false;
};
C4.Gamebar.Next = function () {
    if (!C4.Gamebar.disableControls) {
        if (C4.Gamebar.currentround < C4.Gamebar.rounds - 1) {
            C4.Gamebar.currentround++;
            C4.Gamebar.$roundslist.animate({
                left: -(C4.Gamebar.currentround * C4.Gamebar.width)
            }, 500);
        }
        else {
            var nextRound = C4.Gamebar.$roundslist.find("li:last-child").attr("NextRoundId");
            C4.Gamebar.disableControls = true;
            $(".carouselWrap a.next").css({
                'background-image': "URL(/portals/0/images/css/gamebar/gameBar-loader2.gif)",
                'background-position': "0px 30px"
            });
            if (nextRound) {
                $.get("/datafeeder/feed.aspx", {
                    format: "barexml",
                    feed_SportFixtureGetRoundGamebar: "req0",
                    param_req0_roundId: nextRound,
                    param_req0_moduleId: C4.Gamebar.ModuleId
                }, C4.Gamebar.NextLoaded);
            }
        }
        if (C4.Gamebar.currentround == C4.Gamebar.rounds - 1 && (!C4.Gamebar.$roundslist.find("li:last-child").attr("NextRoundId") || C4.Gamebar.$roundslist.find("li:last-child").attr("NextRoundId") <= 0)) {
            $(".carouselWrap a.next").css("display", "none");
        }
        $(".carouselWrap a.prev").css("display", "block");
    }
};
C4.Gamebar.NextLoaded = function (result, status) {
    if (status == "success") {
        C4.Gamebar.$roundslist.append(result);
        C4.Gamebar.rounds++;
        C4.Gamebar.$roundslist.css({
            width: C4.Gamebar.rounds * C4.Gamebar.width + "px"
        });
        C4.Gamebar.addCarouselEffects();
        C4.Gamebar.findTeamNames();
        C4.Gamebar.currentround++;
        C4.Gamebar.$roundslist.animate({
            left: -(C4.Gamebar.currentround * C4.Gamebar.width)
        }, 500);
        $(".carouselWrap a.next").removeAttr("style");
        var nextRound = C4.Gamebar.$roundslist.find("li:last-child").attr("NextRoundId");
        if (!nextRound) {
            $(".carouselWrap a.next").css("display", "none");
        }
    }
    C4.Gamebar.disableControls = false;
};
C4.Gamebar.addTransparency = function () {
    if ($.browser.msie) {
        if ($.browser.version == "6.0") {
            $(".matchType").addClass("pngbg");
            $(".roundNum h3").addClass("pngbg");
            $(".roundNum h4").addClass("pngbg");
        }
    }
};
C4.Gamebar.findTeamNames = function () {
    $("#carouselModule .gameInfo").each(function () {
        var teamA = $(this).children("div:eq(0)").children("h2").text();
        var teamB = $(this).children("div:eq(1)").children("h2").text();
        var infoIndex = $(this).parent("div").children(".gameInfo").index(this);
        var matchIndex = ".matchInfo:eq(" + infoIndex + ")";
        $(this).parent("div").children(".matchInfoWrap").children(matchIndex).children("h5").children("span:eq(0)").html(teamA);
        $(this).parent("div").children(".matchInfoWrap").children(matchIndex).children("h5").children("span:eq(2)").html(teamB);
    });
};
C4.Gamebar.addCarouselEffects = function () {
    $(".matchInfoClicked").css("display", "block");
    C4.Gamebar.addTransparency();
    $("#carouselModule .roundinfo > div.gameInfo").mouseover(function () {
        $(this).addClass("gameInfoHover");
    });
    $("#carouselModule .roundinfo > div.gameInfo").mouseout(function () {
        $(this).removeClass("gameInfoHover");
    });
    $("#carouselModule .roundinfo > div.gameInfo").unbind("click");
    $("#carouselModule .roundinfo > div.gameInfo").click(function () {
        $(this).addClass("gameInfoClicked");
        $(this).siblings(".gameInfoClicked").removeClass("gameInfoClicked");
        var divIndex = $(this).parent("div").children(".gameInfo").index(this);
        var matchInfoIndex = ".matchInfo:eq(" + divIndex + ")";
        $(this).parent("div").children(".matchInfoWrap").children().removeClass("matchInfoClicked");
        $(this).parent("div").children(".matchInfoWrap").children().css("display", "none");
        $(this).parent("div").children(".matchInfoWrap").children(matchInfoIndex).css("display", "block").fadeTo("50", ".5").fadeTo("1000", "1");
        $(this).parent("div").children(".matchInfoWrap").children(matchInfoIndex).addClass("matchInfoClicked");
        return false;
    });
};
$(function () {
    //C4.Gamebar.Initialise();
});
