﻿// JavaScript Document
function Advertise(_lat,_lng,_boundRadius,_distanceUnit){
	this.AdUrl = "/Xml/adXml/adXml.xml";
	var AdDataArray = new Array();
	var DistanceUnit = "m";
	var Lat = _lat;
	var Lng = _lng;
	var BoundRadius = _boundRadius;
	if(_distanceUnit!=null && _distanceUnit!="") DistanceUnit = _distanceUnit;
	
	var AdFilter = function(_lat,_lng,_arrayAd,_boundRadius){
		var newArrayAd = new Array();
		var _EarthDistance = new EarthDistance(DistanceUnit);
		for(var i=0; i<_arrayAd.length;i++){
			var tempDistance = _EarthDistance.GetDistance(parseFloat(_arrayAd[i].getAttribute("lat")),parseFloat(_arrayAd[i].getAttribute("lng")),parseFloat(_lat),parseFloat(_lng));
			var tempBoundRadius;
			if(parseFloat(_arrayAd[i].getAttribute("boundradius"))>_boundRadius){
				tempBoundRadius = parseFloat(_arrayAd[i].getAttribute("boundradius"));
			}else{
				tempBoundRadius = _boundRadius;
			}
			if(tempDistance<=tempBoundRadius){
				newArrayAd.push(_arrayAd[i]);
			}
			//alert("to "+ _arrayAd[i].getAttribute("title") +":"+tempDistance+" comp " + tempBoundRadius);
		}
		return newArrayAd;
	};
	
	var GetAdDataArray = function(_obj){
	};
	
	
	this.Ad_ihome99 = function(_adDiv){
		var showAd = function(_obj){
			var xmlDoc=_obj.responseXML;
			var showElements = xmlDoc.getElementsByTagName("ad");
			if(showElements!=null && showElements.length>0){
				AdDataArray = AdFilter(Lat,Lng,showElements,BoundRadius);
			}
			var tempHtml="";
			for(var i=0;i<AdDataArray.length;i++){
				tempHtml += "<div style='margin-bottom:5px; padding:5px; font-size:14px;font-weight:bold; border:1px dotted #9F2D20; width:210px;'>找<span style='color:#9F2D20; '>" + AdDataArray[i].getAttribute("title") + "</span>的朋友聊聊？</div>";
			}
			document.getElementById(_adDiv).innerHTML = tempHtml;
			document.getElementById(_adDiv).style.display = "block";
		};
		
		Request.showStatus=false;
		Request.send(this.AdUrl,"",showAd,"GetAd");
		
	};
	
	this.Ad_Normal = function(_adDiv){
		var showAd = function(_obj){
			var xmlDoc=_obj.responseXML;
			var showElements = xmlDoc.getElementsByTagName("ad");
			if(showElements!=null && showElements.length>0){
				AdDataArray = AdFilter(Lat,Lng,showElements,BoundRadius);
			}
			var tempHtml="";
			for(var i=0;i<AdDataArray.length;i++){
				tempHtml += AdDataArray[i].getAttribute("detail");
			}
			if(tempHtml!=""){
				document.getElementById(_adDiv).innerHTML = tempHtml;
				document.getElementById(_adDiv).style.display = "block";
			}
		};
		
		Request.showStatus=false;
		Request.send(this.AdUrl,"",showAd,"GetAd");
	}
}

function ihome99Ad(){
	var _advertisement = new Advertise(getCookie("homeLat"),getCookie("homeLng"),15,"m");
	//_advertisement.Ad_ihome99('Ad2');
	_advertisement.Ad_Normal('Ad1');
}

function normalAd(){
	var _advertisement = new Advertise(getCookie("homeLat"),getCookie("homeLng"),300,"m");
	_advertisement.Ad_Normal('Ad1');
}