var gmap = null;

var Mapper = {
	init: function() {
		if (GBrowserIsCompatible()) {
	        
	        mapDiv = document.getElementById("map");

	        zoomLi = document.getElementById("zoomFeature");
	        zoomLi.style.display="inline";
	        DOM.initAddressZoomListeners();
	        
	        //graceful degradation
	        mapWrapDiv = document.getElementById("mapWrap");
	        mapWrapDiv.style.display = "block";
	        
	        gmap = new GMap2(mapDiv);
	        gmap.addControl( new GLargeMapControl() );
			gmap.addControl( new GMapTypeControl() );
			
			//Zoom to the USA		
			coords = "37.090240, -95.712891";		
			zoom = 10;
			
			coords = DOM.getFirstChildNoNull("focusAddress");
		
			homepoint = Mapper.getPointFromCoords( coords );
			if( homepoint ) {
				zoom = parseInt(DOM.getFirstChildNoNull( "focusZoom"));
				gmap.setCenter( homepoint, zoom );
			}		
			Mapper.setGeoPoints();
		}	
	},
	setGeoPoints: function() {
		$( ".geo" ).each( function() {
			var latlng = this.getAttribute( "title" );
			var childLink = Mapper.getLinkForPanning( this );
			var childAbbr = $( this ).children( "div.mapContent" )[0];
			var infoText = childAbbr.innerHTML;
			var title = Mapper.getTitleFromLink(childLink);
			var defaultIcon = Mapper.getDefaultIcon();
			markerOptions = { title: title, icon:defaultIcon };
			if( this.className.match("barchosen") ) {
				focusIcon = Mapper.getFocusBarIcon();
				markerOptions = {title : title, icon : focusIcon, zIndexProcess: function(){ return 1;}}
			}
			
			Mapper.showAddress( latlng, markerOptions, infoText, childLink );
		});
	},
	getPointFromCoords: function( coords ) {
		point = null;
		if( coords && coords != "") {
			coordsArr = coords.split(",");
			if( coordsArr && coordsArr.length > 1 ) {
				lat = coordsArr[0];
				lng = coordsArr[1];
				point = new GLatLng(lat, lng);
			}
		}
		return point
	},
	showAddress: function(latlng, markerOptions, infoText, link ) {
		point = Mapper.getPointFromCoords( latlng );
		if( point ) {
			var marker = new GMarker(point, markerOptions );
			GEvent.addListener(marker, "click", function() {
		    	marker.openInfoWindowHtml( infoText );
			});
			
			if( link ) {
				link.onclick = function() {
					gmap.setCenter(point, 18);
					marker.openInfoWindowHtml( infoText );
				}
			}
			gmap.addOverlay(marker);
		}
	},
	getLinkForPanning: function( elem ) {
		var childLink = null;
		if( elem.className.match( "pan") ) {
			childLink = elem;
			//childLinks = $( elem ).children( "a" );
			//if( childLinks && childLinks.length > 0 ) {
			//	childLink = childLinks[0];
			//}
		}
		return childLink;	
	},
	getTitleFromLink: function( link ) {
		var title = "";
		if( link && link.firstChild ) {
			title = link.firstChild.nodeValue;
		}
		return title;
	},
	getDefaultIcon: function (){
		icon = new GIcon(G_DEFAULT_ICON);
		icon.mozPrintImage =  "/media/images/default.gif";
		icon.image =  "/media/images/default.png";
		return icon;
    },
	getFocusBarIcon: function (){
		icon = new GIcon(G_DEFAULT_ICON);
		icon.mozPrintImage =  "/media/images/focus.gif";
		icon.image =  "/media/images/focus.png";
		return icon;
    },
    getZoomIcon: function() {
		icon = new GIcon(G_DEFAULT_ICON);
		icon.mozPrintImage =  "/media/images/zoom.gif";
		icon.image =  "/media/images/zoom.png";
		return icon;    
    },
    zoomOnAddress: function( address ) {
    	var geocoder = new GClientGeocoder();
    	geocoder.getLatLng( 
    		address,
            function(point) {
                if (!point) {
                        alert(address + " not found.  Sorry.");
                        } else {
                        gmap.setCenter(point, 12);
                        icon = Mapper.getZoomIcon();
                        var marker = new GMarker(point, {title:address, icon:icon, zIndexProcess: function(){ return 1;}});
                        GEvent.addListener(marker, "click", function() {
                        	marker.openInfoWindowHtml(address);
                        });
                        gmap.addOverlay(marker);
                        gmap.setCenter( point, 16 );
                        marker.openInfoWindowHtml(address);
                    }
            }
        );
    }
}

var DOM = {
	getFirstChildNoNull: function( elemId ) {
		childValue = "";
		elem = document.getElementById( elemId );
		if( elem ) {
			if( elem.firstChild ) {
				childValue = elem.firstChild.nodeValue;
			}
		}
		return childValue;
	},
	initAddressZoomListeners: function() {
		$("#addressZoom").keydown(function(e) {
	    	if(e.keyCode == 13) {
		   		 DOM.zoomOnAddress();
   			}
 		}).click( function() {
 			$(this).val("");
 		});
 		$("#addressZoomBtn").click(function() {
 			DOM.zoomOnAddress();
 		});
	},
	zoomOnAddress: function() {
		address = $("#addressZoom").val();
		if(address && address != '') {
			Mapper.zoomOnAddress( address );
		}
	}
}

$( document ).ready( Mapper.init );
$( document ).unload( GUnload() );
