/*
 * KoldCast Featured Scroller Class
 * @author Sean Hsieh (envsn.com)
 *
 */

var LandingScroller = Class.create({
    
    holder: null,
    itemWidth: null,
    position: 1,
    queue: Effect.Queues.get('LanderScroller'),
    bookmarks: null,
    interval: null,
    lastMove: null,
   
    initialize: function(holder, itemWidth, bookmarks, interval) {
        this.holder = $(holder);
        this.itemWidth = itemWidth;
        this.position = 1;
        this.interval = interval;
        this.bookmarks = $(bookmarks).childElements();
        this.lastMove = Math.round(new Date().getTime() / 1000);
        
        this.holder.setStyle({'width': this.itemWidth*this.bookmarks.size() + 'px'});
        $('LandingTitle').innerHTML = $$('li .pageTitle')[0].innerHTML;
    },
    
    periodicalNext: function(obj) {
        if ((Math.round(new Date().getTime() / 1000) - obj.lastMove) >= obj.interval) {
            obj.scrollNext(obj);
        }
    },

    scrollNext: function(obj) {
        //landerInterval.stop();
        if (obj.queue.size() < 1) {
            if (obj.position < obj.bookmarks.size()) {
                obj.slideDelta(obj, obj.position+1);
            }
            else {
                obj.slideDelta(obj, 1);
            }
        }
    },

    scrollTo: function(e, obj, number) {
        e.stop();
        obj.slideDelta(obj, number);
    },
    
    slideDelta: function(obj, number) {
        var delta = (number - obj.position) / -1;
        new Effect.Move(obj.holder, {
            x: delta * obj.itemWidth,
            y: 0,
            queue: {
                position: 'end',
                scope: 'LanderScroller'
            }
        });
        obj.bookmarks[obj.position-1].removeClassName('active');
        obj.position = number;
        obj.bookmarks[number-1].addClassName('active');
        obj.lastMove = Math.round(new Date().getTime() / 1000);
        $('LandingTitle').innerHTML = $$('li .pageTitle')[number-1].innerHTML;
    }
});


/* Landing Page Requirements */
var landerObj;
var landerInterval = null;  // will store the landing page timer a global scope

function LoadLandingItems() {
    var myAjax = new Ajax.Request('/landingpage/heroitems/', {
        method: 'get',
        onSuccess: function(req) {
            $$('#LandingScroller .main')[0].innerHTML = req.responseText;
            if ($('LandingScrollerItems')) {
                landerObj = new LandingScroller('LandingScrollerItems', 935, 'bookmarks', 10);
                for (i=0;i < landerObj.bookmarks.size(); i++) {
                    landerObj.bookmarks[i].observe('click', landerObj.scrollTo.bindAsEventListener(this, landerObj, i+1));
                }
                
                landerInterval = new PeriodicalExecuter(function(pe) {
                    landerObj.periodicalNext(landerObj);
                }, 10);
                
            }
        }
    });
}
