var scroller = {
    height : 150,
    width : 278,
    scrollerDiv : 'testimonialNugget',
    rows : '',
    position : 0,
    jump: 1, //higher number is faster
    speed: 50,
    item: 1,
    items: 0,
    heights: [],
    top: 0,
    scroll: function() {
        if(this.items == 0) {
            this.items = $(this.scrollerDiv+'ItemContainer').childNodes.length;
        }
        if(this.heights[this.item] == undefined) {
            this.heights[this.item] = $('scrollerItem_'+this.item).getHeight();
            
        }
        
        this.top -= this.jump;
        //console.log(Math.abs(this.top) +" - "+ this.heights[this.item]+" - "+this.item);
        if(Math.abs(this.top) > this.heights[this.item]) {
            

            //$('scrollerItem_'+this.item).remove();
            var length = $(this.scrollerDiv+'ItemContainer').childElements().length-1;
            //console.log($(this.scrollerDiv+'ItemContainer').childNodes[length]);
            ($(this.scrollerDiv+'ItemContainer').childElements()[length]).insert({
                after: $('scrollerItem_'+this.item)
            });
            this.item ++;
            if(this.item > this.items) {
                this.item = 1;
            }
            this.top = 40;

        }
        
        
        $(this.scrollerDiv+'ItemContainer').setStyle({
            top:this.top+"px"
            });

        
        setTimeout(this.scroll.bind(this),this.speed);
        
    }
}


function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}

document.observe( "dom:loaded", function() {
    if($(scroller.scrollerDiv)) {
        var get = getUrlVars();
        new Ajax.Request('/main/includes/xhr_scroller.php', {
            method:     'POST',
            parameters: 'scrollerDiv='+scroller.scrollerDiv+'&height='+scroller.height+'&width='+scroller.width+'&products_id='+get['products_id'],
            onSuccess: function(t) {
                var info = t.responseText.evalJSON();
                $(scroller.scrollerDiv).update(info.output);
                /*$(scroller.scrollerDiv).addClassName("scrollerBody");*/
                $(scroller.scrollerDiv).setStyle({
                    'width':scroller.width+'px',
                    'height':scroller.height+'px'
                });
                
                scroller.rows = info.rows;
                
                scroller.scroll();

            }
        });

        
    }
    
});
