jQuery(function(){
	jQuery.fn.fadeIn = function(speed, callback) { 
		return this.animate({opacity: 'show'}, speed, function() { 
			if (jQuery.browser.msie)  
				this.style.removeAttribute('filter');  
			if (jQuery.isFunction(callback)) 
				callback();  
		}); 
	}; 
	 
	jQuery.fn.fadeOut = function(speed, callback) { 
		return this.animate({opacity: 'hide'}, speed, function() { 
			if (jQuery.browser.msie)  
				this.style.removeAttribute('filter');  
			if (jQuery.isFunction(callback)) 
				callback();  
		}); 
	}; 
	 
	jQuery.fn.fadeTo = function(speed,to,callback) { 
		return this.animate({opacity: to}, speed, function() { 
			if (to == 1 && jQuery.browser.msie)  
				this.style.removeAttribute('filter');  
			if (jQuery.isFunction(callback)) 
				callback();  
		}); 
	};

	var $container = jQuery("div.slideshow");
	var $items = jQuery("div.slide",$container);
	var $next = jQuery("a.next", $container);
	var $prev = jQuery("a.prev", $container);
	var $num = $items.size();
	var $cur = 0;
	var $old = 0;
	var $loop = true;
	var $t;
	var $interval = 5000;

	$items.css({position: "absolute", top: "0"}).not(":eq(0)").css("display","none");
	var $contH = parseInt($container.height());
	
	$next.click(function(e){
		e.preventDefault();
		resize(1);
		moveNext();
	});
	
	function moveNext()
	{
		$cur = ($old+1)%$num;
		$items.eq($old).fadeOut();
		$items.eq($cur).fadeIn();
		$old = $cur;
	}
	
	$prev.click(function(e){
		e.preventDefault();
		resize(0);
		movePrev();
	});
	
	function movePrev(){
		$cur = $old - 1;
		if($cur < 0){ $cur = $num-1; }
		$items.eq($old).fadeOut();
		$items.eq($cur).fadeIn();
		$old = $cur;
	}
	
	function resize(dir){
		var $temp;
		if(dir == 1){ $temp = ($old+1)%$num; }else{ $temp = ($old-1); if($temp < 0){ $temp = $num; } }
		var $h = parseInt($items.eq($temp).outerHeight(false));
		$h = $h + 40;
		if(isNaN($h) == false && $h > $contH)
		{
			$container.animate({height:$h}, {queue:false, duration: 500});
		}
		else if(isNaN($h) == false && $h < $contH)
		{
			$container.animate({height:$contH}, {queue:false, duration:500});
		}
	}
	
	($loop) ? $t = setInterval(function(){ $next.trigger("click"); }, $interval) : clearInterval($t);
	
	if($loop)
	{
		$container.hover(function(){
			clearInterval($t);
		}, function(){
			$t = setInterval(function(){ $next.trigger("click"); }, $interval)
		});
	}
});