﻿function centerMap(_oMap, _aPoints)
	{//Takes oMap object and aPoints array and canter map and set zoom.
	var nOfset = 0, oPointsBounds, nMinY = 0, nMinX = 0, nMaxY = 0, nMaxX = 0, nNE = 0, nSW = 0, oRec, nZoom = 0
	if (!_oMap || !_aPoints)
		{
		return
		}
	oPointsBounds = new GBounds(_aPoints)
	if (oPointsBounds.minY - oPointsBounds.maxY == 0 || oPointsBounds.minX - oPointsBounds.maxX == 0)
		{
		_oMap.setCenter(new GLatLng(0,0),2)
		return 
		}
	var nMinY = oPointsBounds.minY - nOfset
		nMaxY = oPointsBounds.maxY + nOfset
		nMinX = oPointsBounds.minX - nOfset
		nMaxX = oPointsBounds.maxX + nOfset
	nNE = new GLatLng(parseFloat(nMinY), parseFloat(nMinX))
	nSW = new GLatLng(parseFloat(nMaxY),parseFloat(nMaxX))
	oRec = new GLatLngBounds(nNE, nSW)
	nZoom = _oMap.getBoundsZoomLevel(oRec)
	_oMap.setCenter(new GLatLng(nMinY + (nMaxY - nMinY) / 2, nMinX + (nMaxX - nMinX) / 2),nZoom)
	}

function addFavorite(cType, cSubjectID, lDelete)
	{
	callServer("GET", "/Trails/wwTrails.dll/ww.Trails.wwTrailsPages","favoriteAdded()",
		"page=addFavorite&Type=" + cType + "&SubjectID=" + cSubjectID + (lDelete ? "&Delete=Y" : ""))
	}

function favoriteAdded()
	{
	var cSrc
	alert(xmlHttp.responseText)
	if (parent.frames["favFrame"])
		{
		cSrc = parent.frames["favFrame"].document.location.href
		parent.frames["favFrame"].document.location.href = "about:blank"
		parent.frames["favFrame"].document.location.href = cSrc
		}
	}
	
function lTrim(cString)
	{
	while (cString.charAt(0) == " ")
		{
		cString = cString.substr(1)
		}
	return cString
	}

function calcLength(oPoly, lNumberOnly)
	{
	//Later, check here if results are miles or km
	//This accept Google's polyline and calculate length in meters.
	var lMiles, nDevide, cUnit, nTrailLength, cCookie = document.cookie;
	lMiles = cCookie.indexOf("LengthInKM=Y") == -1
	nDevide = (lMiles ? 1609.344 : 1000)
	cUnit = (lMiles ? " Miles" : " KM")
	nTrailLength = oPoly.getLength() / nDevide
	return nTrailLength.toFixed(2) + (lNumberOnly ? "" : cUnit)
	}


function postNewTrail()
  {
  document.location.href = "/Trails/wwTrails.dll/ww.Trails.wwTrailsPages?page=postTrail&func=home&newTrail=1"
  }

function getProperties(oObject)
	//For developement. Show all props / method of an object.
	{
	var cStr = ""
	for (key in oObject)
		{
		cStr = cStr + key + " | " 
		}
	alert(cStr)
	}

var oOpenTopic, 
	cSelBG = "#CCCCCC", //Selected button BG
	cSelBrd = "inset", //Selected buttons border
	cUnSelBG = "" //"#F5F5F5", //Unselected
	cUnSelBrd = "outset" //unselected

//GIcon sizes:
var	GIconTrailhead = "/images/TrailheadIcon.png", 
	GIconMileStone = "/images/MS.png", 
	GIconPOI = "/images/POI.png", 
	GIconMileStoneShadow = "", 
	GIconPOIShadow = "", 
	GIconTrailheadShadow = "", 
	GIconSizeX = 17, 
	GIconSizeY = 17, 
	GIconShadowSizeX = 0, 
	GIconShadowSizeY = 0, 
	GIconAnchorX = 8,
	GIconAnchorY = 8,
	GIconInfoWindowAnchorX = 8, 
	GIconInfoWindowAnchorY = 4, 
	GIconAfterRide = "/images/afterRide*.gif"

/*
<form name="myForm">
Name: <input type="text" onkeyup="ajaxFunction();" name="username" />
Time: <input type="text" name="time" />
</form>
*/

