﻿// JavaScript Document
function mainGetBusiness(_noticeStr,showMeal,showTel,showAdd,showHot,rowNum,rowMargin){
	var getRanColor = function(){
		var arrayColor=["#FCF6E0","#F1EBD6","#F9F2D8","#F9FAE0"];
		var rnd=parseInt(Math.random()*arrayColor.length);
		return arrayColor[rnd];
	}
	var LoadingBusiness = function(_obj){
		var _loadingObj=document.getElementById("BusinessList");
		var totalWidth=100,currentWidth=0;
		var ccWidth=totalWidth/2;
		_loadingObj.innerHTML="<div style='width:"+totalWidth+"px; border:1px #CCCCCC solid; margin:50px "
		+((500-totalWidth)/2)
		+"px 0px ;'><img id='loadingBar' src='images/bar_04_ff7200.gif' height='8' style='width:0px;'></div><div style='margin:0 "
		+((500-totalWidth)/2+10)
		+"px; font-size:11px; color:#666666; display:none;'><span id='loadingNum'>0</span>%</div>";
		var _barObj=document.getElementById("loadingBar"),_numObj=document.getElementById("loadingNum");
		var runInterval=function(){
			currentWidth+=2;
			_numObj.innerHTML=currentWidth;
			_barObj.style.width=currentWidth+"px";
			if(currentWidth>=ccWidth){ _barObj.src="images/bar_08_00a844.gif";}
			if(currentWidth>=totalWidth){ mainShowBusiness(_obj); return false;}
			window.setTimeout(runInterval,10);
		}
		runInterval();
	}
	var mainShowBusiness = function(_obj){
		var tempHtms="";
		var xmlDoc=_obj.responseXML;
		var showElements = sortBusinessList(xmlDoc.getElementsByTagName("marker"));
		document.getElementById("mainArea").style.backgroundColor= getRanColor();
		if(showElements!=null && showElements.length>0){
			var maxHit=showElements[0].getAttribute("hitcount");
			tempHtms += "<div style='margin-bottom:15px;' class='LightText04'>在您的周围大约 500M 范围有<span style='margin-left:5px; margin-right:5px; color:#CC0066' > " + showElements.length + "</span> 个店提供送餐服务</div>";
			var newBusiness = GetNewBusinessList(showElements);
			var _rowCount = 0;
			if(rowNum == null || rowNum == "") rowNum = 99;
			for(var i=0;i<newBusiness.length;i++){
				tempHtms += showBusiness(newBusiness[i],i+1,maxHit,showMeal,showTel,showAdd,showHot,rowMargin);
				_rowCount++;
			}
			for(var i=0;i<showElements.length;i++){
				//避免重复显示置顶的商家列表
				if((showElements[i].getAttribute("isnew")!="1"|| showElements[i].getAttribute("ishavemeal") == "False")
				   &&_rowCount<rowNum){
					tempHtms += showBusiness(showElements[i],i+1,maxHit,showMeal,showTel,showAdd,showHot,rowMargin);
					_rowCount++;
				}
			}
		}else{
			if(_noticeStr == null || _noticeStr == ""){
				tempHtms += "<div style='margin-bottom:15px;' class='LightText04'>在您的周围没有找到快餐店<br/>您可以试试拖动左侧地图上的标注来改变您所处的地理位置</div>";
			}else{
				tempHtms += _noticeStr;
			}
		}
		document.getElementById("BusinessList").innerHTML = tempHtms;
	}
	
	if(getCookie("homeLat")!=null && getCookie("homeLat")!="undefined" && getCookie("homeLng")!=null && getCookie("homeLng")!="undefined"){
		homeLat=parseFloat(getCookie("homeLat"));
		homeLng=parseFloat(getCookie("homeLng"));
		var BoundSWLat,BoundSWLng,BoundNELat,BoundNELng;
		var _EarthDistance = new EarthDistance("m");
		var latLngDif = _EarthDistance.GetLatLngDif(homeLat,homeLng,330);
		BoundSWLat = homeLat-latLngDif[0];
		BoundSWLng = homeLng-latLngDif[1];
		BoundNELat = homeLat+latLngDif[0];
		BoundNELng = homeLng+latLngDif[1];
		
		var businessDataUrl="MapXmlData.aspx?RecordStatus=3&BoundSWLat="+BoundSWLat+"&BoundSWLng="+BoundSWLng+"&BoundNELat="+BoundNELat+"&BoundNELng="+BoundNELng;
		Request.send( businessDataUrl,"",LoadingBusiness,"businessData");
	}else{
		window.location.href="firstNav.aspx?resetPosition=reset";
	}
}

