$(document).ready(function(){
	
	(function(){
		
		var adjustImg = function(dst, ori) {
			if (ori.width == 0 || ori.height == 0)
				return;
			var oriW$H = ori.width / ori.height;
			var maxH = dst.height, maxW = dst.width;
			dst.removeAttribute("width");
			dst.removeAttribute("height");
			var changed = false;
	
			if (ori.height > maxH) {
				changed = true;
				dst.height = maxH;
				dst.width = maxH * oriW$H;
				if (dst.width > maxW) {
					dst.width = maxW;
					dst.height = maxW / oriW$H;
				}
			} else if (ori.width > maxW) {
				changed = true;
				dst.width = maxW;
				dst.height = maxW / oriW$H;
				if (dst.height > maxH) {
					dst.height = maxH;
					dst.width = maxH * oriW$H;
				}
			}
	
			if (dst.height > 0) {
				dst.style.marginTop = (maxH - (changed ? dst : ori).height) / 2 + "px";
			}
		};

		//var tmpImgSrc = "images/" + (SEARCH.paramMap.view == "list" ? "empty_120_120.jpg" : "empty_180_180.jpg");
		var tmpImgSrc = "http://image.gouwuke.com/new/" + (SEARCH.paramMap.view == "list" ? "empty_120_120.gif" : "empty_180_180.gif");

		$("img[orisrc]").each(function(){
			var img = this;
			var errsrc = img.getAttribute("errsrc");
			img.src = errsrc ? errsrc : tmpImgSrc;
			var oriSrc = img.getAttribute("orisrc");
			var gwkSrc = "http://image.gouwuke.com/"+oriSrc.substring(7).replace(/[^\w\d_./\\\\]/g, '_');
			var gwkImg = document.createElement("img");
			gwkImg.onload = function(){
				adjustImg(img, gwkImg);
				img.src = gwkSrc;
			};
			gwkImg.onerror = function(){
				var oriImg = document.createElement("img");
				oriImg.onload = function(){
					adjustImg(img, oriImg);
					img.src = oriSrc;
				};
				oriImg.src = oriSrc;
			};
			gwkImg.src = gwkSrc;
		});

	})();

	(function(){
		var hidedCategory = $("#hidedcategory");
		if(hidedCategory) {
			$("#showAllCategory").click(function(){
				if(hidedCategory.css("display") == "none") {
					hidedCategory.slideDown();
					this.innerHTML = "&lt;&lt;收起";
				} else {
					hidedCategory.hide();
					this.innerHTML = "更多&gt;&gt;";
				}
			});
		}

		var hidedBrand = $("#hidedbrand");
		if(hidedBrand) {
			$("#showAllBrand").click(function(){
				if(hidedBrand.css("display") == "none") {
					hidedBrand.slideDown();
					this.innerHTML = "&lt;&lt;收起";
				} else {
					hidedBrand.hide();
					this.innerHTML = "更多&gt;&gt;";
				}
			});
		}
		
		var hidedMerchant = $("#hidedmerchant");
		if(hidedMerchant) {
			$("#showAllMerchant").click(function(){
				if(hidedMerchant.css("display") == "none") {
					hidedMerchant.slideDown();
					this.innerHTML = "&lt;&lt;收起";
				} else {
					hidedMerchant.hide();
					this.innerHTML = "更多&gt;&gt;";
				}
			});
		}
		
	})();
	
	(function(){		
		var copyParam = SEARCH.paramMapCopy();
		$("#select").change(function(){
		   if($("#select").val()=='gridview'){
		    copyParam.view = ["grid"];
			location.href = SEARCH.action + paramMapToURL(copyParam);
		   }else{
		    copyParam.view = ["list"];
			location.href = SEARCH.action + paramMapToURL(copyParam);
		   }
		})
	})();

	(function(){
		var ordertypeselect = $("#ordertypeselect");
		if(SEARCH.paramMap.ordertype)
			ordertypeselect.val(SEARCH.paramMap.ordertype[0]);
		ordertypeselect.change(function() {
			var pm = SEARCH.paramMapCopy();
			if(pm.page)delete pm.page;
			pm.ordertype = [$(this).val()];
			location.href = SEARCH.action + paramMapToURL(pm);
		});
	})();
	
	(function(){
		if(SEARCH.paramMap.category) {
			var pm = copyTo({}, SEARCH.paramMap, "keyword,ordertype,view,e");
			$("#allinkw").attr("href", SEARCH.action + paramMapToURL(pm));
		}
	})();
	
	(function(){
	
		var pm = copyTo({}, SEARCH.paramMap, "keyword,ordertype,view,e");
			$("#categorys > div > ul > li > h3 > a[category]").each(function(){
				var a = $(this);
				pm.category = [a.attr("category")];
				a.attr("href", SEARCH.action + paramMapToURL(pm));
			});
	})();
	
	(function(){
		var pm = copyTo({}, SEARCH.paramMap, "keyword,ordertype,view,e");
			$("#categorynavigate >.table_1>tbody > tr > td  > a[category]").each(function(){
				var a = $(this);
				pm.category = [a.attr("category")];
				a.attr("href", SEARCH.action + paramMapToURL(pm));
			});
			$("#categorynavigate > .table_1> tbody > tr > td >h2 > a[category]").each(function(){
				var a = $(this);
				pm.category = [a.attr("category")];
				a.attr("href", SEARCH.action + paramMapToURL(pm));
			});
	})();

	(function(){
		$("#hotbrands > div > ul > li > h3 > a[brandname]").each(function(){
			var pm = SEARCH.paramMapCopy();
			if(pm.page)delete pm.page;
			var a = $(this);
			pm.brand = [a.attr("brandname")];
			a.attr("title", pm.brand[0]);
			if(SEARCH.paramMap.brand && pm.brand[0] == SEARCH.javaParam.brand[0]){
				a.css("color", "#B6B6B6");
				pm.brand[0] = encodeKeyWord(encodeURIComponent(pm.brand[0]));
				//add it to navigationbar
//				$("#navigationbar").append('<div class="nav_txt_l_1">&gt;&gt;&nbsp;&nbsp;<a href="'
//						+(SEARCH.action + paramMapToURL(pm))+'" class="color_14">'+pm.brand[0]+'</a></div>');
				//delete pm.brand;
			} else {
				pm.brand[0] = encodeKeyWord(encodeURIComponent(pm.brand[0]));
			}
			a.attr("href", SEARCH.action + paramMapToURL(pm));
		});
	})();
	
	(function(){
		var hotbrand_all = $("#hotbrand_all");
		var pm = SEARCH.paramMapCopy();
		if(pm.page)delete pm.page;
		delete pm.brand;
		hotbrand_all.attr("href", SEARCH.action + paramMapToURL(pm));
	})();

	(function(){
		var midMap = {};
		if(SEARCH.paramMap.merchantids && SEARCH.paramMap.merchantids[0] != "") {
			var mids = SEARCH.paramMap.merchantids[0].split(',');
			for(var i = 0; i < mids.length; ++i) {
				midMap[mids[i]] = true;
			}
		}

		var pm = SEARCH.paramMapCopy();
		if(pm.page)delete pm.page;
		$("#merchants > div > ul > li > h3 > input[name='merchantid']").each(function(){
			var eMerchantid = $(this);
			var link = eMerchantid.next();
			var id = eMerchantid.val();
			pm.merchantids=[id];
			link.attr("href", SEARCH.action + paramMapToURL(pm));
			if(midMap[id]) {
				eMerchantid.attr("checked", true);
				link.css("color", "#B6B6B6");
			}
		});

		$("#merchantsearch").click(function(){
			var pm = SEARCH.paramMapCopy();
			if(pm.page)delete pm.page;
			var checked = $("#merchants > div > ul > li > input[name=merchantid]:checked");
			if(checked.length == 0) {
				delete pm.merchantids;
			} else {
				var ids = [];
				checked.each(function(){
					ids.push($(this).val());
				});
				pm.merchantids = [ids.join(',')];
			}
			location.href = SEARCH.action + paramMapToURL(pm);
		});
	})();
	
	(function(){
		var merchantid_all = $("#merchantid_all");
		var pm = SEARCH.paramMapCopy();
		if(pm.page)delete pm.page;
		delete pm.merchantids;
		merchantid_all.attr("href", SEARCH.action + paramMapToURL(pm));
	})();
	
	(function(){
		var pm = SEARCH.paramMapCopy();
		if(pm.page)delete pm.page;
		var scopePrice = null;
		if(pm.minprice && pm.maxprice)scopePrice = pm.minprice + "-" + pm.maxprice;
		$("#pricescope > h3 > a").each(function(){
			var a = $(this);
			var ptext = a.text();
			if(ptext.charAt(ptext.length-1) == "上"){
				pm.minprice = [ptext.substring(0,ptext.length-3)];
				pm.maxprice = "";
			}else if(ptext.charAt(ptext.length-1) == "下"){
				pm.minprice = "";
				pm.maxprice = [ptext.substring(0,ptext.length-3)];
			}else{
				var price = ptext.split("-");
				pm.minprice = [price[0]];
				pm.maxprice = [price[1]];
			}
			a.attr("href", SEARCH.action + paramMapToURL(pm));
			if(scopePrice && a.text() == scopePrice) {
				a.css("color", "#B6B6B6");
				//a.attr("class", "color_22");
			}
		});
	})();

	(function(){
		var priceFrom = $("#price_from");
		var priceTo = $("#price_to");
		if(SEARCH.paramMap.minprice && !isNaN(SEARCH.paramMap.minprice[0])) {
			priceFrom.val(SEARCH.paramMap.minprice[0]);
		}
		if(SEARCH.paramMap.maxprice && !isNaN(SEARCH.paramMap.maxprice[0])) {
			priceTo.val(SEARCH.paramMap.maxprice[0]);
		}
		
		function changeCheckNumber(event) {
			var v = event.target.value;
			if(v != "" && !/^(\d+)([.]\d+)?$/.test(v)) {
				var indexOfPoint = v.indexOf('.');
				if(indexOfPoint != -1) {
					var beforePoint = v.substring(0,indexOfPoint);if(beforePoint == '')beforePoint = '0';
					var afterPoint = v.substring(indexOfPoint+1);if(afterPoint == '')afterPoint = '0';
					event.target.value = beforePoint.replace(/\D/g,"") + '.' + afterPoint.replace(/\D/g,"");
				} else {
					event.target.value = v.replace(/\D/g,"");
				}
				changeCheckNumber(event);
			}
		}

		priceFrom.change(changeCheckNumber);
		priceTo.change(changeCheckNumber);
		
		//搜索价格按钮变换 搜索/返回
		//if(priceFrom.val() != "" || priceTo.val() != ""){
			//$("#price_search_box").html("<h4><a href='#' id='pricescope_all'>&lt;&lt;收起</a></h4>");
		//}
		
		priceFrom.keyup(function(e){
			if(e.keyCode == 13)priceTo.focus();
		});
		priceTo.keyup(function(e){
			if(e.keyCode == 13)$("#price_search").click();
		});
		
		$("#price_search").click(function(){
		    
			var pm = SEARCH.paramMapCopy();
			if(pm.page)delete pm.page;
			changeCheckNumber({target:priceFrom.get(0)});
			changeCheckNumber({target:priceTo.get(0)});
			if(priceFrom.val() == ""){
				delete pm.minprice;
			} else {
				pm.minprice = [parseInt(priceFrom.val())];
			}
			if(priceTo.val() == ""){
				delete pm.maxprice;
			} else {
				pm.maxprice = [parseInt(priceTo.val())];
			}
			if(pm.minprice && pm.maxprice) {
				if(pm.minprice[0] > pm.maxprice[0]) {
					var tmp = pm.minprice;
					pm.minprice = pm.maxprice;
					pm.maxprice = tmp;
				}
			}
			var kw = $('#condition_keyword').val();
			kw = encodeKeyWord(encodeURIComponent(kw));
			if(kw!=null&&kw!="")
			SEARCH.action = "class.asp?keyword="+kw+"&";
			location.href = SEARCH.action + paramMapToURL(pm);
			
		});
	})();
	
	(function(){
		var pricescope_all = $("#pricescope_all");
		pricescope_all.click(function(){
			var pm = SEARCH.paramMapCopy();
			if(pm.page)delete pm.page;
			delete pm.minprice;
			delete pm.maxprice;
			//pricescope_all.attr("href", SEARCH.action + paramMapToURL(pm));
			location.href = SEARCH.action + paramMapToURL(pm);
		});
	})();
	
});
