$(document).ready(function(){
	
	$(".flash_obj,.flash_label").addClass("hidden").animate({opacity:0.0},0,null,function(){
		$(".flash_obj[rel=1],.flash_label[rel=1]").animate({opacity:1.0},0,null,function(){
			$(this).removeClass("hidden");
		});
	});
	
	var rotate = setInterval(startRotate, 5000);
	
	$("#flash").mouseenter(function(){
		clearInterval(rotate);
	}).mouseleave(function(){
		rotate = setInterval(startRotate, 5000);
	});
	
	$("#flash_next").mouseenter(function(){
		clearInterval(rotate);
	}).mouseleave(function(){
		rotate = setInterval(startRotate, 5000);
	});
	
	$("#flash_prev").mouseenter(function(){
		clearInterval(rotate);
	}).mouseleave(function(){
		rotate = setInterval(startRotate, 5000);
	});
	
	$("#flash_next").click(function(){
		$("#flash_obj").dequeue();	
		$(this).next_obj();
		return false;
	});
	
	$("#flash_prev").click(function(){
		clearInterval(rotate);
		var count = $(".flash_obj").size();
		
		var current_obj = $(".flash_obj:visible").attr("rel");
		var next_obj = current_obj * 1 - 1;
		if( next_obj == 0){
			next_obj = count;
		}
		$(".flash_obj:visible").stop().animate({
			opacity: 0.0,
			left: "-100px"
		}, 500, null, function(){
			$(".flash_obj").css("opacity",0.0);
			$(".flash_obj[rel=" + next_obj + "]").removeClass("hidden").stop().animate({opacity: 1.0}, 1000);
			$(this).addClass("hidden").removeAttr("style");
		});
		
		$(".flash_label:visible").stop().animate({
			opacity: 0.0,
			marginLeft: "-100px"
		}, 500, null, function(){
			$(".flash_label").css("opacity",0.0);
			$(".flash_label[rel=" + next_obj + "]").removeClass("hidden").stop().animate({opacity: 1.0}, 1000);
			$(this).addClass("hidden").removeAttr("style");
		});
		
		
		return false;
	});
	
	function startRotate() {
		$("#flash_next").next_obj();
	}
	
});

jQuery.fn.next_obj = function(){
		
	var current_obj = $(".flash_obj:visible").attr("rel");
	var next_obj = current_obj * 1 + 1;
	
	if( $(".flash_obj[rel=" + next_obj + "]").length == 0){
		next_obj = 1;
	}
	
	$(".flash_obj:visible").stop().animate({
		opacity: 0.0,
		left: "100px"
	}, 500, null, function(){
		$(".flash_obj").css("opacity",0.0);
		$(".flash_obj[rel=" + next_obj + "]").removeClass("hidden").stop().animate({opacity: 1.0}, 1000);
		$(this).addClass("hidden").removeAttr("style");
	});
	
	$(".flash_label:visible").stop().animate({
		opacity: 0.0,
		marginLeft: "100px"
	}, 500, null, function(){
		$(".flash_label").css("opacity",0.0);
		$(".flash_label[rel=" + next_obj + "]").removeClass("hidden").stop().animate({opacity: 1.0}, 1000);
		$(this).addClass("hidden").removeAttr("style");
	});
}