﻿//Containse Utility, browser detect code and code to fix virtual earth popup window location and apperance
var Utility = {
	/// <summary>
	///   static Utility class
	/// </summary>

	OnFailed: function(error) {
		/// <summary>
		///     This is the failed callback function for all webservices.
		/// </summary>  
		/// <param name="error">The error object from the webservice</param>          

		var stackTrace = error.get_stackTrace();
		var message = error.get_message();
		var statusCode = error.get_statusCode();
		var exceptionType = error.get_exceptionType();
		var timedout = error.get_timedOut();

		// Display the error.    
		//		var RsltElem =
		//            "Stack Trace: " + stackTrace + "<br/>" +
		//            "Service Error: " + message + "<br/>" +
		//            "Status Code: " + statusCode + "<br/>" +
		//            "Exception Type: " + exceptionType + "<br/>" +
		//            "Timedout: " + timedout;
		var errString = "This item has either been updated or removed. click ok to refresh your screen.";
		//alert(RsltElem);
		map._GetPinData();

	},

	decodeLine: function(encoded) {
		/// <summary>
		///     Decode an encoded string into a list of VE lat/lng.
		/// </summary>  
		/// <param name="encoded">The encoded string</param>       
		/// <returns>Array of VELatLong</returns>

		var len = encoded.length;
		var index = 0;
		var array = [];
		var lat = 0;
		var lng = 0;
		try {
			while (index < len) {
				var b;
				var shift = 0;
				var result = 0;
				do {
					b = encoded.charCodeAt(index++) - 63;
					result |= (b & 0x1f) << shift;
					shift += 5;
				} while (b >= 0x20);
				var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
				lat += dlat;

				shift = 0;
				result = 0;
				do {
					b = encoded.charCodeAt(index++) - 63;
					result |= (b & 0x1f) << shift;
					shift += 5;
				} while (b >= 0x20);
				var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
				lng += dlng;

				array.push(new VELatLong((lat * 1e-5), (lng * 1e-5)));
			}
		} catch (ex) {
			//error in encoding.
		}
		return array;
	},

	createEncodings: function(points) {
		/// <summary>
		///     Create the encoded bounds.
		/// </summary>  
		/// <param name="points">Array of VELatLong</param>       
		/// <returns>The encoded string</returns>    
		var i = 0;
		var plat = 0;
		var plng = 0;
		var encoded_points = "";

		for (i = 0; i < points.length; ++i) {
			var point = points[i];
			var lat = point.Latitude;
			var lng = point.Longitude;

			var late5 = Math.floor(lat * 1e5);
			var lnge5 = Math.floor(lng * 1e5);

			dlat = late5 - plat;
			dlng = lnge5 - plng;

			plat = late5;
			plng = lnge5;

			encoded_points += this._encodeSignedNumber(dlat) + this._encodeSignedNumber(dlng);
		}
		return encoded_points;
	},

	_encodeSignedNumber: function(num) {
		/// <summary>
		///     Encode a signed number in the encode format.
		/// </summary>  
		/// <param name="num">signed number</param>       
		/// <returns>encoded string</returns>       
		var sgn_num = num << 1;

		if (num < 0) {
			sgn_num = ~(sgn_num);
		}

		return (this._encodeNumber(sgn_num));
	},

	_encodeNumber: function(num) {
		/// <summary>
		///     Encode an unsigned number in the encode format.
		/// </summary>  
		/// <param name="num">unsigned number</param>       
		/// <returns>encoded string</returns>        
		var encodeString = "";

		while (num >= 0x20) {
			encodeString += (String.fromCharCode((0x20 | (num & 0x1f)) + 63));
			num >>= 5;
		}

		encodeString += (String.fromCharCode(num + 63));
		return encodeString;
	},

	_getMapDivSize: function() {
		mapWrapperDiv = document.getElementById("mapWrapper");
		controlsDiv = document.getElementById("controlsWrapper");
		mapDiv = document.getElementById("myMap");

		var mapWrapperSizeObj = new GetDivDim(mapWrapperDiv);
		var ControlsSizeObj = new GetDivDim(controlsDiv);
		var mapDivSizeObj = new GetDivDim(mapDiv);

		w = mapWrapperSizeObj.Width - ControlsSizeObj.Width - 14;
		h = 510;
		mapDiv.style.width = w + "px";
		mapDiv.style.height = h + "px";

		mapH = h;
		mapW = w;
	},

	_getDivDim: function(divID) {

		this.Width = 0;
		this.Height = 0;

		if (divID && (divID.offsetWidth || divID.offsetHeight)) {
			this.Width = divID.offsetWidth;
			this.Height = divID.offsetHeight;
			this.Left = divID.offsetLeft;
		}


	},

	_getBrowserDim: function() {
		this.Width = 0;
		this.Height = 0;

		if (typeof (window.innerWidth) == 'number') {
			//Non-IE
			this.Width = window.innerWidth;
			this.Height = window.innerHeight;
		}
		else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			//IE 6+ in 'standards compliant mode'
			this.Width = document.documentElement.clientWidth;
			this.Height = document.documentElement.clientHeight;
		}
		else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			//IE 4 compatible
			this.Width = document.body.clientWidth;
			this.Height = document.body.clientHeight;
		}

	},

	createXmlDoc: function() {
		// Mozilla and Netscape browsers
		if (document.implementation.createDocument) {
			var parser = new DOMParser()
			doc = parser.parseFromString(xmlString, "text/xml")
			// MSIE
		} else if (window.ActiveXObject) {
			doc = new ActiveXObject("Microsoft.XMLDOM")
			doc.async = "false"
			doc.loadXML(xmlString)
		}
		return myDocument;
	},

	setCookie: function(name, value, expiredays, path, domain, secure) {
		var expireDate = new Date();
		expireDate.setDate(expireDate.getDate() + expiredays);
		document.cookie = name + "=" + escape(value) +
			((expiredays) ? ";expires=" + expireDate.toGMTString() : "") +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			((secure) ? ";secure" : "");
	},


	getCookie: function(name) {
		if (document.cookie.length > 0) {
			var start = document.cookie.indexOf(name + "=");
			if (start != -1) {
				start = start + name.length + 1;
				end = document.cookie.indexOf(";", start);
				if (end == -1) end = document.cookie.length;
				return unescape(document.cookie.substring(start, end));
			}
		}
		return "";
	}
}


