if( hq == undefined ) {
	var hq = {};
}

hq.map = {
	
	gMap : null,
	
	init : function() {
		console.group( 'map::init' );
		
		if( GBrowserIsCompatible() == false ) {
			throw new Error( "This browser is not compatible with Google Maps." );
		}
		
		this.gMap = new GMap2( $j( '#map' )[0] );
		
		this.gMap.addControl( new GLargeMapControl() );
		this.gMap.addControl( new GMapTypeControl() );
		this.gMap.addControl( new GScaleControl() );
		this.gMap.addControl( new GOverviewMapControl() );
		
		this.gMap.enableContinuousZoom();
		this.gMap.enableScrollWheelZoom();
		
		// While the map does get recentered during the initial pane processing, removing this causes google maps to crash.
		this.gMap.setCenter( new GLatLng( 45.5266, -122.6754 ), 13 );
		var $pane = $j( '#hq_idx_content' );
	
		this.mkt = $pane.attr( 'Market' );	
		this.defaultLayer = new GTileLayer( null, 0, 18, {
                        tileUrlTemplate : '/g-tiles/'+this.mkt+'/main/{Z}/{X}/{Y}',
                        isPng : true,
                        opacity : .7
        });	
	
		this.defaultOverlay = new GTileLayerOverlay( this.defaultLayer );
		
		this.gMap.addOverlay( this.defaultOverlay );

		//this.mmgrOptions = { borderPadding: 50, maxZoom: 17, trackMarkers: false };
		//this.mmgr = new MarkerManager(this.gMap,this.mmgrOptions);
		
		$j( '#mapWrapper' ).addClass( 'evtBound' ).resize( toolbox.delegate( this.resize, this ) );
		$j( '#contentWrapper' ).bind( 'postPaneLoad', toolbox.delegate( this.onPostPaneLoad, this ) );
		$j( window ).unload( toolbox.delegate( this.unload, this ) );
		
		this.onPostPaneLoad();
		
		console.groupEnd();
	},
	
	resize : function() {
		console.group( 'map::resize' );
		
		this.gMap.checkResize();
		this.onPostPaneLoad();
		console.groupEnd();
	},
	
	unload : function() {
		console.group( 'map::unload' );
		
		GUnload();
		
		console.groupEnd();
	},
	
	onPostPaneLoad : function( evt ) {
		console.group( 'map::onPostPaneLoad' );

		var $pane = $j( '#hq_idx_content' );
		
		var yMin = $pane.attr( 'yMin' );
		var yMax = $pane.attr( 'yMax' );
		var xMin = $pane.attr( 'xMin' );
		var xMax = $pane.attr( 'xMax' );
	

			if( yMin && yMax && xMin && xMax ) 
			{

				var bounds = new GLatLngBounds();
				bounds.extend( new GLatLng( yMin, xMin, true ) );
				bounds.extend( new GLatLng( yMax, xMax, true ) );

				 hq.map.gMap.setCenter( bounds.getCenter() );
				 hq.map.gMap.setZoom(  hq.map.gMap.getBoundsZoomLevel( bounds ) );
			}


		console.groupEnd();
	},
	
	getBounds : function() {
		console.group( 'map::getBounds' );
		
		var bounds = hq.map.gMap.getBounds();
		var ne = bounds.getNorthEast();
		var sw = bounds.getSouthWest();
		
		//console.info(ne.lng()+','+sw.lng()+','+sw.lat()+','+ne.lat());
		var res = {
			minX : ne.lng(),
			maxX : sw.lng(),
			minY : sw.lat(),
			maxY : ne.lat()
		};
		
		console.groupEnd();
		return res;
	},
	
	addLayer : function(mlayer, url) {
		if(this.overlay)
			this.gMap.removeOverlay(this.overlay);

		if(!url)
			url = '/g-tiles/'+this.mkt+'/' + mlayer + '/{Z}/{X}/{Y}';
		
		this.layer = new GTileLayer( null, 0, 18, {
                        tileUrlTemplate : url,
                        isPng : true,
                        opacity : .7
        });	
	
		this.overlay = new GTileLayerOverlay( this.layer );
		
		this.gMap.addOverlay( this.overlay );

	}
	
};

$j( document ).ready( toolbox.delegate( hq.map.init, hq.map ) );

