/*Hiscroll 类
方法：
scrollLeft
scrollRight
scrollTop
scrollBottom
参数：
ele 滚动的dom元素
type 横向还是纵向 [0|1]
step 滚动步长
*/
$(function(){
	var HiScroll = function(ele,type,step) {
		this.ele = ele;
		this.step = step;
		this.stepWidth = 0;
		this.stepHeight = 0;
		this.doing = false;
		this.type = type;
		this.init(type);
	}
	HiScroll.prototype = {
		init:function(type){
			var that = this;
			var ele = this.ele;
			var stepWidth;
			var a =  ele.children().eq(0);
			var childNum = ele.children().length;
			this.stepWidth = parseInt(a.width()) + parseInt(a.css("padding-left")) + parseInt(a.css("padding-right")) + parseInt(a.css("border-left-width")) + parseInt(a.css("border-right-width")) + parseInt(a.css("margin-left")) + parseInt(a.css("margin-right"));
			this.stepHeight = parseInt(a.height()) + parseInt(a.css("padding-top")) + parseInt(a.css("padding-bottom")) + parseInt(a.css("border-top-width")) + parseInt(a.css("border-bottom-width")) + parseInt(a.css("margin-top")) + parseInt(a.css("margin-bottom"));
			var wra = $("<div>");
			wra.css({"position":"relative"});
			ele.children().clone(true).prependTo(ele).clone(true).appendTo(ele);
			ele.wrap(wra);
			if(type == 0){
				var w = childNum*this.stepWidth;
				ele.css({"position":"absolute","left":-w,"top":0,"width":w*3});
			}else if(type == 1){
				var h = childNum*this.stepHeight;
				ele.css({"position":"absolute","left":0,"top":-h,"height":h*3});
			}
		},
		scrollLeft:function(){
			this.doing = true;
			var that = this;
			var a = parseInt(this.ele.css("left")) - this.stepWidth*this.step;
			this.ele.animate({"left":a},{"duration":500,"complete":function(){that.callBack1()}});
		},
		scrollRight:function(){
			this.doing = true;
			var that = this;
			var a = parseInt(this.ele.css("left")) + this.stepWidth*this.step;
			this.ele.animate({"left":a},{"duration":500,"complete":function(){that.callBack2()}});
		},
		callBack1:function(){
			this.doing = false;
			for(var i=0; i<this.step; i++){
				this.ele.children().eq(0).appendTo(this.ele);
			}
			var a = parseInt(this.ele.css("left")) + this.stepWidth*this.step;
			this.ele.css({"left":a});
		},
		callBack2:function(){
			this.doing = false;
			for(var i=0; i<this.step; i++){
				this.ele.children().eq(this.ele.children().length-1).prependTo(this.ele);
			}
			var a = parseInt(this.ele.css("left")) - this.stepWidth*this.step;
			this.ele.css({"left":a});
		}
	};
	window.HiScroll = HiScroll;
});

//推荐车型
$(function ($) {

	var e = $(".IndexPicCnt>ul");
	if(e.length){
		var leftBtn = $(".IndexRebiew .btnLeft");
		var rightBtn = $(".IndexRebiew .btnRight");
		var myHiScroll = new HiScroll(e,0,3);
		var intervalID = setInterval(function(){myHiScroll.scrollLeft()},5000);
		e.mouseenter(function(){
			clearInterval(intervalID);
		});
		e.mouseleave(function(){
			intervalID = setInterval(function(){myHiScroll.scrollLeft()},5000);
		});
		leftBtn.mouseenter(function(){
			clearInterval(intervalID);
		});
		leftBtn.mouseleave(function(){
			intervalID = setInterval(function(){myHiScroll.scrollLeft()},5000);
		});
		rightBtn.mouseenter(function(){
			clearInterval(intervalID);
		});
		rightBtn.mouseleave(function(){
			intervalID = setInterval(function(){myHiScroll.scrollLeft()},5000);
		});
		leftBtn.click(function(){
			if(!myHiScroll.doing){
				myHiScroll.scrollRight();
			}
		});
		rightBtn.click(function(){
			if(!myHiScroll.doing){
				myHiScroll.scrollLeft();
			}
		});
	}
});
