var isRectangleSearch=false;

var theMap=null;

var GlobalCodeMap=null;
var GlobalMarkerMap=null;
var GlobalHotelsMap=null;

var ParentIcon = new GIcon(G_DEFAULT_ICON);
ParentIcon.image="http://resource.elong.com/cn/hotels/images/h_11.gif";
ParentIcon.iconSize = new GSize(16, 16);
ParentIcon.shadowSize = new GSize(16, 16);
ParentIcon.iconAnchor = new GPoint(16, 16);
ParentIcon.infoWindowAnchor = new GPoint(9, 2);

// 这里开始，款选搜索按钮控件
function RectangleSearchControl() {}
		
RectangleSearchControl.prototype = new GControl();

RectangleSearchControl.prototype.initialize = function(map)
{
	var container = document.createElement("div");
	
	var buttonDiv = document.createElement("div");
	this.setButtonStyle_(buttonDiv);
	container.appendChild(buttonDiv);
	buttonDiv.appendChild(document.createTextNode("框选搜索"));
	GEvent.addDomListener(buttonDiv, "click", function()
	{
		recSearch();// 当点击这个按钮，开始框选搜索功能
	});
	map.getContainer().appendChild(container);
	return container;
}
		
RectangleSearchControl.prototype.getDefaultPosition = function()
{
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10));
}

RectangleSearchControl.prototype.setButtonStyle_ = function(button)
{
	button.style.color = "#000000";
	button.style.backgroundColor = "white";
	button.style.border = "2px solid black";
	button.style.textAlign = "center";
	button.style.width = "4.8em";
	button.style.height="2em";
	button.style.cursor = "pointer";
}
// 上面的框选搜索控件的代码


function initOverviewMap(container, latitude, longitude, content)
{
	var temmap = new GMap2(document.getElementById(container));
	var center = new GLatLng(latitude, longitude);
	temmap.setCenter(center, 12);
	temmap.addOverlay(new GMarker(center));
	temmap.addControl(new GSmallMapControl());
	
}

function loadBigMap(container, latitude, longtitude, name)
{
	var temmap = new GMap2(document.getElementById(container));
	var center = new GLatLng(latitude, longtitude);
	
	temmap.setCenter(center, 13);
	
	temmap.addOverlay(new GMarker(center));
	
	temmap.addControl(new GSmallMapControl());
}


function initHotelsMap(hotels)
{
	var map = new GMap2(document.getElementById("hotelsMap"));
	theMap=map;
	var mapShowCenter=getCenter(hotels);
	
	var maxLen=getZoomByHotels(hotels,730,410);
	var zoom=getZoomByMaxLen(maxLen);
	//alert(maxLen+":"+zoom);
	if(mapShowCenter==null){
		if (recSearchCenter == null) 
		{
			map.setCenter(new GLatLng(30.659034, 104.077399), zoom);
		}else{
			map.setCenter(recSearchCenter, zoom);
		}
	}else{
		map.setCenter(mapShowCenter,zoom);
	}
	map.addControl(new GSmallMapControl());
	//map.addControl(new GMapTypeControl());
	try 
	{
		map.addControl(new RectangleSearchControl());
	}catch(ex){alert(ex);}
	map.removeMapType(G_HYBRID_MAP);
	
	addAllHotelNodes(map, hotels);
}

function getCenter(hotels)//根据酒店群，得到地图应该显示的中心位置
{
	try
	{
		if (!hotels || hotels.length == 0) 
		{//如果没有找到酒店 
			return null;
		}
		else 
		{
			
			var maxLat = hotels[0].latitude;
			var minLat = hotels[0].latitude;
			var maxLng = hotels[0].longitude;
			var minLng = hotels[0].longitude;
			
			
			for(var i=0;i<hotels.length;++i)
			{
				
				if(maxLat<hotels[i].latitude){
					maxLat=hotels[i].latitude;
				}
				
				if(minLat>hotels[i].latitude){
					minLat=hotels[i].latitude;
				}
				
				if(maxLng<hotels[i].longitude){
					maxLng=hotels[i].longitude;
				}
				
				if(minLng>hotels[i].longitude){
					minLng=hotels[i].longitude;
				}
				
			}
			var avgLat=(maxLat+minLat)/2;
			var avgLng=(maxLng+minLng)/2;
			
			return new GLatLng(avgLat,avgLng);
		}
	}catch(ex){
		alert("获取地图中心发生错误！\r\n"+ex);
	}
}




