var user_agent=navigator.userAgent.toLowerCase();
var is_iphone = ((user_agent.indexOf('iphone')!=-1));
var map;
var map_large;
var pgButtonDiv;
var xShowPG = false;
var xSplashMapOn = false;

//--------------------------------------------------------------------------------------------
// Setup P&G Locations Map Button
//--------------------------------------------------------------------------------------------
if(typeof GControl != 'undefined'){
	function pgLocations(){}
	pgLocations.prototype = new GControl();
	pgLocations.prototype.initialize = function(map){
	
	  pgButtonDiv = document.createElement("div");
	  this.setButtonStyle_(pgButtonDiv);
	  pgButtonDiv.appendChild(document.createTextNode("Toggle P&G Locations"));
	  google.maps.Event.addDomListener(pgButtonDiv, "click", function(){pgLocationsToggle(map);});
	
	  map.getContainer().appendChild(pgButtonDiv);
	  return pgButtonDiv;
	}
	pgLocations.prototype.getDefaultPosition = function(){return new google.maps.ControlPosition(G_ANCHOR_BOTTOM_RIGHT, new google.maps.Size(3,3));}
	pgLocations.prototype.setButtonStyle_ = function(button){button.className='pgLocations';}
}
//--------------------------------------------------------------------------------------------
// Function for initializing Google Maps
//--------------------------------------------------------------------------------------------
function gMapInitialize(){

	if(GBrowserIsCompatible()){

		map = new google.maps.Map2(document.getElementById('google_map_splash'));
		map.setCenter(new google.maps.LatLng('39.10','-84.52'),11);

		// Displaying List of Neighborhoods to Play With
		var uiOptions = map.getDefaultUI();
		uiOptions.zoom.scrollwheel = false;
		map.setUI(uiOptions);
		map.addControl(new pgLocations());

	}

} 

//--------------------------------------------------------------------------------------------
// Loads a Larger Map in modal window
//--------------------------------------------------------------------------------------------
function getLargeHoodMap(){

	map_large = new google.maps.Map2(document.getElementById("google_map_large"));

	// Setup Large Neighborhood Map
    map_large.setCenter(new google.maps.LatLng('39.10','-84.52'),11);
    var uiOptions = map_large.getDefaultUI();
    uiOptions.zoom.scrollwheel = false;
    map_large.setUI(uiOptions);
	map_large.addControl(new pgLocations());

	// Get Large Neighborhood Boundaries
	if(xHoodMap != false){
		var geoXml = new google.maps.GeoXml(xHoodMap);
		map_large.addOverlay(geoXml);
		google.maps.Event.addListener(geoXml,"load",function(){
			if(geoXml.loadedCorrectly()){
				google.maps.Event.addListener(map_large,"moveend",function(){$("#google_map_large_loader").fadeOut('slow');});
				pgVisible = false;
				if(xShowPG) pgLocationsToggle(map_large);
				geoXml.gotoDefaultViewport(map_large);
				xShowPG=false;
			}
		});
	}
}
function getLargeHoodMapPG(){xShowPG=true;getLargeHoodMap();}

//--------------------------------------------------------------------------------------------
// Function Used for loading Maps "on-call"
//--------------------------------------------------------------------------------------------
function getGeo(xObj,xMap,xURI){

	xObj.className='loading';

	xURI_friendly = xURI.replace(/-/g,'_');

	eval('var runGeo = (typeof geo_'+xURI_friendly+' == "undefined" && typeof marker_'+xURI_friendly+' == "undefined") ? true : false;');

	if(runGeo === false){
		eval('map.removeOverlay(marker_'+xURI_friendly+'); marker_'+xURI_friendly+' = undefined;');
		eval('map.removeOverlay(geo_'+xURI_friendly+'); geo_'+xURI_friendly+' = undefined;');
		xObj.className='off';
	}else{

		//Get Map Data
	    eval('geo_'+xURI_friendly+' = new google.maps.GeoXml(xMap);');
	    eval('var geoXml = geo_'+xURI_friendly+';');
	    map.addOverlay(geoXml);
	    
	    //Setup Viewport
	    google.maps.Event.addListener(geoXml,"load",function(){
	    	if(geoXml.loadedCorrectly()){

				map.closeInfoWindow();

	    		centerXml=geoXml.getDefaultCenter();
				eval('marker_'+xURI_friendly+' = new google.maps.Marker(centerXml,{title:"Click for more information"});');
				eval('var markerXml = marker_'+xURI_friendly+';');

				google.maps.Event.addListener(map,"addoverlay",function(){
					google.maps.Event.clearListeners(map,"addoverlay");
					
					if(!is_iphone){
						if(map.getCenter()==centerXml){
			    			markerXml.openInfoWindowHtml($('#data_'+xURI).html(),{maxWidth:350});
							google.maps.Event.clearInstanceListeners(map);
			    			xObj.className="on";
						}else{
							map.panTo(centerXml);
						    if(map.getCenter() == centerXml){
						    	//This is a JUMP (too far out of range to pan)
				    			markerXml.openInfoWindowHtml($('#data_'+xURI).html(),{maxWidth:350});
								google.maps.Event.clearInstanceListeners(map);
				    			xObj.className="on";
						    }else{
						    	// This is a PAN
							    google.maps.Event.addListener(map,"moveend",function(){
					    			markerXml.openInfoWindowHtml($('#data_'+xURI).html(),{maxWidth:350});
									google.maps.Event.clearInstanceListeners(map);
					    			xObj.className="on";
							    });
							}
						}
					}else{map.setCenter(centerXml);}
					google.maps.Event.addListener(markerXml,"click",function(){markerXml.openInfoWindowHtml($('#data_'+xURI).html(),{maxWidth:350});});
				});

				map.addOverlay(markerXml);
	    	}
	    });			
	}
}
//--------------------------------------------------------------------------------------------
// Control large interactive map for Key Areas splash page
//--------------------------------------------------------------------------------------------
function showMap(xMode){$('#controls_wrap,#map_button,#google_map_splash').slideToggle('slow',function(){if(xMode == true && xSplashMapOn == false){gMapInitialize();xSplashMapOn = true;}});}
//This function is used to control the "Stick" on the Key Areas list
//--------------------------------------------------------------------------------------------
function areaSwitch(xArea){if($("#"+xArea+" > .subs").is(":hidden")){$('#keyAreaNav > li > .subs:visible').hide('medium',function(){$('#keyAreaNav > li > a').removeClass('on');$('#'+xArea+' > a').addClass('on');$('#'+xArea+' > .subs').show('slow');});}}
//This function is used to expand the description of Neighborhoods
//--------------------------------------------------------------------------------------------
function moreHood(x){if(x=='open'){$('#extended_open').hide('slow',function(){$('#extended_desc').slideDown('slow');});}else if(x=='close'){$('#extended_desc').slideUp('slow',function(){$('#extended_open').show('slow');});}}
//This function is used to switch the "Stick" on the Feature tabs
//--------------------------------------------------------------------------------------------
function featureSwitch(xBucket,xTab){$('#featureContainer').html($('#'+xBucket).html());$('#featureNav > li').attr('class','off');$('#featureNav > #'+xTab).attr('class','selected');}
