/**
 * File:    SFC.Home.js
 * Author:  RP =)
 */

/**
 * SFC.Home class handles all stuff regarding the homepage.
 */
SFC.Home = (function() {

    return {
        generateRotatingImages: function(container, imageContainer, timeout, start) {
            var that = this;
            var elements = $(imageContainer).children();
            that.currentFocalIndex = start || 0;
            that.timeout = timeout || 5000;
            that.nextChange = (new Date).getTime() + that.timeout;

            for (var i = 0; i < elements.length; i++)
            {
                $(elements[i]).css('z-index', String(elements.length - i)).css('position', 'absolute').hide();
            }

            that.generateRotatingImagesPager(container, elements, that.currentFocalIndex);

            if (!that.focalStarted) {
                that.focalStarted = true;
                SFC.Container.bindHoverToPointer(container);
                window.setInterval(function() {
                    if ((new Date).getTime() > that.nextChange) {
                        that.nextChange = (new Date).getTime() + that.timeout;
                        that.rotateNext(elements, ((that.currentFocalIndex+1)%elements.length));
                    }
                }, 20);
                $(elements[0]).show();
            }
        },

        rotateNext: function(elements, current) {
            var that = this;
            var last = that.currentFocalIndex;
            that.currentFocalIndex = current;
            that.nextChange = (new Date).getTime() + that.timeout;

            $(elements[last]).fadeOut();
            that.setSelectedRotatingImagePager(current);
            $(elements[current]).fadeIn("slow", function(){
                that.removeFilter($(this)[0]);
            });
        },

        removeFilter: function (element) {
            if (element.style.removeAttribute)
            {
                element.style.removeAttribute('filter');
            }
        },

        generateRotatingImagesPager: function(container, elements, selected) {
            var that = this;
            var itemList = $('<ul/>');
            for (var i=0; i<elements.length; i++) {
                var item = $('<li class="focalPage"></li>').text(i+1);
                if (i == selected) {
                    item.addClass("selected");
                }
                SFC.Container.bindHoverToPointer(item);
                item.attr('ordinal', i);
                item.click(function(e){
                    var s = $(this).attr('ordinal');
                    that.rotateNext(elements, s);
                    SFC.Container.bindHoverToPointer(container, 'default');
                    e.preventDefault();
                });
                itemList.append(item);
            }
            container.append($('<div class="focalPageContainer">').append(itemList));
        },

        setSelectedRotatingImagePager: function(selected) {
            $("div.focalPageContainer li.focalPage").removeClass("selected");
            $("div.focalPageContainer li.focalPage:eq("+selected+")").addClass("selected");
        }
    };
}());