var AjaxDisplay = new Class ({

	Implements: [Events, Options],

	options: {
/*		onStart: $empty,				//The function to be fired at the beginning of the animation
		onSlideStart: $empty,			//The function to be fired before each slide
		onSlideComplete: $empty,		//The function to be fired after each slide		
		onStop: $empty, */				//The function to be fired after the end of the animation		
		loadEvent: 'click',				//The event that initiates the change
		activeClass: null,				//The active class of the link for which information is being shown
		onRequestMessage: 'Loading...', //If a delay occurs on the request, this message will show
		autocycle: false,				//Cycle through each of the links
		cycleOrder: 'forward',			//The order the cycle will go through - forward or reverse
		timer: 5000,					//The time period between each cycle in milliseconds
		startLinkNumber: 0,				//The number of the link that is active first
		previousButtonId: null,			//The id of the previous button
		nextButtonId: null,				//The id of the next button
		playButtonId: null,				//The id of the play button
		stopButtonId: null				//The id of the stop button
	},

	initialize: function(linkClass, targetId, options){
		this.setOptions(options);
		this.target = $(targetId);
		this.linkClass = linkClass;
		this.numberLinks = ($$('.'+linkClass).length - 1).toInt();		
		this.currentLink = 0;
		this.targetCache = [];

		$$('.'+linkClass).each(function(item, index){
			this.preload(item.get('rel'), index);
		}, this);

		$$('.'+linkClass).each(function(item){
			item.addEvent(this.options.loadEvent, function(e){
				e.stop();
				this.stopCycle();
				this.resetActive();
				this.makeActive(item);
		   }.bind(this));
		}, this);
		
		this.makeActive($$('.'+linkClass)[this.options.startLinkNumber]);
		
		if (this.options.autocycle) { this.playCycle(this.options.cycleOrder); }
		if (this.options.previousButtonId) { 
			$(this.options.previousButtonId).addEvent('click', function(e){
				e.stop();
				this.previous();		
			}.bind(this));
		}
		if (this.options.nextButtonId) { 
			$(this.options.nextButtonId).addEvent('click', function(e){
				e.stop();
				this.next();		
			}.bind(this));
		}
		if (this.options.playButtonId) { 
			$(this.options.playButtonId).addEvent('click', function(e){
				e.stop();
				this.playCycle(this.options.cycleOrder);		
			}.bind(this));
		}
		if (this.options.stopButtonId) { 
			$(this.options.stopButtonId).addEvent('click', function(e){
				e.stop();
				this.stopCycle();		
			}.bind(this));
		}		
	},
	
	resetActive: function(){
		if (this.options.activeClass){ $$('.'+this.options.activeClass).removeClass(this.options.activeClass); }
	},
	
	makeActive: function(activeItem){
		this.currentLink = $$('.'+this.linkClass).indexOf(activeItem).toInt();
		if (this.options.activeClass){ activeItem.addClass(this.options.activeClass); }
		this.ajaxLoad(activeItem.get('rel'));
	},

	preload: function(lookupUrl, slideNumber){
		var req = new Request.HTML({
			url: lookupUrl,  								   
			onSuccess: function(slideTree, slideElements, slideHTML){
				this.targetCache[slideNumber] = slideHTML;
			}.bind(this)
		}).send();	
	},

	ajaxLoad: function(lookupUrl){
		this.fireEvent('slideStart');
		if (this.targetCache[this.currentLink]) {
			this.target.set('html', this.targetCache[this.currentLink]);
			this.fireEvent('slideComplete');
		} else {
			var req = new Request.HTML({
				url: lookupUrl,  								   
				onRequest: function() { this.target.setProperty('html', this.options.onRequestMessage); }.bind(this),
				onSuccess: function() { 
					this.targetCache[this.currentLink] = this.target.get('html');
					this.fireEvent('slideComplete');
				}.bind(this),
				update: this.target  
			}).send();
		}
	},
	
	update: function(linkNumber){
		this.resetActive();
		this.makeActive($$('.'+this.linkClass)[linkNumber]); 
	},
	
	setActive: function(linkNumber){ //Public 
		this.resetActive();
		this.makeActive($$('.'+this.linkClass)[linkNumber]);
	}, 
	
	stopCycle: function(){ //Public
		$clear(this.autoplay);
		this.fireEvent('stop');
	},
	
	playCycle: function(order){ //Public
		this.fireEvent('start');
		if (order == 'forward') {
			this.autoplay = this.next.periodical(this.options.timer, this);
		} else if (order == 'reverse') {
			this.autoplay = this.previous.periodical(this.options.timer, this);
		}
	},
	
	previous: function(){ //Public
		var linkNumber = (this.currentLink > 0) ? this.currentLink - 1 : this.numberLinks;
		this.update(linkNumber);	
	},
	
	next: function(){ //Public
		var linkNumber = (this.currentLink < this.numberLinks) ? this.currentLink + 1 : 0;
		this.update(linkNumber);
	}
});
//*
window.addEvent('domready', function(){
	if ($('main').hasChild($('slideshow'))){
		refreshTips();
		var carousel = new AjaxDisplay('slideshowPageLink', 'slideshowContent', {
			activeClass: 'active', 
			autocycle: true,
			timer: 7000,
			onSlideComplete: refreshTips
		});
	}
	if ($('main').hasChild($('productShow'))){
		refreshTips();
		var carousel = new AjaxDisplay('productShowPageLink', 'productShowContent', {
			activeClass: 'active', 
			autocycle: true,
			timer: 7000,
			onSlideComplete: refreshTips
		});
	}	
});
//*/