// JavaScript Document
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-10240841-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
  
jLib.extend({
  scrolls : function(selectors, jsonSettings){
		if(!selectors)return false;
    
    var settings = {speed:1, stopTime:8, overhead:0, button:{stop:null, play:null}};
    if(jsonSettings && jLib.isObject(jsonSettings))jLib.extend(settings, jsonSettings);
    
    if(jLib.isString(selectors))selectors = [selectors];
    
    selectors.Each(function(div, i){
      div = jLib.extend(jLib(div), {
        s1: 0,
        s2: 0,
        counter: 1,
        timer1: null,
        timer2: null,
        ul: jLib(div).getElementsByTagName('ul'),
        len: jLib(div).getElementsByTagName('ul').length,
        scrollNext:function(){
          var self = this;
          
          if(this.scrollTop > this.s2){
            clearInterval(this.timer2);
            this.timer2 = null;
            this.scrollTop = this.s2;
            this.timer1 = setTimeout(function(){self.startScroll()}, (settings.stopTime*1000));
          }else this.scrollTop += settings.speed;
        },
        startScroll: function(){
					var self = this;
          
					if(this.counter > this.len){
            this.counter = 1;
            this.s1 = this.s2 = this.scrollTop = 0;
            self.startScroll();
            return;
          };
          
          this.s1 = this.scrollTop;
          this.s2 += (this.ul[this.counter-1].offsetHeight+settings.overhead);
          this.counter++;
          
          this.timer2 = setInterval(function(){self.scrollNext()}, 20);
        }
      });
      
			if(div.scrollHeight<=0)return;
			
			if(settings.button.stop){
				jLib(settings.button.stop).addEvent('click', function(e){
					clearTimeout(div.timer1);
          clearInterval(div.timer2);
          div.timer1 = div.timer2 = null;
				});
			};
			
			if(settings.button.play){
				jLib(settings.button.play).addEvent('click', function(e){
					if(!div.timer1 && !div.timer2)div.timer2 = setInterval(function(){div.scrollNext()}, 20);
				});
			};
      
      //repeat trick
      for(var h=0, i=0, l=div.len; i<l; i++){
        h += div.ul[i].offsetHeight;
        if(h>div.getHeight()){
          for(h=0; h<=i; h++)div.append(jLib(div.ul[h]).clone(true));          
          break;
        };
      };
      
      div.timer1 = setTimeout(function(){div.startScroll()}, (settings.stopTime*1000));
    });
  }
});

$(window).addEvent('load', function(){
  //$.scrolls('bbc testimonials'.split(' '), {speed:2, overhead:15});
	$.scrolls('bbc', {speed:2, stopTime:5, overhead:15, button:{stop:'pausebbc', play:'playbbc'}});
	$.scrolls('testimonials', {speed:2, stopTime:8, overhead:15, button:{stop:'pausetestimonials', play:'playtestimonials'}});
});
