var MegosiMap = function() {
	
	// Set some variables
	this.DEBUG = false;
	this.PREFIX = "Map: ";
	
	//this.domain = "office.pugetworks.com/idbill";
	this.domain = "megosi.com";
	this.map = null;
	this.defaultLat = 47.6100979;
	this.defaultLng = -122.339286;
	this.defaultZoom = 10;
	this.listener = false;
	if ( this.DEBUG ) { console.log( this.PREFIX + "initialized." ); }
}

// Initialize the map with basic settings
MegosiMap.prototype.initMap = function( elementID ) {
	if ( this.DEBUG ) { console.log( this.PREFIX + "Drawing map...in element: " + elementID ); }
	
	if ( ! document.getElementById( elementID ) ) {
		errorDisplay("Missing element: " + elementID + " and cannot draw map!" );
	}
 	// Verify that browser can handle maps...
	if ( GBrowserIsCompatible() ) {
	
		// Initialize Google Map
		this.map = new GMap2( document.getElementById( elementID ) );
		this.map.setCenter( new GLatLng( this.defaultLat, this.defaultLng ), this.defaultZoom );
		// Set mainMap options
		//mainMap.setMapType( G_HYBRID_MAP );
		this.map.enableScrollWheelZoom();
								
	} else {
		errorDisplay("Browser... cannot... handle.... MAPS!" );
	}
};
	
// Display KML in map
MegosiMap.prototype.displayKML = function( mapRef, kmlRef, display, center, zoomlevel ) {
	if ( this.DEBUG ) { console.log( this.PREFIX + "Displaying KML..." ); }
	
	if ( kmlRef.loadedCorrectly() === true ) {
		
		if ( display ) {
			mapRef.addOverlay( kmlRef );
		}
		
		if ( center ) {
			if ( zoomlevel ) {
				mapRef.setCenter( kmlRef.getDefaultCenter(), zoomlevel );
			} else {
				mapRef.setCenter( kmlRef.getDefaultCenter(), kmlRef.gotoDefaultViewport( mapRef ) );
			}
		}
	} else {
		if ( this.DEBUG ) { console.log( this.PREFIX + "...failed!" ) }
	}
};
	
// Retrieve and Load a KML that contains 1 waypoint of a given type
MegosiMap.prototype.loadKMLPoint = function( mapRef, waypointId, type, center, zoomlevel ) {
	if ( this.DEBUG ) { console.log( this.PREFIX + "Fetching new xml for waypointId: " + waypointId + " /w args:\n\tType: " + type + "\n\tCenter: " + center + "\n\tZoom: " + zoomlevel ); }
	var display;
	
	var url = "http://" + this.domain + "/map/mapServlet:getWaypoint";
	    url += "?id=" + waypointId + "&type=" + type;
	    url += "&time=" + new Date().getTime(); // subvert google cache
	    
	if ( this.DEBUG ) { console.log( this.PREFIX + "from: " + url ); }
	
	if ( type === "off" || type === "zoom" ) {
		display = true;
	} else {
		display = false;
	}
	    
	var geoxmlPt = new GGeoXml( url, function() { mapRef.displayKML( mapRef.map, geoxmlPt, display, center, zoomlevel ); } );

	return geoxmlPt;
};

// Retrieve and Load a KML Object (a Challenge Line or a Challenge Polygon)
MegosiMap.prototype.loadKMLObject = function( mapRef, challengeId ) {
	if ( this.DEBUG ) { console.log ( this.PREFIX + "Fetching new xml object for challengeId: " + challengeId ); }
	
	var url = "http://" + this.domain + "/map/mapServlet:getObject";
	url += "?id=" + challengeId;
	url += "&time=" + new Date().getTime(); // subvert google cache
	    
	if ( this.DEBUG ) { console.log( this.PREFIX + "from: " + url ); }

	var geoxmlArea = new GGeoXml( url, function() { mapRef.displayKML( mapRef.map, geoxmlArea, true, true, false ); } );
	return geoxmlArea;
};

//Retrieve and Load a KML Object (a Challenge Line or a Challenge Polygon)
MegosiMap.prototype.loadKMLObjectNew = function( mapRef, challengeId, waypointId, newLat, newLng ) {
	if ( this.DEBUG ) { console.log ( this.PREFIX + "Fetching new xml object for challengeId: " + challengeId ); }
	
	var url = "http://" + this.domain + "/map/mapServlet:getObject";
	url += "?id=" + challengeId;
	if ( waypointId !== 0 ) { url += "&waypointId=" + waypointId; }
	url += "&lat=" + newLat;
	url += "&lng=" + newLng;
	url += "&time=" + new Date().getTime(); // subvert google cache
	    
	if ( this.DEBUG ) { console.log( this.PREFIX + "from: " + url ); }

	var geoxmlArea = new GGeoXml( url, function() { mapRef.displayKML( mapRef.map, geoxmlArea, true, false ); } );
	return geoxmlArea;
};

// Retrieve and load a KML with all the Challenge info
MegosiMap.prototype.loadKMLChallenge = function( mapRef, challengeId, waypointId ) {
	if ( this.DEBUG ) { console.log ( this.PREFIX + "Fetching new xml complete info for challengeId: " + challengeId ); }
	
	var url = "http://" + this.domain + "/map/mapServlet:getObjectAndWaypoints";
	url += "?id=" + challengeId;
	url += "&waypointId=" + waypointId;
	url += "&time=" + new Date().getTime(); // subvert google cache
	    
	if ( this.DEBUG ) { console.log( this.PREFIX + "from: " + url ); }

	var geoxmlArea = new GGeoXml( url, function() { mapRef.displayKML( mapRef.map, geoxmlArea, true, false ); } );
	return geoxmlArea;
};