function addHotelNode(map, hotel, index)//将一个酒店的位置标记在map上
{
	var point = new GLatLng(hotel.latitude, hotel.longitude);
	var temIcon = new GIcon(ParentIcon);
	temIcon.image = "http://resource.elong.com/cn/hotels/images/h_" + (index + 1) + ".gif";
	var mk = new GMarker(point, temIcon, false);
	
	addEventToHotelNode(mk, hotel, index);//添加事件，点击mark 弹出窗口
	GlobalCodeMap[index] = hotel.code;
	GlobalMarkerMap[hotel.code] = mk;
	GlobalHotelsMap[hotel.code] = hotel;
	
	map.addOverlay(mk);
}

function addAllHotelNodes(map,hotels)//将所有酒店的的位置都标记在map上
{
	GlobalMarkerMap=new Array();///每刷新一次页面，清空缓存
	GlobalHotelsMap=new Array();
	GlobalCodeMap=new Array();
	
	if(hotels==undefined)
	{
		return;
	}
	for(var i=0;i<hotels.length;++i)
	{
		addHotelNode(map,hotels[i],i);
	}
}

function addEventToHotelNode(mk,hotel,index)//为地图上的酒店图标添加弹出窗口。
{
	GEvent.addListener(mk,"click",
	function()
	{
		var temHtml=makeHtmlForHotelNode(hotel,index);
		mk.openInfoWindow(temHtml);
	});
}

function makeHtmlForHotelNode(hotel,index)//制造弹出窗口显示的html
{
	var html="<table cellspacing=0 cellpadding=0 border=0><tr>"+
	//"<td class='unitrip-list-number' width=20 align=center>"+(index+1)+"</td>"+
	"<td class=unitrip-hotel-title>"+
	"<a class=redlink target=_self href='/hotels/"+hotel.code+"'>"+hotel.name+"</a>"+
	"<span class=unitrip-orange unitrip-hotel-star>"+makeStar(hotel.star)+"</span>"+
	getHotAndRecommendedImg(hotel)+
	"</td>"+
	"<td class=unitrip-orange width=60 align=center/></tr></table>";
	
	html=html+getHotelDescription(hotel);
	return html;
	
}

function makeStar(num)//弹出窗口显示细节1：制造星级
{
	var colorNum=num%10;
	num=num/10;
	var star="☆";
	if(colorNum>0){
		star="★";
	}
	var res="";
	for(var i=0;i<num;++i){
		res=res+star;
	}
	return res;
}

function getHotAndRecommendedImg(hotel)//弹出窗口显示细节2：是否含有热门或则推荐的图标
{
	var res="";
	if(hotel.hot>0){
		res=res+"<img title=热门 src='/theme/default/images/product/hot.gif'/>";
	}
	if(hotel.recommended>0)
	{
		res=res+"<img title=推荐 src='/theme/default/images/product/recommended.gif'/>";
	}
	
	return res;
}


