/**
 *@author readkid
 *@dependence jquery 1.2
 *@since 2010/1/15
 *@version 1.0
 */ 
(function () {
	//using namespace $.suning;
	$.suning || ($.suning = {});
	//Extends
	$.suning.extend = function (subClass, superClass) {
		var F = function() {};
		F.prototype = superClass.prototype;
		subClass.prototype = new F();
		superClass.prototype.constructor = superClass;
		subClass.prototype.constructor = subClass;
		subClass.base = superClass.prototype;
	}
})();

//SideNav
$.suning.MenuNav = function() {
	if($("#SideNav").css("display") == "none"){
		$(".sn_Menu").hover(function(){
			$("#SideNav").show();						 
		},function(){
			$("#SideNav").hide();	
		});
	};
	
	$("#SideNav").find("dd").each(function(i){
		$(this).hover(function(){
		$(this).find(".icon").addClass("on");	
			$(this).parent().find("div").css("display","block");
			$(this).parent().find("div").hover(function(){
				$("#SideNav").find("dd").find(".icon").eq(i).addClass("on");											
				$(this).css("display","block");									
				},function(){
				$(this).css("display","none");	
				$("#SideNav").find("dd").find(".icon").eq(i).removeClass("on");				
			});
		},function(){
			$(this).find(".icon").removeClass("on");	
			$(this).parent().find("div").css("display","none");
		})										   
	})			   
}


/* 首页八联版 */
$.suning.SNTabFlash = function(){
	var len = $("#SNflashBtn").find("li").length ;
	var index = 0 ;
	var timer,settime;
	$("#SNflashContent").find("ul").width( 580*len );
	$("#SNflashBtn").find("li").click(function(e){
		index  =  $("#SNflashBtn li").index(this);
		settime = setTimeout(function(){scrollAuto(index)},60);
	})
	
	$("#SNflash").hover(function(){
		 clearInterval(timer);
	},function(){
		timer = setInterval(function(){
		   scrollAuto(index);			
		   index++;
		   if(index==len){index=0;}
	    } , 3600);
	}).trigger("mouseleave");
	
	function scrollAuto(index){
		$("#SNflashBtn").find("li").removeClass("on").eq(index).addClass("on");				   
		$("#SNflashContent").find("ul").stop().animate({"marginLeft":-580*index},300);	
	};		
}

/* tab点击 */
$.suning.TabClick = function(id,showId){
	$("#"+id).find("li").each(function(i){
		$(this).find("a").mouseover(function(e){
			$(this).addClass("on").parent().siblings().find("a").removeClass("on");	
			$("."+showId).hide().eq(i).show();
		})
	});	
}


/* 切换事件 */
$.suning.IndexTab = function(){
	var index = 0 ;
	var timer,settime;
	
	$("#sn_sevice").hover(function(){
		$("#sn_seviceMore").show();						   
	},function(){
		$("#sn_seviceMore").hide();
	});
	
	$("#basket").hover(function(){
		$("#basketShow").show();						   
	},function(){
		$("#basketShow").hide();
	});
	
	var len = $("#Package").find("li").length ;
	$("#Package").find(".Package ul").width(576*len);
	$("#Package").find(".Packageright a").click(function(){
		if(index>=len-1){
			return false;
		}												 
		index ++ ;												 
		$("#Package").find(".Package ul").stop().animate({"marginLeft":-576*index},300);
	});
	
	$("#Package").find(".Packageleft a").click(function(){
		if(index<=0){
			return false;
		}												 
		index -- ;												 
		$("#Package").find(".Package ul").stop().animate({"marginLeft":-576*index},300);
	});
	
	$("#Package").hover(function(){
		 clearInterval(timer);
	},function(){
		timer = setInterval(function(){
		   if(index==len-1){index=-1;}						 
		   index++;						
		   $("#Package").find(".Package ul").stop().animate({"marginLeft":-576*index},300);		
		} , 4000);
	}).trigger("mouseleave");
	
	
	$("#promoreTab").find("a").click(function(e){
		e.preventDefault();									  
		index  =  $("#promoreTab a").index(this);	
		scrollAuto(index);
	});	
	
	$("#promoreTab").hover(function(){
		 clearInterval(timer);
	},function(){
		timer = setInterval(function(){
		   scrollAuto(index);			
		   index++;
		   if(index==7){index=0;}
	    } , 3600);
	}).trigger("mouseleave");
	
	function scrollAuto(index){
		$("#promoreTab").find("a").eq(index).addClass("on").siblings().removeClass("on");	
		$("#promoreTabList li").hide().eq(index).show();		
	};
}



