/**
 * 全屏幻灯片类
 */
function SlideLeftRight(DivIdName){
	//幻灯节点对象
	this.DivObj=$("#"+DivIdName);
	//大图节点对象
	this.DivBigImgObj=$("#"+DivIdName+" .list");
	this.BigLiArrUl = $("#"+DivIdName+" .list ul");
	this.BigLiArr=$("#"+DivIdName+" .list ul li");
	//缩略图节点对象
	this.next = $("#"+DivIdName+" .left");
	this.prev = $("#"+DivIdName+" .right");
	this.theWidth = this.BigLiArr.width();
	if(this.theWidth <= 0){
		this.theWidth = this.DivObj.width();
	}
	//alert(this.theWidth);
	this.DivObj.append("<div class='Thumb'></div>");
	this.DivThumbObj=$("#"+DivIdName+" .Thumb");
	this.ThumbLiObj=null;
	this.fangxiang = "Left";
	this.isHover = false;
	//其他设置
	this.index=1;
	this.timeer=null;
	this.speed=8000;//切换速度
}

/**
 * 初始化
 */
SlideLeftRight.prototype.Init=function(n){
	this.BigLiArrUl.css("width",(this.theWidth * (this.BigLiArr.length+2)));
	this.BigLiArr.css("width",this.theWidth);
	this.SetBigMarginLeft();
	this.SetBtn();
	this.BindEvt();
	this.BigLiArrUl.append(this.BigLiArr.eq(0).clone());
	this.BigLiArrUl.prepend(this.BigLiArr.last().clone());
	this.BigLiArrUl.css("left",-(this.theWidth*this.index));
	this.ShowOne();
	this.BigLiArr=this.DivObj.find(" .list ul li");
	this.Paly();
}

/**
 * 设置大图距离左边的位置
 */
SlideLeftRight.prototype.SetBigMarginLeft = function(){
	var width = $(this.DivBigImgObj).width();
	for (var i = 0; i < this.BigLiArr.length; i++) {
		this.SetSize(this.BigLiArr[i]);
	}
}

/*设置居中*/
SlideLeftRight.prototype.SetSize=function(LiObj) {
	var theWidht=parseInt($(this.DivBigImgObj).width(),10);
	var imgObj=$(LiObj).find("img").get(0);
	var width=parseInt($(imgObj).attr("oldW"),10);
	var height=parseInt($(imgObj).attr("oldH"),10);
	var nowHeight=parseInt($(imgObj).height(),10);
	var nowWidht=parseInt(width*nowHeight/height,10);
	var left=parseInt((theWidht-nowWidht)/2,10);
	$(imgObj).css({"left":left+"px"});
}

/**
 * 设置按钮
 */
SlideLeftRight.prototype.SetBtn = function(){
	var width=parseInt(20 * this.BigLiArr.length, 10);
	var theHtml = "<ul style='width:" + width + "px;'>";
	for (var i = 0; i < this.BigLiArr.length; i++) {
		//theHtml += "<li rel='" + i + "'>" + parseInt(i + 1, 10) + "</li>";
		theHtml += "<li rel='" + i + "'>&nbsp;</li>";
	}
	theHtml += "</ul>";
	$(this.DivThumbObj).html(theHtml);
	//$(this.DivThumbObj).css({marginLeft:"-"+parseInt(width/2,10)+"px"});
	this.ThumbLiObj = $(this.DivThumbObj).find("li");
	$(this.ThumbLiObj[0]).addClass("onfocus");
}

/**
 * 为大图及按钮绑定事件
 */
SlideLeftRight.prototype.BindEvt = function(){
	var theObj = this;
	$(this.BigImgArr).hover(function(){
		clearInterval(theObj.timeer);
	}, function(){
		theObj.Paly();
	});
	$(this.ThumbLiObj).hover(function(){
		clearInterval(theObj.timeer);
		theObj.index=parseInt($(this).attr("rel"),10)+1;
		theObj.ShowOne();
		
	}, function(){
		theObj.Paly();
	});
	
	this.next.click(function(){
		clearInterval(theObj.timeer);
		theObj.index++;
		theObj.fangxiang = "Left";
		theObj.ShowOne();
		theObj.Paly();
	})
	
	this.prev.click(function(){
		clearInterval(theObj.timeer);
		theObj.index--;
		theObj.fangxiang = "Right";
		theObj.ShowOne();
		theObj.Paly();
	})
	
	$(window).resize(function(){
		//theObj.theWidth = $(window).width()<1003?1003:$(window).width();
//		theObj.BigLiArrUl.css("width",(theObj.theWidth * theObj.BigLiArr.length));
//		theObj.BigLiArr.css("width",theObj.theWidth);
//		theObj.SetBigMarginLeft();
//		theObj.ShowOne();
	})
	
}

/**
 * 播放
 */
SlideLeftRight.prototype.Paly=function(){
	var theObj=this;
	this.timeer=setInterval(function(){theObj.setPaly()},this.speed);
}

/*播放设置*/
SlideLeftRight.prototype.setPaly=function(){
	if(this.fangxiang == "Left"){
		this.index++;
	}else{
		this.index--;
	}
	this.ShowOne();
}

/**
 * 显示一个
 */
SlideLeftRight.prototype.ShowOne=function(){
	var theObj = this;
	
	theObj.BigLiArrUl.stop().animate({
		"left":-(theObj.theWidth * theObj.index)
	},800,function(){
		if(theObj.index > theObj.BigLiArr.length-2){
			theObj.index = 1;
			theObj.BigLiArrUl.css("left",-(theObj.theWidth));
		}else if(theObj.index < 1){
			theObj.index = theObj.BigLiArr.length-2;
			theObj.BigLiArrUl.css("left",-(theObj.theWidth*(theObj.index)));
		}
	})
	
	
	$(theObj.ThumbLiObj).removeClass("onfocus");
	
	if(theObj.index > (theObj.ThumbLiObj.length)){
		$(theObj.ThumbLiObj[0]).addClass("onfocus");
	}else if(theObj.index < 1){
		$(theObj.ThumbLiObj[theObj.ThumbLiObj.length-1]).addClass("onfocus");
	}else{
		$(theObj.ThumbLiObj[theObj.index-1]).addClass("onfocus");
	}
		


	
	
}

