/**************************************************************
	Script		: Switcher
	Version		: 1.0
	Authors		: Wim Smits
	Desc		: a Sticky Class for controlling sticky slides.
	Licence		: property of Red & Ivory
**************************************************************/

var Switcher = new Class({

	//implements
	Implements: Options,	
	
	//variables setup
	numNav: new Array(),
	timer: null,				
	isSliding: 0,				
	
	origColor: null,			

	//options
	options: {
		autoloop: false,
		animate: false,
		slideTimer: 8000,  		
		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:  '.featured', 				
		itemNum: 0,
		hasControls:false,
		numNavActive: false
	},

	//initialization
	initialize: function(list, options) {
		alert('init');
		this.setOptions(options);
		this.container = list;
		this.items = $$(this.options.items);
		this.width = this.container.getSize().x;
		this.height = this.container.getSize().y;
		
		this.resetStyles = {};
		this.active = 0;
		this.paused = false;
		
		this.container.setStyle('overflow', "hidden");
		this.switchFx = new Fx.Elements(this.items, {duration: this.options.transitionTime, transition: Fx.Transitions.Cubic.easeOut, wait:false});
		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) {
					if(i > 0)el.setStyle('left', (this.options.direction * elw));
					this.resetStyles[''+i+''] = {'left': this.options.direction * elw};
				}
				else {
					if(i > 0)el.setStyle('top', (this.options.direction * elh));
					this.resetStyles[''+i+''] = {'top': this.options.direction * elh};
				}
				if(this.options.autoloop){
					el.addEvents({
						'mouseenter': this.pauseIt.bind(this),				
						'mouseleave': this.pauseIt.bind(this)
					});
				}
			 }, this);
			
		}
		this.items.each(function(el, i){
			el.setStyle('visibility', 'visible');
		});
		this.container.setStyle('visibility', 'visible');
		this.start();
	},

	start: function() {
		if(this.items.length > 1) {
			this.slideIt(this.active);
			if(this.options.autoloop) this.theTimer = this.nextSticky.periodical(this.options.slideTimer, this, null);
		}
	},
	
	nextSticky: function(){
		this.slideIt((this.active+1)%this.items.length);
	},


	slideIt: function(passedID){
		if(this.options.animate) this.switchFx.start(this.getStyles(passedID));
		else this.switchFx.set(this.getStyles(passedID));
		
		this.active = passedID;
	},

	pauseIt: function(){
		if(this.options.autoloop){
			if(this.paused){//unpause
				this.theTimer = this.nextSticky.periodical(this.options.slideTimer, this, null);
				this.paused = false;
			} else {
				$clear(this.theTimer);
				this.paused = true;
			}
		}
		
	},
	
	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
	},
	
	getStyles: function(nb){
		var ow = clone(this.resetStyles);
		if(this.options.horizontal) ow[''+(nb)+''] = {'left': 0};
		else ow[''+(nb)+''] = {'top': 0};
		return ow;
	},
});