//END UTILITY CLASS  


//function checkIE6() {
//	var isIE6 = false;

//	//check for IE6 on Windows.
//	if (BrowserDetect.OS == "Windows" && BrowserDetect.browser == "Explorer" && BrowserDetect.version >= 6 && BrowserDetect.version < 7)
//		isIE6 = true;

//	if (BrowserDetect.OS == "Mac" && BrowserDetect.browser == "Firefox")
//		isIE6 = true;

//	return isIE6;
//}


var BrowserDetect = {
	init: function() {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function(data) {
		for (var i = 0; i < data.length; i++) {
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
	},
	dataBrowser: [
		{ string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS: [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};



BrowserDetect.init();

var isIE6 = false;

//check for IE6 on Windows.
if (BrowserDetect.OS == "Windows" && BrowserDetect.browser == "Explorer" && BrowserDetect.version >= 6 && BrowserDetect.version < 7)
	isIE6 = true;

//if(BrowserDetect.OS == "Mac" && BrowserDetect.browser == "Firefox")
//    isIE6 = true;

if (isIE6) {
	document.write("<iframe id=\"theframe\" src=\"\" class=\"frmcls\" frameborder=\"0\" scrolling=\"no\"></iframe>");
	HideFixIE6div();
}




function getInfoReference() {
	var infoDiv = null;

	//infoDiv = document.getElementById("MSVE_navAction_palette").nextSibling;

	infoDiv = window.ero.getElement();

	return infoDiv;

}


function waitForPopup() {
	if (isPopupHidden() == false) {
		clearInterval(timerRefPop);
		timerRefPop = setInterval("checkPopupHide()", 100);
	}
}


function checkPopupHide() {
	if (isPopupHidden() == true) {
		if (isIE6) {
			clearInterval(timerRefPop);
			HideFixIE6div();
		}
	}
}


function isPopupHidden() {
	isHidden = false;

	try {
		//var InfoBoxElement = document.getElementById("MSVE_navAction_palette").nextSibling;
		var InfoBoxElement = getInfoReference();


		if (InfoBoxElement.style.visibility == "hidden") {
			isHidden = true;
		}
	}
	catch (error) {
		//alert("Error finding infobox: " + error);
	}

	return isHidden;
}

function FixIE6div() {
	var DivRegion = document.getElementById(DivRegionRef);

	DDRegionX = getLeftX(DivRegion);
	DDRegionWidth = parseInt(DivRegion.offsetWidth);
	DDRegionY = getTopY(DivRegion);
	DDRegionHeight = parseInt(DivRegion.offsetHeight);
	DDRegionBottom = DDRegionY + DDRegionHeight;

	try {
		var InfoBoxElement = getInfoReference();
		var popTop = getTopY(InfoBoxElement)

		if (popTop < DDRegionBottom) {
			var frm = document.getElementById("theframe");

			frm.style.top = parseInt(DivRegion.offsetTop) + "px";
			frm.style.height = DDRegionHeight;

			if (InfoBoxElement.className.indexOf("ero-leftBeak") == -1) {
				frm.style.left = getLeftX(InfoBoxElement) - 207 + "px";
			}
			else {
				frm.style.left = getLeftX(InfoBoxElement) - 188 + "px";
			}
			frm.style.width = (parseInt(InfoBoxElement.offsetWidth) - 16) + "px";

			frm.style.display = "block";
		}

	}
	catch (error) {
		//alert("Error finding infobox: " + error);
	}

	//alert("DDbottom = " + DDRegionBottom + "\nDDRegionY = " + DDRegionY);
	//alert("top = " + DivRegion.offsetTop);
}

function HideFixIE6div() {
	var frm = document.getElementById("theframe");
	frm.style.left = "-300px";
	frm.style.top = "-300px";
	frm.style.height = "10px";
	frm.style.width = "10px";
	frm.style.display = "block";
}

function getLeftX(elemID) {
	x = elemID.offsetLeft;
	tempEl = elemID.offsetParent;
	while (tempEl != null) {
		x += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return x;
}

function getTopY(elemID) {
	y = elemID.offsetTop;
	tempEl = elemID.offsetParent;
	while (tempEl != null) {
		y += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return y;
}

//Control the +/- zoom action
function changeZoomLevel(zoomDirection) {
	var currentZoom = map.GetZoomLevel();

	if (zoomDirection == 'in' && currentZoom < map._MaxMapZoomLevel) {
		map.ZoomIn();
		currentZoom++;
	} 
	if (zoomDirection == 'out' && currentZoom > map._MinMapZoomLevel) {
		map.ZoomOut();
		currentZoom--;
	}

	if (currentZoom < map._MaxMapZoomLevel) {
		$get("imgZoomIn").src = "Images/VirtualEarth/zoom_plus_up.png";
	} else {
		$get("imgZoomIn").src = "Images/VirtualEarth/zoom_plus_null.png";
	}

	if (currentZoom > map._MinMapZoomLevel) {
		$get("imgZoomOut").src = "Images/VirtualEarth/zoom_minus_up.png";
	} else {
		$get("imgZoomOut").src = "Images/VirtualEarth/zoom_minus_null.png";
	}
}


if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();



