function searchmap() {
	loading();
	var query = document.getElementById('query').value;
	var scripturl = "http://www.roompage.net/centamapsearchjs?query=" + encodeURI(query) + "&feedbackfunc=searchmapfeedback";
	var headTag = document.getElementsByTagName('head')[0]; 
	var script = document.createElement('script'); 
	script.type = 'text/javascript'; 
	script.src = scripturl; 
	headTag.appendChild(script);
}

function searchmapfeedback(centamapsearchrs) {
	endloading();
	var searchresultbox = document.getElementById('searchresultbox');
	MeUnload();
	searchresultbox.innerHTML="";
	if (centamapsearchrs.locations.length==0) {
		searchresultbox.innerHTML="沒有搜尋結果";
	}
	else {
		searchresultbox.appendChild(document.createTextNode("找到"+centamapsearchrs.locations.length+"個地方"));
		var ul = document.createElement("ul");
		searchresultbox.appendChild(ul);
		for (var i=0; i<centamapsearchrs.locations.length; i++) {
			var li = document.createElement("li");
			li.appendChild(createLocationLink(centamapsearchrs.locations[i]));
			ul.appendChild(li);
		}
	}
	var sourcediv = document.createElement("div");
	sourcediv.innerHTML = '資料來源: <a href="http://www.centamap.com/" target="_BLANK">中原地圖</a>';
	searchresultbox.appendChild(sourcediv);
}

function gotoMap(locationObj) {
	point = new GLatLng(locationObj.lat,locationObj.lon);
	var level = 20-locationObj.z;
	if (level>=19) level=18;
	map.setZoom(level);
	map.panTo(point);
	marker.setPoint(point);
	curLocationObj=locationObj;
	curRestaurantObj=null;
	curPhotoObj = null;
	markerClicked();
}

atags = [];
function createLocationLink(locationObj) {
	var atag = document.createElement("a");
	atag.href = "#";
	atag.onclick = function() {
		gotoMap(locationObj);
		return false;
	}
	atag.appendChild(document.createTextNode(locationObj.chi_name));
	atags.push(atag);
	return atag;
}

function markerClicked() {
	setTimeout(
		function(){
			if (curLocationObj!==null) {
				marker.openInfoWindowHtml(
					curLocationObj.chi_name+"<br/>"+
					curLocationObj.detail+"<br/>"+
					'<a href="http://www.centamap.com/gc/location.aspx?z='+
						curLocationObj.z+'&x='+curLocationObj.lx+'&y='+curLocationObj.ly+
						'&sx='+curLocationObj.lx+'&sy='+curLocationObj.ly+'" target="centamap">' +
					'中原地圖'+
					'</a>',
					{maxWidth:400}
				);
			}
			
			if (curRestaurantObj!==null) {
				marker.openInfoWindowHtml(
					curRestaurantObj.chi_name+"<br/>"+
					curRestaurantObj.chi_address+"<br/>"+
					curRestaurantObj.phone+"<br/>"+
					'<a href="http://www.centamap.com/gc/location.aspx?z=3'+
						'&x='+curRestaurantObj.lx+'&y='+curRestaurantObj.ly+
						'&sx='+curRestaurantObj.lx+'&sy='+curRestaurantObj.ly+'" target="centamap">' +
					'中原地圖'+
					'</a>',
					{maxWidth:400}
				);
			}
			
			if (curPhotoObj!==null) {
				marker.openInfoWindowHtml(
					'<a href="' + curPhotoObj.urls.url[0]._content + '" target="flickr">' +
					'<img style="margin:2px 2px 2px 2px" align="left" src="' + "http://farm" + curPhotoObj.farm + ".static.flickr.com/" + curPhotoObj.server + "/" + curPhotoObj.id + "_" + curPhotoObj.secret + "_t.jpg" + '"/></a>' +
					'<a href="' + curPhotoObj.urls.url[0]._content + '" target="flickr">' +
					curPhotoObj.title._content+"</a><br/>"+
					curPhotoObj.description._content+"<br/>"+
					"by " + '<a href="http://www.flickr.com/photos/' + curPhotoObj.owner.nsid + '" target="flickr">' + 
					((curPhotoObj.owner.realname!="")?curPhotoObj.owner.realname:curPhotoObj.owner.username) + "</a><br/>",
					{maxWidth:400}
				);
			}
		}, 500 );
}