function setStatus(cMessage, lAlert)
	//This functon color the status span yellow and show cMessage.
	//If cMessage is empty background is restoore to white.
	//Require oStatus exist!
	{
	//oStatus.innerHTML = "<p style='font-size:10pt;'>" + (cMessage ? cMessage : "") + "</p>"
	oStatus.innerHTML = (cMessage ? cMessage : "")
	oStatus.style.backgroundColor = (lAlert ? "#FFFFCC" : "")
	}

function restorePosition(centerMapTo, nZoom)
	//This functon set center to map from cookie if can find it.
	{
	var aParts = new Array(), i, aPos = new Array(), cLat = false, cLng = false, cZoom = false
	if (centerMapTo) //Cordinates for centering map sent. cLat,cLng,cZoom
		{
		aParts = centerMapTo.split(",")
		oMap.setCenter(new GLatLng(parseFloat(aParts[0]), parseFloat(aParts[1])), Number(aParts[2]));
		return
		}
	aParts = document.cookie.split(";")
	for (i = 0 ; i < aParts.length ; i++)
		{
		cPos = aParts[i]
		if (cPos.indexOf("mapLat") > -1)
			{
			cLat = cPos.substr(cPos.indexOf("=") + 1)
			}
		if (cPos.indexOf("mapLng") > -1)
			{
			cLng = cPos.substr(cPos.indexOf("=") + 1)
			}
		if (cPos.indexOf("mapZoom") > -1)
			{
			cZoom = cPos.substr(cPos.indexOf("=") + 1)
			}
		}

	if (cLat && cLat !== "NaN" && cLng && cLng !== "NaN" && cZoom && cZoom !== "NaN")
		{
		if (nZoom)
			{
			cZoom = nZoom.toString()
			}
		oMap.setCenter(new GLatLng(cLat, cLng), Number(cZoom));
		}
		else
		{
		oMap.setCenter(new GLatLng(0, 0), 1);
		}
	}

