/*
 * 	qSlider 1.0 - jQuery plugin
 *	written by Eric Sha	
 *	www.pokesss.com
 *
 *	
 *	
 *	
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#img-box").qSlider();
 *	
 * 	<div id="img-box">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="title1" /></li>
 *			<li><img src="images/02.jpg" alt="title2" /></li>
 *			<li><img src="images/03.jpg" alt="title3" /></li>
 *			<li><img src="images/04.jpg" alt="title4" /></li>
 *			<li><img src="images/05.jpg" alt="title5" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.qSlider = function(options){
	
		// default configuration properties	
		var settings = {
			width:			335,
			height:			300,
			speed: 			1000,
			pause:			3000,
			caption:		true,
			smooth:			true
		};	
		var options=$.extend(settings,options);
		
		this.each(function() {
			var obj = $(this);			
			var s = $("li", obj).length;
			var w = options.width;
			var h = options.height;
			var ul = $("ul", obj);
			var c = 0;		//current img
			var timeout = null;
			var click;
			
			if (options.caption) {
				var referent = '<a href="#" class="current">1</a>';
				for (var v = 2;v<=s;v++) {
					referent = referent+'<a href="#">'+v+'</a>';
				}			
				var title = ul.find("img").eq(0).attr("alt");
				var caption = $('<div class="qCaption"><div class="qTransparency"></div><h4>'+title+'</h4><div class="qReferent">' + referent + '</div></div>');
				ul.after(caption);
			}
			
 			if (options.smooth) {
				ul.children("li:first").clone().appendTo(ul);
				s=s+1;
			}
			obj.width(w); 
			obj.height(h);
			$("img" , obj).width(w);
			$("img" , obj).height(h);
			obj.css({"overflow":"hidden" , "position":"relative"});
			ul.css('width',s*w);
			$("li", obj).css('float','left');
			
			setTimeout(function(){
					animate("next");
			},1000);
				
			$(".qCaption a" , obj).click(function(){
				var num= $(this).text();
				click = true;
				c= num-2;
				clearTimeout(timeout);
				animate("next");
				return false;
			});
			
			function animate(dir){	
			
				switch(dir){
					case "next":
						c = (c>=s-1) ? 0 : c+1;				
						break; 
				};
				
				if (c==0 && !click&&options.smooth) {
					$("ul",obj).css({"marginLeft": "0"});
					animate("next");
					return;
				}

				click = false;				
				$("ul",obj).animate({ marginLeft: c*w*-1 }, options.speed);
				
				if (options.caption) {
					caption.find(".qReferent a").eq((c>=s-options.smooth) ? 0 : c).addClass("current").siblings().removeClass("current");
					caption.find("h4:first").text(ul.find("img").eq(c).attr("alt"));
				}
				
				timeout = setTimeout(function(){
					animate("next");
				}, options.speed + options.pause);
			}
		});
	  
	};
})(jQuery);