/* 

 * NOTE: This plugin depends on jQuery.  Download jQuery at www.jquery.com
 *
 */


/*
SCRATCH PAD


*/
(function(jQuery)
{
	var self = null;
	jQuery.fn.slideShow = function(o) {
		return this.each(function() {
			new jQuery.slideShow(this, o);
		});
	};
	
	jQuery.slideShow = function (e, o) {
		this.options = o || {};
		this.speed = this.options['speed'] || null;
		this.parent_elem = e;
		this.init();
	};
	
	jQuery.slideShow.fn = jQuery.slideShow.prototype = {
		slideShow: '0.0.1'
	};
	
	jQuery.slideShow.fn.extend = jQuery.slideShow.extend = jQuery.extend;
	jQuery.slideShow.fn.extend({
		init: function() {
			var self = this;
			var elem_counter = 0;
			var outer_elem = $(this.parent_elem);
			outer_elem.wrap('<div class="slideShowWrapper">');
			var container = $('<div>');
			var prevButton = $('<div>');
			prevButton.append($('<a>').attr({'href':'javascript:', 'class' : 'prev'}).click(function(){
				self.prevElem();
			}).append("&lt;"));
			
			var elem_height = $(outer_elem.children('li')[0]).height();
			var nextButton = $('<div>');
			nextButton.append($('<a>').attr({'href':'javascript:', 'class' : 'next'}).click(function(){
				self.nextElem();
			}).append("&gt;"));
			container.append(prevButton);
			outer_elem.children('li').each(function() {
				elem_counter += 1;
				this.elem_number = elem_counter;
				var heading = $('<div>');
				this.control_box = heading;
				heading.append('<a>'+elem_counter+'</a>');
				var child_link = heading.children('a');
				child_link.attr({'href':'javascript:'});
				child_link.data('elem_number', elem_counter).click(function(){
					self.setShow($(this).data('elem_number'));
				});
				if (elem_counter == 1){
					heading.addClass('selected');
				}
				container.append(heading);
				$(this).css({'position':'absolute',
					'left':'0',
					'top':'0',
					'z-index':'50'
				});
			});
			container.append(nextButton);
			container.addClass('slideShowControls');
			outer_elem.after(container);
			this.elem_count = elem_counter;
			this.top_elem = $(this.parent_elem).children('li')[0];
			$(this.top_elem).css({'z-index':'52'});
			$(this.parent_elem).children('li').not(this.top_elem).hide();
			if (this.speed){
				this.anim_timer = setTimeout(function(){self.autoAdvance(this.speed);}, self.speed);
			}
			outer_elem.parent().hover(
				function(){
					clearTimeout(self.anim_timer);},
				function(){
					if(self.speed){
						clearTimeout(self.anim_timer);
						self.anim_timer = setTimeout(function(){self.autoAdvance();}, self.speed);
					}
				}
			);
		},
		autoAdvance: function(){
			var self = this;
			self.nextElem(true);
		},
		nextElem: function(autoadv){
			this.setShow((this.top_elem.elem_number) % (this.elem_count) + 1, autoadv);
		},
		prevElem: function(){
			
			this.setShow(
				this.elem_count - ((this.elem_count - this.top_elem.elem_number + 1) % this.elem_count)
			);
		},
		setShow: function(number, autoadv){
			autoadv = autoadv || false;
			var self = this;
			var new_top = $(this.parent_elem).children('li').filter(function(index){
				return this.elem_number == number;
			})[0];
			if (this.top_elem != new_top){
				var new_top_sel = $(new_top);
				new_top_sel[0].control_box.siblings().removeClass('selected');
				new_top_sel[0].control_box.addClass('selected');
				var old_top_sel = $(this.top_elem);
				new_top_sel.css({'z-index':'51'}).fadeIn('medium');
				old_top_sel.fadeOut('fast', function(){
					new_top_sel.css({'z-index':'52'});
					old_top_sel.css({'z-index':'50'}).hide();
					if (autoadv){
						self.anim_timer = setTimeout(function(){self.autoAdvance();}, self.speed);
					}
				});
				this.top_elem = new_top;
			}
		}
	});
})(jQuery);
