var MAP_CENTER = new GLatLng(49.224773,-94.699402)
var MAP_ZOOM = 9

function killChildren(element){
	children = element.childNodes;
	
	for(i=0;i<children.length;i++){
		child = children[i];
		element.removeChild(child);
	}
	return true;
}


function MarkFactory(){
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	
	this.makeMark = function(point, letter){
		// Create a lettered icon for this point using our icon class
		var letter = String.fromCharCode(letter.charCodeAt(0));
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
		
		// Set up our GMarkerOptions object
		markerOptions = { icon:letteredIcon };
		var marker = new GMarker(point, markerOptions);
		
		return marker;
	}
	
	this.makeAnchorageMark = function(point){
		var resortIcon = new GIcon();
		resortIcon.image = "images/anchorage.png";
		resortIcon.iconSize = new GSize(20, 20);
		resortIcon.iconAnchor = new GPoint(10,10);
		resortIcon.infoWindowAnchor = new GPoint(10, 10);
		
		return new GMarker(point, {icon:resortIcon});
	}
	this.makeMarinaMark    = function(point){
		var resortIcon = new GIcon();
		resortIcon.image = "images/marina.png";
		resortIcon.iconSize = new GSize(20, 20);
		resortIcon.iconAnchor = new GPoint(10,10);
		resortIcon.infoWindowAnchor = new GPoint(10, 10);
		
		return new GMarker(point, {icon:resortIcon});
	}
	this.makeLaunchMark    = function(point){return this.makeMark(point, "L");}
	this.makeResortMark    = function(point){
		var resortIcon = new GIcon();
		resortIcon.image = "http://maps.google.com/mapfiles/kml/pal2/icon28.png";
		resortIcon.iconSize = new GSize(32, 32);
		resortIcon.iconAnchor = new GPoint(16,16);
		resortIcon.infoWindowAnchor = new GPoint(16, 16);
		
		return new GMarker(point, {icon:resortIcon});
	}
	this.makeCustomsMark   = function(point){return this.makeMark(point, "C");}
	this.makeBeachMark     = function(point){
		var resortIcon = new GIcon();
		resortIcon.image = "images/beach.png";
		resortIcon.iconSize = new GSize(20, 20);
		resortIcon.iconAnchor = new GPoint(10,10);
		resortIcon.infoWindowAnchor = new GPoint(10, 10);
		
		return new GMarker(point, {icon:resortIcon});
	}
	this.makeParkMark      = function(point){
		var resortIcon = new GIcon();
		resortIcon.image = "images/park.png";
		resortIcon.iconSize = new GSize(20, 20);
		resortIcon.iconAnchor = new GPoint(10,10);
		resortIcon.infoWindowAnchor = new GPoint(10, 10);
		
		return new GMarker(point, {icon:resortIcon});
	}
	this.makeHazardMark    = function(point){
		var resortIcon = new GIcon();
		resortIcon.image = "http://maps.google.com/mapfiles/kml/pal3/icon42.png";
		resortIcon.iconSize = new GSize(32, 32);
		resortIcon.iconAnchor = new GPoint(16,16);
		resortIcon.infoWindowAnchor = new GPoint(16, 16);
		
		return new GMarker(point, {icon:resortIcon});
	}
	this.makeFishingMark    = function(point){
		var resortIcon = new GIcon();
		resortIcon.image = "http://maps.google.com/mapfiles/ms/micons/fishing.png";
		resortIcon.iconSize = new GSize(32, 32);
		resortIcon.iconAnchor = new GPoint(16,16);
		resortIcon.infoWindowAnchor = new GPoint(16, 16);
		
		return new GMarker(point, {icon:resortIcon});
	}
	
	
}

function Mark(name, description, mark){
	this.name = name;
	this.description = description;
	this.mark = mark;
	
	GEvent.addListener(
		this.mark,
		"click",
		function() {
			this.openInfoWindowHtml("<b>" + name + '</b><p class="info_window">'  + description + '</p>', {maxWidth:350});
		}
	);

}

