var spec = [];
function goodsAttr(spec,attr,val){ 
	var newAttr = true;for(var item in spec){ for(var attrItem in spec[item]){ if(attrItem==attr){ spec[item][attr]=val;newAttr = false; break; } } } var temp = { };temp[attr] = val;if(newAttr){ spec.push(temp); }return spec;
}
function attrToString(spec){ var s='';for(var item in spec){ for(var attr in spec[item]){ s+=attr+":"+spec[item][attr]+" "; } } return s; }
/*jqzoom*/
$(function(){
	$.Template={
		parse:function(c,a,b){
			//alert(a);
			var d=[];
			for(i=0;i<c.length;i++){
				e = c[i];
				d.push(a.replace(/{(\w+)}/g,function(g,f){return e[f]!==undefined?e[f]:(f=="index"?i:g)}))
				
			};
			return d.join(b||"")
			//alert(c[a]);
		},
		stuff:function(e,f,c,d){
				c=$("#"+(c||f)+"_tpl").val();
				f=$("#"+f);
				var a=c.match(/<#([\s\S]+)#>/);
				var b=a[1];
				//alert(a[0]);
				f.html(c.replace(a[0],this.parse(e,b,d)));f.show();
		}
	};
	function MouseEvent(e){this.x=e.pageX;this.y=e.pageY;}
	var a = jQuery;
	jQuery.fn.jdMarquee=function(h,b){if(typeof h=="function"){b=h;h={};}var j=a.extend({deriction:"up",speed:10,auto:false,width:null,height:null,step:1,control:false,_front:null,_back:null,_stop:null,_continue:null,wrapstyle:"",stay:5000,delay:20,dom:"div>ul>li".split(">"),mainTimer:null,subTimer:null,tag:false,convert:false,btn:null,disabled:"disabled",pos:{ojbect:null,clone:null}},h||{});var u=this.find(j.dom[1]);var e=this.find(j.dom[2]);var r;if(j.deriction=="up"||j.deriction=="down"){var l=u.eq(0).outerHeight();var c=j.step*e.eq(0).outerHeight();u.css({width:j.width+"px",overflow:"hidden"});}if(j.deriction=="left"||j.deriction=="right"){var n=e.length*e.eq(0).outerWidth();u.css({width:n+"px",overflow:"hidden"});var c=j.step*e.eq(0).outerWidth();}var o=function(){var s="<div style='position:relative;overflow:hidden;z-index:1;width:"+j.width+"px;height:"+j.height+"px;"+j.wrapstyle+"'></div>";u.css({position:"absolute",left:0,top:0}).wrap(s);j.pos.object=0;r=u.clone();u.after(r);switch(j.deriction){default:case"up":u.css({marginLeft:0,marginTop:0});r.css({marginLeft:0,marginTop:l+"px"});j.pos.clone=l;break;case"down":u.css({marginLeft:0,marginTop:0});r.css({marginLeft:0,marginTop:-l+"px"});j.pos.clone=-l;break;case"left":u.css({marginTop:0,marginLeft:0});r.css({marginTop:0,marginLeft:n+"px"});j.pos.clone=n;break;case"right":u.css({marginTop:0,marginLeft:0});r.css({marginTop:0,marginLeft:-n+"px"});j.pos.clone=-n;break;}if(j.auto){k();u.hover(function(){m(j.mainTimer);},function(){k();});r.hover(function(){m(j.mainTimer);},function(){k();});}if(b){b();}if(j.control){g();}};var k=function(s){m(j.mainTimer);j.stay=s?s:j.stay;j.mainTimer=setInterval(function(){t();},j.stay);};var t=function(){m(j.subTimer);j.subTimer=setInterval(function(){q();},j.delay);};var m=function(s){if(s!=null){clearInterval(s);}};var p=function(s){if(s){a(j._front).unbind("click");a(j._back).unbind("click");a(j._stop).unbind("click");a(j._continue).unbind("click");}else{g();}};var g=function(){if(j._front!=null){a(j._front).click(function(){a(j._front).addClass(j.disabled);p(true);m(j.mainTimer);j.convert=true;j.btn="front";t();if(!j.auto){j.tag=true;}f();});}if(j._back!=null){a(j._back).click(function(){a(j._back).addClass(j.disabled);p(true);m(j.mainTimer);j.convert=true;j.btn="back";t();if(!j.auto){j.tag=true;}f();});}if(j._stop!=null){a(j._stop).click(function(){m(j.mainTimer);});}if(j._continue!=null){a(j._continue).click(function(){k();});}};var f=function(){if(j.tag&&j.convert){j.convert=false;if(j.btn=="front"){if(j.deriction=="down"){j.deriction="up";}if(j.deriction=="right"){j.deriction="left";}}if(j.btn=="back"){if(j.deriction=="up"){j.deriction="down";}if(j.deriction=="left"){j.deriction="right";}}if(j.auto){k();}else{k(4*j.delay);}}};var d=function(w,v,s){if(s){m(j.subTimer);j.pos.object=w;j.pos.clone=v;j.tag=true;}else{j.tag=false;}if(j.tag){if(j.convert){f();}else{if(!j.auto){m(j.mainTimer);}}}if(j.deriction=="up"||j.deriction=="down"){u.css({marginTop:w+"px"});r.css({marginTop:v+"px"});}if(j.deriction=="left"||j.deriction=="right"){u.css({marginLeft:w+"px"});r.css({marginLeft:v+"px"});}};var q=function(){var v=(j.deriction=="up"||j.deriction=="down")?parseInt(u.get(0).style.marginTop):parseInt(u.get(0).style.marginLeft);var w=(j.deriction=="up"||j.deriction=="down")?parseInt(r.get(0).style.marginTop):parseInt(r.get(0).style.marginLeft);var x=Math.max(Math.abs(v-j.pos.object),Math.abs(w-j.pos.clone));var s=Math.ceil((c-x)/j.speed);switch(j.deriction){case"up":if(x==c){d(v,w,true);a(j._front).removeClass(j.disabled);p(false);}else{if(v<=-l){v=w+l;j.pos.object=v;}if(w<=-l){w=v+l;j.pos.clone=w;}d((v-s),(w-s));}break;case"down":if(x==c){d(v,w,true);a(j._back).removeClass(j.disabled);p(false);}else{if(v>=l){v=w-l;j.pos.object=v;}if(w>=l){w=v-l;j.pos.clone=w;}d((v+s),(w+s));}break;case"left":if(x==c){d(v,w,true);a(j._front).removeClass(j.disabled);p(false);}else{if(v<=-n){v=w+n;j.pos.object=v;}if(w<=-n){w=v+n;j.pos.clone=w;}d((v-s),(w-s));}break;case"right":if(x==c){d(v,w,true);a(j._back).removeClass(j.disabled);p(false);}else{if(v>=n){v=w-n;j.pos.object=v;}if(w>=n){w=v-n;j.pos.clone=w;}d((v+s),(w+s));}break;}};if(j.deriction=="up"||j.deriction=="down"){if(l>=j.height&&l>=j.step){o();}}if(j.deriction=="left"||j.deriction=="right"){if(n>=j.width&&n>=j.step){o();}}};
	jQuery.fn.jqueryzoom=function(options){
		var settings={xzoom:200,yzoom:200,offset:10,position:"right",lens:1,preload:1};
		if(options){jQuery.extend(settings,options);};
		var noalt='';
		jQuery(this).hover(function(){
			var imageLeft=$(this).offset().left;
			var imageTop=$(this).offset().top;
			var imageWidth=$(this).children('img').get(0).offsetWidth;
			var imageHeight=$(this).children('img').get(0).offsetHeight;
			noalt=$(this).children("img").attr("alt");
			var bigimage=$(this).children("img").attr("jqimg");
			jQuery(this).children("img").attr("alt",'');
			if(jQuery("div.zoomdiv").get().length==0){$(this).after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
			jQuery(this).append("<div class='jqZoomPup'>&nbsp;</div>");};
			if(settings.position=="right"){
				if(imageLeft+imageWidth+settings.offset+settings.xzoom>screen.width){
					leftpos=imageLeft-settings.offset-settings.xzoom;
				}else{
					leftpos=imageLeft+imageWidth+settings.offset;
				}
			}else{
				leftpos=$(this).width();//-settings.xzoom-settings.offset;
				if(leftpos<0){leftpos=imageLeft+imageWidth+settings.offset;}
			};
			//imageTop = 0;
			if(leftpos<385){leftpos=385;}
			$("div.zoomdiv").css({top:10,left:leftpos+11});
			$("div.zoomdiv").width(settings.xzoom);
			$("div.zoomdiv").height(settings.yzoom);
			$("div.zoomdiv").show();
			if(!settings.lens){
				$(this).css('cursor','crosshair');};
				$(document.body).mousemove(function(e){
					mouse=new MouseEvent(e);
					var bigwidth=$(".bigimg").get(0).offsetWidth;
					var bigheight=$(".bigimg").get(0).offsetHeight;var scaley='x';var scalex='y';
					if(isNaN(scalex)|isNaN(scaley)){
						var scalex=(bigwidth/imageWidth);var scaley=(bigheight/imageHeight);$("div.jqZoomPup").width((settings.xzoom)/(scalex*1));
						$("div.jqZoomPup").height((settings.yzoom)/(scaley*1));
						if(settings.lens){$("div.jqZoomPup").css('visibility','visible');}
					};
					xpos=mouse.x-$("div.jqZoomPup").width()/2-imageLeft;ypos=mouse.y-$("div.jqZoomPup").height()/2-imageTop;
					if(settings.lens){xpos=(mouse.x-$("div.jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $("div.jqZoomPup").width()/2>imageWidth+imageLeft)?(imageWidth-$("div.jqZoomPup").width()-2):xpos;ypos=(mouse.y-$("div.jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $("div.jqZoomPup").height()/2>imageHeight+imageTop)?(imageHeight-$("div.jqZoomPup").height()-2):ypos;};
					if(settings.lens){$("div.jqZoomPup").css({top:ypos,left:xpos});};scrolly=ypos;
					$("div.zoomdiv").get(0).scrollTop=scrolly*scaley;scrollx=xpos;$("div.zoomdiv").get(0).scrollLeft=(scrollx)*scalex;
				});
			},function(){
				$(this).children("img").attr("alt",noalt);$(document.body).unbind("mousemove");if(settings.lens){$("div.jqZoomPup").remove();};$("div.zoomdiv").remove();
			});count=0;
			if(settings.preload){jQuery('body').append("<div style='display:none;' class='jqPreload"+count+"'>qkweb</div>");
			jQuery(this).each(function(){var imagetopreload=jQuery(this).children("img").attr("jqimg");var content=jQuery('div.jqPreload'+count+'').html();jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');});}}

	jQuery(".jqzoom").jqueryzoom({xzoom:400,yzoom:400,offset:0,position:"left",preload:1,lens:1});
	jQuery("#spec-list").jdMarquee({deriction:"left",width:360,height:56,step:2,speed:4,delay:10,control:true,_front:"#spec-right",_back:"#spec-left"});
	jQuery("#spec-list img").bind("mouseover",function(){
		var src=jQuery(this).attr("src");
		var large = jQuery(this).attr("large");
		var middle = jQuery(this).attr("middle");
		jQuery("#spec-n1 img").eq(0).attr({src:middle,jqimg:large});
	jQuery(this).css({"border":"2px solid #ff6600","padding":"1px"});}).bind("mouseout",function(){$(this).css({"border":"1px solid #ccc","padding":"2px"});});
	var nowCount = 1;
	if($("#goodsCountAdd").length==1){
		$("#goodsCountAdd").click(function(){
			var count = $("#goodsCount").val();
			if(count==''){return false;}
			count=parseInt(count,10)
			if(!isNaN(count)){
					nowCount = count+1;
					$("#goodsCount").val(nowCount);
					$("#goodsCountView").html(nowCount);
					
					if(nowCount>1){
						$("#goodsCountMinus").removeClass('num_reduce_disabled');
					}else{
						$("#goodsCountMinus").addClass('num_reduce_disabled');
						//$("#goodsCountAdd").addClass('num_reduce_disabled');
					}
			}
		});
		$("#goodsCountMinus").click(function(){
			var count = $("#goodsCount").val();
			if(count==''){return false;}
			count=parseInt(count,10)
			if(!isNaN(count) && count>1){
					nowCount = count-1;
					$("#goodsCount").val(count-1);
					$("#goodsCountView").html(count-1);
					if(nowCount==1){
						$("#goodsCountMinus").addClass('num_reduce_disabled');
					}else{
						$("#goodsCountMinus").removeClass('num_reduce_disabled');
						//$("#goodsCountAdd").addClass('num_reduce_disabled');
					}
			}
		});
	}
	$(".goods_nav .tab li").click(function(){
		$(this).siblings().removeClass('curr');$(this).removeClass('curr').addClass('curr');
		var index=$(this).index();
		$(".goods_c").each(function(idx){
			if(idx<index){ $(this).hide(); }else{ $(this).show(); }
		});
	});
	$(".attr-vals").find("ul").find("li").click(function(){
		$(this).addClass("selected").siblings().removeClass("selected");
		var attrName = $(this).parent("ul").parent(".attr-vals").prev(".attr-name").text();
		var attrVal = $(this).find("a").text();
		var attr = attrName + ":" + attrVal;
		goodsAttr(spec,attrName,attrVal);
		$("#goods-attr-selected").show();
		$("#goods-attr-selected").find(".attr-vals span").html(attrToString(spec));
		$("#spec").val(attrToString(spec));
	});
	$(".goods_thumb a").hover(function(){
		$("#goods_big_img").attr("src",$(this).attr("big-src"));
	});
});



