$( function() {

	var totalPanels			= $(".scrollContainer").children().size();
	var regParSize			= $(".panel p").css("font-size");

	var movingDistance	    = 218;

	var $panels				= $('#slider .scrollContainer > div');
	var $container			= $('#slider .scrollContainer');

	$panels.css({'float' : 'left','position' : 'relative'});

	$("#slider").data("currentlyMoving", false);

	$container
	.css('width', ($panels[0].offsetWidth * $panels.length))
	.css('left', 0);

	var scroll = $('#slider .scroll').css('overflow', 'hidden');

	$panels.each( function(i) {
		if($panels.length>=3 && i==2) {
			$(this).attr("class","lastPanel");
		} else if($panels.length<3 && i == $panels.length -1) {
			$(this).attr("class","lastPanel");
		}
	});
	//direction true = right, false = left
	function change(direction) {
		//alert(totalPanels);
		//alert(curPanel);
		//alert(totalPanels - curPanel);
		//if not at the first or last panel
		if((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))||(direction && (totalPanels - curPanel == 2))) {
			return false;
		}

		//if not currently moving
		if (($("#slider").data("currentlyMoving") == false)) {

			$("#slider").data("currentlyMoving", true);

			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;

			$(".scrollContainer")
			.stop()
			.animate({
				"left": movement
			}, function() {
				$("#slider").data("currentlyMoving", false);
			});
			//如果向右移
			if(direction) {
				$panels.eq(curPanel+1).attr('class','panel');
				$panels.eq(curPanel+2).attr('class','lastPanel');
			} else {
				$panels.eq(curPanel).attr('class','lastPanel');
			}
			curPanel = next;

			//remove all previous bound functions
			$("#panel_"+(curPanel+1)).unbind();

			//go forward
			$("#panel_"+(curPanel+1)).click( function() {
				change(true);
			});
			//remove all previous bound functions
			$("#panel_"+(curPanel-1)).unbind();

			//go back
			$("#panel_"+(curPanel-1)).click( function() {
				change(false);
			});
			//remove all previous bound functions
			$("#panel_"+curPanel).unbind();
		}

		registerEvent_();

	}

	// Set up "Current" panel and next and prev
	var curPanel = 1;

	$("div.inside").click(function(){
		var url = $(this).attr("id");
		if(url!="")
			window.location.href = url;
	});
	

	//when the left/right arrows are clicked
	$(".right").click( function() {
		change(true);
	});
	$(".left").click( function() {
		change(false);
	});
	$(window).keydown( function(event) {
		switch (event.keyCode) {
			case 13:
				//enter
				$(".right").click();
				break;
			case 32:
				//space
				$(".right").click();
				break;
			case 37:
				//left arrow
				$(".left").click();
				break;
			case 39:
				//right arrow
				$(".right").click();
				break;
		}
	});
	function registerEvent_() {
		$(".panel,.lastPanel").hover( function(e) {
			$(this).addClass("panel_");
		}, function(e) {
			$(this).removeClass("panel_");
		}
		);
	}

	registerEvent_();

});


(function() {
	
	var yidatec = window.yidatec = {};
	yidatec.ads_slider = function(){

		//图片滚动时间间隔
		var play = 3000;
		var control = $("#thumbs");
		var kids = $("#preview_inner").find("div");
		//创建图片滚动按钮
		control.append('<a class="nivo-prevNav"></a>');
		for(var i = 0; i < kids.length; i++) {
			control.append('<a class="nivo-control">'+(i+1)+'</a>');
		}
		control.append('<a class="nivo-nextNav"></a>');

		// Save  the jQuery objects for later use.
		var outer   = $("#preview_outer");
		var thumbs    = $("#thumbs a.nivo-control");

		//设置自动滚动
		var timer = 0;
		if(kids.length > 1) {
			timer = setInterval( function() {
				var index = thumbs.index($("#thumbs a.active"));
				if(index == thumbs.length-1) {
					index = -1;
				}
				index = index +1;
				sliderRun(index);
			}, play);
			//默认选中第一个
			thumbs.eq(0).addClass('active');
		}

		var preview_pos;
		var preview_els = $("#preview_inner div");
		var image_width = preview_els.eq(0).width(); // Get width of imaages

		// 选择编号 显示编号对应的图片
		thumbs.click( function() {
			var index = thumbs.index( this);
			sliderRun(index);
		});
		// Reset positions on load
		outer.animate( {'scrollLeft' : 0}, 0 );

		// Set initial width
		$("#preview_inner").css('width', preview_els.length * image_width);

		$("a.nivo-prevNav").hover( function(e) {
			$(this).attr("class","prevHover");
		}, function(e) {
			$(this).attr("class","nivo-prevNav");
		});
		$("a.nivo-nextNav").hover( function(e) {
			$(this).attr("class","nextHover");
		}, function(e) {
			$(this).attr("class","nivo-nextNav");
		});
		$(".nivo-prevNav").click( function() {
			var index = thumbs.index($("#thumbs a.active"));
			if(index == 0)
				index = thumbs.length;
			index = index-1;
			sliderRun(index);
		});
		$(".nivo-nextNav").click( function() {
			var index = thumbs.index($("#thumbs a.active"));
			if(index == thumbs.length-1) {
				index = -1;
			}
			index = index +1;
			sliderRun(index);
		});
		sliderRun = function(index) {
			preview_pos = preview_els.eq( index).position();
			thumbs.eq(index).addClass('active');
			for(var i=0;i<thumbs.length;i++) {
				if(i!=index)
					thumbs.eq(i).removeClass('active');
			};
			outer.stop().animate( {'scrollLeft' : preview_pos.left},  500 );
		}
		//当鼠标停放在图片上时停止滚动 	当鼠标离开图片后开始滚动
		$("#preview_inner div,#thumbs").hover( function(e) {
			clearInterval(timer);
		}, function(e) {
			timer = setInterval( function() {
				var index = thumbs.index($("#thumbs a.active"));
				if(index == thumbs.length-1) {
					index = -1;
				}
				index = index +1;
				sliderRun(index);
			}, play);
		}
		);
	}
})();

