var map;
var directions;
var userMark;

var youIcon;

var markerUserOptions = { icon:youIcon };
var userMarker = null;
var userPoint = null;

var gMapScript = 'http://maps.google.com/maps?file=api&v=2&key=<KEY>&async=2';

var gKeys = {
	'http://www.poconoalliance.com':'ABQIAAAApaGquiJ1YI8VJVhJlLjBBhQT9XauxgqmCBoGjBz-YpzgocZd5hTUeNypwDdgxSb2-_onvvpyFIUIeg',
	'http://www.poconoalliance.org':'ABQIAAAApaGquiJ1YI8VJVhJlLjBBhT5te-LSNTNvHqKvcVWt5529Js66RRHy7CpW1D9rY-2-kxP02x2j8G7WA'
}

function loadGMap(){
	
	if (GBrowserIsCompatible())
	{	
		youIcon = new GIcon(G_DEFAULT_ICON);
		map = new GMap2(document.getElementById('map'));
		directions = new GDirections(map, document.getElementById('directions'));
		createUserMarker(40.9839869, -75.1991719, 'Pocono Healthy Communities Alliance');
	}
	
}

function loadGDirections(form){
	var address = form.from_box.value;
	var query = 'from:' + address + ' to:40.9839869,-75.1991719';
	directions.load(query);
}

function createUserMarker(aLatitude, aLongitude, name){
	if(userMarker == null) {
		try {
			userPoint = new GLatLng(aLatitude, aLongitude);
			userMarker = new GMarker(userPoint, markerUserOptions);
			
			var html = '<strong>' + name + '</strong>';
			
			GEvent.addListener(userMarker, 'click', function(){
				userMarker.openInfoWindowHtml(html);
			});
			
			map.setCenter(userPoint, 15);
			map.addOverlay(userMarker);
			
			//directions.load('from:Seattle to:New York');
			
		} catch(e) {
			alert("(001) Error: " + e);
		}
	} else {
		try {
			userPoint = new GLatLng(aLatitude, aLongitude);
			userMarker.setLatLng(userPoint);
			map.setCenter(userPoint, 15);
		} catch(e) {
			alert("(002) Error: " + e);
		}
		
	}
}


Function.prototype.bindTo = function(scope) {
	var _function = this;
	return function() {
		return _function.apply(scope, arguments);
	}
}

function ViewLoader(){
	this.url = '';
	this.xhr = null;
	this.xhrTimeout			= null;
	this.xhrTimeoutInterval = 60000;
	
	// Callbacks
	this.onSuccess = null;
	this.onFailure = null;
}

ViewLoader.prototype = {
	request: function(aOnSuccess, aOnFailure){
	
		// Set success/failure methods
		this.onSuccess = aOnSuccess || function(){};
		this.onFailure = aOnFailure || function(){};
		
		// Create async object and make request
		try {
			this.xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			this.xhr.onreadystatechange = this.onReadyStateChange.bindTo(this);
			this.xhr.open('GET', this.url, true);
			this.xhr.send(null);
		} catch(e){
			clearInterval(this.xhrTimeout);
			this.onFailure(e);
		}
	},
	
	/**
	 * Used internally to process XMLHttpRequest.
	 */
	onReadyStateChange: function(){
		
		var readystate = this.xhr.readyState;

		switch(readystate){
		case 0:
			//console.log("Request initializing...");
			break;
		case 1:
			//console.log("Request initialized...");
			this.startTimeout();
			break;
		case 2:
			//console.log("Request being sent...");
			break;
		case 3:
			//console.log("Request in process...");
			break;
		case 4:
			//console.log("Request complete.");
			clearInterval(this.xhrTimeout);
			this.doRequestComplete();
			break;
		}
	},
	
	/**
	 * Used internally to process response.
	 */
	doRequestComplete: function(){
		
		var statusCode 	 = this.xhr.status;
		var responseText = this.xhr.responseText;
		
		if(statusCode == 200){
			if(responseText != null && responseText != '')
				this.onSuccess(responseText);
		} else {
			this.onFailure(statusCode);
		}
	},
		
	/**
	 * Starts timeout thread.
	 */
	startTimeout: function(){
		var self = this;	
		this.xhrTimeout = setTimeout(function(){
			if (self.xhr.readyState != 4) {
				self.xhr.abort();
				self.onFailure(408);
			}		
		}, this.xhrTimeoutInterval);
	}
}