Zee = window.Zee || {};

Zee.Slideshow = function(options) {
	var me = this;
	
	me.currentItem = 0;
	
	me.options = $.extend({
		slides: [],
		text: false,
		itemsSelector: '#slide-items',
		navSelector: '#slide-nav',
		slideMethod: 'crossFade',
		timerDuration: 0,
		fadeInDuration: 800,
		fadeOutDuration: 800,
		crossFadeDuration: 1600,
		textFadeInDuration: 800,
		textFadeOutDuration: 800
		
	}, options);
	
	me.slides = me.options.slides;
	
	me.cache = function() {
		me.$items = $(me.options.itemsSelector);
		me.$nav = $(me.options.navSelector);
		me.$text = me.$nav.find('span');
		me.$slideNext = me.$nav.find('.next');
		me.$slidePrev = me.$nav.find('.prev');
	};
	
	me.getCurrentItem = function() {
		return me.slides[me.currentItem];
	};
	
	me.loadSlides = function() {
		for(var i = 0, len = me.slides.length; i < len; i++) {
			me.slides[i].loaded = false;
			me.slides[i].$img = false;
			me.slides[i].txt = me.slides[i].txt || '';
			me.loadSlide(i);
		}
	};
	
	me.loadSlide = function(id, callback) {
		me.slides[id].$img = $('<img/>').bind('load', function() {
			me.slides[id].loaded = true;
			if(id === 0) {
				me.slides[id].$img.prependTo(me.$items);
				me.showSliderNav();
				if(me.options.text) {
					me.$text.html(me.slides[id].txt);
				}
			}
			if(typeof callback === 'function') {
				callback();
			}
		})
		.attr({
			src: me.slides[id].url,
			alt: me.slides[id].txt,
			title: me.slides[id].txt
		});
	};
	
	me.move = function(step) {
		me.resetTimer();
		
		me.currentItem += step;
		me.currentItem = me.currentItem < 0 ? me.slides.length-1 : me.currentItem > me.slides.length-1 ? 0 : me.currentItem;
		
		if(me.options.text) {
			me.fadeTextIn(function() {	
				me.$text.html(me.getCurrentItem().txt);
				me.fadeTextOut();
			});
		}
		me[me.options.slideMethod]();
	};
	
	me.next = function() {
		me.move(1);
	};
	
	me.prev = function() {
		me.move(-1);
	};
	
	me.horizontalSlide = function(callback) {
		me.$items.animate({'margin-left': '-'+me.currentItem*676+'px'}, 500);
	};
	
	me.crossFade = function(callback) {
		me.getCurrentItem().$img
			.css({opacity: 0})
			.detach()
			.appendTo(me.$items)
			.stop(true, true)
			.animate({opacity: 1}, me.options.crossFadeDuration, callback);
	};
	
	me.fadeTextIn = function(callback) {
		me.$text
			.stop(true, true)
			.animate({opacity: 0}, me.options.textFadeInDuration, callback);
	};
	
	me.fadeTextOut = function(callback) {
		me.$text
			.animate({opacity: 1}, me.options.textFadeOutDuration, callback);
	};
	
	me.resetTimer = function() {
		if(me.options.timerDuration) {
			clearTimeout(me.timer);
			me.timer = setTimeout(me.next, me.options.timerDuration);
		}
	};
	
	me.showSliderNav = function() {
		if(me.slides.length > 1) {
			// UI
			me.$slideNext.show().beVisible().bind('click', function(e) {
				e.preventDefault();
				me.next();
			});
			
			me.$slidePrev.show().beVisible().bind('click', function(e) {
				e.preventDefault();
				me.prev();
			});
			
			// Timer
			me.resetTimer();
		}
	};
	
	// Start
	me.cache();
	me.loadSlides();
};
