// JavaScript Document
// utf-8

//============================= 广告图片滚动类 =======================
//构建类的参数 imgObj 图片对应的类  tipsObj 图片附加内容显示框
function Class_advReturn(imgObj,tipsObj){
	this.turnSpeed=3500;   //轮播间隔时间
	this.img=imgObj;       //图片对象
	this.tips=tipsObj;     //附加内容
	this.nowID=0;          //当前图片显示游标
	this.imgArray=new Array();  //存放图片对象数组
	this.tipArray=new Array();  //存放图片附加内容数组
	this.timeID="";             //时间函数变量
	
	var obj=this;
	//初始化
	//写入图片对象到数组
	this.img.each(function(){
      obj.imgArray.push($(this));
   });
	if (this.tips){
		var tempStr="";
		for (var i=0;i<this.imgArray.length;i++){
			var word="";
			if (this.imgArray[i][0].tagName!="img"){
				word=this.imgArray[i].children("img").attr("title");
			}
			else{
				word=this.imgArray[i].attr("title");
			}
			if (this.nowID==i){
			   tempStr+="<div class='advButton sel'>"+word+"</div>";
			}
			else{
				tempStr+="<div class='advButton'>"+word+"</div>";
			}
		}
		this.tips.html(tempStr);
		//添加至数组
		tipsObj.children(".advButton").each(function(){
         obj.tipArray.push($(this));
		});
	}
	for (var i=0;i<this.imgArray.length;i++){
		if (this.nowID==i){
			this.imgArray[i].css("display","block");
		}
		else{
			this.imgArray[i].css("display","none");
		}
	}
	
	//绑定图片附件元素的时间
	if (this.tips){
	   this.tips.children().click(function(){
			clearInterval(obj.timeID)
			var nowID;
         for (var i=0;i<obj.tipArray.length;i++){
				if (obj.tipArray[i].html()==$(this).html()){
					nowID=i;
					break;
				}
			}
			obj.AutoTurn(nowID);
         obj.goTurn();
      });
	}
}
//图片自动轮播
Class_advReturn.prototype.AutoTurn=function(nextID){
	var obj=this;
	var id=this.nowID;   //当前图片ID
	var j;               //需要切换的图片ID
	if (nextID+""!=""){
		if (nextID==id){
			return false;
		}
		j=nextID;
	}
	else{
	   j=id+1
	   if (j>=this.imgArray.length){
		   j=0;
	   }
	}
	this.imgArray[j].css("zIndex",1).css("opacity",1).show();
	this.imgArray[id].css("zIndex",6).animate({opacity:0},500,function(){obj.imgArray[id].hide();});
	this.nowID=j;
	if (this.tips){
		for (var i=0;i<this.tipArray.length;i++){
			if (i==j){
				this.tipArray[i].addClass("sel");
			}
			else{
				this.tipArray[i].removeClass("sel");
			}
		}
	}
}
Class_advReturn.prototype.goTurn=function(){
	var obj=this;
	this.timeID=setInterval(function(){obj.AutoTurn("");},this.turnSpeed);
}

//============================= 类结束 ==============================


//推荐商品左右移动效果实现
function moveSPtuijian(position){
   var perWidth=178;                                 //每张图片占据的宽度
	var allWidth=$("#SPtuijian")[0].scrollWidth;      //商品推荐移动框总宽
	var leftWidth=$("#SPtuijian")[0].scrollLeft;      //隐藏滚动条的横向滚动值
	var limitWidth=$("#SPtuijian").width();           //显示框宽
	i=0;
	if (position=="right"){
	   while(perWidth*i<=limitWidth && perWidth*i<allWidth-(leftWidth+4*perWidth)){
		   i++;
	   }
		$("#SPtuijian").animate({scrollLeft:leftWidth+perWidth*i},500);
	}
	else{
		while(leftWidth-perWidth*i>0 && perWidth*i<=limitWidth){
			i++;
		}
		$("#SPtuijian").animate({scrollLeft:leftWidth-perWidth*i},500);
	}
}

//显示推荐商品
function showTuijian(tuijian){
	var Datalist="tj="+tuijian
	Ajaxpage("getTuijian.asp",Datalist,$("#SPtuijian"),"");
}

var tuijian="";

$(document).ready(function(){
	//下拉菜单类
   var selectObj=new Class_selectionList($("#searchBar_value"),$("#b_searchBar"),$("#searchBar_Menu").children(".subMenu"));
	//广告图片轮播类
	var advObj=new Class_advReturn($("#advImages").children(),$("#advButton"));
	//图片播放
	advObj.goTurn();
	
	//商品左侧类别/品牌菜单点击事件
	$("#part2").hide();   //先隐藏品牌分类
   $(".topMenu").children("li").click(function(){
		if ($(this)[0].id=="byMenu1"){
			$("#byMenu2").addClass("sel");
			$(this).removeClass("sel");
			$("#part1").show();
			$("#part2").hide();
		}
		else{
			$("#byMenu1").addClass("sel");
			$(this).removeClass("sel");
			$("#part2").show();
			$("#part1").hide();
		}
	});
	
	//商品排行
	$(".topProgram").children("li").click(function(){
      $(".topProgram").children("li").children("span").each(function(){
         $(this).removeClass("sel");
		});
		$(this).children("span").addClass("sel");
   });
	
	//显示推荐商品
	tuijian="woman"
	showTuijian(tuijian);
	
	//推荐商品
	$("#commend").children("ul").children("li").click(function(){
      var obj=$(this);
		$("#commend").children("ul").children("li").each(function(){
			var imgpath=$(this).children("img").attr("src");
		   imgpath=imgpath.replace("images/tuijian_","");
		   imgpath=imgpath.replace(".gif","");												  
			if ($(this).children("img").attr("src")==obj.children("img").attr("src")){
				if (ChkReg(imgpath,"^\\da$")){
					$(this).children("img").attr("src","images/tuijian_"+imgpath.replace("a","")+".gif");
					tuijian=$(this).children("img")[0].id;
					showTuijian(tuijian);
				}
			}
			else{
				if (!ChkReg(imgpath,"^\\da$")){
					imgpath=imgpath+"a";
				}
				$(this).children("img").attr("src","images/tuijian_"+imgpath+".gif");
			}
		});
	});
	
	//推荐商品移动
	$("#tjMoveright").click(function(){
		moveSPtuijian("right");
	});
	
	$("#tjMoveleft").click(function(){
		moveSPtuijian("left");
	});
	
	$("#tjMoveright").hover(
		function(){$(this).attr("src","images/rightMove_a.gif");},
		function(){$(this).attr("src","images/rightMove.gif");}
	);
	$("#tjMoveleft").hover(
		function(){$(this).attr("src","images/leftMove_a.gif");},
		function(){$(this).attr("src","images/leftMove.gif");}
	);
});

