/* Requires jquery.slideshow-2.0.css */
jQuery.fn.slideshow = function(options) {
	if (!options) { options = {}; }
	if (!options.delay) options.delay = 1500;
	if (!options.transition_speed) options.transition_speed = 1500;
	if (!options.inactive_opacity) options.inactive_opacity = 1;
	if (!options.active_opacity) options.active_opacity = 1;

	var ss = $("<div class=\"jq-slideshow\">");
	var container = $("<div class=\"jq-slideshow-container\">");
	ss.append($(this).find("a"));
	container.append(ss);
	var imgs = ss.find("img");
	var slideshow = null;
	var index = {current:Math.floor(imgs.length/2),max:imgs.length-1};
	var slide = {inactive:{opacity:options.inactive_opacity},active:{opacity:options.active_opacity},previous:{},current:$(imgs[index.current])}
	var buttons = {left:$("<div class=\"jq-slideshow-button-left\">"),right:$("<div class=\"jq-slideshow-button-right\">")}
	buttons.left.css("opacity",slide.inactive.opacity);
	buttons.right.css("opacity",slide.inactive.opacity);
	buttons.left.hover(function() {
		clearInterval(slideshow);
		ss.stop();
		buttons.left.css("opacity",slide.active.opacity);
		var transition_speed = options.transition_speed/2;
		if (index.current==0) clearInterval(slideshow);	
		else {
			var id = index.current-1;
			shift_slide(id, transition_speed, "linear");
			slideshow = setInterval(function() {
				id = index.current-1;
				if (index.current==0) clearInterval(slideshow);
				else shift_slide(id, transition_speed, "linear");
			},transition_speed);
		}
	},function() {
		clearInterval(slideshow);
		buttons.left.css("opacity",slide.inactive.opacity);
		slideshow = setInterval(function() { shift_slide(); },options.delay);
	});
	buttons.right.hover(function() {
		ss.stop();
		clearInterval(slideshow);
		buttons.right.css("opacity",slide.active.opacity);
		var transition_speed = options.transition_speed/2;
		if (index.current==index.max) clearInterval(slideshow);
		else {
			var id = index.current+1;
			shift_slide(id, transition_speed, "linear"); 
			slideshow = setInterval(function() {
				id = index.current+1;
				if (index.current==index.max) clearInterval(slideshow);
				else shift_slide(id, transition_speed, "linear"); 
			},transition_speed);
		} 
	},function() {
		clearInterval(slideshow);
		buttons.right.css("opacity",slide.inactive.opacity);
		slideshow = setInterval(function() { shift_slide(); },options.delay);
	});
	container.append(buttons.left);
	container.append(buttons.right);
	$(this).empty();
	$(this).append(container);
	$(this).css({visibility:"visible"});
	imgs.css({"opacity":slide.inactive.opacity});
	/*
	//issues w/ slide in focus
	imgs.hover(function() {
		$(this).css("opacity",slide.active.opacity);
	},function() {
		$(this).css("opacity",slide.inactive.opacity);
	});*/
	var run_after_graphics_load = setInterval(function() {
		var check = 0;
		for (var i=0; i<$(".jq-slideshow img").length; i++) {
			if ($($(".jq-slideshow img")[i]).width()>0) check++;
		}
		if (check==imgs.length) {
			clearInterval(run_after_graphics_load);
			container.animate({height:"280px"},"slow",function() {
				imgs.fadeIn();
				if (options.loader_graphic) {
					//console.log(options.loader_graphic);
					options.loader_graphic.fadeOut();
				}
				var offset = get_offset(index.current);
				var left = offset-$("body").width()/2+slide.current.width()/2;
				ss.animate({marginLeft:-left+"px"},options.transition_speed);
				slide.current.animate({opacity:slide.active.opacity},options.transition_speed);
				slideshow = setInterval(function() { shift_slide(null,options.transition_speed); },options.delay);			
			});
		}
	},250);
	function get_offset(current_index) {
		var sum = 0;
		for(var i=0; i<current_index; i++) sum+=$(imgs[i]).width();
		return sum;
	}
	function shift_slide(id, transition_speed, callback, easing) {
		slide.previous = $(imgs[index.current]);
		index.current = (id==null) ? (index.current<index.max) ? index.current+1 : 0 : id;
		slide.current = $(imgs[index.current])
		transition_speed = (!transition_speed) ? options.transition_speed : transition_speed;
		var offset = get_offset(index.current);
		var left = offset-$("body").width()/2+slide.current.width()/2;
		slide.previous.animate({opacity:slide.inactive.opacity},transition_speed,easing);
		ss.animate({marginLeft:-left+"px"},transition_speed,easing,callback);
		slide.current.animate({opacity:slide.active.opacity},transition_speed,easing);
	}
};