/**
 * common.js
 * - CarouFredSel起動
 *
 * @author Hiyoshi
 *
 */
$(document).ready(function() {
	var tween_duration = 150;
	var pause_duration = 6000;

	var current_img = $("#carou_fred_sel li:first a:first").attr('rel');
	var main_img = $('#main_image a img');

	var cache = [];

	setTimeout(function(){
		$(main_img).fadeOut(1, function(){
			$(this).attr('src',current_img).fadeIn(tween_duration)
			.parent('a').attr('href', $("#carou_fred_sel li:first a:first").attr('href'));
		});
	}, 1000);

	// CarouFredSel起動
	$("#carou_fred_sel").carouFredSel({
		items				: {
			visible			: 3,
			start			: -1
		},
		direction			: "up",
		infinite			: true,
		height				: 200,
		scroll				: {
			items			: 1,
			effect			: "easeOutBounce",
			duration		: tween_duration*2,
			pauseOnHover	: true,
			onBefore		: function( oldItems, newItems, newSizes, duration ) {
				var img_src = $(newItems[1]).find('a:first').attr('rel');
				var a_href = $(newItems[1]).find('a:first').attr('href');
				if(current_img != img_src){
					current_img = img_src;
					$(main_img).fadeOut(tween_duration, function(){
						$(this).attr('src',current_img).fadeIn(tween_duration)
						.parent('a').attr('href', a_href);
					});
				}
			}
		},
		auto				: {
			play			: true,
			pauseDuration	: pause_duration
		},
		prev				: "#carou_fred_sel_prev > a",
		next				: "#carou_fred_sel_next > a"
	});

	// ロールオーバー時動作
	$("#carou_fred_sel").find("li > a").mouseover(function(){
		var img_src = $(this).attr('rel');
		var a_href = $(this).attr('href');
		if(current_img != img_src){
			current_img = img_src;
			$(main_img).fadeOut(tween_duration, function(){
				$(this).attr('src',img_src).fadeIn(tween_duration)
				.parent('a').attr('href', a_href);
			});
		}
	});

	// プリロード
	$("#carou_fred_sel").find("li > a").each(function(){
		var cacheImage = document.createElement('img');
		cacheImage.src = $(this).attr('rel');
		cache.push(cacheImage);
	});
});

