$(function() {
	new ss('stage');
	//var ss2 = new ss('slideshow2', { anim: 'fade', speed: 1000, delay: 1000 });
	
	$('.home .slideshow').each(function() { 
		var slideshow = new ss(this.id, { anim: 'fade', speed: 1200, delay: 3000 });
		slideshow.start();
	});
});
	
	
var ss = function(id, opt) {
	this.ss(id, opt);
};

ss.prototype = {
	selector: '',
	stageWidth: 0,
	curSlide: 1,
	numItems: 0,
	z: 0,
	
	delay: 1000,
	
	id: '',
	anim: 'slide',
	animSpeed: 500,
	
	ss: function(id, opt) {
		// Initialize, and go to a specific item if called via #
		this.id = id;
		this.selector = '#' + id;
		// d('Initializing');
		this.numItems = $('.slide', this.selector).length;
		this.z = this.numItems + 1;
		
		if (opt) {
			if (opt.anim) this.anim = opt.anim;
			if (opt.animSpeed) this.animSpeed = opt.speed;
			if (opt.delay) this.delay = opt.delay;
		}
		
		this.stage();
		
		if (this.anim == 'slide')
			this.prepSlide();
		else if (this.anim == 'fade')
			this.prepFade();
			
		this.events();
	},
	
	// Test these two; should work;
	
	prev: function() {
		newSlide = this.curSlide - 1;
		if (newSlide < 1) newSlide = this.numItems;
		
		this.go(newSlide);
	},
	
	next: function() {
		//d('Current slide: ' + this.curSlide);
		newSlide = this.curSlide + 1;
		
		if (newSlide > this.numItems) newSlide = 1;
		
		if (newSlide == 0) newSlide = 1;
		
		//d('From ' + this.curSlide + ', New slide: ' + newSlide + ', numItems: ' + this.numItems);
		
		this.go(newSlide);
	},
	
	go: function(n) {
		// Convert n into a number
		n = parseInt(n);
		
		
		if ($(this.selector).is(':hidden') || $(this.selector).parent().is(':hidden')) return;
		// Add "active" class to current thumbnails
		
	
		$('a[href=' + this.selector + '-slide-' + this.curSlide + ']').removeClass('active');
		$('a[href=' + this.selector + '-slide-' + n + ']').addClass('active');


		//d('Going to slide: ' + n + ' from ' + this.curSlide);
	//	alert(this.anim);
		switch (this.anim) {
			case 'slide':
				$(this.selector + '-wrap').stop().animate({ 'left': -(n - 1) * this.stageWidth }, this.animSpeed, 'easeOutCubic');
				break;
			
			case 'fade':
				//alert('anim fade' + $(this.selector + '-slide-' + this.curSlide).length);
				
				var self = this;

				$(this.selector + '-slide-' + n).css({ 'z-index': ++this.z, 'opacity': 0 }).stop().animate({ 'opacity': 1 }, this.animSpeed);
				
				//alert(this.curSlide);
				break;
		}

		// Get the offset of the new slide [i * offset] and animate left to it
		this.curSlide = n;

		// To jump directly, without animation:
		// 
	},
	
	
	start: function(delay) {
		if (delay) this.delay = delay;
		
		var self = this;
		
		// +animSpeed since it animates and that takes time
		this.loopTimer = setInterval(function() { self.next.apply(self); }, this.delay + this.animSpeed);
	},
	
	stop: function() {
		clearTimer(this.loopTimer);
	},
	
	//
	// Boring init functions
	//
	
	stage: function() {
		this.stageWidth = $('#stage').width();
		$('#stage').wrapInner('<div id="' + this.id  + '-wrap"></div>');
	},

	// Loop through the items, set the width of the container and the left offset.		
	prepSlide: function() {
		var self = this;
		
		$('.slide', this.selector).each(function(n) {
			$(this)
				.css('width', self.stageWidth)
				.css('left', n * self.stageWidth);
				
			// Set width of images to that of the container;
			// this shouldn't be necessary.
			$('img', this).css('width', self.stageWidth);
		});
	},

	// Loop through the items, set the width of the container and the left offset.		
	prepFade: function() {
		var self = this;
		
		
		$('.slide', this.selector).each(function(n) {
			//alert(this.id);
			
			$(this).css({ 'z-index': self.numItems - n });
			$(this).css('opacity', 1);
		});
	},
	
	// Hook up prev/next links, as well as thumbs.
	events: function() {
		/* 
			Find all links that link to #selector-next, or #selector-x			
		*/
		var self = this;
		
		// Get the relevant links and attach click events
		
		$('a[href^=' + this.selector + ']').each(function() {
			// Get the command (prev, next, 1, 2, ...)
			var cmd = this.href.substring(this.href.indexOf('#') + self.selector.length + 1);
			//d('cmd: ' + cmd);
			
			$(this).click(function() {
				switch (true) {
					case cmd == 'next': self.next(); break;
					case cmd == 'prev': self.prev(); break;
					// cmd = 'slide-x'
					case cmd.length > 6 && !isNaN(cmd.substring(6)): self.go(cmd.substring(6)); break;
				}
				
				return false;
						
			});

		});
		
	//	$('a[href=' + this.selector + '-next]').click(function() { self.next(); return false; });
	//	$('a[href=' + this.selector + '-prev]').click(function() { self.next(); return false; });
			
	}
	
};

function d(s) {

	return;
	if (console == null) return;
	
	if (console.dir)
		if (s instanceof Array)
			console.dir(s)
		else
			console.log(s);
	
	if (console.write)
		console.write(s);
	
	// alert(s);
}
