(function($){
	
	// Standard settings
	var settings = {
		"effect" 			: "fade",
		"duration"			: "5",
		"effectDuration"	: 800,
		"bullets"			: true
	}

	var methods = {
		init : function(options){
			var self = this;
			
			// Extend the standard settings
			$.extend(settings, options);
			
			// Hide unwanted scrollbar
			self.css({
				"overflow" : "hidden"
			});
			
			// The slideshow items
			self.children('div').addClass("slideshow-item");
			
			// Bullets
			if(settings.bullets == true && $(this).children(".slideshow-bullets").length == 0)
			{
				self.append('<div class="slideshow-bullets-wrap"></div>');
				$.each(self.children(".slideshow-item"), function(i){
					self.children(".slideshow-bullets-wrap").append
						('<span class="slideshow-bullet">Slideshow item ' + (i+1) + '</span>');
				});
				$(".slideshow-bullet:not(.slideshow-bullet-active)").live("click", function(){
					methods[settings.effect](self, $(".slideshow-bullet").index($(this)));
					clearInterval(changeInterval);
					changeInterval = setInterval(function(){ 
						methods[settings.effect](self); }, (settings.duration * 1000));
				});
			}
			
			// CSS specific for the fade effect
			if(settings.effect == "fade" || settings.effect == "topToBottom")
			{
				$(".slideshow-item").css({
					"position" : "absolute",
					"top" : "0px",
					"left" : "0px"
				});
			}
			if(settings.effect == "crossfade")
			{
				$(".slideshow-item").css({
					"display" : "none",
					"position" : "absolute",
					"top" : "0px",
					"left" : "0px"
				});
			}
			
			// alert(methods[settings.effect]);
			
			// Call the function defiened in settings.effect
			methods[settings.effect](self);
			if($(".slideshow-item").length > 1)
			{
				var changeInterval = setInterval(function(){ 
					methods[settings.effect](self); }, (settings.duration * 1000));
			}
		},
		fade : function(self, index)
		{
			if(index == undefined)
			{
				index = $(".slide-active").length > 0 ? 
					($(".slideshow-item").index($(".slide-active"))+1) : 0;
				if(index == $(".slideshow-item").length)
					index = 0;
			}
			
			var target = self.children(".slideshow-item:eq(" + index + ")");
			var previous = $(".slide-active");
			
			previous.css({
				"z-index" : "50"
			});
			
			target.addClass("slide-active");
			
			previous.fadeOut((settings.effectDuration), function(){
				$(".slideshow-item:not(:eq(" + index + "))").removeClass("slide-active").css({
					"z-index" : ""
				});
				target.addClass("slide-active");
				$(".slideshow-item").show();
			});
		},
		crossfade : function(self, index){
			if(index == undefined)
			{
				index = $(".slide-active").length > 0 ? 
					($(".slideshow-item").index($(".slide-active"))+1) : 0;
				if(index == $(".slideshow-item").length)
					index = 0;
			}
			
			var target = self.children(".slideshow-item:eq(" + index + ")");
			var previous = $(".slide-active");
			
			previous.css({
				"z-index" : "50"
			});
			
			target.addClass("slide-active");
			
			previous.fadeOut((settings.effectDuration), function(){
				$(".slideshow-item:not(:eq(" + index + "))").removeClass("slide-active").css({
					"z-index" : ""
				});
			});
			target.addClass("slide-active").fadeIn("slow");
			
			// Bullets
			if(settings.bullets == true) methods.bullets(index);
		},
		topToBottom : function(self, index){
			if(index == undefined)
			{
				index = $(".slide-active").length > 0 ? 
					($(".slideshow-item").index($(".slide-active"))+1) : 0;
				if(index == $(".slideshow-item").length)
					index = 0;
			}
			
			var target = self.children(".slideshow-item:eq(" + index + ")");
			var previous = $(".slide-active");
			
			if($(".slide-active").length > 0)
			{
				target.css({
					top: "-" + self.height() + "px"
				});
				
				target.animate({
					top: "0px"
				}, (settings.effectDuration));
				
				previous.animate({
					top: self.height() + "px"
				}, (settings.effectDuration));
			}
			
			previous.removeClass("slide-active");
			target.addClass("slide-active");
			
			// Bullets
			if(settings.bullets == true) methods.bullets(index);
		},
		bullets : function(index)
		{
			$(".slideshow-bullet").removeClass("slideshow-bullet-active");
			$(".slideshow-bullet:eq(" + index + ")").addClass("slideshow-bullet-active");
		}
	};
	
	$.fn.slideshow = function(method){
		// Method calling logic
		if(methods[method]){
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else if(typeof method === 'object' || ! method){
			return methods.init.apply(this, arguments);
		} 
		else{
			$.error( 'Method ' +  method + ' does not exist on jQuery.slideshow' );
		}
	}	
})(jQuery);
