if(!$chk(karw)) {
	var karw = {};
}

karw.SlideShow = new Class({

	options: {
		autoStart: true,
		slideDuration: 5000,
		appearDuration: 2000,
		disappearDuration: 1800,
		useCallout: true,
		onMouseOver: Class.empty,
		onMouseOut: Class.empty,
		onStart: Class.empty,
		onPauze: Class.empty,
		onNext: Class.empty,
		onPrevious: Class.empty,
		onChangedSlide: Class.empty
	},
	
	target: null,
	
	entries: null,
	
	slides: null,
	
	slideDescriptions: null,
	
	currentIndex: 0,
	
	slideTimer: null,
	
	appearFx: null,
	
	disappearFx: null,
	
	width: null,
	
	height: null,
	
	isPlaying: true,
	
	initialize: function(target, options) {
		this.setOptions(options);
		this.target = $(target);
		
		this.setup();
	},
	
	setup: function() {
		
		// Apply styles on container
		this.target.setStyles({
								'overflow': 'hidden',
								'position': 'relative'
							   });

		// Store dimensions
	    this.width = this.target.getSize().x;
	    this.height = this.target.getSize().y;

		// Setup images styles
		var slideShowSlides	= this.target.getElements('li');
		this.slides = [];
		this.slideDescriptions = [];
		
		if(slideShowSlides.length < 2) {
			this.options.autoStart = false;
		}
		
		// Setup list elements
		for(var i=0;i<slideShowSlides.length;i++) {
			
			var slideShowImage = slideShowSlides[i].getElement('img');
			var slideShowDescription = slideShowSlides[i].getElement('span');

			// Check for description element
			if($chk(slideShowDescription)) {

				// Copy text
				this.slideDescriptions[i] = slideShowDescription.get('html');
				
				// Remove element
				slideShowDescription.destroy();

			}
			
			// Style list elements
			slideShowSlides[i].setStyles({
											'display': 'block',
											'position': 'absolute',
											'left': 0,
											'top': 0,
											'opacity': (i == 0) ? 1 : 0
										 });

			this.slides[i] = slideShowSlides[i];
		}		
		
		this.target.addEvent('mouseenter', this.mouseEnterHandler.bindWithEvent(this));
		this.target.addEvent('mouseleave', this.mouseLeaveHandler.bindWithEvent(this));
		
		if(this.options.autoStart) {
			this.startSlideShow();
		}

	},
	
	mouseEnterHandler: function() {
		this.fireEvent('onMouseEnter');
	},
	
	mouseLeaveHandler: function() {
		this.fireEvent('onMouseLeave');	
	},
	
	startSlideShow: function() {
		
		this.isPlaying = true;
		if($chk(this.slideTimer)) {
			this.slideTimer = $clear(this.slideTimer);
		}
		
		if(!this.isPlaying) {
			this.next();
		} else {
			this.slideTimer = this.next.periodical(this.options.slideDuration, this);			
		}

		this.fireEvent('onStart');
		
		
	},
	
	pauzeSlideShow: function() {
		if($chk(this.slideTimer)) {
			this.slideTimer = $clear(this.slideTimer);
			this.cancelFx();
		}
		this.isPlaying = false;
		this.fireEvent('onPauze');
	},
	
	cancelFx: function() {

		this.slideTimer = $clear(this.slideTimer);

		if($chk(this.appearFx)) {
			
			var ato		= this.appearFx.to;
			var aelm	= this.appearFx.element;
			this.appearFx.cancel();

			for(var e in ato) {
				aelm.setStyle(e, ato[e][0].value);
			}
		}
		
		if($chk(this.disappearFx)) {
			
			var dto		= this.disappearFx.to;
			var delm	= this.disappearFx.element;
			this.disappearFx.cancel();
			
			for(var e in dto) {
				delm.setStyle(e, dto[e][0].value);
			}
		}
		
	},
	
	next: function() {


		var targetIndex = ((this.currentIndex + 1) < this.slides.length) ? (this.currentIndex + 1) : 0;
		this.displaySlideAtIndex(targetIndex);
		this.fireEvent('onNext');
	},
	
	previous: function() {

		var targetIndex = ((this.currentIndex - 1) > -1) ? (this.currentIndex - 1) : this.slides.length-1;
		this.displaySlideAtIndex(targetIndex);
		this.fireEvent('onPrevious');	

	},
	
	displaySlideAtIndex: function(index) {
		
		this.cancelFx();

		this.disappearFx = new Fx.Morph(this.slides[this.currentIndex], {duration: this.options.disappearDuration, transition: Fx.Transitions.Sine.easeOut});
		
		this.currentIndex = index;
		this.appearFx = new Fx.Morph(this.slides[this.currentIndex], {duration: this.options.appearDuration, transition: Fx.Transitions.Sine.easeOut});
		
		this.appearFx.start({'opacity':[0, 1]});
		this.disappearFx.start({'opacity': 0});
		
		if(this.isPlaying) {
			this.slideTimer = this.next.periodical(this.options.slideDuration, this);
		}
		
		this.fireEvent('onChangedSlide');
				
	},
	
	getSize: function() {
		return {'x':this.width, 'y':this.height};
	},
	
	getTarget: function() {
		return this.target;
	},
	
	getNumberOfSlides: function() {
		return this.slides.length;
	},
	
	getSlideDescriptions: function() {
		return this.slideDescriptions;
	},
	
	getCurrentIndex: function() {
		return this.currentIndex;
	},
	
	getPosition: function() {
		return this.target.getPosition();
	}
	
});
karw.SlideShow.implement(new Options);
karw.SlideShow.implement(new Events);


karw.mainSlideShow = null;
karw.mainSlideShowController = null;
karw.mainCalloutController = null;

karw.initSlideShow = function() {
	var slideshow= $$('.slideshow-container').pop();
	if (slideshow) {
		karw.mainSlideShow = new karw.SlideShow(slideshow);
		var controller = $$('.slideshow-controller').pop();
		if (controller) {
		karw.mainSlideShowController = new karw.PlaybackController(controller, karw.mainSlideShow);
		karw.mainCalloutController = new karw.CalloutController(karw.mainSlideShow, karw.mainSlideShow.getSlideDescriptions(), karw.mainSlideShowController);
		}
	}
}

window.addEvent('domready', karw.initSlideShow);