var getHost = function(url) { 
	var host = "null";
	if(typeof url == "undefined"
	|| null == url)
	url = window.location.href;
	var regex = /.*\:\/\/([^\/]*).*/;
	var match = url.match(regex);
	if(typeof match != "undefined"
	&& null != match)
	host = match[1];
	return host;
	}
var path = location.href.toLowerCase();
var msrBaseurl="http://"+getHost(path);

if (typeof(ctx) == 'undefined') {
	ctx = '/foode-new';
}

// cookies_Set
var cookiesname = 'foodeMemberOperator';
// 基本url
var mainbaseurl = msrBaseurl+ctx+'/';
var membaseurl = msrBaseurl+ctx+'/customer/';



// 参数
var pValue = 'prosend';// 发布产品
var dValue = 'demsend';// 发布求购(招标)

// 转向地址
var loginurl = membaseurl + 'login.xhtml';
var regurl= membaseurl + 'register/register.xhtml';
var searchprodbyclassidUrl = '/production/production-searchProdByProdClassId.xhtml?prodClassId=';
// ajaxurl
var onlineStateurl = ctx + '/inc/onlineState_forlist.jsp';
var autocompleteurl = '/production/production-searchAutocomplete.xhtml';


// 识别(参数)
var detail = 'DET';
var onLine = 'onLineService';
var tradeRequest = 'tradeRequest';
var askPrice = 'askPrice';
var collect = 'collect';
var addCustomer = 'addCustomer';
var answerBid = 'answerBid';
var answerBidonLine = 'answerBidonLine';
var forget = 'forgetPassWindow';
var demCt = 'demandContact';
var classL = 'l_'
var classRedStr = '食品类,农副食品加工产品类,饮料、酒类,烟草制品类,农牧渔类,食品添加剂类,包装制品类,专用设备类,通用设备类';
var moreChar = '更多';
var provselect = 'currentProvId';
var cityselect = 'currentCityId';
var proClass = 'proClass_Tabs';
var areaName = 'areaName';
var cityName = 'cityName';

// 按钮/样式
var longinBtn = "img#lgi_btn";
var regBtn= "img#reg_btn";
var sendproBtn = "a.send_Message_prodcuts_css";
var senddemBtn = "a.send_Message_demand_css";
var onLineBtn = "img[name^='" + onLine + "']";
var tradeRequestBtn = "img[name^='" + tradeRequest + "']";
var askPriceBtn = "img[name^='" + askPrice + "']";
var collectBtn = "img[name^='" + collect + "']";
var addCustomerBtn = "a[name^='" + addCustomer + "']";
var classLinkBtn = "a[class^='" + classL + "']";
var provSelectMenu = "select[name='" + provselect + "']";
var citySelectMenu = "select[name='" + cityselect + "']";
var proclassMenu = "a[name^='" + proClass + "']";
var proclassMenuParent = "div[name='proClass_Menu']";
var proclassTabs1Style = "background:url("+mainbaseurl+"images/fenlei2_f.jpg);height:34px;background-position: center bottom;background-repeat: no-repeat;";
var proclassTabs2Style = "background:url("+mainbaseurl+"images/fenlei2_fb.jpg);height:34px;background-position: center bottom;background-repeat: no-repeat;";
var proclassTabs3Style = "background:url("+mainbaseurl+"images/fenlei2_fc.jpg);height:34px;background-position: center bottom;background-repeat: no-repeat;";
var hotsearchkey = "a[class='huang12']";
var areaClicktoCompanyList = "a[class='tscy_2']";
var areaCityClicktoCompanyList = "a[class='tscy_3']";
var onlineClass = "div[class^='online||']";
var outocompletePlainClass = "div[class='autocomplete-w1']";

String.prototype.trim = function() {
    var reExtraSpace = /^\s+(.*?)\s+$/;
    return this.replace(reExtraSpace, "$1");
}
// 打开window
function opennewwindow(url) {
	var wn = 'foodeMemberOfficeWorkspace';
	var height = screen.height - 64;
	var width = screen.width - 4;
	var openparams = 'top=0,left=0,height=' + height + ',width=' + width
			+ ',location=no,status=no';
	var memberWin = window.open(url, wn, openparams);
	if (memberWin && memberWin.open && !memberWin.closed) {
		memberWin.focus();
	}

}
//打开window2　
function openwin(url) { 
	  var iWidth=580;                          //弹出窗口的宽度;
	  var iHeight=510; 
	  var iTop = (window.screen.availHeight-30-iHeight)/2;       //获得窗口的垂直位置;
	  var iLeft = (window.screen.availWidth-10-iWidth)/2;  
window.open (url, 'newwindow', 'height='+iHeight+', width='+iWidth+', top='+iTop+', left='+iLeft+', toolbar=no, menubar=no, scrollbars=no, resizable=yes,location=no, status=no') //这句要写成一行 
 } 
// cookies缓存
function SetCookie(name, value) {
	var exp = new Date();
	exp.setTime(exp.getTime() + 2 * 60 * 1000);// 10分钟
	document.cookie = name + "=" + escape(value) + ";expires="
			+ exp.toGMTString() + ";   path=/";
}

