﻿/// <reference path="../Intellisense.js" />

Agility.RegisterNamespace("CleanEating.Modules");

(function () {

    var DinnerToniteAndCleanEatingLoves = CleanEating.Modules.DinnerToniteAndCleanEatingLoves = function (containerID, dinners) {
        this._containerID = containerID;
        this._dinners = dinners;
        this._dinnerIndex = new Date().getDay();
    };

    DinnerToniteAndCleanEatingLoves.prototype._getChild = function (idSuffix) {
        return $("#" + this._containerID + "_" + idSuffix);
    };

    DinnerToniteAndCleanEatingLoves.prototype.init = function () {
        var module = this,
            controls = this._getChild("Controls");
				
		$(".DinnerTonite").jcarousel({
			itemVisibleInCallback: { 
				onBeforeAnimation: function(carousel, li, index, state) {
					--index;
					module.onCarouselItemVisible(module, carousel, li, index, state);
				}
			},
			scroll: 1,
			wrap: "circular"
		});
		
		$("img", controls).css("display", "inline");
    };

    DinnerToniteAndCleanEatingLoves.prototype.prev = function () {
        var currentIndex = this._dinnerIndex,
            newIndex = (currentIndex > 0) ? currentIndex - 1 : this._dinners.length - 1;
        this._dinnerIndex = newIndex;
        this._showCurrent();
    };

    DinnerToniteAndCleanEatingLoves.prototype.next = function () {
        var currentIndex = this._dinnerIndex,
            newIndex = (currentIndex < this._dinners.length - 1) ? currentIndex + 1 : 0;
        this._dinnerIndex = newIndex;
        this._showCurrent();
    };

    DinnerToniteAndCleanEatingLoves.prototype._showCurrent = function () {
        var index = this._dinnerIndex,
            dinner = this._dinners[index],
            imageSrc = (dinner && dinner.SquaredImage) ? dinner.SquaredImage.URL : Agility.ResolveUrl("~/img/blank.gif"),
            date = new Date();
        
        if (!dinner) return;

        while(date.getDay() != index) {
            date.addDays(-1);
        }
        this._getChild("Background").css("background-image", "url(" + imageSrc + ")");
        this._getChild("Day").text(date.toString("dddd").toUpperCase());
        this._getChild("Link").attr("href", Agility.ResolveUrl(dinner.AppRelativeUrl)).html(dinner.Title);
    };

    DinnerToniteAndCleanEatingLoves.prototype.onCarouselItemVisible = function (module, carousel, li, index, state) {
		var length = module._dinners.length,
			index = (index < 0) ? index + (Math.abs(index) * length) : index,
			newIndex = (module._dinnerIndex + index) % length;
			
		module._showItem(li, newIndex);
	};

    DinnerToniteAndCleanEatingLoves.prototype._showItem = function (container, index) {
        var dinner = this._dinners[index],
            imageSrc = (dinner && dinner.SquaredImage) ? dinner.SquaredImage.URL : Agility.ResolveUrl("~/img/blank.gif"),
            date = new Date();
			
        if (!dinner) return;

        while(date.getDay() != index) {
            date.addDays(-1);
        }
		$(".Background", container).css("background-image", "url(" + imageSrc + ")");
        $(".Day", container).text(date.toString("dddd").toUpperCase());
        $(".Link", container).attr("href", Agility.ResolveUrl(dinner.AppRelativeUrl)).html(dinner.Title);
    };

}());
