var gLocalSearch;
var gMap;
var gInfoWindow;
var gSelectedResults = [];
var gCurrentResults = [];
var gSearchForm;
var gGeocoder;
var gSearchWell;
var gAddress;
var gCanvasId;

function popupMap(addr,name,faddr){
	var url='http://www.aviationschoolsonline.com/google-popup-map.php?address='+addr+'&school_name='+name+'&faddress='+faddr;
	if (document.all) {
		var xMax = screen.width, yMax = screen.height
	} else if (document.layers) {
		var xMax = window.outerWidth, yMax = window.outerHeight
	} else {
		var xMax = 640, yMax=480
	}; 
	var xOffset = (xMax - 200)/3, yOffset = (yMax - 200)/4; 
	window.open(url,'AviationSchoolsOnline_Maps','width=700,height=437,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',resizable=0,statusbar=0,menubar=0,directories=0');
}

//
// Initialize & draw map
//
function initializeMap(canvas, address) {

	gGeocoder = new google.maps.Geocoder();
	gGeocoder.geocode( { 'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
			var myOptions = {
				zoom: 13,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			gMap = new google.maps.Map(document.getElementById(canvas), myOptions);
			var marker = new google.maps.Marker({
				map: gMap,
				position: results[0].geometry.location
			});
		}else{
			//console.log("Geocode was not successful for the following reason: " + status);
			//alert("Geocode was not successful for the following reason: " + status);
			var error = "SOL";
		}
	});

	// Create one InfoWindow / InfoBox to open when a marker is clicked.

	gInfoWindow = new google.maps.InfoWindow();

	google.maps.event.addListener(gInfoWindow, 'closeclick', function() {
		unselectMarkers();
	});

	// Initialize the local searcher
	gLocalSearch = new GlocalSearch();
	gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);

}

//
// Create "tiny" marker icons
//
var gYellowIcon = new google.maps.MarkerImage(
	"http://labs.google.com/ridefinder/images/mm_20_yellow.png",
	new google.maps.Size(12, 20),
	new google.maps.Point(0, 0),
	new google.maps.Point(6, 20));

var gRedIcon = new google.maps.MarkerImage(
	"http://labs.google.com/ridefinder/images/mm_20_red.png",
	new google.maps.Size(12, 20),
	new google.maps.Point(0, 0),
	new google.maps.Point(6, 20));

var gSmallShadow = new google.maps.MarkerImage(
	"http://labs.google.com/ridefinder/images/mm_20_shadow.png",
	new google.maps.Size(22, 20),
	new google.maps.Point(0, 0),
	new google.maps.Point(6, 20));
 
//
// Set up the map and the local searcher.
//
function OnLoad() {

	// Create one InfoWindow to open when a marker is clicked.
	/*
	gInfoWindow = new google.maps.InfoWindow;
	google.maps.event.addListener(gInfoWindow, 'closeclick', function() {
		unselectMarkers();
	});
*/

}
 
function unselectMarkers() {

	for (var i = 0; i < gCurrentResults.length; i++) {
		gCurrentResults[i].unselect();
	}

}
 
function doSearch(query, canvas, address, searchwell) {

	gLocalSearch.setCenterPoint(gMap.getCenter());
	gLocalSearch.execute(query);
	gSearchWell = searchwell;
	gAddress = address;
	gCanvasId = canvas;

}

//
// Called when Local Search results are returned, we clear the old
// results and load the new ones.
//
function OnLocalSearch() {

	if (!gLocalSearch.results) return;
	var searchWell = document.getElementById(gSearchWell);
	//searchWell.style.display='none';
 
	// Clear the map and the old search well
	searchWell.innerHTML = "";
	for (var i = 0; i < gCurrentResults.length; i++) {
		gCurrentResults[i].marker().setMap(null);
	}
	// Close the infowindow
	gInfoWindow.close();
 
	gCurrentResults = [];
	for (var i = 0; i < gLocalSearch.results.length; i++) {
		gCurrentResults.push(new LocalResult(gLocalSearch.results[i]));
	}
 
	var attribution = gLocalSearch.getAttribution();
	if (attribution) {
		document.getElementById(gSearchWell).appendChild(attribution);
	}
 
	// Move the map to the first result
	var first = gLocalSearch.results[0];
	gMap.setCenter(new google.maps.LatLng(parseFloat(first.lat),
	parseFloat(first.lng)));
 
}
 
//
// Cancel the form submission, executing an AJAX Search API search.
//
function CaptureForm(searchForm) {

	gLocalSearch.execute(searchForm.input.value);
	return false;

}
 
// 
// A class representing a single Local Search result returned by the
// Google AJAX Search API.
//
function LocalResult(result) {

	var me = this;
	me.result_ = result;
	me.resultNode_ = me.node();
	me.marker_ = me.marker();
	google.maps.event.addDomListener(me.resultNode_, 'mouseover', function() {
		// Highlight the marker and result icon when the result is
		// mouseovered.  Do not remove any other highlighting at this time.
		me.highlight(true);
	});

	google.maps.event.addDomListener(me.resultNode_, 'mouseout', function() {
		// Remove highlighting unless this marker is selected (the info
		// window is open).
		if (!me.selected_) me.highlight(false);
	});
	
	google.maps.event.addDomListener(me.resultNode_, 'click', function() {
		me.select();
	});
	
	document.getElementById(gSearchWell).appendChild(me.resultNode_);

}
 
LocalResult.prototype.node = function() {
	if (this.resultNode_) return this.resultNode_;
	return this.html();
};

//
// Returns the GMap marker for this result, creating it with the given
// icon if it has not already been created.
//
LocalResult.prototype.marker = function() {

	var me = this;
	if (me.marker_) return me.marker_;
	var marker = me.marker_ = new google.maps.Marker({
		position: new google.maps.LatLng(parseFloat(me.result_.lat),
		parseFloat(me.result_.lng)),
		icon: gYellowIcon, shadow: gSmallShadow, map: gMap
	});
	google.maps.event.addListener(marker, "click", function() {
		me.select();
	});
	return marker;

};

//
// Unselect any selected markers and then highlight this result and
// display the info window on it.
//
LocalResult.prototype.select = function() {

	unselectMarkers();
	this.selected_ = true;
	this.highlight(true);
	gInfoWindow.setContent(this.html(true));
	//gInfoWindow.setContent("<div>TEST</div>");
	gInfoWindow.open(gMap, this.marker());

};
 
LocalResult.prototype.isSelected = function() {

	return this.selected_;

};
 
//
// Remove any highlighting on this result.
//
LocalResult.prototype.unselect = function() {

	this.selected_ = false;
	this.highlight(false);

};
 
//
// Returns the HTML we display for a result before it has been "saved"
//
LocalResult.prototype.html = function() {

	var me = this;
	var container = document.createElement("div");
	/* CHANGE INFOWINDOW/BOX CONTENTS HERE */
	container.className = "unselected";
	container.innerHTML = me.result_.html.childNodes[0].innerHTML + "<br />"
		+ me.result_.html.childNodes[1].innerHTML
		+ me.result_.html.childNodes[2].innerHTML + "<br /><br />";

	return container;

}
 
LocalResult.prototype.highlight = function(highlight) {
	this.marker().setOptions({icon: highlight ? gRedIcon : gYellowIcon});
	this.node().className = "unselected" + (highlight ? " red" : "");
}
 
GSearch.setOnLoadCallback(OnLoad);


