/**


 * Slideshow Lite plugin for jQuery


 *


 * v0.5.3


 *


 * Copyright (c) 2009 Fred Wu


 *


 * Dual licensed under the MIT and GPL licenses:


 *   http://www.opensource.org/licenses/mit-license.php


 *   http://www.gnu.org/licenses/gpl.html


 */





/**


 * Configuration options:


 *


 * pauseSeconds  float    number of seconds between each photo to be displayed


 * fadeSpeed     float    number of seconds for the fading transition, the value should not exceed 'pauseSeconds'


 * width         integer  width of the slideshow, in pixels


 * height        integer  height of the slideshow, in pixels


 * caption       boolean  display photo caption?


 * cssClass      string   name of the CSS class, defaults to 'slideshowlite'


 */





(function($j){


	$j.fn.slideshow = function(options){


		


		var defaults = {


			pauseSeconds: 8,


			fadeSpeed: 0.5,


			width: 495,


			height: 230,


			caption: true,


			cssClass: 'slideshowlite'


		};


		


		var options = $j.extend(defaults, options);


		


		// ----------------------------------------


		// slideshow objects and variables


		// ----------------------------------------


		


		var target = this;


		var items  = $j(target).children("a");


		var instance;


		


		// ----------------------------------------


		// some mandontory styling


		// ----------------------------------------


		


		if ( ! $j(this).hasClass(options.cssClass)) $j(this).addClass(options.cssClass);


		


		$j(this).css({


			width: options.width + "px",


			height: options.height + "px"


		});


		


		// ----------------------------------------


		// create anchor links to make the structure simpler for manupilation


		// ----------------------------------------


		


		$j(this).children("img").wrap(document.createElement("a"));


		$j(this).children("a").attr("target", "blank");


		


		// ----------------------------------------


		// add item sequence markups


		// ----------------------------------------


		


		var i = 1;


		$j(this).children("a").each(function(){


			$j(this).attr("rel", i++);


		});


		


		// ----------------------------------------


		// create pagination and caption


		// ----------------------------------------


		


		$j(this).append("<ul></ul>");


		$j(this).append("<ol></ol>");


		var pagination = $j(this).children("ul");


		var caption = $j(this).children("ol");


		


		var i = 1;


		var j = 0;


		$j(this).children("a").each(function(){


			pagination.append("<li><a href=\"#\">" + i++ + "</a></li>");


			caption.append("<li>" + $j("#" + $j(target).attr("id") + " img:nth(" + j++ + ")").attr("alt") + "</li>");


		});


		pagination.fadeTo(0, 0.8);


		caption.fadeTo(0, 0.6);


		caption.hide();


		


		// ----------------------------------------


		// shortcuts


		// ----------------------------------------


		


		var firstItem   = $j(target).children("a:first");


		var lastItem    = $j(target).children("a:last");


		var currentItem = firstItem;


		


		// ----------------------------------------


		// pagination highlight


		// ----------------------------------------


		


		var paginationHighlight = function(sequence){


			pagination.children("li").children("a").removeClass("current");


			pagination.children("li").children("a:nth(" + sequence + ")").addClass("current");


		}


		


		// ----------------------------------------


		// caption


		// ----------------------------------------


		


		var showCaption = function(sequence){


			caption.show();


			caption.children("li").hide();


			caption.children("li:nth(" + sequence + ")").fadeIn();


		}


		


		// ----------------------------------------


		// slideshow logic


		// ----------------------------------------


		


		var makeSlideshow = function(){


			


			// pagination click


			pagination.children("li").children("a").click(function(){


				if ( ! $j(this).hasClass("current"))


				{


					// select the current item after the pagination click


					currentItem = $j(target).children("a:nth(" + ($j(this).text()-1) + ")");





					currentItem.show();


					startSlideshow();


				}


			});


			


			// pagination highlight


			paginationHighlight(currentItem.attr("rel")-1);


			


			// show caption


			if (options.caption == true)


			{


				showCaption(currentItem.attr("rel")-1);


			}


			


			// show the current slide


			currentItem.fadeIn(options.fadeSpeed*1000, function(){


				$j(target).children("a").hide();


				$j(this).show().css("z-index", 1);


			});


			


			// prepare for the next slide


			// determines the next item (or we need to rewind to the first item?)


			if (currentItem.children("img").attr("src") == lastItem.children("img").attr("src"))


			{


				currentItem = firstItem;


				currentItem.css("z-index", 2);


			}


			else


			{


				currentItem = currentItem.next();


			}


		};


		


		var startSlideshow = function(){


			clearInterval(instance);


			makeSlideshow();


			instance = setInterval(makeSlideshow, options.pauseSeconds*1000);


		};


		


		// ----------------------------------------


		// start the slideshow!


		// ----------------------------------------


		


		startSlideshow();


	};


})(jQuery);
