/**
 *@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;
	}
})();

//Accordion
$.suning.kidtab = function(tabs, datas, curId, tabon, dataon, onchange) {
	this.tabs = tabs;
	this.datas = datas;
	this.curId;
	this.tabon = tabon;
	this.dataon = dataon;
	this.ontabChange = onchange || (function(){});
	this.init(curId);
}
$.suning.kidtab.prototype = {
	init: function(locId) {
		var ref = this;
		this.tabs.each(function(i) {
			
			$(this).click(function(e) {
				if (this.tagName == "A")  e.preventDefault();
				this.blur();
				if (ref.curId != i) 
					ref.changeTab(i);
			});
		});
		ref.changeTab(locId);
	},
	changeTab: function(loc) {
		if (this.curId || this.curId == 0) {
			$(this.tabs[this.curId]).removeClass(this.tabon);
			if (this.dataon && this.dataon.length > 0) {
				$(this.datas[this.curId]).removeClass(this.dataon);
			} else {
				$(this.datas[this.curId]).css("display", "none");
			}
		}
		this.ontabChange(loc);
		this.curId = loc;
		$(this.tabs[this.curId]).addClass(this.tabon);
		if (this.dataon) {
			$(this.datas[this.curId]).addClass(this.dataon);
		} else {
			$(this.datas[this.curId]).css("display", "block");
		}
		
	}
}

//basket 
$.suning.basket = function(){
	var basket = $('#basket');
	var basket_out = $('#basket_out');
	return{
		init:function(){
			basket.find('a').hover(function(){
				basket_out.css('display','block');					  
			},function(){
				basket_out.hover(function(){
					basket_out.css('display','block');							  
				},function(){
					basket_out.css('display','none');
					});
				basket_out.css('display','none');
			})
		}		
	}
}

//UIScrollBar
$.suning.scrollBar = function(lab, arrow, type) {
	var ref = this;
	this.bp = type == 0 ? parseInt($(lab).css("top")) :  parseInt($(lab).css("left"))
	this.startp = 0;
	this.lab = $(lab);
	this.type = type;
	this.percent = 0;
	this.step = .1;
	if (arrow.length > 1)  this.arrow = arrow;
}
$.suning.scrollBar.prototype = {
	onMousemove: function(e) {
		var ref = e.data.ref;
		var tx = ref.type == 0 ? (e.pageY - ref.bpoint.top ): (e.pageX - ref.bpoint.left);
		tx -= ref.startp;
		if (tx <  0) {
			tx = 0;
		}else if (tx > ref.maxlen) {
			tx = ref.maxlen;
		}
		ref.view(tx/ref.maxlen);
		e.stopPropagation();
		return false;
	},
	view: function(p, motion) {
		this.percent = p;
		var temp = this.maxlen * p + this.bp;
		var pcss = (this.type == 0  ? "top" : 'left');
		if (motion) {
			this.lab.stop();
			if (this.type == 0) {
				this.lab.animate({top: Math.floor(temp)}, 500);
			} else {
				this.lab.animate({left: Math.floor(temp)}, 500);
			}
			this.onscroll(p, true);
		} else {
			this.onscroll(p);
			this.lab.css(pcss, Math.floor(temp));
		}
	},
	onMouseUp: function(e) {
		var ref = e.data.ref;
		$(document).unbind("mouseup", ref.onMouseUp);
		$(document).unbind("mousemove", ref.onMousemove);
		$(document).unbind("selectstart", ref.onSelect);
	},
	onSelect: function(e) {
		return false;
	},
	init: function(maxlen, onscroll, step) {
		this.maxlen = maxlen;
		this.onscroll = onscroll;
		this.step = step;
		this.percent = 0;
		var ref = this;
		$(this.lab).unbind();
		if(this.arrow) {
			$(this.arrow[0]).unbind();
			$(this.arrow[1]).unbind();
		}
		$(this.lab).mousedown(function(e) {
			if (!ref.bpoint) {
				ref.bpoint = ref.lab.offset();
			}
			ref.startp = ref.type == 0 ? (e.pageY - ref.lab.offset().top) : (e.pageX - ref.lab.offset().left);
			$(document).bind("mouseup",{ref:ref}, ref.onMouseUp);
			$(document).bind("mousemove",{ref:ref}, ref.onMousemove);
			$(document).bind("selectstart", {ref:ref}, ref.onSelect);
			e.stopPropagation();
			return false;
		}).css(this.type == 0 ? "top" : "left", this.bp);
		if (this.arrow) {
			$(this.arrow[0]).click(function(e) {
				e.stopPropagation();
				if (ref.percent > 0) {
					var temp = ref.percent - ref.step;
					ref.view(temp < 0 ? 0 : temp, true);
				}
				return false;
			});
			$(this.arrow[1]).click(function(e) {
				e.stopPropagation();
				if (ref.percent < 1) {
					var temp = ref.percent + ref.step;
					ref.view(temp > 1 ? 1 : temp, true);
				}
				return false;
			});
		}
	}
}

//SideNav
$.suning.SideNav = function() {
	var links = $("#sidenav ul li a");
	var cur = -1;
	var datas = [];
	datas[0] = links.parent().parent().next();
	for (var i=1,len=links.length; i<len; i++) {
		datas[i] = datas[i-1].next();
	}
	if ($("#sidenav").hasClass("column_nav_inner")) {
		var taoutTimer, cloTimer, leaveTimer;
		var ob = $("#sidenav");
		var ta = ob.prev().find("a");
		var tag = false;
		ob.bind("mouseleave", function(e) {
			leaveTimer = setTimeout(function() {
				ob.css("display", "none");
				ta.removeClass("on");
			}, 10);
		}).bind("mouseover", function(e) {
				clearTimeout(cloTimer);
		});
		ta.mouseover(function(e) {
			clearTimeout(taoutTimer);
			clearTimeout(cloTimer);
			clearTimeout(leaveTimer);
			taoutTimer = setTimeout(function() {
				ob.css("display", "block");
				ta.addClass("on");
			},10);
		}).mouseout(function(e) {
			clearTimeout(taoutTimer);
			clearTimeout(cloTimer);
			
			cloTimer = setTimeout(function() {
				ob.css("display", "none");
				ta.removeClass("on");
			},10);
		});
	}
	return {
		init: function() {
			var ref = this;
			
			links.each(function(i) {
				var timer = 1, t2 = 2;
				var t3=0;
				var data = datas[i];
				var link = $(this);
				data.mouseover(function(e) {
					clearTimeout(timer);
				}).mouseleave(function(e) {

					t3=setTimeout(function() {
						ref.hideL2(link.parent(), data);
					},10);
					
				});
				link.mouseover(function(e) {

					clearTimeout(t2);
					clearTimeout(timer);
					clearTimeout(t3);
					t2 = setTimeout(function() {
						ref.showL2(link.parent(), data);
					},10);
				}).mouseleave(function(e) {
					clearTimeout(t2);
					timer = setTimeout(function() {
						ref.hideL2(link.parent(), data);
					},10);
				});
			});
		},
		showL2: function(n, d) {
			d.removeClass("datanone");
			n.addClass("on");
		},
		hideL2: function(n, d) {
			d.addClass("datanone");
			n.removeClass("on");
		}
	}
}

//Product Category
$.suning.ProductCategory = function(sLink, sInput) {
	var link = $("#" + sLink);
	var input = $("#" + sInput);
	var data = link.parent().next();
	link.parent().parent().mouseleave(function(e) {
		data.addClass("datanone");
		e.preventDefault();
	});
	data.find("a").click(function(e) {
		e.preventDefault();
		link.html(this.innerHTML);
		input.value = this.innerHTML;
		data.addClass("datanone");
	});
	link.mouseover(function(e) {
		data.removeClass("datanone");
		e.preventDefault();
	}).click(function(e) {
		e.preventDefault();
	});
}

//Tabgroup
$.suning.IndexTabGroup = function() {
	function createTab1(str) {
		var tabs = $(str);
		var datas = [tabs.next(), tabs.next().next()];
		tabs = tabs.find('a');
		new $.suning.kidtab(tabs, datas, 0, "on", "", function(loc) {
			this.tabs.eq(this.curId).parent().removeClass("on");
			this.tabs.eq(loc).parent().addClass("on");
		});
	}
	
	createTab1("#tabs01");
	createTab1("#tabs02");
	
	var tabs = $("#tab03 li");
	tabs.find("a").click(function(e) {
		e.preventDefault();
	});
	var datas = [$("#tab03").next()]
	for (var i=1, len=tabs.length; i<len; i++) {
		datas[i] = datas[i-1].next();
	}
	new $.suning.kidtab(tabs, datas, 0, "on", "");
}

//Terran 8BB VS Zerg 5D
$.suning.rush = function() {
	var lis = $("#rush_title li");
	var cur = 0;
	var ul = $("#rush_title").next().find("ul");
	lis.each(function(i) {
		$(this).find("a").click(function(e) {
			e.preventDefault();
		});
		$(this).click(function(e) {
			lis.eq(i).addClass("on");
			lis.eq(cur).removeClass("on");
			cur = i;
			ul.animate({marginLeft:-768 * i},"slow");
		});
	});
}


//bottom
$.suning.service = function(t, d) {
	this.tabs = t;
	this.data = d.children();
	this.loc = -1;
	this.d = d;
	this.btn = $(this.data[3]);
	this.isopen = false;
	this.type = 0;
	this.delayTimer = 1;
	this.showTimer = 2;
	this.hT = 3;
}
$.suning.service.prototype = {
	init: function() {
		var that = this;
		this.data.each(function(i) {
			if (i < 3) {
				$(this).find("a").mouseover(function() {
					that.type = 1;
				});
				$(this).mouseover(function(e) {
					that.type = 1;
					clearTimeout(that.hT);
				}).mouseout(function(e) {
					that.type = 2;
					that.delay();
				});
			}
		});
		this.tabs.each(function(i) {
			$(this).mouseover(function(e) {
				clearTimeout(that.hT);
				if (that.loc != i) {
					clearTimeout(that.showTimer);
					that.showTimer = setTimeout(function() {
						that.change(i);
					}, 50);
				}
			}).mouseout(function() {
				clearTimeout(that.showTimer);
				that.hT = setTimeout(function() {
					that.close();
				},1000);
			});
		});
		this.btn.click(function(e) {
			that.close();
		});
	},
	delay: function() {
		clearTimeout(this.delayTimer);
		var ref = this;
		this.delayTimer = setTimeout(function() {
			ref.delayCall();
		}, 300);
	},
	delayCall: function() {
		if (this.type == 2) {
			this.close();
		}
	},
	close:function() {
		$(this.data[this.loc]).animate({'top': 200});
		var that = this;
		this.isopen = false;
		this.btn.animate({'top':200},function() {
			if (!that.isopen) {
				that.d.css('height', '0px');
			}
		});
		this.loc = -1;
	},
	change: function(loc) {
		this.type = 1;
		this.isopen = true;
		$(this.data[loc]).animate({'top': 130});
		if (this.loc != -1) {
			$(this.data[this.loc]).animate({'top': 200});
		} else {
			this.d.css('height', '200px');

			this.btn.animate({'top':135});
		}
		this.loc = loc;
	}
}


//tickcount

//1hour start
$.suning.ohour=function(){
	var d=new Date();if(d<new Date(2010,2,27,20,30)||d>new Date(2010,2,27,21,30))return;
	var flashurl="/wcsstore/ConsumerDirectStorefrontAssetStore/images/oneHourImage/960.swf"; //flash文件路径，请准确填写
	var closebtnurl="/wcsstore/ConsumerDirectStorefrontAssetStore/images/oneHourImage/btn.gif"; // 关闭flash层的图片按钮地址
	var width=!+"\v1"?$().width()-20:$().width();
	var height=$().height();
	if($("#onehourbg,#onehour").length==0)
	{
		var onehourbg=$('<div id="onehourbg" style="filter: Alpha(opacity=50);-moz-opacity:.5;opacity:0.5;background:#333; position:absolute; z-index:9999"></div>');
		var onehour=$('<div id="onehour" style="position:absolute;z-index:9999;width:960px; height:500px;left:50%; margin-left:-480px;top:133px; border:1px solid #000"><div><img src="'+closebtnurl+'" id="onehourbtn" style="position:absolute;right:0px; cursor:pointer" alt="关闭"/></div><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="960" height="500" id="focusshow" align="middle">\
				<param name="allowScriptAccess" value="sameDomain" />\
				<param name="allowFullScreen" value="false" />\
				<param name="wmode" value="opaque" />\
				<param name="movie" value="'+flashurl+'" />\
				<param name="quality" value="high" /><param name="bgcolor" value="#000000" />\
				<embed src="'+flashurl+'" quality="high" bgcolor="#000000" width="960" height="500" name="focusshow" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  wmode="opaque" ></embed>\
	</object></div>');	
		onehourbg.css({"width":width+"px","height":height+"px","left":"0","top":"0"});
		$(document.body).append(onehourbg).append(onehour);		
		$("#onehourbtn").bind("click",function(){$("#onehourbg,#onehour").remove();$(window).unbind("resize",$.suning.ohour)});
		$(window).bind("resize",$.suning.ohour)
	}else
	{
		$("#onehourbg").css({"width":width+"px","height":height+"px","left":"0","top":"0"});
	}
};
//1hour end

$.suning.activeTick = function(startTime, endTime) {
	if (endTime <=0) 
		return;
	var base = $("#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','5px');
				}else{
					s1.css('letter-spacing','0');	
				}
				if(arr[1] > 9){
					s2.css('letter-spacing','5px');
				}else{
					s2.css('letter-spacing','0');
				}
				if(arr[2] > 9){
					s3.css('letter-spacing','5px');
				}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);
}

//added by lihf
var bbs = function(){
	var bbs = $('.userbbsScript');
	var name = $('.nameJcript');
	var input = $('#user_text');
	return{
		init:function(){
			bbs.each(function(i){
				$(this).find('a').click(function(){
					var text = $(this).text();	
					var user = name.eq(i).text();
					input.text(text+user+"：");								   
				})			  
			})
		}
	}
}
$.suning.seach = function() {
	var link = $("#p_category_3");
	var input = $("#product_category_3");
	var data = link.parent().next();
	var len = data.find('li').length;
	var top = -(len * 28);
	data.css('top',top);
	link.parent().parent().mouseleave(function(e) {
		data.addClass("datanone");
		e.preventDefault();
	});
	data.find("a").click(function(e) {
		e.preventDefault();
		link.html(this.innerHTML);
		input.value = this.innerHTML;
		data.addClass("datanone");
	});
	link.mouseover(function(e) {
		data.removeClass("datanone");
		e.preventDefault();
	}).click(function(e) {
		e.preventDefault();
	});
}

var rangList = function(id){
	var btn = $('.'+id).find('ul');
	var len = btn.length ;
		
	return{
		init:function(){
			var that = this ;
			btn.each(function(i){
				$(this).mouseover(function(){
					$(this).addClass('on').siblings().removeClass('on');
					$(this).find('li').eq(0).hide();
					for(var j=0;j<len;j++){
						$(this).find('li').parent().siblings().eq(j).find('li').eq(0).show(); 
					}
				});
			});
		}
	}
}

$(document).ready(function() {

  
	bbs().init();
	rangList('top_list').init();
	
});
	
