$(document).ready( function() {
	
	// main banner
	var start = Math.floor(Math.random() * $('#tp-main-banner-images a').size());
	$('#tp-main-banner-images').cycle({
		fx: 'scrollHorz',
		easing: 'easeOutExpo',
		speed: 1000,
		timeout: 5000,
		next: '#next-banner',
		prev: '#prev-banner',
		pause: 1,
		startingSlide: start
	});
	
	// recommend contents
	rcmInit();
	
	// what's new
	newsInit();
	
	// recommended new releases
	simple_tip('#tp-pickup-wrapper a', 'tip_book');
	pickupInit();
	
	// twitter
	$('#timeline').tweet({
		username: ['yasanichi_asuka', 'OBN_soc', 'olex_editors', 'passnasubi', 'hapilab_oyako', 'obunsha_eltg', 'obunsha80'],
		avatar_size: 48,
		count: 3,
		template: "{avatar}{user} {text} {time}",
		loading_text: "loading tweets..."
	}).bind("loaded", function() {$(this).find("a").attr("target", "_blank");});
	
});




// recommended contents - loop

function rcmInit() {
	
	var w = 200;
	var sliding = 0;
	
	$("#tp-recommend-banners ul li:last").clone().prependTo("#tp-recommend-banners ul");
	$("#tp-recommend-banners ul li:eq(1)").clone().appendTo("#tp-recommend-banners ul");
	var max = -($('#tp-recommend-banners ul').children().length - 2);
	
	var x = -(Math.floor(Math.random() * (max * -1 + 1)));
	$("#tp-recommend-banners ul").css('left', w * x);
	
	$('#prev-recommend img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
	$('#next-recommend img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
	
	$("#prev-recommend").click( function() {
		if (!sliding) {
			if (x == 0) {
				$("#tp-recommend-banners ul").css('left', w * max);
				x = max;
			}
			sliding = 1;
			x++;
			var left = w * x;
			$("#tp-recommend-banners ul").animate({left: left}, 'easeOutExpo', function(){ sliding = 0; });
		}
		return false;
	});
	
	$("#next-recommend").click( function() {
		if (!sliding) {
			if (x == max) {
				$("#tp-recommend-banners ul").css('left', 0);
				x = 0;
			}
			sliding = 1;
			x--;
			var left = w * x;
			$("#tp-recommend-banners ul").animate({left: left}, 'easeOutExpo', function(){ sliding = 0; });
		}
		return false;
	});
	
}




// recommended contents - non-loop

function _rcmInit() {
	
	var w = 200;
	var x = 0;
	var max = $('#tp-recommend-banners ul').children().length - 2;
	var sliding = 0;
	
	rcmNav(x, max);
	
	$("#prev-recommend").click( function() {
		if (!sliding && x > 0) {
			sliding = 1;
			x--;
			var left = w * x;
			$("#tp-recommend-banners ul").animate({left: -left}, 'easeOutExpo', function(){sliding = 0});
			rcmNav(x, max);
		}
		return false;
	});
	
	$("#next-recommend").click( function() {
		if (!sliding && x < max) {
			sliding = 1;
			x++;
			var left = w * x;
			$("#tp-recommend-banners ul").animate({left: -left}, 'easeOutExpo', function(){sliding = 0});
			rcmNav(x, max);
		}
		return false;
	});
	
}

function rcmNav(x, max) {
	
	if (x == 0) {
		$('#prev-recommend img').attr('src', 'images/top/top_rcm_btnl.gif');
		$('#prev-recommend img').unbind().fadeTo(150, 1);
		$('#prev-recommend').addClass('disabled');
	} else {
		$('#prev-recommend img').attr('src', 'images/top/top_rcm_btnl_on.gif');
		$('#prev-recommend img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
		$('#prev-recommend').removeClass('disabled');
	}
	
	if (x == max) {
		$('#next-recommend img').attr('src', 'images/top/top_rcm_btnr.gif');
		$('#next-recommend img').unbind().fadeTo(150, 1);
		$('#next-recommend').addClass('disabled');
	} else {
		$('#next-recommend img').attr('src', 'images/top/top_rcm_btnr_on.gif');
		$('#next-recommend img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
		$('#next-recommend').removeClass('disabled');
	}
	
}




// what's new

function newsInit() {
	
	var tab = 0;
	
	$('#tp-information-tab-1 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
	$('#tp-information-tab-2 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
	$('#tp-information-tab-3 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
	
}

function newsInit_tab() {
	
	var tab = 0;
	
	$('#tp-information-tab-1 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
	$('#tp-information-tab-2 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
	$('#tp-information-tab-3 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
	
	$('#tp-information-tab-1').click( function() {
		if (tab != 1) {
			$('#tp-information-' + tab).fadeOut(500, function() {
				$('#tp-information-1').fadeIn(1000);
			});
			$('#tp-information-tab-1 img').attr('src', 'images/top/top_info_tab_topics_on.gif');
			$('#tp-information-tab-2 img').attr('src', 'images/top/top_info_tab_newsrelease.gif');
			$('#tp-information-tab-3 img').attr('src', 'images/top/top_info_tab_publicity.gif');
			$('#tp-information-tab-1 img').unbind().fadeTo(150, 1);
			$('#tp-information-tab-2 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
			$('#tp-information-tab-3 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
			tab = 1;
		}
		return false;
	});
	
	$('#tp-information-tab-2').click( function() {
		if (tab != 2) {
			$('#tp-information-' + tab).fadeOut(500, function() {
				$('#tp-information-2').fadeIn(1000);
			});
			$('#tp-information-tab-1 img').attr('src', 'images/top/top_info_tab_topics.gif');
			$('#tp-information-tab-2 img').attr('src', 'images/top/top_info_tab_newsrelease_on.gif');
			$('#tp-information-tab-3 img').attr('src', 'images/top/top_info_tab_publicity.gif');
			$('#tp-information-tab-1 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
			$('#tp-information-tab-2 img').unbind().fadeTo(150, 1);
			$('#tp-information-tab-3 img').hover( function(){$(this).fadeTo(150, 0.5);}, function(){$(this).fadeTo(150, 1);} );
			tab = 2;
		}
		return false;
	});
	
}




// recommended new releases

function pickupInit() {
	
	$('#tp-pickup li img').animate({width: 60}, 0);
	$('#tp-pickup li').mouseover( function() {
			$('#tp-pickup ul').stop().animate({left: 1}, 150);
			gridimage = $(this).find('img');
			gridimage.stop().animate({width: 88}, 150);
			//$(this).find('div').show();
		}).mouseout( function() {
			$('#tp-pickup ul').stop().animate({left: 18}, 150);
			gridimage.stop().animate({width: 60}, 150);
			//$(this).find('div').hide();
	});
	
}
