/**
 * 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(jQuery){
	jQuery.fn.slideshow = function(options){
		var defaults = {
			pauseSeconds: 5,
			width: 760,
			height: 252,

			//caption: true,
		caption: false,
			cssClass: 'slideshowlite'
		};
		var options = jQuery.extend(defaults, options);
		// ----------------------------------------

		// slideshow objects and variables

		// ----------------------------------------
		var target = this;
		var items  = jQuery(target).children("a");
		var instance;
		// ----------------------------------------

		// some mandontory styling

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

		if ( ! jQuery(this).hasClass(options.cssClass)) jQuery(this).addClass(options.cssClass);
		jQuery(this).css({
			width: options.width + "px",
			height: options.height + "px"
		});
		// ----------------------------------------

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

		// ----------------------------------------
		jQuery(this).children("img").wrap(document.createElement("a"));
		jQuery(this).children("a").attr("target", "blank");

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

		// add item sequence markups

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

		var i = 1;
		jQuery(this).children("a").each(function(){
			jQuery(this).attr("rel", i++);
		});
		// ----------------------------------------

		// create pagination and caption

		// ----------------------------------------
		jQuery(this).append("<p class='first'></p>");
		jQuery(this).append("<div class='first' id='tada' ></div>");
		jQuery(this).append("<ul ></ul>");
		jQuery(this).append("<ol></ol>");
		var startlink = jQuery(this).children("div");
		var pagination = jQuery(this).children("ul");
		var caption = jQuery(this).children("ol");
		var stoplink = jQuery(this).children("p");

		var i = 1;
		var j = 0;
		
		
		stoplink.append("<a href=\"#\" style='padding-top:5px;'><font color='#ffffff' size='1px'  style=' font-weight:bolder; ' >||</font></a>");
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 			var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 
 
 			if (ieversion==6){
 // document.write("You're using IE6.x")
 				startlink.append("<a href=\"#\" style='padding-top:5px;' ><font color='#ffffff' size='3px'>></font></a>");
			}
			 else
			{
				startlink.append("<a href=\"#\" style='padding-top:5px;' ><font color='#ffffff' size='3px'>&#9658;</font></a>");
			}
		}
		else
			{
				startlink.append("<a href=\"#\" style='padding-top:5px;' ><font color='#ffffff' size='3px'>&#9658;</font></a>");
			}
		

	    
		jQuery(this).children("a").each(function(){
			pagination.append("<li><a href=\"#\">" + i++ + "</a></li>");
			caption.append("<li>" + jQuery("#" + jQuery(target).attr("id") + " img:nth(" + j++ + ")").attr("alt") + "</li>");
		});
		pagination.fadeIn(0, 0.10);
		caption.fadeTo(0, 0.6);
		caption.hide();
		// ----------------------------------------
		// shortcuts
		// ----------------------------------------
		
		$('p').show();
		$("#tada").hide();
		startlink.addClass("first");
		stoplink.addClass("first");
		var firstItem   = jQuery(target).children("a:first");
		var lastItem    = jQuery(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(1000);
		}

		// ----------------------------------------
		// slideshow logic
		// ----------------------------------------
		var makeSlideshow = function(){
			// pagination click
			pagination.children("li").children("a").click(function(){
				if ( ! jQuery(this).hasClass("current"))
				{
					// select the current item after the pagination click
					currentItem = jQuery(target).children("a:nth(" + (jQuery(this).text()-1) + ")");
					//currentItem.fadeTo(0, 0.5);
					currentItem.show();
					//clearInterval(instance);			

			makeSlideshow();
					//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(1000,function(){
				jQuery(target).children("a").hide();
				jQuery(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();
			}
//			t=setTimeout(makeSlideshow,options.pauseSeconds*1000);
		};
		var startSlideshow = function(){
			
			clearInterval(instance);			

			makeSlideshow();
			instance = setInterval(makeSlideshow, options.pauseSeconds*1000);
			
			//instance=setTimeout("makeSlideshow",options.pauseSeconds*1000);
		
		
		};
		var stopslide=function()
		{
			
		}
		stoplink.children("a").click(function(){
//						alert('sdfsdf');								
		
//		clearTimeout(t);
		clearInterval(instance);
		$('#tada').show();
		$('p').hide();
				
		
		});
		startlink.children("a").click(function(){

		startSlideshow(firstItem);
		$('p').show();
		$('#tada').hide();
	});
												
		// ----------------------------------------
		// start the slideshow!
		// ----------------------------------------
		startSlideshow(firstItem);
//		makeSlideshow();
		
	};
})(jQuery);