var SlideShow = Class.create({
  initialize: function(id,options) {
  	
  	// Declare member variables for class
    this.id = id;
    this.tabs = $(id).down('.slider_controls').identify();
    this.slider = $(id).down('.slider').identify();
	this.width = options['width'] ? options['width'] : '900';
	this.height = options['height'] ? options['height'] : '400';	
    this.duration = options['duration'] ? options['duration'] : 3000;
    this.effect = options['effect'] ? options['effect'] :'fade'; //slide, fade
    this.activeFeature = 1;
	this.totalFeatures = $(this.slider).down().childElements().size();
	this.position = 0;
	this.listWidth = this.width * this.totalFeatures;
	this.timer = null;
	this.lock = false;
	
	// Mousever effect for stopping slide show
	var t = this; // defines the class object to be referenced when scope changes
	Event.observe(this.id,'mouseover', function(){t.stopTimer()});
	Event.observe(this.id,'mouseout', function(){t.startTimer()});
	
	//
	t = this;
	i = 1;
	j = 1;
	$(this.tabs).down('ul').childElements().each(
		function (s){
			s.setAttribute('id', t.id + '_tab_' + i );
			Event.observe(s,'mouseover',prepLoadItem(i));
			i++;
		}
	);
	$(this.slider).down('ul').childElements().each(
		function (s){
			if(j != 1) s.setStyle('display: none');
			s.setAttribute('id', t.id + '_' + j);
			s.setStyle('z-index: 100');
			j++;
		}
	);
	function prepLoadItem(id) {
    	return function() {
        	t.switchTo(id);
    	};
	}
	// Start the loop
	this.startTimer();
	
	// Execute the script
	this.run();	
	},
	run: function() {
		
	},
	fadeTo: function(nextElementIndex){
		if(this.lock == false){
			if(nextElementIndex != this.activeFeature){
				$(this.id +'_tab_' + this.activeFeature).removeClassName('active');
				$(this.id +'_tab_' + nextElementIndex).addClassName('active');
				currentElement = this.id + '_' + this.activeFeature;
				nextElement = this.id + '_' + nextElementIndex;
				topPosition = $(currentElement).getStyle('z-index');
				var t = this;
				//alert(topPosition);
				$(nextElement).setStyle('z-index:'+(topPosition-1));
				$(nextElement).show();
				$(currentElement).fade({
					duration: .5,
					afterFinish: function(){
						$(nextElement).setStyle('z-index:'+(topPosition));
					} 
				});
			this.activeFeature = nextElementIndex;
			}

		}
	},
	switchTo: function(nextElementIndex){
		if(this.lock == false){
			if(nextElementIndex != this.activeFeature){
				this.lock = true;
				$(this.id +'_tab_' + this.activeFeature).removeClassName('active');
				$(this.id +'_tab_' + nextElementIndex).addClassName('active');
				currentElement = this.id + '_' + this.activeFeature;
				nextElement = this.id + '_' + nextElementIndex;
				topPosition = $(currentElement).getStyle('z-index');
				var t = this;
				//alert(topPosition);
				$(nextElement).setStyle('z-index:'+(topPosition-1));
				$(nextElement).show();
				$(currentElement).fade({
					duration: .01,
					afterFinish: function(){
						$(nextElement).setStyle('z-index:'+(topPosition));
						t.lock = false;
					} 
				});
			this.activeFeature = nextElementIndex;
			}

		}
	},
  startTimer: function(){
  	var t = this;
	this.timer = setInterval(
		function(){
			if(t.activeFeature >= t.totalFeatures){
				next = 1;
			}
			else{
				next = t.activeFeature + 1;
			}
			t.fadeTo(next);
		},
		this.duration
	);
  },
  stopTimer: function(){
  	this.timer=window.clearInterval(this.timer)
  }
	
});
