/**************************************************************
	Script		: Sticky
	Version		: 1.0
	Authors		: Wim Smits
	Desc		: a Sticky Class for controlling sticky slides.
	Licence		: property of Red & Ivory
**************************************************************/

var StickyList = new Class({

	//implements
	Implements: Options,	
	
	//variables setup
	numNav: new Array(),
	timer: null,				
	isSliding: 0,				
	
	origColor: null,			

	//options
	options: {
		slideTimer: 5000,  		
		isPaused: 0,
		horizontal: 0,//0 = vertical scroll, 1 = horizontal scroll
		direction: -1,//1 = right to left or bottom to top, -1 = left to right or top to bottom
		transitionTime: 1000, 			
		items:  '.sitem', 				
		itemNum: 0,
		hasControls:false,
		numNavActive: false
	},

	//initialization
	initialize: function(list, options) {
		this.setOptions(options);
		this.container = list;
		this.items = $$(this.options.items);
		this.width = this.container.getSize().x;
		this.height = this.container.getSize().y;
		
		this.container.setStyle('overflow', "hidden");
		if(this.items.length > 1){
			this.items.each(function(el, i){
				el.setStyle('position', "absolute");
				var elw = el.getSize().x;
				var elh = el.getSize().y
				if(this.options.horizontal) el.setStyle('left', (this.options.direction * elw));
				else el.setStyle('top', (this.options.direction * elh));
			 }, this);
			
			this.start();
		}
		this.items.each(function(el, i){
			el.setStyle('visibility', 'visible');
		});
		this.container.addEvents({
			'mouseenter': this.activateIt.bind(this),				
			'mouseleave': this.deactivateIt.bind(this)
		});
	},

	start: function() {
		this.slideIt(this.options.itemNum);
		this.theTimer = this.slideIt.periodical(this.options.slideTimer, this, null);
	},
	
	activateIt: function(){
		this.container.addClass('hover');
		this.pauseIt();
	},
	
	deactivateIt: function(){
		this.container.removeClass('hover');
		this.pauseIt();
	},

	slideIt: function(passedID) {
		
		var curItem = this.items[this.options.itemNum]; 
		
		if(this.options.numNavActive){
			var curNumItem =  this.numNav[this.options.itemNum];
		}
		
		if(this.options.horizontal) var curX = this.options.direction * this.width;
		else var curX = this.options.direction * this.height;
		
		this.changeIndex();
		
		//check for passedID presence
		if(passedID != null) {
			if(this.options.itemNum != passedID){
				this.options.itemNum = passedID;
			}
		}
		
		//now get item to slide in using new index
		var newItem = this.items[this.options.itemNum];
		
		if(this.options.numNavActive){
			var newNumItem =  this.numNav[this.options.itemNum];
		}
		
		if(this.options.horizontal) var newX = (this.options.direction * newItem.getSize().x);
		else var newX = (this.options.direction * newItem.getSize().y);
		
		//set up our animation stylings
		var item_in = new Fx.Morph(newItem, {
		     duration: this.options.transitionTime, 
		     transition: 'cubic:inOut', 
		     link: 'ignore',
		     
		     onStart: this.toggleSlidingOn.create({
				bind: this
			}),
		     
		     onComplete: this.toggleSlidingOff.create({
				bind: this
			})
		     
		});
		
		//add/remove active number's highlight
		if(this.options.numNavActive){
			newNumItem.addClass('active');
		}
		if(this.options.horizontal){
			var inopt = {'opacity':[0,1],'left' : [newX, 0]};
			var outopt = {'opacity':[0],'left' : [(curX)]};
		}
		else {
			var inopt = {'opacity':[0,1],'top' : [newX, 0]};
			var outopt = {'opacity':[0],'top' : [(curX)]};
		}
		
		item_in.start(inopt);
		
		if(curItem != newItem){
			var item_out = new Fx.Morph(curItem, {
				duration: this.options.transitionTime, 
				transition: 'cubic:inOut', 
				link: 'ignore'
			});
			
			if(this.options.numNavActive){
				curNumItem.removeClass('active');
			}
			
			item_out.start(outopt);
		}
	
	},

	pauseIt: function () {
		
		//only move if not currently moving
		if(this.isSliding == 0){
			if(this.options.isPaused == 0){
				this.options.isPaused = 1;
				$clear(this.theTimer);
			}
			else{
				this.options.isPaused = 0;
				this.slideIt();
				this.theTimer = this.slideIt.periodical(this.options.slideTimer, this, null); 
			}
		} //end if not sliding
		
	},
	
	changeIndex: function() {
		var numItems = this.items.length;  //get number of slider items
		
		//change index based on value of 'direction' parameter
		if(this.options.direction == 1){
			if(this.options.itemNum < (numItems - 1)){
				this.options.itemNum++; 
			}
			else{
				this.options.itemNum = 0;
			}
		}
		else if(this.options.direction == -1){
			if(this.options.itemNum > 0){
				this.options.itemNum--; 
			}
			else{
				this.options.itemNum = (numItems - 1);
			}
		}
		
	},
	
	numPress: function (e, theIndex) {
		
		if((this.isSliding == 0) && (this.options.itemNum != theIndex)){
			if(this.options.isPaused == 0){
				$clear(this.theTimer);
				this.theTimer = this.slideIt.periodical(this.options.slideTimer, this, null);
			}
			this.slideIt(theIndex);
		}
	
	},
	
	toggleSlidingOn: function () {
		this.isSliding = 1;  //prevents extra clicks
	},
	
	toggleSlidingOff: function () {
		this.isSliding = 0;  //prevents extra clicks
	}
});
