var map;

var geocoder;


var address;	


function mapInit(){
	map = new Array();
	
	geocoder = new Array();
	
	address = new Array();

}











function initMap(id){
	
	map[id] = new GMap2(document.getElementById("map_"+id)); 
	map[id].enableDoubleClickZoom();
	map[id].enableContinuousZoom();    
	//map[id].setCenter(new GLatLng(42.3340, -83.47500), 15);
	map[id].addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 5)));

	geocoder[id] = {};
	geocoder[id] = new GClientGeocoder();
	//map[id].setMapType(G_HYBRID_MAP);	

	ajax("/tools/dealer_map_json.php?id="+id,null,function(foo){dealerInfo(foo)});
}

function dealerInfo(foo){
	dealer = eval("("+foo+")");
	
	var id = dealer.carid;
	
	if(dealer.lon && dealer.lat){
		address[id] = dealer.lat+","+dealer.lon;
	}
	else{
		address[id] = dealer.address+" "+dealer.zip;
	}
	
	var text = dealer.name+"<br />"+dealer.address+"<br />"+dealer.city+", "+dealer.state+" "+dealer.zip;

	
	geocoder[id].getLatLng(address[id],
		function(point) {
			map[id].setCenter(point, 8);
			if(!map[id].marker){
				map[id].marker = new GMarker(point);
				map[id].addOverlay(map[id].marker);
				GEvent.addListener(map[id].marker, "click", function() {
					map[id].marker.openInfoWindowHtml(text);
				});
			}
		}
	);
}




