﻿﻿﻿﻿﻿var public_search = function() {

	var opts = {
		prevCount : '',
		detailUrl : '',
		categoryId : '',
		newsKeyword : '',
		inputkeyword : '',
		maintitle:'name',
		subtitle:'code',
		markshow:true,
		priceshow:true,
		introductionshow:true,
		mainfloat:false,
		fontcolor:'z3g-textRed',
		symbolValue:'￥',
		isColumn:true
	};

	var init = function(options) {
		$.extend(opts, options);

		$("#searchweighing").on("touchmove",function(e){e.stopPropagation()});
		
		var selectType = "";
		var selectId = "";
		var beforeurl = "";
		var prvecount = 0;// 保存来源路径

		var box_mainBodyHeight = $("#box_mainBody").height();
		var searchTab = $(".searchTab").height();
		var searchInput = $(".searchInput").height();
		var logoheight = $(".PublicConfig_showLogo01-d1_c1").height();

		// 滑动栏宽度
		var liwidthlist = new Array();
		liwidthlist = $("#firstUL ul li");
		var liwidth = 0;
		for ( var i = 0; i < liwidthlist.length; i++) {
			liwidth = liwidth + (liwidthlist[i].offsetWidth);
		}
		$("#firstUL ul").css("width", liwidth);

		selectId = document.getElementById("type").value;// 默认选择
		var e = document.getElementById("firstUL");
		var u = e.getElementsByTagName("li");
		for ( var i = 0; i < u.length; i++) {
			var obj = u[i];
			if (obj.value == selectId) {
				obj.className = "current active js_selecttype";
			}
		}
		prvecount = $("#fromfirstHTml").val();
		selectType = document.getElementById("selectTypeId").value;

		document.getElementById("keywords").value = $("#selectcontent").val();

		
		var sc = new iScroll("firstUL", {
			// hScroll : false,
			// vScroll : true,
			hScrollbar : false,
			vScrollbar : false,
			snap : "li",
			momentum : false
		});
		
		var columnSiteId = document.getElementById("type").value;
		//点击搜索
		$("#searchValue").bind("tap",function() {
							columnSiteId = document.getElementById("type").value;
							$(".searchResult").show();
							var newsKeyword = document.getElementById("keywords").value;
							opts.newsKeyword = newsKeyword.trim();
							newsKeyword = newsKeyword.trim();
							var inputvalue = "";
							if (newsKeyword == null || newsKeyword == "") {
								newsKeyword = "";
								inputvalue = newsKeyword;
							} else {
								for ( var i = 0; i < newsKeyword.length; i++) {
									var chr = newsKeyword.charAt(i);
									if (chr == '?' || chr == '/' || chr == '%'
											|| chr == '&' || chr == '^'
											|| chr == '#' || chr == '\\'
											|| chr == '+') {
										var c = chr.charCodeAt(0);
										if (inputvalue.length > 0) {
											inputvalue = inputvalue + "^" + c;
										} else {
											inputvalue = "^" + inputvalue + c;
										}
									} else {
										inputvalue = inputvalue + chr;
									}

								}
							}
							if(inputvalue.length>0){
							if (selectType != "1") {//咨询的情况
								$.ajax( {
											url : '/Public.do?method=infolist01&newsKeyword='
													+ inputvalue
													+ '&InfoListColumnID='
													+ columnSiteId
													+ '&prevHTML='
													+ prvecount
													+ '&selectTypeId='
													+ selectType
													+ '&time='
													+ new Date().getTime(),
											type : 'POST',
											dataType : 'html',
											success : function(json) {
												if (json) {
													$(".searchResult").empty();
													$(".loading").clone().attr("id","js_loading").prependTo($("#js_searchResult"));
													$("#js_loading").show();
													if (json == "nocontent") {
														$("#infosmileface").clone().attr("id","js_smileface").prependTo($("#js_searchResult"));
														$("#js_loading").hide();
														$("#js_smileface").show();
													} else {
														var datavalue= json.split("||");
														$(".searchResult").empty();
														document.getElementById("js_searchResult").innerHTML = datavalue[0];
														if(thedivScroll){
															thedivScroll.destroy();
														}
														resultscroll();
														
														 $(".js_newslink").bind('click',js_newslink);
														 $("#newsmoreId").clone().attr("id","js_moreWrap").insertAfter($("#newsul"));
														 if (datavalue.length>1) {
															 $("#js_moreWrap").show();
														 }
														 $(".loading").clone().attr("id","js_loading").insertAfter($("#newsul"));
														 $("#js_moreWrap").unbind("click").bind('click',infotiaozhuan);
													}
												}
												thedivScroll.refresh();
											}
										});
							} else {//产品的情况
								$.ajax({
									url: "/Products.do?method=mobilelist01&productCategoryColumnId="+columnSiteId+"&compId=Products_mobilelist01-001&pageno=1&keywords="+inputvalue+"&pageSize=10&orderStr=8",
									type:'POST',
									dataType:'html',
									success:function(json){
										$(".loading").clone().attr("id","js_loading").prependTo($("#js_searchResult"));
										$("#js_loading").show();
										var viewTypestart= json.indexOf("viewType:");
										var viewType= json.substr(viewTypestart+9,1);
										$("#productsviewType").val(viewType);
										
										var productvalue= json.substr(0,json.lastIndexOf("/section")+9);
										$(".searchResult").empty();
										if(productvalue.match(/z3g-noContent/)){
											$("#productsmileface").clone().attr("id","js_smileface").prependTo($("#js_searchResult"));
											$("#js_smileface").show();
										}else{
											var productvalueismore=false;
											if(productvalue.match(/moreWrap active j-productlist-more/)){
												var c=productvalue.substr(productvalue.indexOf("<div class=\"moreWrap active j-productlist-more\">"),productvalue.lastIndexOf("</div>")-20);
												productvalue=productvalue.replace(c,"");
												productvalueismore=true;
											}
											document.getElementById("js_searchResult").innerHTML = productvalue;
											if(productvalueismore){
												var moretishi=$("#searchproductmoreId").clone().attr("id","js_productmoreWrap");
												$("#js_searchResult section:first-child").append(moretishi);
												$("#js_productmoreWrap").show();
												$("#js_productmoreWrap").bind('click',productsmore);
											}
											if(thedivScroll){
												thedivScroll.destroy();
											}
											resultscroll();
											$(".loading").clone().attr("id","js_loading").insertAfter($("#js_productmoreWrap"));
											$(".js-product-click").bind('click',productstiaozhuan);
											lazyLoad.setting();
											lazyLoad.imgLoader();
										}
									}
								});
							}
							}
						});
		//关闭按钮
		$("#backToFromHtml").bind("click", function() {
//			$("#searchmoney").removeClass("current");
//			var back = opts.prevCount;
//			if (back != 8) {
//				window.history.go(back);
//			} else {
//				window.location.href = "/";
//			}
			$("#searchmoney").removeClass("current");
			$("#searchweighing").height(0);
			//$("#box_mainBody").css("position","absolute");
			$("#box_mainBody").css("bottom",mainBodyBottom);
			$(".controlBar").show();
			$('.circleIcon,.controlBar').show();
			isdianji=true;
		});
		
		//咨询跳转
		function js_newslink() {
			var pageNum = $(this).data('pagenum');
			if (pageNum.length > 5) {
				window.location.href = pageNum;
			} else {
				window.location.href = "/news_detail/"
						+ opts.detailUrl
						+ "&newsId="
						+ pageNum
						+ "&newsKeyword="
						+ opts.newsKeyword
						+ "&newsCategoryId="
						+ opts.categoryId
						+ "&comeFrom=search.html";
			}
		}
		
		//产品跳转
		$(".js_productlink").bind('click', js_productlink);
		function js_productlink() {
			var pageNum = $(this).data('pagenum');
			window.location.href = pageNum;
		}

		//选择搜索类型
		$(".js_selecttype").bind("click",function() {
							var pageNum = $(this).data('pagenum');
							var e = document.getElementById("firstUL");
							var u = e.getElementsByTagName("li");
							for ( var i = 0; i < u.length; i++) {
								var obj = u[i];
								obj.className = "active js_selecttype";
							}
							this.className = "current active js_selecttype";
							document.getElementById("type").value = $(this)
									.val();
							selectType = pageNum;
							$("#selectTypeId").val(selectType);
							$("#News_list01-d1_c1").remove("ul");
							$("#product-list-Public_totalSearch01-001").remove("ul");

							$("#keywords").val("");
							document.getElementById("keywords").placeholder = opts.inputkeyword;
							//$(".searchResult").css("height",document.body.scrollHeight-searchTab-searchInput-logoheight);
							$(".searchResult").empty();
							myScroll.refresh();
						});

		// 咨询点击更多
		function infotiaozhuan(){
			var totils = '';
			var root = $("#news-list");
			var pageno = $("#pageno").val();
			var durl = '${detailUrl}';
			$("#js_loading").show();
			pageno = parseInt(pageno) + 1;
			var categoryId = $(".j-categoryId").val();
			var keyword = $(".j-keyword").val();
			$.ajax({
							url:'/News.do?method=mobilelist01json&pageno='
									+ pageno + '&categoryId='
									+ opts.categoryId
									+ '&keyword='
									+ keyword
									+ '&dataType=content',
							type : 'POST',
							dataType : 'json',
							success:function(json) {
								if (json) {

									if (json.length > 0) {
										for ( var i = 0; i < json.length; i++) {
											var data = json[i];
											totils = data.totle;
											var li = [];
											li.push("<li class='active'>");
											if (data.link == "") {
												li.push("<div data-pagenum='"
																+ data.id
																+ "' class=\"js_newslink\" >");

											} else {
												li.push("<div data-pagenum='"
																+ data.link
																+ "' class=\"js_newslink\">");
											}
											li.push('<h3 class="productTitle">');
											if (data.top == 1) {
												li.push('<strong class="top">[顶]</strong>');
											}
											li.push('<span class="name">');
											li.push(data.name);
											li.push('</span>');
											li.push('</h3><p class="time">');
											li.push(data.datetime);
											li.push('</p></div></li>');
											$("#newsul").append(li.join(''));
										}
										root.find('#pageno').val(pageno);
									}
									$("#pageno").val(pageno+1);
									if (json.length < 10
											|| totils < 0) {
										$('#js_moreWrap').remove();
									}
									$("#js_moreWrap").show();
									$('#js_loading').hide();
									$(".js_newslink").bind('click',js_newslink);
									thedivScroll.refresh();
								}
							}
			})
		}
		
		var thedivScroll;
		//滑动事件
		function resultscroll(){
			thedivScroll = new iScroll(
					'js_searchResult',
					{
						zoom : false,
						hScroll : false,
						hScrollbar : false,
						vScrollbar : false,
						bounce : false,
						onScrollMove: function(){
							lazyLoad.imgLoader();
						},
						onScrollEnd: function(){
							lazyLoad.imgLoader();
						},
						onBeforeScrollStart : function(e) {
							e.stopPropagation();
							var target = e.target;
							thedivScroll.targetElement = target;
							// thelistScroll.clickFunc
							// =
							// $(target).attr("onclick");

							while (target.nodeType != 1)
								target = target.parentNode;
							if (target.tagName != 'SELECT'
									&& target.tagName != 'INPUT'
									&& target.tagName != 'TEXTAREA'
									&& target.tagName != 'BUTTON') {
								//if (!$.os.ios) {
									e.preventDefault();
								//}
							}
						}
					})
		}
		
		//产品页面跳转
		function productstiaozhuan(){
				var url=$(this).data('phref');
				var otherLink=$(this).data('otherlink');
				if(/zmobile/.test(navigator.userAgent)){
					var isother=context.loadUrl(url);//客户端处理链接
					if(!isother){
						window.location.href=url;
					}
				}else{
					if(otherLink=='0'){
						window.location.href=url;//浏览器处理链接
					}else{
						window.open(url);//浏览器处理链接
					}
				}
		}
		
		//点击产品更多
		function productsmore(){
			$("#js_productmoreWrap").hide();
			$("#js_loading").show();
			var root=$("#product-list-Products_mobilelist01-001");
			var pageno = $('#pageno').val();
			var pageSize=$("#pageSize").val();
			var orderStr=$('#orderStr').val();
			pageno = parseInt(pageno)+1;
			pageSize = parseInt(pageSize);
			var categoryId = root.find('#j-categoryId').val() ? root.find('#j-categoryId').val() : '';
			var nameorcode = root.find('#j-keywords').val() ? root.find('#j-keywords').val() : '';
			var keyword = root.find('#j-keyword2').val() ? root.find('#j-keyword2').val() : '';
			$.getJSON('/Products.do?method=mobilelist01json&viewType='+$("#productsviewType").val()+'&productCategoryColumnId='+columnSiteId+'&compId=Products_mobilelist01-001&maintitle='+opts.maintitle+
				'&subtitle='+opts.subtitle+'&pageno='+pageno+'&categoryId='+ categoryId+'&nameorcode='+nameorcode+'&keywords='+keyword+'&pageSize='+pageSize+'&orderStr='+orderStr
			 ,function(json){
				if(json){
					if(json.data){
						if($("#productsviewType").val()==1){
							view1(json,root);
						}else if($("#productsviewType").val()==2){
							view2(json,root);
						}else if($("#productsviewType").val()==3){
							view3(json,root);
						}else if($("#productsviewType").val()==4){
							view4(json,root);
						}else if($("#productsviewType").val()==5){
							view5(json,root);
						}
						
						root.find('#pageno').val(pageno);
						if(json.totalnum <= pageno * pageSize){
							root.find('.j-productlist-more').remove();
							$("#js_loading").hide();
							root.find('#pageno').val(1);
						}else{
							$("#js_productmoreWrap").show();
							$("#js_loading").hide();
						}
						$(".js-product-click").bind('click',productstiaozhuan);

						if(window.lazyLoad){
							window.lazyLoad.setting();
						    window.lazyLoad.imgLoader();
						}
						thedivScroll.refresh();
						myScroll.refresh();
					}
				}
			},'json');
		}
		
	//1对3jsp 2对1jsp 3对2jsp
	var view1=function(json,root){
		var ul = root.find(".productListCont");
		var li = [];
		var psize=json.data.length;
		for(var i=0;i<psize;i++){
			var data = json.data[i];
			if(i%3==0){
				if(i>0 && (i!=psize-1 || (psize-1)%3==0)){
					li.push('</dl>');
					ul.append(li.join(''));
				}
			li = [];
			li.push('<dl class="rows">');
			}
			li.push('<dd class="active'); 
			if(opts.mainfloat){
				li.push(' titleCoverStyle">');
			}else{
				li.push('">');
			}
			
			link(data,li);
			
			pimgs(data,li);
			
			li.push('<h3 class="productTitle">');
			var name = String(data.name);
			if(name.length>0){
			li.push(name);
			}
			li.push('</h3>');
									
			pcode(data,li);
			
			if (opts.priceshow){	
				price(data,li)
			}
			if(opts.markshow){
				mark2(data,li);
			}		
			li.push('</div></div></dd>');
			if(i==psize-1){
			li.push('</dl>');
			ul.append(li.join(''));
			}
		}
	};
	var view2=function(json,root){
		var ul = root.find("ul");
		for(var i=0;i<json.data.length;i++){
			var data = json.data[i];
			var li = [];
			li.push('<li class="active">');
			
			link(data,li);
			
			pimgs(data,li);
			
			li.push('<h3 class="productTitle"><span>');
			var name = String(data.name);
			if(name.length>0){
			li.push(name);
			}
			li.push('</span>');			
			if (opts.markshow){
				mark(data,li);
			}			
			li.push('</h3>');			
			pcode(data,li);
			if (opts.priceshow){	
				price(data,li)
			}
			if (opts.introductionshow){
				cont(data,li);
			}
			li.push('</div></div></li>');
			ul.append(li.join(''));
		}
	};
	var view3=function(json,root){
		var ul = root.find("ul");
		for(var i=0;i<json.data.length;i++){
			var data = json.data[i];
			var li = [];
			li.push('<li class="active">');
			
			link(data,li);
			
			li.push('<h3 class="productTitle"><span><span>');
			var name = String(data.name);
			if(name.length>0){
			li.push(name);
			}
			li.push('</span>');
			if (opts.markshow){
				mark(data,li);
			}
			li.push('</span></h3>');
			
			pcode(data,li);							
			if (opts.priceshow){	
				price(data,li)
			}
			if (opts.introductionshow){
				cont(data,li);
			}
			li.push('</div></div></li>');
			ul.append(li.join(''));
		}
	};
	var view4=function(json,root){
		var ul = root.find("ul");
		for(var i=0;i<json.data.length;i++){
			var data = json.data[i];
			var li = [];
			li.push('<li class="active ');
			if(opts.mainfloat){
				li.push(' titleCoverStyle">');
			}else{
				li.push('">');
			}
			
			link(data,li);
			
			pimgs(data,li);
			
			li.push('<div class="productTitleWrap"><div class="productTitleWrap2">');
			
			li.push('<h3 class="productTitle"><span class="name">');
			var name = String(data.name);
			if(name.length>0){
			li.push(name);
			}
			li.push('</span>');
			
			if (opts.markshow){
				mark(data,li);
			}
			li.push('</h3>');
			if (opts.priceshow){	
				price(data,li)
			}
			li.push('</div>');
			pcode(data,li);
			li.push('</div>');
			if (opts.introductionshow){
				cont(data,li);
			}
			li.push('</div></div></li>');
			ul.append(li.join(''));
		}
	};
	var view5=function(json,root){
		var ul = root.find(".productListCont");
		var li = [];
		var psize=json.data.length;
		for(var i=0;i<psize;i++){
			var data = json.data[i];
			if(i%2==0){
			li = [];
			li.push('<dl class="rows">');
			}
			li.push('<dd class="active ');
			if(opts.mainfloat){
				li.push(' titleCoverStyle">');
			}else{
				li.push('">');
			}
			
			link(data,li);
			
			pimgs(data,li);
			
			li.push('<h3 class="productTitle">');
			var name = String(data.name);
			if(name.length>0){
			li.push(name);
			}
			li.push('</h3>');							
			pcode(data,li);
			if (opts.priceshow){	
				price(data,li)
			}
			if(opts.markshow){
				mark2(data,li);
			}

			li.push('</div></div></dd>');
			if(i%2!=0 || i==psize-1){
			li.push('</dl>');
			ul.append(li.join(''));
			}
		}
	};
	
	var link=function(data,li){ 
		var url =data.link;
		var isotherlink=data.isOtherLink
		li.push('<div class="js-product-click" data-phref="');
		li.push(url);
		li.push('" data-otherLink="'+isotherlink);
		li.push('">');
	};
	var pimgs=function(data,li){
		li.push('<div class="imgWrap"><img class="img"  lazy-src="');
		li.push(data.simg);
		li.push('" alt="" onerror="this.src=\'/'+opts.onerrorimg+'\'"/></div>');
		li.push('<div class="infoWrap">')
	};
	var pcode=function(data,li){
		var code = String(data.code);
		if(code.length>0){
			li.push('<p class="code">');
			li.push(''+code);
			li.push('</p>');
		}
	};
	//标记
	var mark=function(data,li){
		li.push('<cite class="markWrap">');
		if(data.markimgs.length>0){
			var markimgs = String(data.markimgs).split(',');
			var mcount=markimgs.length > 3 ? 3:markimgs.length;
			for(var j=0;j<mcount;j++){
				if(markimgs[j].length>0){
					li.push('<img  lazy-src="'+ markimgs[j] + '" alt="" onerror="this.src=\'/${onerrormarkimg}\'"/>');
				 }
			}
		}
		li.push('</cite>');
	};
	
	//产品价格
	var price=function(data,li){
		var listprice = String(data.listprice);
		if(listprice.length>0){
		li.push('<p class="price"><span class="number '+opts.fontcolor+'">'+opts.symbolValue);
		li.push(listprice);
		li.push('</span></p>');
		}
	};
	//产品描述
	var cont=function(data,li){
		var desc = String(data.desc);
		if(desc.length>0){
		li.push('<p class="cont '+opts.displayLine+'">');
		li.push(desc);
		li.push('</p>');
		}
	};
	
	var　mark2=function(data,li){
		if(data.markimgs.length>0){
		var markimgs = String(data.markimgs).split(',');
		var mcount=markimgs.length > 3 ? 3:markimgs.length;
			if(mcount>0){
			li.push('<p class="mark">');
				for(var j=0;j<mcount;j++){
					if(markimgs[j].length>0){
					li.push('<img  lazy-src="'+ markimgs[j] + '" alt="" onerror="this.src=\'/'+opts.onerrormarkimg+'\'"/>');
					}
				}
			li.push('</p>');
			}
		}
	};
}
	
	
	return {
		init : init
	}

}();
