$(document).ready(function(){
	var is_active = false;
	var time_lag_increment = 110;
	var interval = 10000;
	var efect_time = 600;
	var rightSectionInnerList_obj = $('#rightSectionInnerList');

	var get_next_news = function() {
		startAction();
		var time_lag = 0;
		var shop_ele_count = 0;
		$('#rightSectionInner > div > div.shopInfo').each(function(index, shop_ele){
			var shop_obj = $(shop_ele);
			var img_obj = shop_obj.find('div.shopInfoImgBox:first img:first');
			var text_obj = shop_obj.find('div.shopInfoTextBox:first > dl:first');
			var height = img_obj.outerHeight();  
			var width = img_obj.outerWidth();  
			
			shop_ele_count += 1;
			if (shop_ele_count == 6) {
				var complete_function_option = {
					duration: efect_time,
					complete: function() {stopAction();}
				}; 
			} else {
				var complete_function_option = {
					duration: efect_time
				};
			}
			
			setTimeout(function(){
				text_obj.animate({ top : height },{duration: efect_time});  
				img_obj.animate({
					top    : height,
					left   : width / 2,
					height : 0,
					width  : 0,
					borderWidth : 0
				},
				{
					duration:efect_time,
					complete: function(){
						var parent_id = shop_obj.parent().get(0).id;
						rightSectionInnerList_obj.append(shop_ele);
						var new_shop_obj = $('#rightSectionInnerList > div.shopInfo:first').appendTo('#' + parent_id);
						var new_img_obj = new_shop_obj.find('div.shopInfoImgBox:first img:first');
						var new_text_obj = new_shop_obj.find('div.shopInfoTextBox:first > dl:first');
						new_img_obj.css({
							top    : 0,
							left   : width / 2,
							height : 0,
							width  : 0,
							borderWidth : 3
						});
						new_text_obj.css('top', '-' + height + 'px');
						new_img_obj.animate({ top: 0, left: 0, width: 64, height: 64}, complete_function_option);
						new_text_obj.animate({ top: 0 },{duration: efect_time});
					}
				});  
			}, time_lag);
			time_lag += time_lag_increment;
		});
	}

	var get_previous_news = function() {
		startAction();
		var time_lag = 0;
		var shop_ele_count = 0;
		$($('#rightSectionInner > div > div.shopInfo').get().reverse()).each(function(index, shop_ele){
			var shop_obj = $(shop_ele);
			var img_obj = shop_obj.find('div.shopInfoImgBox:first img:first');
			var text_obj = shop_obj.find('div.shopInfoTextBox:first > dl:first');
			var height = img_obj.outerHeight();  
			var width = img_obj.outerWidth();  

			shop_ele_count += 1;
			if (shop_ele_count == 6) {
				var complete_function_option = {
					duration: efect_time,
					complete: function() {stopAction();}
				}; 
			} else {
				var complete_function_option = {
					duration: efect_time
				};
			}

			setTimeout(function(){
				text_obj.animate({ top : '-' + height },{duration: efect_time});  
				img_obj.animate({
					top    : 0,
					left   : width / 2,
					height : 0,
					width  : 0,
					borderWidth : 0
				},
				{
					duration:efect_time,
					complete: function(){
						var parent_id = shop_obj.parent().get(0).id;
						rightSectionInnerList_obj.prepend(shop_ele);
						var new_shop_obj = $('#rightSectionInnerList > div.shopInfo:last').appendTo('#' + parent_id);
						var new_img_obj = new_shop_obj.find('div.shopInfoImgBox:first img:first');
						var new_text_obj = new_shop_obj.find('div.shopInfoTextBox:first > dl:first');
						new_img_obj.css({
							top    : height,
							left   : width / 2,
							height : 0,
							width  : 0,
							borderWidth : 3
						});
						new_text_obj.css('top', height);
						new_img_obj.animate({ top: 0, left: 0, width: 64, height: 64}, complete_function_option);
						new_text_obj.animate({ top: 0 },{duration: efect_time});
					}
				});  
			}, time_lag);
			time_lag += time_lag_increment;
		});
	}

	var setMouseover = function() {
		$('#rightSection').unbind('mouseover', setMouseover);
		$('#rightSection').bind('mouseout', setMouseout);
		clearInterval(timer);
	}

	var setMouseout = function() {
		$('#rightSection').unbind('mouseout', setMouseout);
		$('#rightSection').bind('mouseover', setMouseover);
		timer = setInterval(get_next_news, interval);
	}
	
	var startAction = function() {
		is_active = true;
		$('#previous_button').attr('src', 'images/arrow_rightSection01_out.gif');
		$('#next_button').attr('src', 'images/arrow_rightSection02_out.gif');
	}

	var stopAction = function() {
		is_active = false;
		$('#previous_button').attr('src', 'images/arrow_rightSection01.gif');
		$('#next_button').attr('src', 'images/arrow_rightSection02.gif');
	}



	var timer = setInterval(get_next_news, interval);
	$('.newsButton').css('visibility', 'visible');
	$('#rightSection').bind('mouseover', setMouseover);
	$('#next_button').bind('click', function(){
		clearInterval(timer);
		if (!is_active) {
			get_next_news();
		}
	});
	$('#previous_button').bind('click', function(){
		clearInterval(timer);
		if (!is_active) {
			get_previous_news();
		}
	});
});
