/**
 * jCarouselLite - jQuery plugin to navigate images/any content in a carousel style widget.
 * @requires jQuery v1.2 or above
 *
 * http://gmarwaha.com/jquery/jcarousellite/
 *
 * Copyright (c) 2007 Ganeshji Marwaha (gmarwaha.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Version: 1.0.1
 * Note: Requires jquery 1.2 or above from version 1.0.1
 */

(function($) {                                          // Compliant with jquery.noConflict()
$.fn.slideshow = function(o) {
    o = $.extend({
			images: null,
			
			interval: 10000,
			speed: 100,

			loader: null,
			
			thumbs: null,
			thumb_width: null,
			thumb_height: null,
			debug: null
    }, o || {});

    return this.each(function() {                           // Returns the element collection. Chainable.

		if (o.images.length == 0) return;

		var timer;
		var slides = $(this);
		var imageCount = o.images.length;
		var loaded = -1; // loaded images count
		var curr = 0; 	// currently loading image
		
		function addImage() {
			curr = loaded + 1;
			if (!o.images[curr]) return;
			
			var img = new Image();
			
			$(img).addClass("loading").load(function() {
				loaded++;
				$(this).removeClass("loading");
				addThumb(curr); // add thumb after image is loaded
				if (curr == 0) {
					// hide loader
					$(o.loader).removeClass("loading").fadeTo(300, 0).hide();
					// set interval
					clearInterval(timer);
					timer = setInterval(function() { slideSwitch(null); }, o.interval );
				}
				// start loading next image
				if (loaded < imageCount) addImage();
			});
			
			if (curr == 0) $(img).addClass("active");
			$(slides).append(img);
			$(img).attr("src", o.images[curr]);
			
		}
		
		function addThumb(i) {
			if (o.thumbs) {
				var thumb = new Image();
				if (i == 0) $(thumb).addClass("active");
				$(thumb).attr("src", o.images[i]);
				if ($.browser.msie) {
					$(thumb).css({"width": o.thumb_width, "height": o.thumb_height});
				} else {
					$(thumb).attr("width", o.thumb_width).attr("height", o.thumb_height);
				}
				$(thumb).hover(
					function() {
						$(this).stop().animate( {opacity: 1}, 200);
					},
					function() {
						if (!$(this).is(".active") ) {
							$(this).stop().animate( {opacity: 0.5, borderTopColor: '#ffffff', borderRightColor: '#ffffff', borderBottomColor: '#ffffff', borderLeftColor: '#ffffff'}, 600);
						}
					}
				);
				$(thumb).click(
					function() {
						if (!$(this).is(".active") ) {
							$(this).stop()
								.animate( {opacity: 1.0, borderTopColor: '#333333', borderRightColor: '#333333', borderBottomColor: '#333333', borderLeftColor: '#333333'}, 10)
								.addClass("active");
							$(this).siblings()
								.stop()
								.animate( {opacity: 0.5, borderTopColor: '#ffffff', borderRightColor: '#ffffff', borderBottomColor: '#ffffff', borderLeftColor: '#ffffff'}, 600)
								.removeClass("active");
							var theSrc = $(this).attr("src");
							var slide = $(slides).find("img[src='" + theSrc + "']");
							clearInterval(timer);
							slideSwitch(slide);
							timer = setInterval(function() { slideSwitch(null); }, o.interval );
						}
					}
				);
				$(o.thumbs).append(thumb);
			}
		}
		
		function slideSwitch(slide) {
			var active = $(slides).find("img.active");
			
			if (!active.is(".loading")) {
				if ( active.length == 0 ) active = $(slides).find("img:last");

				//if (slide.length) $(o.debug).prepend("<p><b>slide</b></p>");
				
				//$(o.debug).prepend("<p><b>active</b>: " + $(active).attr("src") + "</p>");

				// use this to pull the images in the order they appear in the markup
				var next = slide ? slide : $(active).next("img").length ? $(active).next("img") : $(slides).find("img:first");
				var duration = slide ? 200 : 1000;

/*				if ($(o.loader).is(".loading")) {
					$(o.loader).removeClass("loading").fadeOut();
					return;
				} */
				
				if ($(next).is(".loading")) {
					$(o.loader).addClass("loading").show().fadeTo(300, 0.5);
					return;
				} else {
					if ($(o.loader).is(".loading")) {
						$(o.loader).removeClass("loading").fadeTo(300, 0).hide();
					}
				}

				//$("#debug").prepend("<p><b>next</b>: " + $(next).attr("src") + "</p>");
				
				if ($(next).attr("src") != active.attr("src")) {
					active.addClass("last-active");

					if (o.thumbs) {
						var theSrc = $(next).attr("src");
						var thumb =  $(o.thumbs).find("img[src='" + theSrc + "']");
						if (!thumb.is(".active")) {
							thumb.animate( {opacity: 1.0, borderTopColor: '#333333', borderRightColor: '#333333', borderBottomColor: '#333333', borderLeftColor: '#333333'}, 1000)
								.addClass("active")
								.siblings()
								.stop()
								.animate( {opacity: 0.5, borderTopColor: '#ffffff', borderRightColor: '#ffffff', borderBottomColor: '#ffffff', borderLeftColor: '#ffffff'}, 600)
								.removeClass("active");
						} 
					}
					$(next).css({opacity: 0.0})
							.addClass("active")
							.stop()
							.animate({opacity: 1.0}, duration, function() {
									active.removeClass("active last-active");
							});
							
				}
			}
		}
		
		addImage();
  });
};

})(jQuery);