YahooMapper.prototype.buildMap = buildMap;
YahooMapper.prototype.addOverlay = addOverlay;
YahooMapper.prototype.zoomOverlay = zoomOverlay;
YahooMapper.prototype.addInteractiveMarker = addInteractiveMarker;
YahooMapper.prototype.addStaticMarker = addStaticMarker;
YahooMapper.prototype.onZoomFunction = onZoomFunction;
YahooMapper.prototype.disableDrag = disableDrag;
YahooMapper.prototype.enableDrag = enableDrag;
//
var scope;
function YahooMapper () {
	this.map;
	this.controlType;
	this.zoomLevel;
	this.startPoint;
	this.type;
	this.hasTypeControl;
	//
	this.hasOverlay = false;
	this.overlayObject;
	this.overlayPosition;
	this.overlayPrefix;
	this.overlayExtension;
	this.overlayZoomLimits = new Array();
	this.mapInfoLayer;
	this.zoomFunction;
	this.contDiv;
	//
	this.dragDisabledBounds;
	//
	this.markers = new Array();
	this.interactiveMarkers = new Array();
	this.staticMarkers = new Array();
	this.infoWindow;
	//
	this.rootDir;
	this.locID;
	//
	this.req = window.XMLHttpRequest && !window.ActiveXObject ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	scope = this;
}
function buildMap(targetDiv, config) {
	this.map = new YMap(targetDiv);
	this.contDiv = targetDiv;
	this.controlType = config.controlType || "short";
	this.zoomLevel = config.zoomLevel || 9;
	this.startPoint = config.startPoint ||  new YGeoPoint(43.85879188670805, 18.434371948242188); //  <------ peak district
	this.type = config.type || YAHOO_MAP_REG;
	this.hasTypeControl = config.hasTypeControl || true;
	this.rootDir = config.rootDir || "http://www.surfimpress.com/peak3";
	this.locID = config.locID || 18;
	//
	if(this.controlType == "short") {
		this.map.addZoomShort();
	} else if(this.controlType == "long") {
		this.map.addZoomLong();
	}
	if(this.hasTypeControl) {
		this.map.addTypeControl();
	} else {
		this.map.removeTypeControl();
	}
	this.map.setMapType(this.type);
	this.map.drawZoomAndCenter(this.startPoint, this.zoomLevel);
	//
	this.mapInfoLayer = document.createElement("DIV");
	this.mapInfoLayer.setAttribute("id", "mapinfolayer");
	this.mapInfoLayer.style.visibility = "hidden";
	this.mapInfoLayer.style.position = "absolute";
	//this.mapInfoLayer.style.left = "0px";
	//this.mapInfoLayer.style.top = "0px";
	this.mapInfoLayer.style.zIndex = 314;
	var tempDiv = document.getElementById(targetDiv);
	tempDiv.parentNode.insertBefore(this.mapInfoLayer, tempDiv);
	YEvent.Capture(this.map, EventsList.changeZoom, this.onZoomFunction);
};
//
function disableDrag(bounds) {
	if(bounds) {
		scope.dragDisabledBounds = bounds;
		if(scope.map.getZoomLevel() >= scope.dragDisabledBounds[0] && scope.map.getZoomLevel() <= scope.dragDisabledBounds[1]) {
			scope.map.disableDragMap();
		}
	} else {
		scope.map.disableDragMap();
	}
}
function enableDrag() {
	scope.map.enableDragMap();
	scope.dragDisabledBounds = undefined;
}
function onZoomFunction() {
	if(scope.hasOverlay) scope.zoomOverlay();
	if(scope.dragDisabledBounds != undefined) {
		if(scope.map.getZoomLevel() >= scope.dragDisabledBounds[0] && scope.map.getZoomLevel() <= scope.dragDisabledBounds[1]) {
			scope.map.disableDragMap();
		} else {
			scope.map.enableDragMap();
		}
	}
}
function addOverlay(oLat, oLong, overlayPrefix, overlayExtension, overlayZoomLimits) {
	if(oLat == undefined || oLong == undefined || !overlayPrefix || !overlayExtension) {
		alert("The first 3 parameters in addOverlay() are required");
		return false;
	}
	this.map.addZoomShort();
	this.controlType = "short";
	//
	this.hasOverlay = true;
	this.overlayPosition = new YGeoPoint(oLat, oLong); //overlayPosition;
	this.overlayPrefix = overlayPrefix;
	this.overlayExtension = overlayExtension;
	this.overlayZoomLimits = overlayZoomLimits || [1, 16];
	this.zoomOverlay();
	//YEvent.Capture(this.map, EventsList.changeZoom, this.zoomOverlay);
}
function zoomOverlay() {
	if(scope.overlayObject) scope.map.removeOverlay(scope.overlayObject);
	if(scope.map.getZoomLevel() >= scope.overlayZoomLimits[0] && scope.map.getZoomLevel() <= scope.overlayZoomLimits[1]) {
		var contDiv = document.createElement("DIV");
		var imageOverlay =  document.createElement("IMG"); //new Image();
		imageOverlay.src = scope.overlayPrefix+scope.map.getZoomLevel()+"."+scope.overlayExtension;
		contDiv.appendChild(imageOverlay);
		scope.overlayObject = new YCustomOverlay(scope.overlayPosition, contDiv);
		scope.map.addOverlay(scope.overlayObject);
		fixThePNGs();
	}
}
//
function addStaticMarker(pLat, pLong, locationID, removeLast, icon) {
	if(pLat == undefined || pLong == undefined) {
		return false;
	}
	if(removeLast && scope.staticMarkers.length > 0) {
		scope.map.removeOverlay(scope.staticMarkers.pop());
	}
	var iconToUse = icon || staticIcon;
	var iconContainer = document.createElement("DIV");
	var iconImage = document.createElement("IMG");//new Image();
	iconImage.src = iconToUse;
	iconContainer.appendChild(iconImage);
	var imageMarker = new YCustomOverlay(new YGeoPoint(pLat, pLong), iconContainer);
	scope.markers.push(imageMarker);
	scope.staticMarkers.push(imageMarker);
	scope.map.addOverlay(imageMarker);
	//var a = prompt("", document.getElementById(scope.contDiv).innerHTML);
	fixThePNGs();
}
function addInteractiveMarker(pLat, pLon, locationID, method, hasRollover, title, picture, icon) {
	if(pLat == null || pLat == undefined || pLon == undefined || pLon == null) return false;
	var iconToUse = icon || interactiveIcon;
	var interactiveMarkerContainer = document.createElement("DIV");
	var interactiveMarkerImage = document.createElement("IMG");
	interactiveMarkerImage.setAttribute("id", "imarker"+locationID);
	interactiveMarkerImage.setAttribute("src", iconToUse);
	interactiveMarkerContainer.appendChild(interactiveMarkerImage);
	var interactiveMarker = new YCustomOverlay(new YGeoPoint(pLat, pLon), interactiveMarkerContainer);
	this.markers.push(interactiveMarker);
	this.interactiveMarkers.push(interactiveMarker);
	if ( hasRollover ) {
		interactiveMarkerContainer.onmouseover = function(e) {
			var y = getScrollAmount().y;
			
			scope.mapInfoLayer.innerHTML = (picture.indexOf("media/&width") != -1 ? "" : "<img src=\""+picture+"\" /><br />")+((title == "" || title == undefined) ? "" : title);
			
			scope.mapInfoLayer.style.top = (this.offsetTop +35)+"px";
			scope.mapInfoLayer.style.left = (this.offsetLeft - scope.mapInfoLayer.offsetWidth/2)+"px";
			scope.mapInfoLayer.style.visibility = "visible";
		};
		interactiveMarkerContainer.onmouseout = function(e) {
			scope.mapInfoLayer.style.visibility = "hidden";
		};
	}
	if(method == "goto") {
		interactiveMarkerContainer.onclick = function(e) {
			window.open(scope.rootDir+"/?location_id="+scope.locID+"&item="+locationID);
		}
	} else if(method == "window") {
		interactiveMarkerContainer.onclick = function(e) {
			if(Windower) {
				var tempWin = new Windower();
				tempWin.buildWindow(200, 100, 500, 400, ++wDepth, {});
				tempWin.setContent(scope.rootDir+"/?location_id="+scope.locID+"&item="+locationID);
				tempWin.setTitle((title != "" && title != undefined) ? title:"" );
				tempWin.swapDepthOnDrag = true;
			}
		};
	} else if(method.indexOf("window:") != -1) {
		var mTemp = method.split(":");
		interactiveMarkerContainer.onclick = function(e) {
			if (Windower) {
				//scope.req.open("GET", "public_scripts/getHTML.php?url="+escape(scope.rootDir+"/?location_id="+scope.locID+"&item="+locationID)+"&id="+(mTemp[1] == "" ? "windower_content" : mTemp[1])+"&itemoffset=1", false);
				//prompt("","public_scripts/getHTML.php?url="+escape(scope.rootDir+"/?location_id="+scope.locID+"&item="+locationID)+"&id="+(mTemp[1] == "" ? "windower_content" : mTemp[1])+"&itemoffset=1");
				scope.req.open("GET", ("index.php?location_id="+scope.locID+"&item="+locationID)+"&windower=1", false);
				scope.req.onreadystatechange = function() {
					if (scope.req.readyState==4) {
						if(scope.infoWindow) {
							scope.infoWindow.close();
						}
						var sa = getScrollAmount();
						var winDim = getScreenDimensions();
						scope.infoWindow = new Windower();
						scope.infoWindow.buildWindow(winDim.w/2 - 250, sa.y+(winDim.h/2 - 200), 500, 400, ++wDepth, {supressButtonGraphics:true});
						scope.infoWindow.setTitle("About this place:");
						scope.infoWindow.setStatus("<a href=\"index.php?location_id="+scope.locID+"&item="+locationID+"\">See more about this Place</a>");
						scope.infoWindow.swapDepthOnDrag = true;
						scope.infoWindow.setContent(scope.req.responseText);
						scope.infoWindow.setStyle("resizecorner", ["backgroundColor", "backgroundImage"], ["#CCCCCC", "url("+domainDirectory+"/local/images/resizecornerbg.gif)"]);
						scope.infoWindow.setStyle("titlebar", "paddingLeft", "5px");
						scope.infoWindow.setStyle("closebutton", ["background", "border"],["url("+domainDirectory+"/local/images/closebtn.gif) no-repeat", "none"]);
						scope.infoWindow.setStyle("minimisebutton", ["background", "border"],["url("+domainDirectory+"/local/images/minbtn.gif) no-repeat", "none"]);
						scope.infoWindow.onClose = function() { scope.infoWindow = undefined; };
						scope.infoWindow.onMinimise = function() {
							scope.infoWindow.setStyle("minimisebutton", "background","url("+domainDirectory+"/local/images/maxbtn.gif) no-repeat");
						}
						scope.infoWindow.onMaximise = function() {
							scope.infoWindow.setStyle("minimisebutton", "background","url("+domainDirectory+"/local/images/minbtn.gif) no-repeat");
						}
						initLightbox();
					}
					
				};
				scope.req.send(null);
			}
		}
	}
	this.map.addOverlay(interactiveMarker);
	fixThePNGs();
}
var wDepth = 2;
function getScrollAmount() {
	var x,y;
	if (self.pageYOffset) {
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	} else if (document.body) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return {x:x, y:y};
}
function getScreenDimensions() {
	var w,h;
	if (self.innerHeight) {
		w = self.innerWidth;
		h = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if (document.body) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	return {w:w, h:h};
}
function fixThePNGs() {
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if(version < 7) {
		fixPNGs();
	}
}