/**
 * @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 = this.target_div.offsetWidth + "px";
		this.mc.style.height = this.target_div.offsetHeight + "px";
		this.mc.style.border = "1px solid #dcdcdc";
		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://media.bonnevillecincinnati.com/coupon/piggy_small.png";
			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"));
			for(var i=0;i<MapOpts.locations.length;i++) {
				
				
				
				var loc = MapOpts.locations[i];
				loc.marker = new GMarker(
					new GLatLng(loc.lat,loc.lon),
					{
						draggable:false
						//icon: this.icon
					}
				);
				this.map.addOverlay(loc.marker);
				
				
				loc.marker._cvmapLoc = loc;
				
				GEvent.addListener(loc.marker, "click", function() {
						LocationTable.address_click(this._cvmapLoc);
						Mapper.speak(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(loc.city == "Cincinnati") {
				break;
			}
		}
		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);
	},
	
	
	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.client_name) {
			var client_href = FilledElement("a",loc.client_name);
			client_href.href = "/cincycoupons/view/detail/" + loc.client_id;
			client_name = FilledElement("b",client_href);
		}
		
		var info_box = FilledElement("div",
			[
				client_name,
				(function() {
					var x = FilledElement("div",
						[
							loc.address_a + "<br/>" + loc.city + ", " + loc.state + "<br/>" +
							loc.zip + "<br/>" + loc.phone + "<br/>",
							dir_div
						]
					);
					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);
	},
	
	
	dirs: 0,
	directions: function() {
		var lhs = getElement("mapdirbox_" + this.curLoc.id).value;
		var rhs = "Waypoint@" + this.curLoc.lat + "," + this.curLoc.lon;
		this.dirs = new GDirections(this.map);
		this.dirs.load("from: " + lhs + " to: " + rhs);
		
	}
	
	
 };


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

 
 
 
 
 
 