function getCookie(name)// 取cookies函数
{
	var arr = document.cookie
			.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if (arr != null)
		return unescape(arr[2]);
	return null;
}

function getStr(body, s1, s2) {

	var n1 = body.lastIndexOf(s1) + s1.length;
	var n2 = body.lastIndexOf(s2)
	body = body.substring(n1, n2)
	return body;

}
// 提取参数
function getParam(value, begin_str) {
	var tempArray = value.split('_');
	var returnstr = '';
	if (tempArray.length >= 2) {
		returnstr = tempArray[1]
	}
	return returnstr;
}

function getClassListURL(val) {
	if (path.indexOf('/production/') > 0) {
		//return mainbaseurl+'production/production-searchProdByProdClassName.xhtml?prodClassName='
				//+ encodeURIComponent(val);
		
		return mainbaseurl+'production/production-searchProdByProdName.xhtml?prodName='
		+ encodeURIComponent(val);
	}
	if (path.indexOf('/demand/') > 0) {
		return mainbaseurl+'demand/searchBidByProdClassName.xhtml?productClassName='
				+ encodeURIComponent(val);
	}
	if (path.indexOf('/company/') > 0) {
		return mainbaseurl+'company/searchCorpByProdClassName.xhtml?prodClassName='
				+ encodeURIComponent(val);
	} else {
		return mainbaseurl+'production/production-searchProdByProdClassName.xhtml?prodClassName='
				+ encodeURIComponent(val);
	}
}

function getcompanyProvtoListUrl(val) {
	return mainbaseurl+'company/search/location-name.xhtml?locationName=' + encodeURIComponent(val);
}
function getcompanyCitytoListUrl(val) {
	return mainbaseurl+'company/company.xhtml?' + cityName + '=' + encodeURIComponent(val);
}
if (path.indexOf('detail') < 0) {
	$(document).ready(function() {
		// 登录
		/*$(longinBtn).click(function() {
					window.open(loginurl);
				});
		// 注册
		$(regBtn).click(function() {
					window.open(regurl);
				});*/
		// 发布产品
		$(sendproBtn).click(function() {
					SetCookie(cookiesname, pValue);
					opennewwindow(membaseurl);
				});

		// 发布招标
		$(senddemBtn).click(function() {
					SetCookie(cookiesname, dValue);
					opennewwindow(membaseurl);
				});

		// 热门搜索
		/*
		$(hotsearchkey).mouseover(function() {
					var value = $(this).html();
					var url = getClassListURL(value);
					setKeywordTocookies('');
					$(this).attr('href', url);
				});
		*/
		
		// 公司>地区打开列表
		$(areaClicktoCompanyList).mouseover(function() {
					var value = $(this).html();
					var url = getcompanyProvtoListUrl(value);
					setKeywordTocookies('');
					$(this).attr('href', url);
				});

		// 公司>地区打开列表
		$(areaCityClicktoCompanyList).mouseover(function() {
					var value = $(this).html();
					var url = getcompanyCitytoListUrl(value);
					setKeywordTocookies('');
					$(this).attr('href', url);
				});


		// 城市选择检索(省)
		$(provSelectMenu).change(function() {
					$.query.SET(provselect, $(this).val());
					$.query.REMOVE(cityselect);
					$.query.REMOVE(areaName);
					$.query.REMOVE(cityName);
					location.href = $.query.toString();
				});
		// 城市选择检索(市/区)
		$(citySelectMenu).change(function() {
					$.query.SET(cityselect, $(this).val());
					$.query.REMOVE(areaName);
					$.query.REMOVE(cityName)
					location.href = $.query.toString();
				});
		// 分类菜单
		var currentClassMenu = '';
		if (path.indexOf('/demand/') > 0) {
			currentClassMenu = proclassTabs2Style;
		} else if (path.indexOf('/company/') > 0) {
			currentClassMenu = proclassTabs3Style;
		} else {
			currentClassMenu = proclassTabs1Style;
		}
		$(proclassMenuParent).attr('style', currentClassMenu);

		// 在线工具
//		$(onlineClass).each(function() {
//					var c = $(this).attr('class');
//					var cs = '?c=' + encodeURIComponent(c);
//					var url = onlineStateurl + cs;
//					$(this).load(url);
//				});
		
		// 搜索
		var searchWord = '';
		function setKeywordTocookies(searchWord) {
			SetCookie('keyword', searchWord)
		}
		function getKeywordFromcookies() {
			return getCookie('keyword')
		}
		if ($.query.GET('prodName') != '') {
			searchWord = $.query.GET('prodName');
          setKeywordTocookies(searchWord);
		} else if ($.query.GET('keyWord') != '') {
			searchWord = $.query.GET('keyWord');
         setKeywordTocookies(searchWord);
		} else if ($.query.GET('demandName') != '') {
			searchWord = $.query.GET('demandName');
        setKeywordTocookies(searchWord);
		}
		if (getKeywordFromcookies()!=''){
		searchWord=getKeywordFromcookies();
		}
		if (searchWord != '' && searchWord != null) {
			$("#fe_keyword").val(searchWord);
		}
		// 搜索补全autocomplete
      //  if ($.query)
		$('#fe_keyword').autocomplete({
					serviceUrl : autocompleteurl,
					minChars : 1,
					maxHeight : 180,
					width : 450,
					// callback function:
					onSelect : function(value, data) {
					location.href = searchprodbyclassidUrl + data;
                    setKeywordTocookies(value);
					}
				});
     
        //select 重写
        window.setTimeout('rewriteselect1()',50);
		

	});
}

//select 重写
function rewriteselect1(){
$('#postTime').customSelect();
}
function doOnlineService(param) {
	SetCookie(cookiesname, detail + onLine + '_' + param);
	opennewwindow(membaseurl);
}
function doTradeRequest(param) {
	SetCookie(cookiesname, detail + tradeRequest + '_' + param);
	opennewwindow(membaseurl);
}
function doAskPrice(param) {
	//SetCookie(cookiesname, detail + askPrice + '_' + param);
	//opennewwindow(param);
	window.open(param,'name1','toolbar=no,scrollbars=no,menubar=no');
}
function doCollect(param) {
	SetCookie(cookiesname, detail + collect + '_' + param);
	opennewwindow(membaseurl);
}
function doAddCustomer(param) {
	SetCookie(cookiesname, detail + addCustomer + '_' + param);
	opennewwindow(membasdeurl);
}
function doAnswerBid(param) {
	SetCookie(cookiesname, detail + answerBid + '_' + param);
	opennewwindow(membaseurl);
}
function doAnswerBidonLine(param) {
	SetCookie(cookiesname, detail + answerBidonLine + '_' + param);
	opennewwindow(membaseurl);
}
function doforgetPassWindow() {
	SetCookie(cookiesname, forget);
	opennewwindow(loginurl);
}
function doScandemandContact(param) {
	SetCookie(cookiesname, detail + demCt + '_' + param);
	opennewwindow(membaseurl);
}

// *****************搜索相关************************
var prodSearchKeywordUrl = mainbaseurl+'production/production-searchProdByProdName.xhtml?prodName=';
var demandSearchKeywordUrl = mainbaseurl+'demand/demand-searchDemandByDemandName.xhtml?demandName=';
var companySearchKeywordUrl = mainbaseurl+'company/search/corp-name.xhtml?corpName=';

function getSearchForwordUrl() {
	var searchURL = "";
	if (path.indexOf('/production/') > 0) {
		searchURL = prodSearchKeywordUrl;
	} else if (path.indexOf('/demand/') > 0) {
		searchURL = demandSearchKeywordUrl;
	} else if (path.indexOf('/company/') > 0) {
		searchURL = companySearchKeywordUrl;
	} else {
		searchURL = prodSearchKeywordUrl;
	}
	return searchURL;
}

function searchForList(keyword) {
	var key = encodeURIComponent(keyword.replace(' ', ''));
	var rediURL = getSearchForwordUrl() + key;
	location.href = rediURL;
}
function search() {

	var k = document.getElementById("fe_keyword").value;
	if (k != '请输入搜索的名称' && k != '') {
		searchForList(k);
	} else {
		if (path.indexOf('/company/') != -1)
		{
			alert("请输入您要搜索的公司名称！");
		} else if (path.indexOf('/production/') != -1)
		{
			alert('请输入您需要的产品名称！');
		} else if (path.indexOf('/demand/') != -1)
		{
			alert("请输入您需要的招标名称！");
		}
	}
}

function shortURL(l) {
	if (l == null)
		return '';
	l = encodeURIComponent(l);
	if (l.length > 500)
		l = l.substring(0, 500);
	return l;
}
var reseveKey = "foodeChatMode80";// 请在这里写入你们想要传递的信息。如果内容中有中文或者特殊字符请用encodeURIComponent编码
function openDoyooCustomChat(c, t) {
	var url = "http://chat.doyoo.net/WebModule/chat/p.do?proxyId=foodqs&c=" + c;
	if (t != null && t.length != 0) {
		url += "&g=" + t;
	}
	var sid = getCookie("DOYOO_VISITOR_ID");
	var cid = getCookie("DOYOO_USER_ID");
	var refer = getCookie("DOYOO_VISITOR_REFER");
	var loc = document.location.href;
	if (sid != null)
		url += "&v=" + sid;
	if (cid != null)
		url += "&u=" + cid;
	if (refer)
		url += "&refer=" + shortURL(refer);
	if (loc)
		url += "&loc=" + shortURL(loc);
	try {
		url = url + "&r=" + reseveKey;
	} catch (e) {
	}
	var param = "height=460,width=690,directories=no,"
			+ "location=no,menubar=no,"
			+ "resizeable=no,status=no,toolbar=no,top=100,left=200";
	var acceptWin = window.open(url, "", param);

}