var MAP = function(){
	
	return{
		dialog:null,
		map:null,
		dealer: null,
		weather:{},
		directionsQuery: '',
		Weather: function(){},
		traffic:null,
		init: function(car){	
			if(!MAP.dialog){
				MAP.dialog = new Ext.LayoutDialog(Ext.id(),
					{
						autoCreate:true,									  
						title: 'Map',
						height:460,
						width:730,
						center:{
							autoTabs:false,
							titlebar:false
							},
						east:{
							initialSize:305,
							split:false,
							titlebar:false,
							autoScroll:true
							},
						modal:true,
						draggable:false,
						resizable:false
					}
				);
			}
			var layout = MAP.dialog.getLayout();
			layout.beginUpdate();
				//layout.add('north',new Ext.ContentPanel('lgNorth',{autoCreate:true}));
				layout.add('center',new Ext.ContentPanel('lgCenter',{autoCreate:true}));
				layout.add('east',new Ext.ContentPanel('lgEast',{autoCreate:true}));
			layout.endUpdate();
			window.scrollTo(0,0);
			MAP.dialog.show(document.body);
			document.getElementById("lgCenter").innerHTML = "<div id='lgMap' style='width:400;height:400;position:fixed;top:0px;left:0px'></div>";
			//document.getElementById("lgNorth").innerHTML = "&nbsp;&nbsp;&nbsp;<a href='javascript:MAP.showHome();'>Map Home</a>&nbsp;&nbsp;&nbsp;<a href='javascript:MAP.directions();'>Driving Directions</a>&nbsp;&nbsp;&nbsp;<a href='javascript:MAP.showWeather();' >Weather Radar</a>&nbsp;&nbsp;&nbsp;<a href='javascript:MAP.showTraffic();' >How's Traffic?</a>&nbsp;&nbsp;&nbsp;<a href='javascript:MAP.showRacing();' >Local Racing</a>";
			MAP.weather.layer = new GTileLayer(new GCopyrightCollection(""),1,17);
			MAP.weather.layer.myFormat = "image/png";
			MAP.weather.layer.myLayers = "NEXRAD-n0r";				
			MAP.weather.layer.myBaseURL= "http://mesonet.agron.iastate.edu/cgi-bin/mapserv/mapserv?map=/mesonet/share/msapps/wms/nexrad/n0r.map&amp;&stub=stub";
			MAP.weather.layer.getTileUrl=CustomGetTileUrl;
			MAP.weather.layer.getOpacity = function() {return .6;};
			MAP.weather.glayer = new GTileLayerOverlay(MAP.weather.layer);
			MAP.traffic = new GTrafficOverlay();
			MAP.racetracks = new GGeoXml("http://chicagosportscars.com/racetracks/tracks.kml?id="+Math.random());

			MAP.initMap();
			ajax("/tools/dealer_map_json.php?id="+car,null,function(foo){MAP.dealerInfo(foo);});
			
			

			

			
		},
		initMap: function(){
			MAP.map = new GMap2(document.getElementById("lgMap")); 
			MAP.map.enableDoubleClickZoom();
			MAP.map.enableContinuousZoom();    
			MAP.map.setCenter(new GLatLng(42.3340, -83.47500), 15);
			MAP.map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10)));
			MAP.map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
			var map_layers = [G_HYBRID_MAP.getTileLayers()[0],G_HYBRID_MAP.getTileLayers()[1],MAP.weather.layer];
		 	var custommap1 = new GMapType(map_layers, G_HYBRID_MAP.getProjection(), "Weather",G_HYBRID_MAP);
		  	MAP.map.addMapType(custommap1);
			document.getElementById("lgMap").style.height = "400px";
			document.getElementById("lgMap").style.width = "400px";
		},
		directions: function(){
			if(MAP.directionsQuery == ""){
			document.getElementById("lgEast").innerHTML = "<div>Get Directions to:<br /> "+MAP.dealer.name+"<br /> from:<br /> <input type='text' id='from' /><input type='button' value='Go' onclick='MAP.getDirections();' /></div><div id='lgDirections' style='width:270px;'></div>";
			}
			else{
				
				document.getElementById("lgEast").innerHTML = "<div>Get Directions to:<br /> "+MAP.dealer.name+"<br /> from:<br /> <input type='text' id='from' /><input type='button' value='Go' onclick='MAP.getDirections();' /></div><div id='lgDirections' style='width:270px;'></div>";
				MAP.reloadDirections();
			}
			
			
		},
		reloadDirections: function(){
			MAP.map.clearOverlays();
			MAP.directionsLayer = new GDirections(MAP.map, document.getElementById("lgDirections"));
			MAP.directionsLayer.load(MAP.directionsQuery);
		},
		getDirections: function(){
			MAP.map.clearOverlays();
			document.getElementById("lgDirections").innerHTML = "";
			MAP.directionsLayer = new GDirections(MAP.map, document.getElementById("lgDirections"));
			MAP.directionsQuery = document.getElementById("from").value+" to "+MAP.address;
			MAP.directionsLayer.load(MAP.directionsQuery);
		},
		dealerInfo: function(foo){
			MAP.dealer = eval("("+foo+")");
			MAP.text = MAP.dealer.name+"<br />"+MAP.dealer.address+"<br />"+MAP.dealer.city+", "+MAP.dealer.state+" "+MAP.dealer.zip;
			MAP.dialog.setTitle('Map for '+MAP.dealer.name);
						if(MAP.dealer.lon && MAP.dealer.lat){
				MAP.address = MAP.dealer.lat+","+MAP.dealer.lon;
			}
			else{
				MAP.address = MAP.dealer.address+" "+MAP.dealer.zip;
			}
			MAP.geocoder = new GClientGeocoder();
			MAP.map.clearOverlays();
			MAP.markDealer();
			MAP.directions();
		},
		
		markDealer: function(level){
			if(!level){
				level = 9;	
			}
			MAP.geocoder.getLatLng(MAP.address,
				function(point) {
					MAP.map.setCenter(point, level);
					MAP.map.marker = new GMarker(point);
					MAP.map.addOverlay(MAP.map.marker);
					GEvent.addListener(MAP.map.marker, "click", function() {
							MAP.map.marker.openInfoWindowHtml(MAP.text);
					});
					
				}
			);	
		}
		
	};
	
}();


