/**
 * Slideshow Lite plugin for jQuery
 *
 * v0.5.2
 *
 * 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  integer  number of seconds between each photo to be displayed
 * 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($){
	$.fn.slideshow = function(options){
		
		var defaults = {
			pauseSeconds: 2,
			width: 468,
			height: 120,
			caption: true,
			cssClass: 'slideshowlite'
		};
		
		var options = $.extend(defaults, options);
		
		// ----------------------------------------
		// slideshow objects and variables
		// ----------------------------------------
		
		var target = this;
		var items  = $(target).children("a");
        var slidePage = 1;
        var slidePage2 = 1;
        var slideNum = $('#slideNum');
		var instance;
		
		// ----------------------------------------
		// some mandontory styling
		// ----------------------------------------
		
		if ( ! $(this).hasClass(options.cssClass)) $(this).addClass(options.cssClass);
		
		$(this).css({
			width: options.width + "px",
			height: options.height + "px"
		});
		
		// ----------------------------------------
		// create anchor links to make the structure simpler for manupilation
		// ----------------------------------------
		
		$(this).children("img").wrap(document.createElement("a"));
		$(this).children("a").attr("target", "blank");
		
		// ----------------------------------------
		// add item sequence markups
		// ----------------------------------------
		
		var i = 1;
		$(this).children("a").each(function(){
			$(this).attr("rel", i++);
		});
		
		// ----------------------------------------
		// create pagination and caption
		// ----------------------------------------
		
		$(slideNum).append("<ul></ul>");
		var pagination = $(slideNum).children("ul");

		var i = 1;
		$(this).children("a").each(function(){
			pagination.append('<li><a href="#" title="' + i + '">&nbsp;</a></li>');
            i++;
		});
		pagination.fadeTo(0, 0.8);
		
		// ----------------------------------------
		// shortcuts
		// ----------------------------------------
		
		var firstItem   = $(target).children("a:first");
		var lastItem    = $(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");
		}


		// ----------------------------------------
		// slideshow logic
		// ----------------------------------------

        $('#nextButton').click(function(){
          if(slidePage != slidePage2){slidePage = slidePage2;}
          currentItem = $(target).children("a:nth(" + slidePage + ")");
          //slideNum.text('#' + (slidePage + 1));
          if(slidePage > items.length){
            slidePage = 0;
            currentItem = firstItem;
          }
          currentItem.show();
          startSlideshow();
          slidePage++;

        });

		var makeSlideshow = function(){

           // pagination click
			pagination.children("li").children("a").click(function(){
				if ( ! $(this).hasClass("current"))
				{
					// select the current item after the pagination click
					currentItem = $(target).children("a:nth(" + ($(this).attr('title')-1) + ")");
                    slidePage2 = $(this).attr('title') - 1;
					currentItem.show();
					startSlideshow();
				}
			});

			// pagination highlight
			paginationHighlight(currentItem.attr("rel")-1);


			// show the current slide
			currentItem.fadeIn(function(){
				$(target).children("a").hide();
				$(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);
                slidePage = 0;
			}
			else
			{
				currentItem = currentItem.next();
			}
            if(!chkInit)
            {
              slidePage2++;
              //slideNum.text('#' + slidePage2);
            }
            if(slidePage2 > items.length){slidePage2 = 0;}
            chkInit = false;

		};

		var startSlideshow = function(){
			clearInterval(instance);
			makeSlideshow();
			instance = setInterval(makeSlideshow, options.pauseSeconds*1000);
		};

		// ----------------------------------------
		// start the slideshow!
		// ----------------------------------------
		var chkInit = true;
		startSlideshow();
	};
})(jQuery);
