/*

Copyright (c) 2009 Dimas Begunoff, http://www.farinspace.com

Licensed under the MIT license
http://en.wikipedia.org/wiki/MIT_License

*/

function imgpreload(a,b){if(b instanceof Function){b={all:b}}if(typeof a=="string"){a=[a]}var c=[];var t=a.length;var i=0;for(i;i<t;i++){var d=new Image();d.onload=function(){c.push(this);if(b.each instanceof Function){b.each.call(this)}if(c.length>=t&&b.all instanceof Function){b.all.call(c)}};d.src=a[i]}}if(typeof jQuery!="undefined"){(function($){$.imgpreload=imgpreload;$.fn.imgpreload=function(b){b=$.extend({},$.fn.imgpreload.defaults,(b instanceof Function)?{all:b}:b);this.each(function(){var a=this;imgpreload($(this).attr('src'),function(){if(b.each instanceof Function){b.each.call(a)}})});var c=[];this.each(function(){c.push($(this).attr('src'))});var d=this;imgpreload(c,function(){if(b.all instanceof Function){b.all.call(d)}});return this};$.fn.imgpreload.defaults={each:null,all:null}})(jQuery)}


$(function(){
		var code=' \
<div id="slideWrap"> \
	<div id="slideCover"> \
	</div>\
	<a href="" class="slideanchor"></a>\
	<div id="slideControl">\
		<div id="slidecontroller">\
		</div>\
	</div>\
</div>';
	$('#slider').after(code);
	$('#slider img').imgpreload(function(){
		var len=$('#slider img').size();
		var timeout;
		if( typeof current == 'undefined' )
		{	current = 0; 
			var source=$('#slider img:eq('+current+')').attr('src');
			$('#slideWrap').css({'background': "transparent url(" + source +") center center no-repeat"  });
			$('#slideWrap .slideanchor').attr('href',$('#slider a:eq('+current+')').attr('href'));
			current++;
		}
		$('#slider img').each(function(index){
			$('#slideWrap #slidecontroller').append('<a alt="'+(index+1)+'">'+ (index+1) +'</a>');
		});
		$('#slideWrap #slidecontroller a').click(function(){
			changeSpecific(($(this).attr('alt') -1));
		});
		
		/*below line stop the slider when hovered over it */
		/* START */
		$('#slideWrap').hover(function(){ clearInterval(timeout);}, function(){ timeout = setInterval(changeSlide, 5000); });
		/* END */
		
		function changeSpecific(index){
			bwidth = $(window).width();
			clearInterval(timeout);
			timeout = setInterval(changeSlide, 5000);
			var source=$('#slider img:eq('+index+')').attr('src');
			$('#slideCover').css({'background': "transparent url(" + source +") -"+bwidth+"px center no-repeat"  });

			$('#slideCover').css({'background-position' : '50% 50%'}) 
			$('#slideCover').stop().fadeIn(1000, function(){
				$('#slideWrap').css({'background': "transparent url(" + source +") center center no-repeat"  });
			});
			$('#slideCover').hide(0);
			$('#slideWrap .slideanchor').attr('href',$('#slider a:eq('+index+')').attr('href'));
			current=index+1;
		}
		
		function changeSlide(){
			if(current == len)
			{
				current = 0;
			}
			bwidth = $(window).width();
			var source=$('#slider img:eq('+current+')').attr('src');
			$('#slideCover').css({'background': "transparent url(" + source +") -"+bwidth+"px center no-repeat"  });

			$('#slideCover').css({'background-position' : '50% 50%'}) 
			$('#slideCover').fadeIn(1000, function(){
				$('#slideWrap').css({'background': "transparent url(" + source +") center center no-repeat"  });
			});
			$('#slideCover').hide(0);
			$('#slideWrap .slideanchor').attr('href',$('#slider a:eq('+current+')').attr('href'));
			current++;
		}
		timeout = setInterval(changeSlide, 5000);
	});
});