var MAP2 = function(){
	
	return{
		dialog:null,
		map:null,
		dealer: null,
		weather:{},
		directionsQuery: '',
		traffic:null,
		layout:null,
		init: function(car){	
			if(!MAP2.dialog){
				MAP2.dialog = new Ext.BorderLayout('map',
					{
						center:{
							autoTabs:false,
							titlebar:false
							},
						east:{
							initialSize:305,
							split:false,
							titlebar:false,
							autoScroll:true
							}
					}
				);
			}
			
			MAP2.dialog.beginUpdate();
				//MAP2.dialog.add('north',new Ext.ContentPanel('lgNorth'));
				MAP2.dialog.add('center',new Ext.ContentPanel('lgCenter'));
				MAP2.dialog.add('east',new Ext.ContentPanel('lgEast'));
			MAP2.dialog.endUpdate();
			
			MAP2.tab = new Ext.NestedLayoutPanel(MAP2.dialog);
			
			Layout.layout.add("center", MAP2.tab);
			MAP2.weather.layer = new GTileLayer(new GCopyrightCollection(""),1,17);
			MAP2.weather.layer.myFormat = "image/png";
			MAP2.weather.layer.myLayers = "NEXRAD-n0r";				
			MAP2.weather.layer.myBaseURL= "http://mesonet.agron.iastate.edu/cgi-bin/mapserv/mapserv?map=/mesonet/share/msapps/wms/nexrad/n0r.map&amp;&stub=stub";
			MAP2.weather.layer.getTileUrl=CustomGetTileUrl;
			MAP2.weather.layer.getOpacity = function() {return .60;};
			MAP2.weather.glayer = new GTileLayerOverlay(MAP.weather.layer);
			MAP2.traffic = new GTrafficOverlay();
			MAP2.racetracks = new GGeoXml("http://chicagosportscars.com/racetracks/tracks.kml?id="+Math.random());

			
			MAP2.initMap();
			ajax("/tools/dealer_map_json.php?id="+car,null,function(foo){MAP2.dealerInfo(foo);});
			
		
			
			
					
			
		},
		initMap: function(){
			MAP2.map = new GMap2(document.getElementById("lgMap")); 
			MAP2.map.enableDoubleClickZoom();
			MAP2.map.enableContinuousZoom();    
			MAP2.map.setCenter(new GLatLng(42.3340, -83.47500), 15);
			MAP2.map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10)));	
			MAP2.map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
			var map_layers = [G_HYBRID_MAP.getTileLayers()[0],G_HYBRID_MAP.getTileLayers()[1],MAP2.weather.layer];
		 	var custommap1 = new GMapType(map_layers, G_HYBRID_MAP.getProjection(), "Weather",G_HYBRID_MAP);
		  	MAP2.map.addMapType(custommap1);
		},
		directions: function(){
			if(MAP2.directionsQuery == ""){
				document.getElementById("lgEast").innerHTML = "<div style='margin:5px;'>Get Directions to:<br /> "+MAP2.dealer.name+"<br /> from:<br /> <input type='text' id='from' /><input type='button' value='Go' onclick='MAP2.getDirections();' /></div><div id='lgDirections' style='width:270px;'></div>";
			}
			else{
			
				document.getElementById("lgEast").innerHTML = "<div>Get Directions to:<br /> "+MAP2.dealer.name+"<br /> from:<br /> <input type='text' id='from' /><input type='button' value='Go' onclick='MAP2.getDirections();' /></div><div id='lgDirections' style='width:270px;'></div>";
				MAP2.reloadDirections();
			}
			
			
		},
		reloadDirections: function(){
			MAP2.map.clearOverlays();
			MAP2.directionsLayer = new GDirections(MAP2.map, document.getElementById("lgDirections"));
			MAP2.directionsLayer.load(MAP2.directionsQuery);
		},
		getDirections: function(){
			MAP2.map.clearOverlays();
			document.getElementById("lgDirections").innerHTML = "";
			MAP2.directionsLayer = new GDirections(MAP2.map, document.getElementById("lgDirections"));
			MAP2.directionsQuery = document.getElementById("from").value+" to "+MAP2.address;
			MAP2.directionsLayer.load(MAP2.directionsQuery);
		},
		
		showHome: function(){
			MAP2.map.clearOverlays();
		document.getElementById("lgEast").innerHTML = "<div style='width:300px;height:400px;position:relative;' ><img src='/img/mapbackground.jpg' style='position:absolute:top:0px;left:0px;' /><div style='position:absolute;top:75px;left:10px;width:280px;'>This is an interactive map.<br /> Move or zoom in and out using your mouse or the tools inside the map area.</div><div style='position:absolute;bottom:20px;left:10px;width:280px;text-align:center;font-weight:bold'>"+MAP2.dealer.statement+"</div></div>";
			
			MAP2.markDealer();
		},
		dealerInfo: function(foo){
			MAP2.dealer = eval("("+foo+")");
			MAP2.text = MAP2.dealer.name+"<br />"+MAP2.dealer.address+"<br />"+MAP2.dealer.city+", "+MAP2.dealer.state+" "+MAP2.dealer.zip;
			MAP2.tab.setTitle('Map for '+MAP2.dealer.name);
			if(MAP.dealer.lon && MAP.dealer.lat){
				MAP.address = MAP.dealer.lat+","+MAP.dealer.llon;
			}
			else{
				MAP.address = MAP.dealer.address+" "+MAP.dealer.zip;
			}
			MAP2.geocoder = new GClientGeocoder();
			MAP2.markDealer();
			MAP2.directions();
		},
		
		markDealer: function(level){
			if(!level){
				level = 9;	
			}
			MAP2.geocoder.getLatLng(MAP2.address,
				function(point) {
					MAP2.map.setCenter(point, level);
					MAP2.map.marker = new GMarker(point);
					MAP2.map.addOverlay(MAP2.map.marker);
					GEvent.addListener(MAP2.map.marker, "click", function() {
							MAP2.map.marker.openInfoWindowHtml(MAP2.text);
					});
					
				}
			);	
		}
		
	};
	
}();
