/*
 * The Bespoke Yahoo Map Handler
 * by mark@thebespokepixel.com
 * Version 1.0 (October 2007)
 */
MAP_DEBUG_MODE = false;
if(MAP_DEBUG_MODE && !utilitiesLoaded) alert("Please include utilities.js");

if(typeof theBespoke == "undefined") var theBespoke = new Object();

theBespoke.YahooMap = function(domID, width, height, minZoom, maxZoom) {
	if (!document.createElement || !document.getElementById) { return; }
	
	this.markerArray = new Array();
	this.domID = domID;
	this.mapSize = new YSize(width,height);
	
	// Create a map object
	this.map = new YMap(document.getElementById(this.domID),YAHOO_MAP_SAT, this.mapSize);
	this.map.setZoomRange(minZoom, maxZoom);
	this.map.removeZoomScale();
	this.map.addZoomLong();
	if(MAP_DEBUG_MODE) {
		this.map.disablePanOnDoubleClick();
		YEvent.Capture(this.map, EventsList.MouseDoubleClick, this.reportPosAndAddMarker, this);
	}
	
	this.onMarkerRollover = function(id) { return false; };
	this.onMarkerClick = function(id) { return false; };
}

theBespoke.YahooMap.prototype = {
	start: function(location, initalZoom) {
		this.map.drawZoomAndCenter(location, initalZoom);
		this.drawMarkers();
	},
	zoom: function(location, zoom) {
		this.map.setZoomLevel(zoom);
		this.map.panToLatLon(location);
	},
	addMarker: function(id, name, GeoPoint, markerGraphic, markerSize, markerOffset, panelOffset ) {
		this.debugStr("Adding marker: "+id);
		this.debugStr("Marker offset: "+markerOffset.x+","+markerOffset.y);
		this.debugStr("Panel offset "+panelOffset.x+","+panelOffset.y);
		this.markerArray[id] = new YMarker(GeoPoint, new YImage(markerGraphic, markerSize, panelOffset, markerOffset));
	},
	drawMarkers: function(event) {
		for(var marker in this.markerArray) {
			this.map.addOverlay(this.markerArray[marker]);
		}
	},
	addSmartWindow: function(id, text, colour) {
		this.markerArray[id].setSmartWindowColor(colour);
		if(!(navigator.userAgent.indexOf("iPod")>-1 || navigator.userAgent.indexOf("iPhone")>-1)) {
			YEvent.Capture(this.markerArray[id], EventsList.MouseOver, function() { this.markerArray[id].openSmartWindow(text); this.onMarkerRollover(id); }, this);
		}
		YEvent.Capture(this.markerArray[id], EventsList.MouseClick, function() { this.onMarkerClick(id); }, this);
	},
	closeSmartWindow: function(id) {
		this.markerArray[id].closeSmartWindow();
	},
	hideMap: function() {
		$(this.domID).style.display='none';
	},
	showMap: function() {
		$(this.domID).style.display='';
		this.map.resizeTo(this.mapSize);
	},
	reportPosAndAddMarker: function(event, location) {
		this.debugStr("Adding marker at....");
		this.debugStr("\'lat\' => " + location.Lat + ",");
		this.debugStr("\'long\' => " + location.Lon + ",");
		this.map.addOverlay(new YMarker(location, new YImage('media/markers/no_show.png', new YSize(20,20), new YCoordPoint(0,0), new YCoordPoint(-6,8))));
	},
	debugStr: function(text) {
		if(MAP_DEBUG_MODE) YLog.print(text);
	}
		
}
/*
 * Create namespace shortcuts
 */
var YahooMap = theBespoke.YahooMap;