/**
 * Scroll3r 
 * @version 0.2 (beta)
 * @author Jason J. Jaeger | greengeckodesign.com
 *	
 changelog:
  JJJ - August 30th 2009 - Added replayDelay which will continue playing after x seconds after a tab has been selected (only if autoPlay is true). Set replayDelay to false to disable.
 **/

var Scroll3r = new Class({
	Implements: Options,
	options: {
        id: 'scroller',
		duration:700,
		physics: Fx.Transitions.Sine.easeOut,
		autoPlay:true,
		delay:5000,
		replayDelay:5000,//amount of time that is waited before playing continues after a tab has been selected
		mode:'horizontal',
		addHashtoURL:false,
		currentClassName:'sCurrent'		
    },
	initialize: function(options){	
        this.setOptions(options);
		document.id(this.options.id).setStyle('overflow','hidden').getElements('a').set('tabindex','-1'); 
		
		var target = document.id(this.options.id);
		
		if(this.options.mode == 'vertical' ){
			target = target.getFirst();
		}
		
		this.scroll = new Fx.Scroll(
			target, {	duration: this.options.duration, transition: this.options.physics,  link: 'cancel' } 
		);
		
		var numOfSlides = 0;
		
		document.id(this.options.id).getElement('ul,ol').getElements('li').filter(function(item, index){ return item.getParent().getParent() == document.id(this.options.id); }.bind(this)).each(function(item,index){
			var anchorId = item.getElement('.anchor').id;
			numOfSlides++;	
			
			//add event handlers to all links to this slide
			document.id(document.body).getElements('a').filter(function(item, index){ return item.getProperty('href') == '#'+anchorId; }).each(function(linkItem,linkIndex){
				linkItem.addEvents({
					'click': function(e){					
						$try(function(){
							e = new Event(e).stop();
							linkItem.blur();
							this.pause();
							this.scrollIt(anchorId);
						}.bind(this));				
					}.bind(this)
				});
			}.bind(this));
		}.bind(this));
		

		document.id('sNext').addEvents({
			'click': function(e){					
				$try(function(){
					e = new Event(e).stop();
					document.id('sNext').blur();
					this.pause();					
					this.next();
				}.bind(this));				
			}.bind(this)
		});
		
		document.id('sPrevious').addEvents({
			'click': function(e){					
				$try(function(){
					e = new Event(e).stop();
					document.id('sPrevious').blur();
					this.pause();
					this.previous();
				}.bind(this));				
			}.bind(this)
		});
		
		if(numOfSlides <2){
			document.id('sPrevious').setStyle('display','none');
			document.id('sNext').setStyle('display','none');
		}
		/*
		document.id('psPlay').addEvents({
			'click': function(e){					
				$try(function(){
					e = new Event(e).stop();
					this.play();
				}.bind(this));				
			}.bind(this)
		});
		
		document.id('psPause').addEvents({
			'click': function(e){					
				$try(function(){
					e = new Event(e).stop();
					this.pause();
				}.bind(this));				
			}.bind(this)
		});
		
*/
		this.current = document.id(this.options.id).getElement('ul,ol').getFirst('li');
		
		this.setSizes();
		window.addEvent('resize', function() {
		    this.setSizes();
			this.scrollIt(this.current.getElement('a.anchor').id,true);
		}.bind(this));
		
		var urlHash = this.getHash();
		if(urlHash && document.id(document.body).getElement('#'+this.options.id+' a#'+urlHash)){
			this.current = document.id(document.body).getElement('#'+this.options.id+' a#'+urlHash).getParent('li');
		}
		
		this.scrollIt(this.current.getElement('a.anchor').id);
		if(this.options.autoPlay){
			this.play();
		}
    },
	
	currentClass:function(anchorId){
		document.id(document.body).getElements('a').removeClass(this.options.currentClassName);
		document.id(document.body).getElements('a').filter(function(item, index){ return item.getProperty('href') == '#'+anchorId; }).each(function(linkItem,linkIndex){
			linkItem.addClass(this.options.currentClassName);
		}.bind(this));
	},
	
	play:function(){
		this.interval = this.next.periodical(this.options.delay,this);
	},
	
	pause:function(){
		$clear(this.interval);
		if(this.options.autoPlay && this.options.replayDelay){
			(function(){
				$clear(this.interval);
				this.play();
			}.bind(this)).delay(this.options.replayDelay)
		}
		
	},
	
	scrollIt:function(anchorId,set){
		this.currentClass(anchorId);
		var itemNum = $$('#'+this.options.id+" li").filter(function(item, index){ return item.getParent().getParent() == document.id(this.options.id); }.bind(this)).indexOf(document.id(anchorId).getParent('li'));
		var height = document.id(anchorId).getParent('li').getSize().y;
		var width = document.id(this.options.id).getSize().x;
		var left = 0;
		var top = height*itemNum;
		if(this.options.mode == 'horizontal'){
			left = width*itemNum;
			top = 0;
		}
		
		if(set){
			this.scroll.set(left, top);
		}else{
			this.scroll.start(left, top).chain(function(){
				if(this.options.addHashtoURL){
					window.location.hash = "#"+anchorId;
				}
			}.bind(this));
		}
		
		this.current = document.id(anchorId).getParent('li');
	},
	
	next:function(){
		var nextLI = this.current.getNext('li') || document.id(this.options.id).getElement('ul,ol').getFirst('li');
		this.scrollIt(nextLI.getElement('a.anchor').id);
	},
	
	previous:function(){
		var previousLI = this.current.getPrevious('li') || document.id(this.options.id).getElement('ul,ol').getLast('li');
		this.scrollIt(previousLI.getElement('a.anchor').id);
	},
	
	getHash:function() {
	  var hash = window.location.hash;
	  hash = hash.substring(1); 
	  if(hash.length > 0){
	  	return hash;
	  }else{
	  	return false;
	  }
	},
	
	setSizes:function(){
		if (this.options.mode!= 'horizontal') {return;}
		
		document.id(this.options.id).getElement('ul,ol').getElements('li').filter(function(item, index){ return item.getParent().getParent() == document.id(this.options.id); }.bind(this)).each(function(item,index){
			var anchorId = item.getElement('.anchor').id;
			//set up styles so slider can work horizontally
			item.setStyles({
				'width':document.id(this.options.id).getSize().x,
				'position':'absolute',
				'left':document.id(this.options.id).getSize().x * index
			});
			
		}.bind(this));
	}
});