function MarkList(){
	
	this.markList = new Array();
	
	this.addMark = function(mark){
		this.markList.push(mark);
	}
	
	this.getMarkList = function(){
		return this.markList;
	}
	
	this.search = function(query){
		//no case search str.search(/w3schools/i)
		expression = new RegExp(query, "i");
		limitedList = new Array();
		for(i=0;i<this.markList.length;i++){ //check for marks that match query
			mark = this.markList[i];
			if(mark.name.search(expression) > -1){ //query matches current mark
				limitedList.push(mark); //add to list of search results.
			}
		}
		
		return limitedList;
	}
}


function MapContainer(canvasElement){
	
	this.loadMarks = function(markList){
		this.markList = markList;
		this.showMarks(this.markList.markList);
	}
	this.addOverlay = function(url){
		var kml = new GGeoXml(url);
		this.map.addOverlay(kml)
	}
	
	this.showMarks = function(markList){
		this.clearMarks();
		bounds = new GLatLngBounds();
		for(i=0;i<markList.length;i++){
			mark = markList[i];
			this.map.addOverlay(mark.mark);
			bounds.extend(mark.mark.getLatLng());
		}
		
		if(this.map.getBoundsZoomLevel(bounds) < 14){
			this.map.setZoom(this.map.getBoundsZoomLevel(bounds));
		}
		else{
			this.map.setZoom(14);
		}
		this.map.setCenter(bounds.getCenter())
		
		this.visibleMarks = markList;
		
		this.showList(markList);
	}
	
	this.clearMarks = function(){
		for(i=0;i<this.visibleMarks.length;i++){
			mark = this.visibleMarks[i];
			this.map.removeOverlay(mark.mark);
		}
		
		this.clearList();
	}
	
	this.showList = function(markList){
		
		ul = document.createElement("ul");
		this.markListElement.appendChild(ul); 
		
		
		for(i=0;i<markList.length;i++){
			mark = markList[i];
			
			li = document.createElement("li");
			ul.appendChild(li);
			
			link = document.createElement("a");
			li.appendChild(link);
			
			link.innerHTML = mark.name;
			link.setAttribute("href", "javascript:;");
			link.mark = mark.mark;
			link.description = mark.description;
			link.name = mark.name;
			
			GEvent.addDomListener(
				link,
				"click",
				function() {
					this.mark.openInfoWindowHtml("<b>" + this.name + '</b><p class="info_window">'  + this.description + '</p>', {maxWidth:350});
					CONTAINER.map.setCenter(this.mark.getLatLng());
				}
			);
			
		}
	}
	this.clearList = function(){
		killChildren(this.markListElement);
	}
	
	this.search = function(query){
		if(query == ""){
			this.showMarks(this.markList.markList);
		}
		else{
			newList = this.markList.search(query);
			this.showMarks(newList);
		}
	}
	
	this.canvasElement = canvasElement;
	
	this.visibleMarks = new MarkList();

	//Create widget
	this.mapElement = document.createElement("div");
	this.mapElement.className = "map";
	this.canvasElement.appendChild(this.mapElement);
	
	/*marksElement = document.createElement("div");
	marksElement.className = "marks";
	this.canvasElement.appendChild(marksElement);
	
	this.searchElement = document.createElement("input");
	this.searchElement.setAttribute("type", "input");
	this.searchElement.className = "search";
	marksElement.appendChild(this.searchElement);
	GEvent.addDomListener(
		this.searchElement,
		"keyup",
		function() {
			CONTAINER.search(this.value);
		}
	);
	
	this.markListElement = document.createElement("div");
	this.markListElement.className = "markList";
	marksElement.appendChild(this.markListElement);*/
	
	
	
	this.map = new GMap2(this.mapElement);
	this.map.setCenter(MAP_CENTER, MAP_ZOOM);
	this.map.setMapType(G_SATELLITE_MAP);
	this.map.addControl(new GLargeMapControl());
	this.map.enableScrollWheelZoom();
	
	
}


