﻿/// <reference path="../Intellisense.js" />

Agility.RegisterNamespace("CleanEating.Modules");

(function () {

    var WhatsFreshAndPeople = CleanEating.Modules.WhatsFreshAndPeople = function (containerID, moduleProperties) {
        this._containerID = containerID;
        this._whatsFreshItems = moduleProperties.WhatsFreshArticleItems;
        this._peopleItems = moduleProperties.PeopleArticleItems;
        this._whatsFreshIndex = 0;
        this._peopleIndex = 0;
    };

    WhatsFreshAndPeople.prototype._getChild = function (idSuffix) {
        return $("#" + this._containerID + "_" + idSuffix);
    };

    WhatsFreshAndPeople.prototype.init = function () {
        var whatsFreshControls = this._getChild("WhatsFreshControls"),
            peopleControls = this._getChild("PeopleControls"),
            module = this;

		this._getChild("WhatsFresh").jcarousel({
			scroll: 2,
			wrap: "circular"
		});
        
        if (this._peopleItems.length > 1) {
            this._getChild("People").jcarousel({
                scroll: 1,
                wrap: "circular"
            });
        }
        
        if (this._whatsFreshItems.length > 2) {
            whatsFreshControls.show();
			$("img", whatsFreshControls).css("display", "inline");
        }
        if (this._peopleItems.length > 1) {
            peopleControls.show();
			$("img", peopleControls).css("display", "inline");
        }
    };

    WhatsFreshAndPeople.prototype.whatsFreshNext = function () {
        var count = this._whatsFreshItems.length,
            firstIndex = this._fixOverflow(this._whatsFreshIndex + 2, count),
            secondIndex = this._fixOverflow(firstIndex + 1, count);

        this._showItem(this._getChild("WhatsFreshItem1"), this._whatsFreshItems[firstIndex]);
        this._showItem(this._getChild("WhatsFreshItem2"), this._whatsFreshItems[secondIndex]);
        this._whatsFreshIndex = firstIndex;
    };

    WhatsFreshAndPeople.prototype.whatsFreshPrev = function () {
        var count = this._whatsFreshItems.length,
            firstIndex = this._fixOverflow(this._whatsFreshIndex - 2, count),
            secondIndex = this._fixOverflow(firstIndex - 1, count);

        this._showItem(this._getChild("WhatsFreshItem1"), this._whatsFreshItems[firstIndex]);
        this._showItem(this._getChild("WhatsFreshItem2"), this._whatsFreshItems[secondIndex]);
        this._whatsFreshIndex = firstIndex;
    };

    WhatsFreshAndPeople.prototype.peopleNext = function () {
        var count = this._peopleItems.length,
            newIndex = this._fixOverflow(this._peopleIndex + 1, count);
        this._showItem(this._getChild("PeopleItem"), this._peopleItems[newIndex]);
        this._peopleIndex = newIndex;
    };

    WhatsFreshAndPeople.prototype.peoplePrev = function () {
        var count = this._peopleItems.length,
            newIndex = this._fixOverflow(this._peopleIndex - 1, count);
        this._showItem(this._getChild("PeopleItem"), this._peopleItems[newIndex]);
        this._peopleIndex = newIndex;
    };

    WhatsFreshAndPeople.prototype._showItem = function (container, item) {
        var url = Agility.ResolveUrl(item.AppRelativeUrl),
            imageSrc = (item.SquaredImage) ? item.SquaredImage.URL : Agility.ResolveUrl("~/img/blank.gif");

        container.find(".ItemTitle").html(item.Title).attr("href", url);
        container.find(".Image").attr("href", url).find("img").attr("src", imageSrc);
    };

    WhatsFreshAndPeople.prototype._fixOverflow = function (index, count) {
        if (index >= count) {
            index = index - count;
        } else if (index < 0) {
            index = count + index;
        }
        return index;
    };

}());