function getHotelDescription(hotel)//弹出窗口显示细节3：简要介绍酒店
{
	var mp="无相关信息";
	if(hotel.minPrice>0){
		mp=hotel.minPrice;
	}
	var res="<table  cellspacing=0 cellpadding=0 border=0><tr>"+
	"<td><img width=104 height=97 src='/images/hotels/"+hotel.code+"/overview01.jpg'/></td>"+
	"<td class='unitrip-menu-onclick'><table cellspacing=0 cellpadding=0 border=0>"+
	"<tr><td><font color=blue>酒店地址:</font>"+hotel.address+"</td></tr>"+
	"<tr><td><font color=blue>最低价格:</font>"+mp+"</td></tr>"+
	"<tr><td><font color=blue>周边环境:</font>"+hotel.tags+"</td></tr>"+
	"<tr><td align='center'><a style='color:green;' href='/hotels/"+hotel.code+"'>[我要预订]</a></td></tr>"+
	"</table></td>"+
	"</tr>"+
	"</table>";
	
	return res;
}


function getZoomByHotels(hotels,width,height)// 优化地图 ： 根据酒店群 最大的位置差距，选择合理的缩放度
{
	var C=width/height;
	try 
	{
		if (!hotels || hotels.length == 0) 
		{//如果没有找到酒店 
			return 0;
		}
		else 
		{
			var maxLat = hotels[0].latitude;
			var minLat = hotels[0].latitude;
			var maxLng = hotels[0].longitude;
			var minLng = hotels[0].longitude;
			for (var i = 0; i < hotels.length; ++i) 
			{
				if (maxLat < hotels[i].latitude) {
					maxLat = hotels[i].latitude;
				}
				if (minLat > hotels[i].latitude) {
					minLat = hotels[i].latitude;
				}
				if (maxLng < hotels[i].longitude){
					maxLng = hotels[i].longitude;
				}
				if (minLng > hotels[i].longitude) {
					minLng = hotels[i].longitude;
				}
			}
			
			var lenX = maxLat - minLat;
			var lenY = maxLng - minLng;
			//alert(lenX+":"+lenY);
			//lenX=lenX*C;
			
			if(lenX>lenY){
				return lenX;
			}else{
				return lenY;
			}
			
		}
	}catch(ex){
		alert(ex);
	}
	
}

function getZoomByMaxLen(maxLen)//根据距离来确定缩放比
{
	//alert(maxLen);
	if(maxLen<0.02){
		return 15;
	}
	if(maxLen<0.04){
		return 14;
	}
	if(maxLen<0.06){
		return 13;
	}
	if(maxLen<0.10){
		return 12;
	}
	if(maxLen<0.20)
	{
		return 11;
	}
	if(maxLen<0.5){
		return 10;
	}
	if(maxLen<1)
	{
		return 9;
	}
	if(maxLen<2.3)
	{
		return 8;
	}
	if(maxLen<4){
		return 7;
	}
	if(maxLen<6)
	{
		return 6;
	}
	if(maxLen<10){
		return 6;
	}
	if(maxLen<15)
	{
		return 6;
	}
	if(maxLen<20)
	{
		return 5;
	}
	if(maxLen<30)
	{
		return 4;
	}
	if(maxLen<50)
	{
		return 3;
	}
	if(maxLen<70)
	{
		return 2;
	}
	if(maxLen<100)
	{
		return 1;
	}
	
	return 1;
}

function showByCode(code)//地图外部，调用这个函数，来打开marker的窗口
{
	var mk=GlobalMarkerMap[code];
	var hotel=GlobalHotelsMap[code];
	var temHtml=makeHtmlForHotelNode(hotel);
	mk.openInfoWindow(temHtml);
	 $("#hotelsMap").show();
	scrollBar();
}

function scrollBar()
{
	var top=$(window).scrollTop();
	if(top<=130){
		return;
	}
	$(window).scrollTop(top-50);
	
	setTimeout(function(){
		scrollBar();
	},5);
	
}

function test(hotels)//此函数，测试，是否坐标有重复的现象
{
	for(var i=0;i<hotels.length;++i)
	{
		var tem=hotels[i];
		for(var j=0;j<hotels.length;++j)
		{
			if(i!=j)
			{
				if(tem.latitude==hotels[j].latitude&&tem.longitude==hotels[j].longitude)
				{
					alert(tem.latitude+"================="+tem.longitude+">>"+i+":"+j);
				}
			}
		}
	}
}
