/**
 * @author christopher
 */


 var Mapper = {
 	ZoomFactor: 12,
	
 	target_div: 0,
	mc: 0,
	geocoder: 0,
	map: 0,
	
	tmpmarker: 0,
	icon:0,
	
	init: function() {
		this.target_div = getElement(MapOpts.div);
		this.mc = Element("div");
		this.mc.setAttribute("id","map_canvas");
		this.mc.style.width = "615px";
		this.mc.style.height = "500px";
		this.mc.style.border = "1px solid #fff";
		this.mc.style.backgroundColor = "#eeeeee";
		this.target_div.appendChild(this.mc);
		this.GMAP_initialize();
	},
	

	GMAP_initialize: function() {
		if (GBrowserIsCompatible()) {
			
			this.icon = new GIcon();
			this.icon.iconSize = new GSize(22,22);
			this.icon.iconAnchor = new GPoint(10,10);
			this.icon.image = "http://www.b105.com/media/video.jpg";
			this.icon.shadow = G_DEFAULT_ICON.shadow;
			this.icon.shadowSize = new GSize(40,20);
			this.icon.infoWindowAnchor = new GPoint(10,10);
			
			
			this.map = new GMap2(document.getElementById("map_canvas"));
			
			//alert(MapOpts.locations.length);
			
			var trafficOptions = {incidents:false};
   			trafficInfo = new GTrafficOverlay(trafficOptions);
    		this.map.addOverlay(trafficInfo);
			
			for(var i=0;i<MapOpts.locations.length;i++) {
				
				
				markerOptions = { icon:this.icon };
				var loc = MapOpts.locations[i];
				
				loc.point = new GLatLng(loc.lat,loc.lon);
				loc.marker = new GMarker(loc.point,markerOptions);
				this.map.addOverlay(loc.marker);
				
				
				loc.marker._cvmapLoc = loc;
				
				GEvent.addListener(loc.marker, "click", function() {
						//LocationTable.address_click(this._cvmapLoc);
						Mapper.speak(this._cvmapLoc);
					}
				);
				
				
			}
			
			
			for(var i=0;i<MapOpts.roadactivity.length;i++) {
			
			var ra = MapOpts.roadactivity[i];
			
				if(ra.category == "construction" || ra.category == "Roadwork - Planned"){
				this.construction_icon = new GIcon();
				this.construction_icon.iconSize = new GSize(22,22);
				this.construction_icon.iconAnchor = new GPoint(10,10);
				this.construction_icon.image = "http://www.b105.com/media/construction.gif";
				this.construction_icon.shadow = G_DEFAULT_ICON.shadow;
				this.construction_icon.shadowSize = new GSize(40,20);
				this.construction_icon.infoWindowAnchor = new GPoint(10,10);
				markerOptions = { icon:this.construction_icon };
				}
				
				if(ra.category == "closure" || ra.category == "Roadwork - Unplanned"){
				this.construction_icon = new GIcon();
				this.construction_icon.iconSize = new GSize(22,22);
				this.construction_icon.iconAnchor = new GPoint(10,10);
				this.construction_icon.image = "http://www.b105.com/media/alert.gif";
				this.construction_icon.shadow = G_DEFAULT_ICON.shadow;
				this.construction_icon.shadowSize = new GSize(40,20);
				this.construction_icon.infoWindowAnchor = new GPoint(10,10);
				markerOptions = { icon:this.construction_icon };
				}
				
				if(ra.category == "accident" || ra.category == "Accident"){
				this.construction_icon = new GIcon();
				this.construction_icon.iconSize = new GSize(22,22);
				this.construction_icon.iconAnchor = new GPoint(10,10);
				this.construction_icon.image = "http://www.b105.com/media/accident_small.png";
				this.construction_icon.shadow = G_DEFAULT_ICON.shadow;
				this.construction_icon.shadowSize = new GSize(40,20);
				this.construction_icon.infoWindowAnchor = new GPoint(10,10);
				markerOptions = { icon:this.construction_icon };
				}
				
				if(ra.category == "Flooding" || ra.category == "Snow/Ice" || ra.category == "Debris"){
				this.construction_icon = new GIcon();
				this.construction_icon.iconSize = new GSize(22,22);
				this.construction_icon.iconAnchor = new GPoint(10,10);
				this.construction_icon.image = "http://www.b105.com/media/roadconditions.png";
				this.construction_icon.shadow = G_DEFAULT_ICON.shadow;
				this.construction_icon.shadowSize = new GSize(40,20);
				this.construction_icon.infoWindowAnchor = new GPoint(10,10);
				markerOptions = { icon:this.construction_icon };
				}
				
				if(ra.category == "Disabled Vehicle" || ra.category == "Other"){
				this.construction_icon = new GIcon();
				this.construction_icon.iconSize = new GSize(22,22);
				this.construction_icon.iconAnchor = new GPoint(10,10);
				this.construction_icon.image = "http://www.b105.com/media/incident_icon.gif";
				this.construction_icon.shadow = G_DEFAULT_ICON.shadow;
				this.construction_icon.shadowSize = new GSize(40,20);
				this.construction_icon.infoWindowAnchor = new GPoint(10,10);
				markerOptions = { icon:this.construction_icon };
				}
				
				
				
				ra.point = new GLatLng(ra.lat,ra.lon);
				ra.marker = new GMarker(ra.point,markerOptions);
				this.map.addOverlay(ra.marker);
				
				
				ra.marker._cvmapLoc = ra;
				
				GEvent.addListener(ra.marker, "click", function() {
						//LocationTable.address_click(this._cvmapLoc);
						Mapper.speakRA(this._cvmapLoc);
					}
				);
				
				
			}
						
			this.GMAP_initial_center();
		}
	},
	
	GMAP_initial_center: function() {
		var loc = 0;
		var i = 0;
		for (i; i < MapOpts.locations.length; i++) {
			loc = MapOpts.locations[i];
			
		}
		if (i >= MapOpts.locations.length) {
			loc = MapOpts.locations[0];
		}
		
		var pos = new GLatLng(loc.lat,loc.lon)
		this.map.setCenter(pos, MapOpts.zoom);
		this.map.addControl(new GLargeMapControl());
	    var mapControl = new GMapTypeControl();
	   	this.map.addControl(mapControl);
	   	
		//this.speak(loc);
	},
	
	clearInt: 0,
	GM_dirlink: "http://maps.google.com/maps?f=d&source=s_d&saddr=&daddr={daddr}&hl=en&geocode=&mra=ls&&ie=UTF8&z=9",
	curLoc: 0,
	speak: function(loc) {
		this.curLoc = loc;
		
		//var img = Element("img");
		//img.src = "http://media.bonnevillecincinnati.com/coupon/piggy_small.png";
		
		//var daddr = loc.address_a + " " + loc.city + ", " + loc.state + " " + loc.zip;
		//var diruri = this.GM_dirlink.replace(/\{daddr\}/,daddr);
		
		
		//var dir_a = FilledElement("a","Get Directions");
		//dir_a.href = "javascript:openHelper('" + diruri + "',800,600)";
		//dir_a.style.fontSize = "12px";
		//var dir_div = FilledElement("div",dir_a);

		

		var client_name = "";
		if(loc.location) {
			var client_href = FilledElement("a",loc.location);
			client_name = FilledElement("b",client_href);
		}
		
		var info_box = FilledElement("div",
			[
				client_name,
				(function() {
				
					clearInterval(Mapper.clearInt);
					
				
				    var img = FilledElement("img");
				    img.src = loc.smallimage;
				    img.style["clear"] = "both";
				    img.style.width = "175px";
				    img.style.height = "138px";
				    img.id = img.src;

				    intval = setInterval(
						(function(g,s) { 
						    return function() { 
							var z = document.getElementById(g);
					   
							z.src = s + "?" + Math.random(); 
						    }
						})(img.id,loc.smallimage),
				    5000);
				    //alert(loc.smallimage);
					var x = FilledElement("div",
						[
						 "<b><u>The camera is facing</u></b><br /> " + loc.direction + "<p></p>",
						 img
						]
					);
					x.style.fontSize = "12px";
					x.style.padding = "10px 10px 3px 10px";
					return x;
					
					
					
				})()
			]
		);
		
		
		var d = FilledElement("div",
			[
				info_box
			]
		);
		
		loc.marker.openInfoWindowHtml(d.innerHTML);
		
		//Mapper.map.setZoom(Mapper.ZoomFactor);
	}
	
	

	
	
 };


 
 function MapperInit() {
 	Mapper.init();
 }
 

 
 
 
 
 
 

