﻿(function($){


$.fn.extend({


slideshow : function(options, param){

	var settings = {
		
		type: "slide",
		width: 500,
		height: 375,
		interval: 0,
		duration: 1000,
		slides: "li",
		image: "img",
		background: true,
		controls: true,
		fullsize: false,
		change: function(){},
		easing: "swing"
		
	}
	
	if(options == "next") return this.transition("next");
	if(options == "prev") return this.transition("prev");
	if(!isNaN(parseInt(options))) return this.transition(options); 
	
	var $this = this, $prev, slideIndex = 0, $controls, $fullsize, $fullsizeModalBg, $fullsizeModal,
		options = $.extend(settings, options), 
		$container = $("<div></div>")
			.addClass("slideshow " + options.type)
			.css({
					width: options.width,
					height: options.height
				}),
		$slides = $this.find(options.slides)
			.each(function(){
				$(this).attr("index", slideIndex++);
			}), 
		$current = $slides.filter(":first").animate({opacity: "show"}, options.duration, options.easing);
	
	this.transition = function(direction){
		if(!isNaN(parseInt(direction))){
			if(options.type == "slide") $this.scrollTo($current = $slides.filter("[index=" + direction + "]"), options.duration, { easing: options.easing, axis: "x" });
			else {
				$current.animate({opacity: "hide"}, options.duration, options.easing);
				$current = $slides.filter("[index=" + direction + "]").animate({opacity: "show"}, options.duration, options.easing);
			}
			options.change($current.attr("index"), $current.get(0));
			return this;
		}
		var pend = (direction == "next") ? "append" : "prepend";
		if($current[direction](options.slides).length == 0){
			$this[pend]($this.find(options.slides).not($current));
			if(options.type == "slide")	$this.scrollTo($current, { easing: options.easing, axis: "x" });
		}
		if(options.type == "slide") $this.scrollTo($current = $current[direction](options.slides), options.duration, { easing: options.easing, axis: "x" });
		else $current = $current.fadeOut(options.duration)[direction](options.slides).animate({opacity: "show"}, options.duration, options.easing);
		options.change($current.attr("index"), $current.get(0));
		return this;
	}
	
	
	if(options.type == "slide") $this.scrollTo(0, { axis: "x" });
	
	$slides
		.addClass("slideshow-slide")
		.css({
				width: options.width,
				height: options.height
			})
		.each(function(){
			if(options.background) $(this).css("background-image", "url('" + $(this).find(options.image).remove().attr("src") + "')");
		})
		.show()
		.appendTo($this)
		.find(options.titleClass);
		
	
	$next = $("<div></div>")
		.slideshowControl("next")
		.click(function(){
			$this.interval("clear")
			$this.transition("next");
			$controls.mouseover();
			return false;
		});
	$prev = $("<div></div>")
		.slideshowControl("prev")
		.click(function(){
			$this.interval("clear");
			$this.transition("prev");
			$controls.mouseover();
			return false;
		});
		
	//fullsize
	$fullsizeModalBg = $("<div></div>")
		.addClass("slideshow-fullsize-modal-bg")
		.css("opacity", .25)
		.prependTo("body");
		
	$fullsizeModal = $("<div></div>")
		.addClass("slideshow-fullsize-modal slideshow-fullsize-modal-loading")
		.click(function(){
			$fullsizeModalBg.add(this).fadeOut(100);
			$fullsizeModal.removeAttr("style");
		})
		.prependTo("body");
		
	$fullsize = $("<div></div>")
		.addClass("slideshow-fullsize")
		.css({
			marginTop: options.height-71,
			marginLeft: options.width-72
		})
		.click(function(){
			var src = $current.attr("fullsize"); 
			if(!src) return;
			$fullsizeModalBg.fadeIn(100);
			$fullsizeModal.fadeIn(100, function(){				
				$('<img src="' + src + '" />').load(function(){
					$fullsizeModal.fadeOut(100, function(){
						$fullsizeModal
							.css("background", "url("+src+") no-repeat center center")
							.fadeIn(500);
					})
				}).prependTo("body").hide();	
			});
		});
	
	
	
	$controls = $("<div></div>")
		.addClass("slideshow-controls")
		.append($prev)
		.append($next)
		.append($fullsize)
		.css({
				width: options.width,
				height: options.height
			})
		.mouseover(function(){
				if($current.attr("fullsize")) $fullsize.show();
				else $fullsize.hide();
			})
		.mouseout(function(){
				$fullsize.hide();
			})
		
	this
		.hide()
		.wrap( $container );

	if(options.controls || options.fullsize) this.before($controls);
	if(!options.controls && options.fullsize) $next.add($prev).hide();
	if(options.interval) this.interval(function(){ $this.transition("next"); }, options.interval)

	if(options.type == "fade") $slides.not(":first").hide();
	
	
	return this.show();
	
	
},

slideshowControl : function(type){
	this
		.addClass("slideshow-" + type)
		.mouseover(function(){
			$(this).css("opacity", 1);
		})
		.mouseout(function(){
			$(this).css("opacity", 0);
		})
		.css("opacity", 0);
	return this;
}




})





})(jQuery);