//排行榜
var snTopList = function(tabId,id){
	var btn = $('#'+id).find('li');
	var len = btn.length ;
	return{
		init:function(){
			var that = this ;
			$("#"+tabId).find("a").click(function(e){
				e.preventDefault();								 
				$(this).addClass("on").parent().siblings().find("a").removeClass("on");
				var href = $(this).attr("href");
				$("#"+id).html("<div class='snToploading'></div>");
				$.post(href,function(data){
					$("#"+id).html(data);	
					snTopList('indexTab01','indexTopList01').changelist();	
					snTopList('indexTab02','indexTopList02').changelist();	
					snTopList('indexTab03','indexTopList03').changelist();
					snTopList('indexTab04','indexTopList04').changelist();
				});
			}).bind('focus', function(){    
				if(this.blur){ 
				   this.blur();    
				}    
  			});							  
			
			that.changelist();
			
		},
		changelist:function(){
			btn.each(function(i){
				$(this).mouseover(function(){
					$(this).addClass('hover').siblings().removeClass('hover');
					btn.eq(0).find(".hot_show").hide();
					btn.eq(0).find(".hot_hide").show();
					for(var j=0;j<len;j++){
						btn.eq(j).find(".hot_show").hide();
						btn.eq(j).find(".hot_hide").show();
					}
					$(this).find(".hot_show").show();
					$(this).find(".hot_hide").hide();
				});
			});
		}
	}
}

//tickcount
$.suning.spiketTime = function(startTime, endTime) {
	if (endTime <=0) 
		return;
	var base = $("#Sn_spike_time span")
	var s1 = base.eq(1);
	var s2 = base.eq(2);
	var s3 = base.eq(3);
	var type = 1;
	
	if (startTime < 0) {
		base.eq(0).html("距离结束时间：")
	
	} else {
			base.eq(0).html("距离开始时间：")
	}
	
	
	function formatTime(t) {
			if (t > 0) {
				var totalSecond = t / 1000;
				var minute = Math.floor(totalSecond / 60);
				var hour = Math.floor(minute / 60);
				minute = minute % 60;
				var second = Math.floor(totalSecond % 60);
				return [hour, minute, second];
				
			} else {
				return [0, 0, 0];
				
			}
	}
	
	function changeType() {
		base.eq(0).html("距离结束时间：")
	}
	
	var timerLeft = function(t, e) {
		var start = (new Date()).getTime();
		
		return {
			getLeft: function() {
				var now = (new Date()).getTime();
				var s = t - (now - start);
				if (s <=0 && type == 1) {
					changeType();
					
					this.getLeft = function() {
						var now = (new Date()).getTime();
						var s = e - (now-start);
						return s;
					}
					return this.getLeft();
				}
				return s;
			},
			render: function() {
				var arr = formatTime(this.getLeft());
				if(arr[0] > 9){
					s1.css('letter-spacing','0');
				}else{
					s1.css('letter-spacing','0');	
				}
				if(arr[1] > 9){
					s2.css('letter-spacing','0');
				}else{
					s2.css('letter-spacing','0');
				}
				if(arr[2] > 9){
					s3.css('letter-spacing','1px');
				}else{
					s3.css('letter-spacing','0');	
				}
				s1.html(arr[0]);
				s2.html(arr[1]);
				s3.html(arr[2])
			}
		}
	};
	
	var tl = timerLeft(startTime, endTime);
	tl.render();
	setInterval(function() {
		tl.render();
	},1000);
}

$(document).ready(function() {
	$.suning.MenuNav();					   
	$.suning.SNTabFlash();
	$.suning.TabClick('snTab','sn_noice_list');
	$.suning.IndexTab();
	snTopList('indexTab01','indexTopList01').init();	
	snTopList('indexTab02','indexTopList02').init();
	snTopList('indexTab03','indexTopList03').init();
	snTopList('indexTab04','indexTopList04').init();
	
	$.suning.spiketTime(2001,7000002);			//距离开始时间的毫秒数，距离结束时间的毫秒数
	$(".allProItem").mouseover(function(){$(this).addClass("allProItemFocus");}).mouseout(function(){$(this).removeClass("allProItemFocus");});	
});


