/*
* Banner Slideshow 
* www.DealerFire.com team
*/
(function($) {
	$.fn.df_slideshow = function(params) {
	
		var obj = $(this[0]);
		var banners = obj.find(".images-list").children();
		var thumbs = obj.find(".thumbs-list").children();
		var currIndex = banners.index(banners.filter(".active"));
		var totalItems = banners.length;
		var timer = false;
		var curr_item = null;
		var dir_next = null;
		var count = 0;
		var paused = false;
		var loadedImgs = 0;
		var totalImgs = 0;
	
		//Default options
		var options = {
			autorotate: false,
			interval: 5000,
			animationSpeed: 700,
			animationType: "fadeIn"
		};
    
		//Overwrite default options
		for (var i in params) {
			options[i] = params[i];
		}
	
		/* Images preload.
		Animation starts after images loading
		*/
		$(window).bind('load', function() {
			var imgs = $('div#mod-slideshow ul.images-list div.image span.img-data');
		
			var loading = function() {
				loadedImgs++;
            
				if(loadedImgs == totalImgs) {
					if(options["autorotate"]) {
						timer = setInterval(function() {
							paused ? null : transform(currIndex+1);
						}, options["interval"]);
					}
				}
			}
		
			if(imgs.size()) {
				imgs.each(function() {					
					var data = $(this).html().match(/(.*)\|(.*)/);

					if(data) {
						totalImgs++;

						var img = $('<img>').bind('load', loading).attr({
							src : $.trim(data[1]),
							title : $.trim(data[2]),
							alt : $.trim(data[2])
						});
						$(this).html('').append(img);
					}
				});
			}
			else {
				if(options["autorotate"]) {
					timer = setInterval(function() {
						paused ? null : transform(currIndex+1);
					}, options["interval"]);
				}
			}
		});

		/* Images preload */

		var transform = function transform (index) {

			if(index >= totalItems){
				index = 0;
			}
   	   
			if(options["animationType"] == "fadeIn"){
				banners.hide();
				banners.eq(currIndex).show().css("z-index", 1).fadeOut(options["animationSpeed"]);
				banners.eq(index).css("z-index", 2).fadeIn(options["animationSpeed"]);
			}
			else if(options["animationType"] == "slideDown"){
				banners.hide();
				banners.eq(currIndex).show().css("z-index", 1).animate({
					top: banners.eq(currIndex).outerHeight(),
					opacity: 0
				}, {
					duration: options["animationSpeed"]
				});
				banners.eq(index).css({
					"z-index": 2,
					top: -100,
					opacity: 0
				}).show();
				banners.eq(index).stop().animate({
					top: 0,
					opacity: 1
				}, {
					duration: options["animationSpeed"]
				});
			}
			else if(options["animationType"] == "slideRight"){
				banners.hide();
				banners.eq(currIndex).show().css("z-index", 1).animate({
					left: banners.eq(currIndex).outerWidth(),
					opacity: 0
				}, {
					duration: options["animationSpeed"]
				});
				banners.eq(index).css({
					"z-index": 2,
					left: -100,
					opacity: "0"
				}).show();
				banners.eq(index).stop().animate({
					left: 0,
					opacity: "1"
				}, {
					duration: options["animationSpeed"]
				});
			}
		   		
			banners.removeClass("active");
			banners.eq(index).addClass("active");
			thumbs.removeClass("active");
			thumbs.eq(index).addClass("active");
			currIndex = index;
   		
		}

		if(options['pause']) {
			$(this).hover(function() {
				paused = true;
			},function() {
				paused = false;
			});
		}

		thumbs.find(".thumb").click(function(){
			clearInterval(timer);
			var index = thumbs.index($(this).parent());
			transform(index);
			return false;
		});

	
		thumbs.hover(function() {
			paused = true;
			var index = thumbs.index($(this));
			transform(index);
			return false;
		}, function() {
			paused = false;
		});
    
    
	};
})(jQuery);
