﻿var GAMES_WIDTH = 826;
var ROUNDS_WIDTH = 921;
var ANIMATION_SPEED = 500;
var roundNum = 0;
var inProgress = false;
var debug = false;

$(function () {
	$(".game:last-child").addClass("last-game");
    $(".rounds .round:first-child").addClass("active-round");
    updateRoundNum();

	bindNextButton();
	bindPrevButton();

	function bindNextButton() {
		$(".next-page a").click(function (event) {
			event.preventDefault();
			
			if (inProgress) {
				return;
			}

			inProgress = true;

			var games = $(".active-round .game").length;
			var pages = Math.floor((games - 1) / 7) + 1;
			var left = $(".active-round .games").css("left");
			if (left == "auto")
				left = 0;
			left = parseInt(left);
			var currentPage = 0 - Math.floor(left / GAMES_WIDTH);

			// if there are more games in the current round
			if (pages > currentPage + 1) {
				log("animate begin - same round. inProgress = " + inProgress);
				$(".active-round .games").animate({
					left: left - GAMES_WIDTH
				}, ANIMATION_SPEED, function () {
					inProgress = false;
					log("animate finish - same round. inProgress = false;");
				});

				return;
			}

			// there are no more games in this round
			var rounds = $(".rounds .round");
			var activeRound = $(".active-round");
			var currentRound = rounds.index(activeRound);

			// see if there is a next round
			if (currentRound < rounds.length - 1) {
				slideNextRound();
			} else {
				// there isn't a next round
				// try to get one with AJAX
				$.get("/FixtureBanner.aspx", {
					Round: roundNum + 1
				}, function (result) {
					 result = trim(result);
					 if (result.length == 0) {
						inProgress = false;
						log("no next round. inProgress = false;");
						return;
					}

					result = $(result);
					result.find(".game:last-child").addClass("last-game");
					$(".rounds").append(result);
					slideNextRound();
				});
			}
		});	
	}
	
	function bindPrevButton() {
		$(".prev-page a").click(function (event) {
			log("prev clicked. inProgress: " + inProgress);
			event.preventDefault();
			
			if (inProgress) {
				log("double click detected. returning");
				return;
			}
				
			inProgress = true;

			var games = $(".active-round .game").length;
			var pages = Math.floor((games - 1) / 7) + 1;

			var left = $(".active-round .games").css("left");
			if (left == "auto")
				left = 0;
			left = parseInt(left);

			var currentPage = 0 - Math.floor(left / GAMES_WIDTH);

			// if there is a previous page in the current round
			if (currentPage > 0) {
				log("animate begin - same round. inProgress = " + inProgress);
				$(".active-round .games").animate({
					left: left + GAMES_WIDTH
				}, ANIMATION_SPEED, function () {
					log("animate finish - same round. inProgress = " + false);
					inProgress = false;
				});

				return;
			}

			// there are no more previous games in this round
			// see if there is a previous round
			var rounds = $(".rounds .round");
			var activeRound = $(".active-round");
			var currentRound = rounds.index(activeRound);

			if (currentRound > 0) {
				slidePrevRound();
			} else if (roundNum > 1) {
				// there isn't a previous round
				// try to get one with AJAX
				$.get("/FixtureBanner.aspx", {
					Round: roundNum - 1
				}, function (result) {
					result = $(result);
					result.find(".game:last-child").addClass("last-game");
					$(".rounds").prepend(result);

					var roundLeft = $(".rounds").css("left");
					if (roundLeft == "auto")
						roundLeft = 0;
					roundLeft = parseInt(roundLeft);
					$(".rounds").css("left", 0 - ROUNDS_WIDTH)

					slidePrevRound();
				});
			} else {
				inProgress = false;
				log("nothing happened. inProgress = false");
			}
		});	
	}

    function slideNextRound() {
        var rounds = $(".rounds .round");
        var activeRound = $(".active-round");
        var currentRound = rounds.index(activeRound);

        if (currentRound < rounds.length - 1) {
            var nextRound = $(rounds[currentRound + 1]);
            activeRound.removeClass("active-round");
            nextRound.addClass("active-round");
            updateRoundNum();

            var roundLeft = $(".rounds").css("left");
            if (roundLeft == "auto")
                roundLeft = 0;
            roundLeft = parseInt(roundLeft);

			log("animate begin - next round. inProgress = " + inProgress);
            $(".rounds").animate({
                left: roundLeft - ROUNDS_WIDTH
            }, ANIMATION_SPEED, function () {
				log("animate finish - next round. inProgress = false;");
                inProgress = false;
            });
        }
    }

    function slidePrevRound() {
        var rounds = $(".rounds .round");
        var activeRound = $(".active-round");
        var currentRound = rounds.index(activeRound);

        if (currentRound > 0) {
            var prevRound = $(rounds[currentRound - 1]);
            activeRound.removeClass("active-round");
            prevRound.addClass("active-round");
            updateRoundNum();

            var roundLeft = $(".rounds").css("left");
            if (roundLeft == "auto")
                roundLeft = 0;
            roundLeft = parseInt(roundLeft);

			log("animate begin - previous round. inProgress: " + inProgress);
            $(".rounds").animate({
                left: roundLeft + ROUNDS_WIDTH
            }, ANIMATION_SPEED, function () {
				inProgress = false;
				log("animate finish - previous round. inProgress: false");
            });
        }
    }

    function updateRoundNum() {
        roundNum = parseInt($(".active-round input[name=RoundNum]").val());
    }

    function trim(s) {
        var l = 0; var r = s.length - 1;
        while (l < s.length && s[l] == ' ')
        { l++; }
        while (r > l && s[r] == ' ')
        { r -= 1; }
        return s.substring(l, r + 1);
    }
	
	function log(message) {
		if (debug)
			console.log(message);
	}
});
