/**
 * @author alettieri
 * @Date Sep 23, 2009
 * @file swapper.js
 * @Buid 1.0.0.0
 * Copyright (c) 2009 Antonio Lettieri
 * Dual licensed under the MIT (MIT-License.txt)
 * and GPL (GPL-License.txt) licenses.
 * 
 * Built for jQuery Library : http://jquery.com
 */
(function($){
	
	$.fn.imageSwap = function(defaults,options){
		var defaults = {
			prevId : 'prevId',
			nextId : 'nextId',
			prevText : '',
			nextText : '',
			speed : 0
		};
		var options = $.extend(defaults,options);
		return this.each(function(){
			obj = $(this);
			var len = $('li',obj).length;
			var w = obj.width();
			var h = obj.height();
			var pCount = len-1;
			var count = 0;
			
			  $("li",obj).hide();
			  $("li:first",obj).show();
				
				$(obj)
					.after("<a href='javascript:void(0);' id='"+options.nextId+"' class='buttonToggle right'>"+options.nextText+"</a>")
					.after("<a href='javascript:void(0);' id='"+options.prevId+"' class='buttonToggle left'>"+options.prevText+"<a/>");
				
				$("a#"+options.nextId).hide().click(function(){
					swap("next");
					if(count>=pCount) $(this).hide();
					$("a#"+options.prevId).show();
				});
				$("a#"+options.prevId).hide().click(function(){
					swap("prev");
					if(count<=0) $(this).hide();
					$("a#"+options.nextId).show();
				});
			
			var swap = function(dir){
				$("ul").find("li").eq(count).hide(options.speed);
				if (dir == 'next') {
					count = (count >= pCount) ? pCount : count + 1;
				}
				else {
					count = (count <= 0) ? 0 : count - 1;
				}
				
				$("ul").find("li").eq(count).show(options.speed);
			
			};
			
				if (len > 1) 
					$("a#" + options.nextId).show();
		
		});
	};
	
})(jQuery);
