(function($) {
	$.fn.marquee = function(o) {
		//获取滚动内容内各元素相关信息
		o = $.extend({
			speed:		parseInt($(this).attr('speed')) || 30, // 滚动速度
			step:		parseInt($(this).attr('step')) || 1, // 滚动步长
			direction:	$(this).attr('direction') || 'up', // 滚动方向
			pause:		parseInt($(this).attr('pause')) || 1000 // 停顿时长
		}, o || {});
		var dIndex = jQuery.inArray(o.direction, ['right', 'down']);
		if (dIndex > -1) {
			o.direction = ['left', 'up'][dIndex];
			o.step = -o.step;
		}
		var mid;
		var div 		= $(this); // 容器对象
		var divWidth 	= div.innerWidth(); // 容器宽
		var divHeight 	= div.innerHeight(); // 容器高
		var ul 			= $("ul", div);
		var li 			= $("li", ul);
		var liSize 		= li.size(); // 初始元素个数
		var liWidth 	= li.width(); // 元素宽
		var liHeight 	= li.height(); // 元素高
		var width 		= liWidth * liSize;
		var height 		= liHeight * liSize;
		if ((o.direction == 'left' && width > divWidth) || 
			(o.direction == 'up' && height > divHeight)) {
			// 元素超出可显示范围才滚动
			if (o.direction == 'left') {
				ul.width(2 * liSize * liWidth);
				if (o.step < 0) div.scrollLeft(width);
			} else {
				ul.height(2 * liSize * liHeight);
				if (o.step < 0) div.scrollTop(height);
			}
			ul.append(li.clone()); // 复制元素
			mid = setInterval(_marquee, o.speed);
			div.hover(
				function(){clearInterval(mid);},
				function(){mid = setInterval(_marquee, o.speed);}
			);
		}
		function _marquee() {
			// 滚动
			if (o.direction == 'left') {
				var l = div.scrollLeft();
				if (o.step < 0) {
					div.scrollLeft((l <= 0 ? width : l) + o.step);
				} else {
					div.scrollLeft((l >= width ? 0 : l) + o.step);
				}
				if (l % liWidth == 0) _pause();
			} else {
				var t = div.scrollTop();
				if (o.step < 0) {
					div.scrollTop((t <= 0 ? height : t) + o.step);
				} else {
					div.scrollTop((t >= height ? 0 : t) + o.step);
				}
				if (t % liHeight == 0) _pause();
			}
		}
		function _pause() {
			// 停顿
			if (o.pause > 0) {
				var tempStep = o.step;
				o.step = 0;
				setTimeout(function() {
					o.step = tempStep;
				}, o.pause);
			}
		}
	};
})(jQuery);

$(document).ready(function(){
		$(".marquee").each(function() {
			$(this).marquee();
		});
		$("#pointer").css("top","244px");
		$("#pointer").css("left","78px");
		
//		var s = "http://hellobooky.com/images/flash/index/"		
//		$.imgpreload([s+"01.jpg",s + "02.jpg",s + "03.jpg",s + "04.jpg",s + "05.jpg"],
//		{
//			all: function(){
			$("div.imgload").hide();
			$("div.indexflash").show();
			flash();
//			}
//		})
 		$('#productList').lavaLamp({
			speed: 200,
			homeTop:-1,
			homeLeft:-1
		});
});

$.fn.cycle.defaults.speed   = 1000;
$.fn.cycle.defaults.timeout = 6000;
var count=0;
var paused= false;
var isanimating= false;
function slideShow(idx){
	if (isanimating){
		$(document).oneTime(50,"transition1",function(){
			slideShow(idx);	
		});	
	}else{
		$(document).stopTime("transition1");
		$('#s1').cycle(idx);
	}
}

function flash(){
	
	$('.latest_span').fadeTo("fast", 0.3);
	$('.thumbcurrent').fadeTo("fast",0.8);
	$("#thumbtimerattach").everyTime(4000,"myslideshow",function(){
		count = (count +1)%5;

		slideShow(parseInt(count));
	});
	$('#s1').cycle({ 
			fx:      'scrollDown', 
			speed: 600,
			timeout:  0,
			before : function(curr, next, opts){
				$('.thumb span.thumbcurrent').fadeTo("fast", 0.3).removeClass("thumbcurrent");
				$('.thumb span').eq(parseInt(count)).fadeTo("fast",1).addClass("thumbcurrent");
				isanimating = true;
			},
			after : function(currSlideElement, nextSlideElement, options, forwardFlag){
				isanimating = false;
			}
	});

	$(".thumb").mouseover(function(){
				$("#thumbtimerattach").stopTime("myslideshow");	
	}).mouseout(function(){  
				$("#thumbtimerattach").stopTime("myslideshow");	
				$("#thumbtimerattach").everyTime(4000,"myslideshow",function(){
						count = (parseInt(count) +1)%5;
						slideShow(parseInt(count));
				});
	});
	 $(".thumb span.latest_span").hoverIntent({
			sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
			interval: 30, // number = milliseconds for onMouseOver polling interval    
			over: function(){
				ref = $(this).attr("ref");
				if (count == parseInt(ref)){
				}else{
					count = parseInt(ref)
					slideShow(parseInt(count));
				}
			}, 
			timeout: 500,
			out : function (){}
	 });
}