function MeUnload() {
	for (var i=0; i<atags.length; i++) {
		atags[i].onclick=null;
	}
	atags=[];
}

// Mouse wheel add-on (http://groups.google.co.uk/group/Google-Maps-API/msg/22adcdbf42cea0f9?dmode=source)
GMap2.prototype.wheelZoom = function(event)
{
	if((event.detail || -event.wheelDelta) < 0){ 
		map.zoomIn(curLatLng, null, true);
	}
	else{ 
		map.zoomOut(curLatLng, true);
	}
	return false;
}

map=null;
marker=null;
point=null;
curLocationObj=null;
curLatLng=null;
function load() {
	var mapbox = document.getElementById('mapbox');
	if (GBrowserIsCompatible()) {
		map = new GMap2(mapbox);
		map.enableContinuousZoom();
		map.enableDoubleClickZoom(); 
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		point = new GLatLng(22.339914,114.108124);
		map.setCenter(point, 11);
		marker = new GMarker(point);
		map.addOverlay(marker);
		map.setMapType(G_HYBRID_MAP);
		GEvent.addListener(marker, "click", markerClicked);
		GEvent.addDomListener(mapbox, "DOMMouseScroll", map.wheelZoom);
		GEvent.addDomListener(mapbox, "mousewheel", map.wheelZoom);
		GEvent.addListener(map, "click", function(marker, point) {
			map.setCenter(point); } );
		GEvent.addListener(map, "mousemove", function(latlng){curLatLng=latlng;});
	}
	setTimeout(
		function() {
			var querybox = document.getElementById('query');
			querybox.focus();
			querybox.select();
		}, 50 );
}

// Tab
var tabberOptions = {
	'onClick': function(argsObj) {
		var t = argsObj.tabber; /* Tabber object */
		var i = argsObj.index; /* Which tab was clicked (0 is the first tab) */
		if (i==0) {
			setTimeout(
				function() {
					var querybox = document.getElementById('query');
					querybox.focus();
					querybox.select();
				}, 50 );
		}
		else if (i==1){
			setTimeout(
				function() {
					var resnamebox = document.getElementById('resname');
					resnamebox.focus();
					resnamebox.select();			
				}, 50 );
		}
		else if (i==2) {
			setTimeout(
				function() {
					var phototextbox = document.getElementById('phototext');
					phototextbox.focus();
					phototextbox.select();			
				}, 50 );
		}
	}
};

loadingDiv = null;
function loading() {
	loadingDiv = document.createElement("div");
	loadingDiv.style.position = "absolute";
	loadingDiv.style.left = "0px";
	loadingDiv.style.top = "0px";
	loadingDiv.style.width = "100%";
	loadingDiv.style.height = "100%";
	loadingDiv.style.zIndex = 100;
	var backgroundDiv = document.createElement("div");
	backgroundDiv.style.position = "absolute";
	backgroundDiv.style.left = "0px";
	backgroundDiv.style.top = "0px";
	backgroundDiv.style.width = "100%";
	backgroundDiv.style.height = "100%";
	backgroundDiv.style.backgroundColor = "white";
	backgroundDiv.style.opacity = "0.5";	// safari, opera
	backgroundDiv.style.MozOpacity = "0.5";	// mozilla
	backgroundDiv.style.filter = "alpha(Opacity=50)";	// IE
	var messageDiv = document.createElement("div");
	messageDiv.innerHTML = "Searching...";
	loadingDiv.appendChild(backgroundDiv);
	loadingDiv.appendChild(messageDiv);
	document.body.appendChild(loadingDiv);
}

function endloading() {
	document.body.removeChild(loadingDiv);
}

window.onload = load;
