var tp;
var max;

//10000=10?
var changeTime = 10000;


//for bigbanner
var tpEc;

//10000=10sec
var changeTimeEc = 8000;
var move = 0;

$(function() {
	max = $('#main .paginate li a').size();
	if(max > 1) {
		$('#main .paginate li a').each(function(index) {
			$(this).click(function() {
				changeRecommend(index);
			});
		});
		changeReccomendStart(1);
	}
	
	//for bigbanner
	$('#eyecatch .textWrapper li:eq(0)').show();
	
	
	if ($('#eyecatch .paginate li a').size() > 1) {
		$('#eyecatch .paginate li a').each(function(index) {
			$(this).click(function() {
				changeEyecatch(index);
			});
		});

        $('#eyecatch .imageWrapper li a').each(function(index) {
			$(this).hover(function() {
					clearTimeout(tpEc);
			},
			function() {
				changeEyecatch(index);
			});

			$(this).mouseover(function() {
				clearTimeout(tpEc);
			});
			$(this).mouseout(function() {
				changeEyecatch(index);
			});
		});
	
		startChangeEyecatch(1);
	}
	$("#commonBannerLink").fancybox({
		'titlePosition'		: 'inside',
/*		'width'				: 450,
		'height'			: 240,
		'autoScale'     	: false,
*/		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
//		'type'				: 'iframe'
	});
	$("#bannerzone .banner a").hover(function() {
		$(this).fadeTo(0,0.8);								  
	},
	function() {
		$(this).fadeTo(0,1);								  
	});
	
	
	
});

function changeRecommend(i) {
	var on_image = $('#main .paginate li a.selectedRecommend img').get();
	if(on_image[0].src.match(/.+_on\.(gif|jpg|jpe|jpeg|png)/)) {
		on_image[0].src = on_image[0].src.replace(/(.+)_on(\.(gif|jpg|jpe|jpeg|png))/, '$1$2');
	}
	$('#main #mainRecommendBooks .sectionInner').css('display', 'none');
	$('#main .paginate li a').removeClass('selectedRecommend');
	
	
	$('#main #mainRecommendBooks .sectionInner:eq(' + i + ')').css('display', 'block');
	var image = $('#main .paginate li:eq(' + i + ') a img').get();
	if(image[0].src.match(/.+\.(gif|jpg|jpe|jpeg|png)/)) {
		image[0].src = image[0].src.replace(/(.+)(\.(gif|jpg|jpe|jpeg|png))/, '$1_on$2');
	}
	$('#main .paginate li:eq(' + i + ')').children('a').addClass('selectedRecommend');
	
	clearTimeout(tp);
	
	if(i + 1 >= max) {
		changeReccomendStart(0);
	} else {
		changeReccomendStart(i + 1);
	}
}

function changeReccomendStart(i) {
	tp = setTimeout("changeRecommend(" + i + ")", changeTime);
}

function startChangeEyecatch(index) {
	tpEc = setTimeout("changeEyecatch('" + index + "')", changeTimeEc);
}

function changeEyecatch(index) {
        $('#eyecatch .imageWrapper li a').unbind("mouseover");
        $('#eyecatch .imageWrapper li a').unbind("mouseout");

	$('#eyecatch .textWrapper li').hide();
	var on_image = $('#eyecatch .paginate li a.selectedRecommend img').get();
	if(on_image[0].src.match(/.+_on\.(gif|jpg|jpe|jpeg|png)/)) {
		on_image[0].src = on_image[0].src.replace(/(.+)_on(\.(gif|jpg|jpe|jpeg|png))/, '$1$2');
	}
	
	$('#eyecatch .paginate li a').removeClass('selectedRecommend');
	
	$('#eyecatch .imageWrapper ul').animate({left:-720 * index}, 'slow',setMouse(index));
	$('#eyecatch .textWrapper li:eq(' + index + ')').fadeIn();
	$('#eyecatch .paginate li:eq(' + index + ') img').attr('src', 'images/top/paginate_'+(parseInt(index)+1)+'_on.gif');

	$('#eyecatch .paginate li:eq(' + index + ')').children('a').addClass('selectedRecommend');

	clearTimeout(tpEc);
	
	if(index < $('#eyecatch .textWrapper li').size() - 1) {
		startChangeEyecatch(parseInt(index) + 1);
	} else {
		startChangeEyecatch(0);
	}
}
function setMouse(index) {
        $('#eyecatch .imageWrapper li a:eq(' + index + ')').bind("mouseover",function() {
            clearTimeout(tpEc);
        });
        $('#eyecatch .imageWrapper li a:eq(' + index + ')').bind("mouseout",function() {
            changeEyecatch(index);
        });
}


