﻿	// small graphics for list support
	//http://static.travelscream.com/images/Icons/tri_left.gif
	//http://static.travelscream.com/images/Icons/tri_right.gif
	//http://static.travelscream.com/images/Icons/tri_up.gif
	//http://static.travelscream.com/images/Icons/tri_down.gif

	//Little lights are 6x6
	//http://static.travelscream.com/images/Icons/light_red.gif
	//http://static.travelscream.com/images/Icons/light_green.gif

// JScript File
	var gTSMap;
	var gPopupArray = new Array();
	
	var gPopupShowing = 0;
	var gFiredEndMoveEvent = 0;

	var gDirObject;
	var gDirPolyline;
	var gMarker0;
	var gMarker1;

    function CreateMap(inElementID) {
		if (GBrowserIsCompatible()) {
			gTSMap = new GMap2(document.getElementById(inElementID));
			gTSMap.addControl(new GSmallMapControl());
			gTSMap.addControl(new GMapTypeControl());

			GEvent.addListener(gTSMap, "infowindowclose", function() {
				if (gFiredEndMoveEvent == 1)
					DisplayMapPopup(gPopupShowing);
				ResetPopupState();
				gPopupShowing = 0;
			});
			GEvent.addListener(gTSMap, "dragstart", function() {
				gPopupShowing = 0;
			});
			GEvent.addListener(gTSMap, "moveend", function() {
				gFiredEndMoveEvent = 1;
			});

			return gTSMap;
		}
		return null;
	}

	function ResetPopupState()
	{
		gFiredEndMoveEvent = 0;
	}
		
	function DisplayMapPopup(inPopupIndex)
	{
		var popupObj;

		for (i = 0; i < gPopupArray.length; i ++) {
			if (gPopupArray[i].mID == inPopupIndex) {
				popupObj = gPopupArray[i];
				break;
			}
		}
		if (popupObj == null) return;
		gTSMap.openInfoWindowHtml(popupObj.mPoint, "<font face=\"verdana\" size=\"4\">"+popupObj.mName+"</font><br/><table width=250><tr><td><font face=\"verdana\" size=\"2\">"+popupObj.mDesc+"</font></td></tr></table>");
		
		populateLinkField(inPopupIndex, popupObj.mName);		
		ResetPopupState();
		gPopupShowing = inPopupIndex;
	}
	
	function RegisterMapPopup(inPoint, inName, inDesc, inID)
	{
		var newPopupObj = new Object();
		newPopupObj.mPoint = inPoint;
		newPopupObj.mName = inName;
		newPopupObj.mDesc = inDesc;
		newPopupObj.mID = inID;

		var itemNum = gPopupArray.length;
		gPopupArray[itemNum] = newPopupObj;
		return itemNum;
	}	

	var myPoint;
	var myIconImg;
	var myMarkerOptions;
	var myMarker;
	
	function myCreateMarker(inPoint, inMap, inMarkerOptions, inName, inDesc, inID) {
		var myMarker = new GMarker(inPoint, inMarkerOptions);
		var newPopupIndex = RegisterMapPopup(inPoint, inName, inDesc, inID);
		GEvent.addListener(myMarker, "click", function() {
			DisplayMapPopup(inID);
		});
		inMap.addOverlay(myMarker);
	}