/*对快餐店数组进行排序*/
function sortBusinessList(arrayMarker){
	var arrayAfterSort=new Array();
	for (var i=0; i<arrayMarker.length; i++){
		arrayAfterSort[i]=arrayMarker[i];
	}
	/*对数组按照hitcount进行排序*/
	for (var i=0; i<arrayAfterSort.length; i++){
		var k=i;
		var temp;
		for (var j=i+1; j<arrayAfterSort.length; j++){
			if(arrayAfterSort[k].getAttribute("hitcount")=="" && arrayAfterSort[j].getAttribute("hitcount")!=""){
				k=j;
			}else if(arrayAfterSort[k].getAttribute("hitcount")!="" && arrayAfterSort[j].getAttribute("hitcount")!=""){
				if(parseInt(arrayAfterSort[k].getAttribute("hitcount"))<parseInt(arrayAfterSort[j].getAttribute("hitcount"))){
					k=j;
				}
			}
		}
		if (k!=i){
			temp=arrayAfterSort[k];
			arrayAfterSort[k]=arrayAfterSort[i];
			arrayAfterSort[i]=temp;
		}
	}
	return arrayAfterSort;
}

/*获取最近更新的店(一周)*/
function GetNewBusinessList(_originArray){
    var arrayNewBusiness=new Array();
    for(var i=0; i<_originArray.length; i++){
        if(_originArray[i].getAttribute("isnew") == "1" && _originArray[i].getAttribute("ishavemeal") == "True"){
            arrayNewBusiness.push(_originArray[i]);
        }
    } 
    return arrayNewBusiness;
}

/*每条商家记录的显示模块*/
function showBusiness(_obj,_num,_maxhit,_showMeal,_showTel,_showAdd,_showHot,_rowMargin){
    var tempHtms="";
    var tempArchFile="business.aspx?BusinessID=" + _obj.getAttribute("id");
    if(_obj.getAttribute("filename")!=""){
       tempArchFile += "&BusinessData=" + base64encode(_obj.getAttribute("filename"));
    }else{
       tempArchFile += "&ContentsID=" + _obj.getAttribute("sortid");
    }
	var rowMargin = 20;
	if(_rowMargin!=null && _rowMargin!=""){ rowMargin = _rowMargin;}
    tempHtms +="<div style='margin-top:"+ rowMargin +"px;'>";
    if(_obj.getAttribute("isnew") == "1") tempHtms += "<img src='images/icon_new.gif' style='margin-right:10px; margin-bottom:-3px;'>";
    tempHtms += "<span>" + (_num) + "  <a href='"+ tempArchFile +"' target='_blank' class='searchList' title='" + _obj.getAttribute("outline") + "' >" + _obj.getAttribute("title") + "</a></span>";
    if(_showMeal!=false){
		tempHtms += "<span style='margin-left:10px;'>";
		if(_obj.getAttribute("ishavemeal") == "True") {
			if(_obj.getAttribute("isorderonline") == "True"){
				tempHtms += "<a href='"+ tempArchFile +"' target='_blank' title='该店已开通在线定餐功能'>在线定餐</a> <img src='images/icon_Yes.gif' style='margin-right:10px; margin-bottom:-2px; border=0px;' title='该店已开通在线定餐功能'>";
			}else{
				tempHtms += "<a href='"+ tempArchFile +"' target='_blank' title=''>菜单</a>";
			}
		}else{
			tempHtms += "<img src='images/icon_Warn.gif' style='margin-bottom:-2px;'> 暂无菜单";
		}
		tempHtms += "</span>";
	}
    if(_showTel!=false) tempHtms += "<span style='margin-left:10px;'>" + _obj.getAttribute("tel") + "</span>";
	if(_obj.getAttribute("modifydiff")!=null && _obj.getAttribute("isnew") != '1') tempHtms+="<span style='margin-left:11px; color:#CC0066'>" + _obj.getAttribute("modifydiff") + "更新</span>";
	tempHtms += "<br />";
    if(_showAdd!=false) tempHtms += "<span><b>地址：</b>" +  _obj.getAttribute("address") + "</a></span><br />";
    if(_obj.getAttribute("hitcount")!="" && _obj.getAttribute("hitcount")>0 && _showHot!=false){
	    tempHtms += "<b>热度：</b><img src='images/Count_bar04.gif' width='"+(_obj.getAttribute("hitcount")/_maxhit)*173+"' height='8'>";//条状图示
	    //tempHtms += "<span style='color:#FEF8E1; font-size:10px; margin-left:5px;'>"+_obj.getAttribute("hitcount")+"</span>";
	    tempHtms += "<br />";
    }
    tempHtms += "</div>";
    return tempHtms;
}