﻿(function($)
{
	$.fn.simpleSlideshow = function(options)
	{
		var defaultOptions =
		{
			enableKeys: true,
			enableDeepLink: true,
			createThumbnails: true
		}

		options = $.extend(defaultOptions, options);
 
		return this.each(function()
		{
			var slideshow = $(this);
			var items = slideshow.children();
 
			items.hide().css("cursor", "pointer");
 
			slideshow.click(function (e)
			{
				slideshow.changeSlide();
			});
 
//			slideshow.bind("contextmenu", function (e)
//			{
//				e.preventDefault();
//				slideshow.changeSlide({ direction: "prev" });
//			});
 
			if(options.createThumbnails)
			{
				var slideshowThumbs = $('<div class="ThumbsSlider" style="overflow: hidden;">').append('<ol class="SlideshowThumbs" style="position: relative; width: 10000px;" />').children(".SlideshowThumbs");
 
				slideshow.children().each(function(index)
				{
					var thumbnail = $(this).find("img:first").clone().wrap('<li style="cursor: pointer; float:left;" />');
					slideshowThumbs.append(thumbnail.parent());
				});
 
				slideshow.after(slideshowThumbs.parent());
 
				var thumbsnails = slideshowThumbs.children();
 
				thumbsnails.click(function(e)
				{
					items.eq(thumbsnails.index(this)).showSlide();
				});
			}
 
			if(options.enableKeys)
			{
				$(document).keydown(function (e)
				{
					switch (e.which)
					{
						case 37:
							slideshow.changeSlide({ direction: "prev" });
						break;
 
						case 39:
							slideshow.changeSlide();
						break;
					}
				});
			}
 
			var startItem = null;
 
			if(options.enableDeepLink)
			{
				var index = parseInt(document.location.hash.substring(1));
 
				if(!isNaN(index))
				{
					startItem = items.eq(index - 1);
				}
			}
 
			if(startItem == null || startItem.length == 0)
			{
				startItem = items.first();
			}
 
			startItem.showSlide({ dontAnimate: true });
		});
	}
 
        $.fn.showSlide = function(options)
        {
                var defaultOptions =
                {
                        dontAnimate: false
                }
 
                options = $.extend(defaultOptions, options);
 
                return this.each(function()
                {
                        var item = $(this);
                        var slideshow = item.parent();
                        var slides = slideshow.children("li");
                        var current = slides.filter(":visible");
 
                        if(current.length == 0 || current[0] != item[0])
                        {
                                var nextBox = item.children("div:first");
                                var nextImg = nextBox.children("img:first");
                                var index = slides.index(item);
 
                                item.show();
                                current.hide();
 
                                var thumbnailSlider = slideshow.siblings(".ThumbsSlider:first");
                                var thumbnailList = thumbnailSlider.children("ol");
                                var thumbnails = thumbnailList.children();
                                var currentThumbnail = thumbnails.eq(index);
               
                                thumbnails.removeClass("Current");
                                currentThumbnail.addClass("Current");
 
                                var thumbnailWidth = currentThumbnail.outerWidth(true);
                                var leftPos = ((thumbnailSlider.width() - thumbnailWidth) / 2) - currentThumbnail.position().left;

                                if(defaultOptions.dontAnimate)
                                {
									thumbnails.parent().clearQueue().css("left", leftPos);
                                }
                                else
                                {
									thumbnails.parent().clearQueue().animate({ left: leftPos }, "fast", "swing");
                                }
 
                                document.location.hash = index + 1;
 
                                //nextImg.css("margin-top", (nextBox.height() - nextImg.height()) / 2);
                        }
                });
        }
 
        $.fn.changeSlide = function(options)
        {
                var defaultOptions =
                {
                        direction: "next"
                }
 
                options = $.extend(defaultOptions, options);
 
                return this.each(function()
                {
                        var slides = $(this).children("li");
                        var current = slides.filter(":visible");
                        var nextFilter = ":first";
                        var next = null;
 
                        if(current.length == 1)
                        {
                                if(options.direction == "prev")
                                {
                                        next = current.prev();
                                        nextFilter = ":last";
                                }
                                else
                                {
                                        next = current.next();
                                }
                        }
 
                        next = (next == null || next.length == 0) ? slides.filter(nextFilter) : next;
 
                        next.showSlide();
                });
        }
})(jQuery);
 
