/*
 * 	Slider - jQuery plugin
 *	written by David Brown
 *
 *	Copyright (c) 2009 David Brown (http://builtforpeople.com)
 *
 *	Built for jQuery
 *	http://jquery.com
 *
 */
(function($) {

jQuery.fn.slider = function slider(images,options) 
{
	if (!images.length) return;
	
	var el = this;
	var options = $.extend({},{
		showControls:true,
		slideShow:false,
		slideShowInterval:5000,
		animateSpeed:'slow',
		animateEase:'swing',
		url:false
	},options);
	
	var timeout = false;
	var staticImgs = $('img', el);
	var currentImg = 0;
	
	
	//var staticImgs = $('img', el).attr('src');
	
	if (staticImgs.length)
	{
		$(images).each( function(i,im) {
			//console.log("checking: " + im.src);
			if (!$("img[src$='"+ im.src + "']",el).length)
			{
				//console.log("preloading: " + im.src);
				$("<img>").attr("src", im.src);
			} else {
				//console.log("static: " + im.src);
				//console.log("currentImg: " + i);
				currentImg = i;
			}
		});
	}
	
	function nextSlide(dir,num) {
		if (!dir) dir = -1;
		
		//console.log("nextSlide("+dir+","+num+")");
		var img = $("img[src$='"+ images[currentImg].src + "']",el).parent('a');
		
		if (num==undefined)
		{
			currentImg++;
					
			if ( currentImg == images.length )
			{
				currentImg = 0;
			}
		} else {
			currentImg = num;
		}
		//console.log(currentImg);
		var im = images[currentImg];
		//var img = (im.href != 'undefined') ? img.parent(0) : img;
		
		var newImg = $('<a href="'+im.href+'"><img></a>');
			
			newImg.css({
				position:'absolute',
				left : -1 * dir * img.width(),
				top : 0
			});
			
		img.after(
			newImg
		).css({
			position : 'absolute',
			top:0,
			left:0
		});
		
		var i = $('img',newImg);
		
		i.load(function(event) {
			$(event.target).parent().animate({left:0}, options.animateSpeed,options.animateEase);
			img.animate({left:dir * img.width()}, options.animateSpeed,options.animateEase, function() {
				$(this).remove();
			});
		});
		
		// set the src right at the end to fix IE onload
		i.attr('src',im.src);
	}
	
	function timedNextSlide () {
		clearTimeout(timeout);
		timeout = setTimeout(function() { nextSlide();timedNextSlide();},options.slideShowInterval);
	}
	
	if (options.slideShow)
	{
		timedNextSlide();
	}
	/*
	if (options.showControls) {
		$('img',el)
		.before(
			$('<a id="leftarrow" href="#"><span>Previous</span></a>').click(function(event) {
				nextSlide(1);
				event.preventDefault();
			})
		)
		.before(
			$('<a id="rightarrow" href="#"><span>Next</span></a>').click(function(event) {
				nextSlide(0);
				event.preventDefault();
			})
		);
	}*/
	
	if (options.showControls) {
		$(el)
		.append(
			$('<div class="controls"></div>').click(function(event) {
				nextSlide(1);
				event.preventDefault();
			})
		)
		.before(
			$('<a id="rightarrow" href="#"><span>Next</span></a>').click(function(event) {
				nextSlide(0);
				event.preventDefault();
			})
		);
	}
	
	if (options.thumbs) {
		options.thumbs.each(function(i,e){
			$(e).click(function(ev){
				options.thumbs.not(this).removeClass('sel');
				$(this).addClass('sel');
				nextSlide(false,i);
				ev.preventDefault();
			});
		});
	}
	
}

})(jQuery);