var didIT = false;

	function doPan(map){
		if (document.getElementById("tsmap_embedCode") != null){
			if (document.getElementById("tsmap_embedCode").style.visibility == 'visible'){
				document.getElementById("tsmap_embedCode").style.visibility = 'hidden';
			}
		}
	
		var mapBounds = map.getBounds();
		var topRight = new String(mapBounds.getNorthEast());
		var bottomLeft = new String(mapBounds.getSouthWest());
		var trArr = topRight.split(",");
		var blArr = bottomLeft.split(",");
		var minX = blArr[0];
		var minY = blArr[1];
		var maxX = trArr[0];
		var maxY = trArr[1];

		var x = new	tsNewX();
		x.open("POST","/blogSetup/getItems.aspx", true);
		x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		inParams = minX + "," + minY + "," + maxX + "," + maxY;
		var allParams="<params>"+inParams+"</params>";
		x.send("inParams="+allParams);

		x.onreadystatechange = function() {
			if (x.readyState == 4){
				if (x.responseXML.getElementsByTagName("resultCode").item(0).childNodes[0].nodeValue == "0") {
					var itemArr = x.responseXML.getElementsByTagName("map_item");

					map.clearOverlays(); // clear everything from the map and redraw fresh (overlaying icons look crappy)
					for (i = 0; i < itemArr.length; i++){
						myPoint = new GLatLng(itemArr[i].getElementsByTagName("latitude").item(0).childNodes[0].nodeValue,itemArr[i].getElementsByTagName("longitude").item(0).childNodes[0].nodeValue);
						myIconImg = new GIcon(G_DEFAULT_ICON);
						myIconImg.shadow = "http://static.travelscream.com/images/spacer.gif";
						myIconImg.image = "http://static.travelscream.com/images/Icons/" + itemArr[i].getElementsByTagName("icon").item(0).childNodes[0].nodeValue;
						myMarkerOptions = { icon:myIconImg };

						var myItemDesc = "";
						// can't deal with an <itemdesc> element that's empty - it comes back as null
						if (itemArr[i].getElementsByTagName("itemdesc").item(0).childNodes[0] != null)
							myItemDesc = itemArr[i].getElementsByTagName("itemdesc").item(0).childNodes[0].nodeValue;
						myCreateMarker(myPoint, map, myMarkerOptions, itemArr[i].getElementsByTagName("itemname").item(0).childNodes[0].nodeValue, myItemDesc, itemArr[i].getElementsByTagName("id").item(0).childNodes[0].nodeValue);
					}
					DisplayItemList(x.responseXML);
					map.addOverlay(gDirPolyline);
					map.addOverlay(gMarker0);
					map.addOverlay(gMarker1);
				}
			}
		};
	}

		
	function DoDir()
	{
		var dirReqString = 'from: ';
		dirReqString = dirReqString.concat(document.getElementById("dirFrom").value);
		dirReqString = dirReqString.concat(' to: ');
		dirReqString = dirReqString.concat(document.getElementById("dirTo").value);

		var startPoint = new String();
		var endPoint = new String();

		if (document.getElementById("coordFrom").value != ''){
			startPoint = document.getElementById("coordFrom").value;
		}
		else if(document.getElementById("dirFrom").value != 'Type Address' && document.getElementById("dirFrom").value.length > 0){
			startPoint = document.getElementById("dirFrom").value;
		}
		else{
			window.alert("Please Enter An Address or Select A Location in the 'from' fields");
			return;
		}

		if (document.getElementById("coordTo").value != ''){
			endPoint = document.getElementById("coordTo").value;
		}
		else if(document.getElementById("dirTo").value != 'Type Address' && document.getElementById("dirTo").value.length > 0){
			endPoint = document.getElementById("dirTo").value;
		}
		else{
			window.alert("Please Enter An Address or Select A Location in the 'to' fields");
			return;
		}

		var coordArr = new Array();
		coordArr[0] = startPoint;
		coordArr[1] = endPoint;

		if (document.getElementById("doWalk").checked){
			gDirObject.loadFromWaypoints(coordArr, {travelMode:G_TRAVEL_MODE_WALKING});
		}
		else if (document.getElementById("doDrive").checked){
			gDirObject.loadFromWaypoints(coordArr, {travelMode:G_TRAVEL_MODE_DRIVING});
		}
	}

	function clearDirField(inObj){
		if (inObj == 'dirFrom' && document.getElementById("dirFrom").value == 'Type Address'){
			document.getElementById("dirFrom").value = "";
		}
		else if (inObj == 'dirTo' && document.getElementById("dirTo").value == 'Type Address'){
			document.getElementById("dirTo").value = "";
		}
	}

	function DisplayItemList(inXMLObj)
	{
		if (document.getElementById("tsmap_mapListDiv") == null) return;
		if (didIT == false){
			var itemListHTML = "";
			itemArr = inXMLObj.getElementsByTagName("ITEM_TYPE");
			itemListHTML += "<ul style=\"list-style-type: none;\" id=\"tsmap_items\">";

			for (i = 0; i < itemArr.length; i++){
				var itemList = itemArr[i].getElementsByTagName("map_item");
				if (itemList.length > 0) {
					itemListHTML += "<li style=\"list-style-type: none; margin-left: 0px; \">";
					itemListHTML += itemArr[i].getElementsByTagName("display_name").item(0).childNodes[0].nodeValue.toUpperCase();
					if (itemList.length > 0){
						itemListHTML += "<ul style=\"list-style-type: none; margin-left: 0px; \">";
						for (y=0;y<itemList.length;y++){
							itemListHTML += "<li style=\"list-style-type: none; margin-left: 0px; \"><a style=\" text-decoration: none; \" href=\"javascript:DisplayMapPopup("+itemList[y].getElementsByTagName("id").item(0).childNodes[0].nodeValue+");\" class=\"mapLink\">"+itemList[y].getElementsByTagName("itemname").item(0).childNodes[0].nodeValue+"</a></li>";
						}
						itemListHTML += "</ul>";
					}
					itemListHTML += "</li>";
				}
			}
			itemListHTML += "</ul>";
			document.getElementById("tsmap_mapListDiv").innerHTML = itemListHTML;
			makeCollapsible(document.getElementById('tsmap_items'));
			didIT = true;
		}
	}

	function DisplayDebug(inMsg)
	{
		document.getElementById("debugInfo").value += inMsg+"-";
	}

	function populateLinkField(inPopupIndex, inItemName){
		if (document.getElementById("tsmap_embedCode") == null) return;
		document.getElementById("tsmap_embedCode").style.visibility = 'visible';
		var tURL = new String(document.location);
		var tmpArr = tURL.split("/");
		document.getElementById("mapLink").value = '<a href="http://' + tmpArr[2] + '/map.aspx?iid=' + inPopupIndex + '">' + inItemName + '</a>';
	}

	/* CLOSED_IMAGE - the image to be displayed when the sublists are closed
	* OPEN_IMAGE   - the image to be displayed when the sublists are opened
	*/
	CLOSED_IMAGE='/images/plus.png';
	OPEN_IMAGE='/images/minus.png';

	/* makeCollapsible - makes a list have collapsible sublists
	* 
	* listElement - the element representing the list to make collapsible
	*/
	function makeCollapsible(listElement){
		// removed list item bullets and the space they occupy
		listElement.style.listStyle='none';
		listElement.style.marginLeft='0';
		listElement.style.paddingLeft='0';
		// loop over all child elements of the list
		var child=listElement.firstChild;
		while (child!=null){
			// only process li elements (and not text elements)
			if (child.nodeType==1){
				// build a list of child ol and ul elements and hide them
				var list=new Array();
				var grandchild=child.firstChild;
				while (grandchild!=null){
					if (grandchild.tagName=='OL' || grandchild.tagName=='UL'){
						grandchild.style.display='none';
						list.push(grandchild);
					}
					grandchild=grandchild.nextSibling;
				}
				// add toggle buttons
				var node=document.createElement('img');
				node.setAttribute('src',CLOSED_IMAGE);
				node.setAttribute('class','collapsibleClosed');
				node.setAttribute('hspace','4');
				node.onclick=createToggleFunction(node,list);
				child.insertBefore(node,child.firstChild);
			}
			child=child.nextSibling;
		}
	}

	/* createToggleFunction - returns a function that toggles the sublist display
	* 
	* toggleElement  - the element representing the toggle gadget
	* sublistElement - an array of elements representing the sublists that should
	*                  be opened or closed when the toggle gadget is clicked
	*/
	function createToggleFunction(toggleElement,sublistElements){
		return function(){
		// toggle status of toggle gadget
		if (toggleElement.getAttribute('class')=='collapsibleClosed'){
			toggleElement.setAttribute('class','collapsibleOpen');
			toggleElement.setAttribute('src',OPEN_IMAGE);
		}
		else{
			toggleElement.setAttribute('class','collapsibleClosed');
			toggleElement.setAttribute('src',CLOSED_IMAGE);
		}
		// toggle display of sublists
		for (var i=0;i<sublistElements.length;i++){
			sublistElements[i].style.display=(sublistElements[i].style.display=='inline')?'none':'inline';}
		}
	}













/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008
	
	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
