google.load("maps", "2");


var mapFuncs  = {
	origLat: 34.500236,
	origLon: -83.320312,
	origZoom: 7,
	currentMarkers: [],
	currentLinks: [],
	init: function() {
		mapFuncs.processIcons();
		map = new google.maps.Map2(document.getElementById("map"));
		map.setCenter(new GLatLng(mapFuncs.origLat,mapFuncs.origLon), mapFuncs.origZoom);
		map.enableScrollWheelZoom();
		map.addMapType(G_PHYSICAL_MAP);
		map.setMapType(G_NORMAL_MAP);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMenuMapTypeControl());
		
		//Setup an icon "shell" to base all my icons off of with all settings except image preset.
		var defaultIcon=new GIcon(G_DEFAULT_ICON);
		defaultIcon.iconSize=new GSize(26, 26);
		defaultIcon.iconAnchor=new GPoint(12, 25);
		defaultIcon.infoWindowAnchor=new GPoint(10, 5);
    
		//Custom Point Icon
		recIcon=new GIcon(defaultIcon);
		recIcon.iconSize=new GSize(23,34);
		recIcon.image="icons/recreation.png";
		recMarkerOptions={icon:recIcon};
	
		wildWaterIcon = new GIcon();
		wildWaterIcon.image="icons/WWLogo.png";
		wildWaterIcon.iconAnchor=new GPoint(12, 25);
		wildWaterIcon.iconSize= new GSize(20,7);
		wildWaterIcon.infoWindowAnchor=new GPoint(10, 5);
		
		cityIcon = new GIcon(defaultIcon);
		cityIcon.iconSize=new GSize(23, 34);
		cityIcon.image="icons/traveller.png";
		cityIconOptions={icon:cityIcon};

		//Plot Major Cities
		var i=0;
		$.each(geoCities, function(k,v) {
			var html=mapFuncs.generateCityMarkerHtml(k);
			var point = new google.maps.LatLng(v.Lat,v.Lon);
			var marker = mapFuncs.createMarker(point,html,cityIcon,'c',v.ID);
			geoCities[k]['marker'] = marker;
			map.addOverlay(marker);
			i++;
    	});
		
		//Plot River Rafting Sites
		$.each(geoRivers, function(k,v) {
			var html=mapFuncs.generateRiverMarkerHtml(k);
			var point = new google.maps.LatLng(v.Lat,v.Lon);
			var marker = mapFuncs.createMarker(point,html,icons[v.MapIconID],'s',v.ID)
			geoRivers[k]['marker']=marker;
			map.addOverlay(marker);
    	});
	},
	
	processIcons:function() {
			$.each(iconsJson,function() {
			var icon = new google.maps.Icon(google.maps.DEFAULT_ICON);
			
			if(this.Image != "") {
				icon.image = this.Image;	
			}
			
			if(this.ImageWidth > 0 && this.ImageHeight > 0) {
				icon.iconSize = new GSize(this.ImageWidth,this.ImageHeight);
			}
			if(this.MapIconID > 12 && this.MapIconID < 18) {
				//Disable custom shadows for river locations
				icon.shadow = "";
			} else {
				if(this.ShadowImage != "") {
					icon.shadow = this.ShadowImage;
				}
				
				if(this.ShadowImageWidth > 0 && this.shadowImageHeight > 0) {
					icon.shadowSize = new GSize(this.ShadowImageWidth, this.ShadowImageHeight);	
				}
			}
			
			if(this.MapAnchorX > 0 && this.MapAnchorY > 0) {
				icon.iconAnchor = new GPoint(this.MapAnchorX,this.MapAnchorY);
			}
			
			if(this.InfoWindowAknchorX > 0 && this.InfoWindowAnchorY > 0) {
				icon.infoWindowAnchor = new GPoint(this.InfoWindowAnchorX, this.InfoWindowAnchorY);
			}

			icons[this.MapIconID] = icon;
				
		});
	},
	
	generateCityMarkerHtml: function(index) {
		if(geoCitiesWithAttractions[index]) {
			var html="<div class='markerHtml'><strong><a href='javascript:mapFuncs.selectAttractionCityFromMap("+geoCities[index].ID+")'>"+geoCities[index].Title+"</a></strong></div>";
		} else {
			var html="<div><strong>"+geoCities[index].Title+"</strong></div>";
		}
		html+="<div>"+geoCities[index].Description+"</div>";
		return html;
	},
	
	/*Still need to deal with plotting images on here.  However without having the site live
	testing will be a bit problematic*/
	generateRiverMarkerHtml: function(index) {
		var address="";
		var url = geoRivers[index].URL;

		if(geoRivers[index].Address.length>0) address+=geoRivers[index].Address+" ";
		if(geoRivers[index].Address2.length>0) address+=geoRivers[index].Address2;
		if(address.length>0) address+="<br />";
		if(geoRivers[index].City.length>0) address+=geoRivers[index].City+", ";
		if(geoRivers[index].State.length>0) address+=geoRivers[index].State+" ";
		if(geoRivers[index].Zip.length>0) address+=geoRivers[index].Zip;
		if(geoRivers[index].Phone.length>0) 
			address+="<br /><strong>Phone: </strong>"+geoRivers[index].Phone;
		
		/*
		if(geoRivers[index].Fax.length>0) { 
			address+="<br /><strong>Fax: </strong>"+geoRivers[index].Fax;
		}	
		*/
		
		if(geoRivers[index].Email.length>0) {
			address+="<br /><strong>Email: </strong>"
			+"<a href=\"mailto:"+geoRivers[index].Email+"\">"+geoRivers[index].Email+"</a>";
		}
		var html="<div title='"+index+"'>";
		if(url.length>0) html+="<a href=\""+url+"\" target=\"_blank\">";
		html+="<strong>"+geoRivers[index].Title+"</strong>";
		if(url.length>0) html+="</a>";
		html+="<table><tr><td>"+address;
		
		html+="</td></tr></table>";		
		html+="</div>";
		return html;
	},
	
	createAreaLinkMarkerHtml: function(link) {
		var address="";
		var url = link.URL;
		if(link.Address.length>0) address+=link.Address+" ";
		if(link.Address2.length>0) address+=link.Address2;
		if(address.length>0) address+="<br />";
		if(link.City.length>0) address+=link.City+", ";
		if(link.State.length>0) address+=link.State+" ";
		if(link.Zip.length>0) address+=link.Zip;
		if(link.Phone.length>0) 
			address+="<br /><strong>Phone: </strong>"+link.Phone;
		
		/*
		if(link.Fax.length>0) { 
			address+="<br /><strong>Fax: </strong>"+link.Fax;
		}	
		*/
		
		if(link.Email.length>0) {
			address+="<br /><strong>Email: </strong>"
			+"<a href=\"mailto:"+link.Email+"\">"+link.Email+"</a>";
		}
		var html="<div class='markerHtml' title='"+link.Title+"'>";
		if(url.length>0) html+="<a href=\""+url+"\" target=\"_blank\">";
		html+="<strong>"+link.Title+"</strong>";
		if(url.length>0) html+="</div>";
		html+="<table><tr><td>"+address+"</td></tr>";
		html+="</table>";		
		html+="</div>";
		return html;
	},
	
	//Travel icons should only display the description
	createTravelDistanceMakerHtml: function(link) {
		var html="<div class='markerHtml'>";
		html += link.Title + "<br />";
		html += link.Description + '</div>';
		return html;
	},
	
	
	createMarker: function(point,html,icon,type,id) {
		var marker = new google.maps.Marker(point,icon);
		//If index was passed in then this is a river location.  Use
		//Click action instead of mouse over.
		if(type!=undefined  ) {
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
				//This will update the combo box, which will then fire the map pan/zoom event.
				$('#raftingSitesSel').val(type+"-"+id);
				if($('#raftingSitesSel').val()!=undefined) {
					mapFuncs.zoomToSite();
				}
			});
			
			if(type=="c") {
				//use mouse over to pop up balloons
				GEvent.addListener(marker, "mouseover", function() {
					marker.openInfoWindowHtml(html);
				});	
			}
			
		} else {
			//use mouse over to pop up balloons
			GEvent.addListener(marker, "mouseover", function() {
				marker.openInfoWindowHtml(html);
			});
		}
		return marker;
	},
	
	createAreaLinkMarker: function(point,html,icon,index) {
		var marker = new google.maps.Marker(point,icon);
		GEvent.addListener(marker, "mouseover", function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	},
	
	zoomToSite: function() {
		//Clear any existing markers
		mapFuncs.clearAllAreaLinks();
		var selValArray = $("#raftingSitesSel").val().split("-");
		var siteId = selValArray[1];
		var centerLat,centerLon,centerPoint;
		if(selValArray[0]=="s") { //rafting site
			mapFuncs.openRiverMarkerInfoWindow(siteId);
			centerPoint = new google.maps.LatLng(geoRivers[siteId]['Lat'],geoRivers[siteId]['Lon']);
		} else if(selValArray[0]=="c") { //city with attractions
			mapFuncs.openCityMarkerInfoWindow(siteId);
			centerPoint = new google.maps.LatLng(geoCitiesWithAttractions[siteId]['Lat'],geoCitiesWithAttractions[siteId]['Lon']);
		} else {
			mapFuncs.zoomOut();
			return;
		}
		
		map.setCenter(centerPoint,10);
		$(".zoomedIn").css('visibility','visible');
		//mapFuncs.showLinks();
		$(".hideOnZoom").hide();
		var x = $('#raftingSites').height();
		var y = $('#topHalf').height();
		$('#links').height(x-y);
	},
	
	zoomOut: function() {
		$(".hideOnZoom").show();
		map.setCenter(new google.maps.LatLng(mapFuncs.origLat,mapFuncs.origLon), mapFuncs.origZoom);
		map.closeInfoWindow();
		mapFuncs.clearAllAreaLinks();
		$(".zoomedIn").css('visibility','hidden');
		$("#raftingSitesSel").attr('selectedIndex',0);
	},
	
	openRiverMarkerInfoWindow: function(markerIndex) {
		var html=mapFuncs.generateRiverMarkerHtml(markerIndex);
		geoRivers[markerIndex]['marker'].openInfoWindowHtml(html);
	},
	
	openCityMarkerInfoWindow: function(markerIndex) {
		var html=mapFuncs.generateCityMarkerHtml(markerIndex);
		geoCities[markerIndex]['marker'].openInfoWindowHtml(html);
	},
	
	showLinks: function() {
		//Clear any existing markers
		mapFuncs.clearAllAreaLinks();
		mapFuncs.currentLinks = [];
		//First get river/city selected selected.
		var s = $("#raftingSitesSel").val().split("-");
		var siteId = s[1];
		if(s[0]=="s") { //river
			var river=geoRivers[siteId].Title;
			var codes=river.split(" ");
			var code=codes[0];
		} else if (s[0]=="c") { //city
			var river=geoCities[siteId].Title;
			var code = geoCities[siteId].City;
		}
		var catStr="";
		//Now get the selected checkboxes.
		$.each($(".zoomedIn :checked"), function(data) {
			catStr+=$(this).val()+",";
		});
		if(catStr.length>0) catStr=catStr.substr(0,catStr.length-1);
		var linkAreaHtml="";
		$.getJSON('mapHelper.php','m=getLinks&code='+code+'&cats='+catStr,
			function (data) {
				var defaultIcon=new GIcon(G_DEFAULT_ICON);
				
				$.each(data,function(i,v) {
					mapFuncs.currentLinks.push(v);
					var summary=""
					if(v.City.length>0) summary+=v.City+", ";
					if(v.State.length>0) summary+=v.State+" ";
					if(v.Zip.length>0) summary+=v.Zip;
					
					linkAreaHtml+="<div style='margin-bottom:10px'><a href=\"javascript:mapFuncs.openAreaInfoBox("+i+")\">"
					+v.Title+"</a><br />"+summary;
					linkAreaHtml+="<br /><a href='/links/?gid="+v.ID+"' target='_blank'>More Info</a></div>";
					
					//now look to see if a marker exists
					//if(!v.Marker) {
						var html ="";
						if(v.travelPoint==0) {
							html=mapFuncs.createAreaLinkMarkerHtml(v);
						} else {
							html = mapFuncs.createTravelDistanceMakerHtml(v);
						}
						var point = new google.maps.LatLng(v.Lat,v.Lon);
						if(v.mapIcon!="") {
							var marker = mapFuncs.createAreaLinkMarker(point,html,icons[v.mapIconID],i);
						} else {
							var marker = mapFuncs.createAreaLinkMarker(point,html,defaultIcon,i);
						}

						mapFuncs.currentMarkers.push(marker);						
						map.addOverlay(marker);
						/*
					} else {
						map.addOverlay(v.Marker);
					}
					*/
				});
				$("#links").html(linkAreaHtml);
			}
		);
	},
	
	clearAllAreaLinks: function () {
		$.each(mapFuncs.currentMarkers,function() {
			map.removeOverlay(this);					  
		});
		mapFuncs.currentMarkers = []; 
		$("#links").html("");
	},
	
	openAreaInfoBox: function(markerIndex) {
		
		var html;
		if(mapFuncs.currentLinks[markerIndex].travelPoint==0) {
				html=mapFuncs.createAreaLinkMarkerHtml(mapFuncs.currentLinks[markerIndex]);
		} else {
				html = mapFuncs.createTravelDistanceMakerHtml(mapFuncs.currentLinks[markerIndex]);
		}
		mapFuncs.currentMarkers[markerIndex].openInfoWindowHtml(html);
	},
	
	selectAttractionCityFromMap: function(cityIndex) {
		$("#raftingSitesSel").val("c-"+cityIndex);
		mapFuncs.zoomToSite();
	}
};