function replaceCommas(cText)
	{
	cText = cText.replace(/'/g, "\\'")
	return cText.replace(/"/g, '\\"')
	}
function replaceChr(cText, cFind, cReplace)
	{
	//DO NOT USE /g it freakout when * are in string.
	if (!cText) {return ""}
	while (cText.indexOf(cFind) > -1)
		{
		cText = cText.replace(cFind,cReplace)
		}
	return cText
	}

function setLF(cText)
	{//Replace <br> with \n for showing in text area.
	return replaceChr(cText,"<br>", "\n")
	}

function clearLF(cText)
	{//Clear linefeed from text area
	return replaceChr(replaceChr(cText, "\n","<br>"),"\r","")
	}

function clearTags(cText)
	{
	//remove offensive tags
	cText = cText.replace(/</g, "")
	cText = cText.replace(/>/g, "")
	return cText
	}

/*
function text2html(cHTML)
	{
	//remove offensive tags
	cHTML = cHTML.replace(/"/g, "&quot;")
	cHTML = cHTML.replace(/'/g, "&apos;")
	cHTML = cHTML.replace(/&/g, "&amp;")
	cHTML = cHTML.replace(/</g, "")
	cHTML = cHTML.replace(/>/g, "")
	return cHTML
	}

function html2text(cText)
	{
	//remove offensive tags
	cText = cText.replace(/&quot;/g, "\"")
	cText = cText.replace(/&apos;/g, "'")
	cText = cText.replace(/&amp;/g, "&")
	cText = cText.replace(/&lt;/g, "<")
	cText = cText.replace(/&gt;/g, ">")
	return cText
	}
*/

function text2escape(cText)
	{
	//Convert & > < ' " to escape characters
	cText = cText.replace(/"/g, escape('"'))
	cText = cText.replace(/'/g,  escape("'"))
	cText = cText.replace(/&/g,  escape("&"))
	cText = cText.replace(/</g,  escape("<"))
	cText = cText.replace(/>/g,  escape(">"))
	return cText
	}

function savePosition()
	//Save center of map into a cookie that can then be restored by restorePosition()
	{
	var cCenter = oMap.getCenter()
	if (!cCenter)
		{
		return
		}
	var aGeo = cCenter.toString().split(","), dToday = new Date(), dExpires = new Date(), nDays=120;
	dExpires.setTime(dToday.getTime() + 3600000*24*nDays);
	document.cookie = "mapLat=" + aGeo[0].replace("(","") + ";expires=" + dExpires.toGMTString() + ";path=/;"
	document.cookie = "mapLng=" + aGeo[1].replace(")","") + ";expires=" + dExpires.toGMTString() + ";path=/;"
	document.cookie = "mapZoom=" + oMap.getZoom() + ";expires=" + dExpires.toGMTString() + ";path=/;"
	}

function setButton(oButton, lSelected)
	{
	if (lSelected)
		{
		oButton.style.borderStyle=cSelBrd
		oButton.style.backgroundColor = cSelBG
		}
		else
		{
		oButton.style.borderStyle=cUnSelBrd
		oButton.style.backgroundColor = cUnSelBG
		}
	}

function setImageButton(cButtonID, lSelected)
	{
	var oButton = document.getElementById(cButtonID)
	if (!oButton)
		{
		return
		}
	if (lSelected)
		{
		oButton.style.borderStyle="inset"
		oButton.style.borderWidth="3px"
		oButton.style.backgroundColor = cSelBG
		}
		else
		{
		oButton.style.borderStyle="outset"
		oButton.style.borderWidth="3px"
		oButton.style.backgroundColor = cUnSelBG
		}
	}

function setColor(oThis,cColor) 
	{
	oThis.style.backgroundColor=cColor	
	}
	
function gotoTrail(cNum)
	{
	document.location.href="Trails/Trail_" + cNum + ".htm"
	}
	
function showTopic(cTopicToOpen)
	{
	var oTopicToOpen = document.getElementById(cTopicToOpen)
	oTopicToOpen.style.display = (oTopicToOpen.style.display == "none" || oTopicToOpen.style.display == "" ? "block" : "none")
	oTopicToOpen.scrollIntoView(true)
	if (oOpenTopic)
		{
		oOpenTopic.style.display = "none"
		}
	oOpenTopic = (oTopicToOpen.style.display == "block" ? oTopicToOpen : false)
	}

function showGoogleMap(cURL)
	{
	window.frames["googleFrame"].location.href = cURL
	//window.frames["googleFrame"].src = cURL
	}

function showTrail()
	{
	alert("OK!")
	}
	
function sortTrailHeadBy(cField)
	{
	alert(cField)
	}


//#999966
function highLight()
	{
//	var aLinks = document.getElementsByName("HiLi"), x
	var aLinks = document.getElementsByTagName("A"), x
	for (x=0;x<aLinks.length;x++)
		{
		cPage = document.location.href
		cHref = aLinks[x].href
		if (cPage.toLowerCase() == cHref.toLowerCase())
			{
			if (aLinks[x].name == "HiLi")
				{
				turnOn(aLinks[x])
				}
				
			if (aLinks[x].name == "tpLink")
				{
				aLinks[x].style.backgroundColor="#999966";
				aLinks[x].style.color="#00FF00" //"#00FF00";
				}
			}
		}
	}

function turnOn(oLnk)
	{
//	oLnk.style.backgroundColor="#996633";
	oLnk.style.color="#333300";
//	oLnk.style.border = "1pt inset #666666"
	oLnk.style.backgroundImage = 'url(/images/topLinksBGOn.png)'
/*
	oLnk.style.borderBottom="1pt inset #666666"
	oLnk.style.borderBottom="1pt inset #FFFFFF"
	oLnk.style.borderRight="1pt inset #FFFFFF"
	oLnk.style.borderTop="1pt inset #666666"
	oLnk.style.borderLeft="1pt inset #666666"
*/
	}

function createInput(oObj, cCode, cNewObjID)
	//Use to convert text in <span></span> into input code.
	//Example: createInput(document.getElementById("aSdate"), "<input name=\"aSelSDate\" id=\"aSelSDate\" onfocus=\"this.select()\" style=\"width:70px;\">", "aSelSDate")
	//oObj = HTML element to insert code in. 
	//cCode = The code to insert inthe element. 
	//cNewObjID = New element ID. If exist will place existing value in the new element.
	{
	var cText = (document.all ? oObj.innerText : oObj.textContent)
	oObj.innerHTML = cCode
	if (cNewObjID)
		{
		document.getElementById(cNewObjID).value = cText
		}
	}

function getCookie(cCookie)
	{
	var i, aParts = document.cookie.split(";")
	for (i = 0 ; i < aParts.length ; i++)
		{
		if (aParts[i].indexOf(cCookie) > -1)
			{
			return aParts[i].substr(aParts[i].indexOf("=") + 1)
			}
		}
	return ""
	}


//MUST BE LOADED ALONE, at the first page that calls openModal()
//Create modal window:
if (typeof(_oModalDiv) == "undefined")
	{
	var _oModalDiv = false, _oModalWind = false, _nWindLeft = 0, _nWindTop = 0
	}

function openModal(cURL, nPercent, nLeft, nTop, nWidth, nHeight, cExtraSetting)
	{
	/*
	This will open mdal window. You can change the nPercent to fit need
	Example: 
		openModal()
		openModal("http://google.com")
		openModal("http://google.com",0.6)
		openModal("http://google.com", 0, 50, 50, 400, 500)
		openModal("http://google.com", 0, 0, 0, 400, 500) This will center a window, 500h and 400w.
	*/
	var nScreenWidth = screenSize(1), nScreenHeight = screenSize(2), oDoc
	nPercent = (nPercent ? nPercent : 0.9)
	_nWindLeft = (nLeft ? nLeft : (nWidth ? nScreenWidth - nWidth : (nScreenWidth * (1 - nPercent))) / 2)
	cLeft = _nWindLeft + "px"
	cWidth = (nWidth ? nWidth : (nScreenWidth * nPercent)) + "px"
	_nWindTop = (nTop ? nTop : (nHeight ? nScreenHeight - nHeight : (nScreenHeight * (1 - nPercent))) / 2)
	cTop = _nWindTop + "px"
	cHeight = (nHeight ? nHeight : (nScreenHeight * nPercent)) + "px"
	if (!_oModalDiv)
		{
		_oModalDiv = document.createElement("div")
		_oModalDiv.style.position = "fixed"
		_oModalDiv.style.left = "-200px"
		_oModalDiv.style.top = "-200px"
		_oModalDiv.style.width = (nScreenWidth + 400) + "px"
		_oModalDiv.style.height = (nScreenHeight + 400 ) + "px"
		_oModalDiv.style.backgroundColor = "#808080"
		_oModalDiv.style.zIndex = 1000


		_oModalWind = document.createElement("iframe")
		_oModalWind.id = "_MW"
		_oModalWind.style.position = "fixed"
		_oModalWind.style.left = cLeft
		_oModalWind.style.top = cTop
		_oModalWind.style.width = cWidth
		_oModalWind.style.height = cHeight
		_oModalWind.style.zIndex = 1001

		_oModalWind.scrolling = "no"

		_oModalWind.style.backgroundColor = "#FFFFFF"
		if (cExtraSetting)
			{
			if (cExtraSetting.indexOf("scrolling=yes") > -1)
				{
				_oModalWind.scrolling = "yes"
				}
			}
		}

	if (document.all)
		{
		_oModalDiv.style.filter = "alpha(opacity = 80)"
		}
		else
		{
		_oModalDiv.style.opacity = 0.8
		}

	document.body.appendChild(_oModalDiv)
	document.body.appendChild(_oModalWind)
	_oModalDiv.setAttribute("onclick","_oModalWind.focus()")
	_oModalWind.focus()

	if (cURL)
		{
		_oModalWind.src = cURL
		}
		else
		{
		oDoc = (_oModalWind.contentWindow ? _oModalWind.contentWindow : window.frames["_MW"])
		oDoc.document.writeln("<table width=100% height=100%><tr><td valign=center style='text-align:center'>" +
			"<button id='oButt' onclick='parent.closeModal()'>" + "Close" + "</button></td></tr></table>" + 
			"<scr" + "ipt>document.getElementById('oButt').focus();</scr" + "ipt>")
		oDoc.document.close()
		}
	return _oModalWind
	}

function screenSize(nWhat)
	{
	switch (nWhat)
		{
		case 1:
			if (self.innerHeight) {return self.innerWidth;}
			if (document.documentElement && document.documentElement.clientWidth) {return document.documentElement.clientWidth;}
			if (document.body) {return document.body.clientWidth}
			break
		case 2: 
			if (self.innerHeight) {return self.innerHeight;}
			if (document.documentElement && document.documentElement.clientHeight) {return document.documentElement.clientHeight;}
			if (document.body) {return document.body.clientHeight}
			break
		}
	return -1
	}

function closeModal(cReturnFunc)
	{
	if (document.all)
		{
		document.body.onscroll = ""
		}
		else
		{
		document.body.setAttribute("onscroll",false)
		}

	if (_oModalWind)
		{
		document.body.removeChild(_oModalWind)
		document.body.removeChild(_oModalDiv)
		}
	_oModalWind = false
	_oModalDiv = false
	if (cReturnFunc)
		{
		eval(cReturnFunc)
		}
	}
//END Create modal window:


