var gLocalSearch;
var gLocalSearch1;
// var gMap;
var gSelectedResults = [];
var gCurrentResults = [];
var gSearchForm;
var gSearchForm1;

// Create our "tiny" marker icon

function OnLoad() {
	gSearchForm = new GSearchForm(false, document.getElementById("searchform"));
	gSearchForm.setOnSubmitCallback(null, CaptureForm);
	gLocalSearch = new GlocalSearch();
	gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
	gSearchForm.execute("");

}




function point_OnLocalSearch() {
	if (!gLocalSearch.results) return;

	/*
	lat
	lng
	title
	titleNoFormatting
	ddUrl
	ddUrlToHere
	streetAddress
	city
	region
	country
	staticMapUrl
	phoneNumbers type  number
	*/
	gCurrentResults = [];
	var inhtml="<table cellpadding=0 cellspacing=0 width=95% style='table-layout:fixed' align=center>";

	inhtml+="<tr height=2px><td colspan=3 bgcolor=#609ECF></td></tr>";
	inhtml+="<col width=110px align=left>";
	inhtml+="<col width=230px align=left>";
	inhtml+="<col width=50px align=left>";
	//$('#seach_form').css({'bottom':'0px'});
	inhtml+="<tr bgcolor='#F9F9F9' height=25px><td align=left><b style='color:#474747'>국가</b></td><td  align=left><b style='color:#474747'>주소</a></td><td><b style='color:#474747'  align=left>위치선택</b></td></tr>";
	//inhtml+="<tr height=1px><td colspan=3 align=right><a href=javascript:void(0) onclick=$('#SearchPopupdata').hide();>닫기</a></td></tr>";

	inhtml+="<tr height=1px><td colspan=3 bgcolor=#ADD1EB></td></tr>";

	for (var i = 0; i < gLocalSearch.results.length; i++) {
		inhtml += "<tr height=25px>";
		inhtml += "<td align=left >"+gLocalSearch.results[i].country+"</td>";
		inhtml += "<td align=left >"+gLocalSearch.results[i].titleNoFormatting +"</td>";
		inhtml +="<td align=left><img src='img/area.gif' style='cursor:pointer' align=absmiddle onclick=document.getElementById('mapiframe').contentWindow.MAPS.Control.viewpoint("+gLocalSearch.results[i].lng+","+gLocalSearch.results[i].lat+",'"+encodeURIComponent(gLocalSearch.results[i].titleNoFormatting)+"','G')></td>";
		inhtml += "</tr>";
		inhtml+="<tr height=1px><td colspan=3 bgcolor=#ADD1EB></td></tr>";

	}
	inhtml+="</table>";

	$("#SearchPopupdata_point").html(inhtml);
	return false;
}

function OnLocalSearch() {
	
	if (!gLocalSearch.results) return;

	/*
	lat
	lng
	title
	titleNoFormatting
	ddUrl
	ddUrlToHere
	streetAddress
	city
	region
	country
	staticMapUrl
	phoneNumbers type  number
	*/
	gCurrentResults = [];
	var inhtml="<table cellpadding=0 cellspacing=0 width=95% style='table-layout:fixed' align=center>";

	inhtml+="<tr height=2px><td colspan=3 bgcolor=#609ECF></td></tr>";
	inhtml+="<col width=110px align=left>";
	inhtml+="<col width=230px align=left>";
	inhtml+="<col width=50px align=left>";
	//$('#seach_form').css({'bottom':'0px'});
	inhtml+="<tr bgcolor='#F9F9F9' height=25px><td align=left><b style='color:#474747'>국가</b></td><td  align=left><b style='color:#474747'>주소</a></td><td><b style='color:#474747'  align=left>위치선택</b></td></tr>";
	//inhtml+="<tr height=1px><td colspan=3 align=right><a href=javascript:void(0) onclick=$('#SearchPopupdata').hide();>닫기</a></td></tr>";

	inhtml+="<tr height=1px><td colspan=3 bgcolor=#ADD1EB></td></tr>";

	for (var i = 0; i < gLocalSearch.results.length; i++) {
		inhtml += "<tr height=25px>";
		inhtml += "<td align=left >"+gLocalSearch.results[i].country+"</td>";
		inhtml += "<td align=left >"+gLocalSearch.results[i].titleNoFormatting +"</td>";
		inhtml +="<td align=left><img src='img/area.gif' style='cursor:pointer' align=absmiddle onclick=document.getElementById('mapiframe').contentWindow.MAPS.Control.viewpoint("+gLocalSearch.results[i].lng+","+gLocalSearch.results[i].lat+",'"+encodeURIComponent(gLocalSearch.results[i].titleNoFormatting)+"','G')></td>";
		inhtml += "</tr>";
		inhtml+="<tr height=1px><td colspan=3 bgcolor=#ADD1EB></td></tr>";

	}
	inhtml+="</table>";

	$("#SearchPopupdata").html(inhtml);
	return false;
	
}

// Cancel the form submission, executing an AJAX Search API search.
function CaptureForm(searchForm) {
	//	alert(searchForm.input.value)
	gLocalSearch.execute(searchForm.input.value);
	//	gLocalSearch.execute(searchForm);
	return false;
}



// A class representing a single Local Search result returned by the
// Google AJAX Search API.
function LocalResult(result) {
	
	//alert(result.unselectedHtml())
	
	return false;
	this.result_ = result;
	this.resultNode_ = this.unselectedHtml();
	parent.document.getElementById("searchwell").appendChild(this.resultNode_);
	
	//alert(this.resultNode_)
	
	//gMap.addOverlay(this.marker(gSmallIcon));
}

// Returns the GMap marker for this result, creating it with the given
// icon if it has not already been created.
LocalResult.prototype.marker = function(opt_icon) {
	if (this.marker_) return this.marker_;
	var marker = new GMarker(new GLatLng(parseFloat(this.result_.lat),parseFloat(this.result_.lng)),opt_icon);
	GEvent.bind(marker, "click", this, function() {
		marker.openInfoWindow(this.selected() ? this.selectedHtml() :
		this.unselectedHtml());
	});
	this.marker_ = marker;
	return marker;
}

// "Saves" this result if it has not already been saved
LocalResult.prototype.select = function() {
	if (!this.selected()) {
	this.selected_ = true;

	// Remove the old marker and add the new marker
	gMap.removeOverlay(this.marker());
	this.marker_ = null;
	gMap.addOverlay(this.marker(G_DEFAULT_ICON));

	// Add our result to the saved set
	parent.document.getElementById("selected").appendChild(this.selectedHtml());

	// Remove the old search result from the search well
	this.resultNode_.parentNode.removeChild(this.resultNode_);
	}
}

// Returns the HTML we display for a result before it has been "saved"
LocalResult.prototype.unselectedHtml = function() {
	var container = document.createElement("div");
	container.className = "unselected";
	container.appendChild(this.result_.html.cloneNode(true));
	var saveDiv = document.createElement("div");
	saveDiv.className = "select";
	saveDiv.innerHTML = "Save this location";
	GEvent.bindDom(saveDiv, "click", this, function() {
		gMap.closeInfoWindow();
		this.select();
		gSelectedResults.push(this);
	});
	container.appendChild(saveDiv);
	return container;
}

// Returns the HTML we display for a result after it has been "saved"
LocalResult.prototype.selectedHtml = function() {
	return this.result_.html.cloneNode(true);
}

// Returns true if this result is currently "saved"
LocalResult.prototype.selected = function() {
	return this.selected_;
}

GSearch.setOnLoadCallback(OnLoad);