﻿
var iCarousel = (function () {
    var defaults = {
        pillarSel: "h3",
        slideSel: "p",
        align: "right",
        animTime: 1000,
        holdTime: 3000
    };
    return {
        c: {
            slides: {},
            pillars: {},
            currentActive: 1
        }, /* Element Cache */
        pWidth: 0,
        sWidth: 0,
        count: 0,
        currIdx: 0,
        config: {},
        init: function (container, params) {
            this.config = $.extend(true, {}, defaults, params);

            // Populating Element Cache
            var container = (typeof container == "undefined") ? $("#bannerNew") : $(container);
            var p = this.c.pillars = container.find("h3");
            var s = this.c.slides = container.find("p");

            // Initializing variables
            this.count = p.length;
            this.pWidth = p.eq(0).width();
            this.sWidth = s.eq(0).width();

            // A little housekeeping
            for (var x = 0; x < this.count; x++) {
                p.eq(x).css("right", (this.count - x - 1) * this.pWidth).data("pos", "right");
            }
            for (var x = 0; x < this.count; x++) {
                s.eq(x)
                            .css({ right: p.eq(x).css("right"), zIndex: this.count - x })
                            .data("pos", "right")
                            .append($("<div />").addClass("curtain").fadeTo(0, 0.5));
            }
            p.eq(0).css("visibility", "hidden");

            // Bind Events
            $(window).load(function () { container.css("visibility", "visible"); $(".bannerLoader").hide(); });
            this.bindEvents();

            //this.auto();
        },
        bindEvents: function () {
            pi = iCarousel.c.pillars
            pi.bind("click", function (e) {
                if (pi.is(":animated")) {
                    e.preventDefault();
                    e.stopPropagation();
                    return false;
                }
                iCarousel.c.currentActive = $.inArray(this, pi) + 1;
                if (iCarousel.c.currentActive > pi.length - 1) { iCarousel.c.currentActive = 0 }
                iCarousel.open(this);
            });
        },
        open: function (el) {
            var p = this.c.pillars;
            var s = this.c.slides;
            var newIdx = p.index(el);
            var diff = newIdx - this.currIdx;
            var pFactor = Math.abs(diff) / diff;
            var sFactor = (pFactor > 0) ? -1 : 0;

            var pList = p.filter(function (i) {
                return ((i >= iCarousel.currIdx && i < newIdx && pFactor > 0) || (i < iCarousel.currIdx && i >= newIdx && pFactor < 0));
            });
            var sList = s.filter(function (i) {
                return ((i >= iCarousel.currIdx && i < newIdx && pFactor > 0) || (i < iCarousel.currIdx && i >= newIdx && pFactor < 0));
            });

            $(el).css("visibility", "hidden");
            s.find(".curtain").fadeIn();

            var num = pList.length;
            sList.add(pList).animate({
                right: "+=" + ((this.sWidth - this.pWidth) * pFactor)
            }, 750, "easeInOutExpo", function () {
                if ($(this).is("h3") && --num == 0) {
                    if (pFactor > 0) {
                        pList.filter(":first").css({ visibility: "visible" });
                    } else {
                        pList.filter(":first").css({ visibility: "hidden" });
                        p.eq(iCarousel.currIdx).css({ visibility: "visible" });
                    }
                    s.find(".curtain").fadeOut();
                    iCarousel.currIdx = newIdx;
                }
            });
        }
    };
})();

$(function () {
    iCarousel.init();
});
