/*=================================================
  Title:	Flash Interactive Map JS Augment
  Author:	Lee Hogg
  Created:	2003-12-09
  Updated:	2004-01-19
  -------------------------------------------------*/
var version = 'js: 0-04';
var validNum = true;
var cookieSetCount = 0;
var cookieReadCount = 0;
var valueWaitCount = 0;
var urlToFlashCount = 0;
var bikky = document.cookie;
var receivedUrlCoords = false;
var formCoordCount = 0;
var isIE=document.all?true:false;
var isDOM=document.getElementById?true:false;

function getCookie(name) {
	var index = bikky.indexOf(name + "=");
	if (index == -1) return null;
		index = bikky.indexOf("=", index) + 1;
	var endstr = bikky.indexOf(";", index);
	if (endstr == -1) endstr = bikky.length;
		return unescape(bikky.substring(index, endstr));
}
var today = new Date();
var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days

function setCookie(name, value) {
	if (value != null && value != "")
	document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
	bikky = document.cookie; // update bikky
}
function flashReady(){
	if(!receivedUrlCoords){
		var xPercent = getCookie("xPercent");
		var yPercent = getCookie("yPercent");
		var zoom = getCookie("zoom");
		if(xPercent){
			window.document.map.SetVariable("newCoords", "true")
			window.document.map.SetVariable("xPercentIn", xPercent);
			window.document.map.SetVariable("yPercentIn", yPercent);
			window.document.map.SetVariable("scaleIn", zoom);
			if(document.info){
				document.info.inputType.value = "cookie input";
				document.info.io.value = 'cookie values->Flash';
				document.info.xPercent.value = Math.round(document.info.xPercent.value*1000)/1000;
				document.info.yPercent.value = Math.round(document.info.yPercent.value*1000)/1000;
				document.info.zoom.value = zoom;
			}
		}
	} else if (receivedUrlCoords) {
		window.document.map.SetVariable("newCoords", "true")
		window.document.map.SetVariable("xPercentIn", xUrl)
		window.document.map.SetVariable("yPercentIn", yUrl)
		window.document.map.SetVariable("scaleIn", zUrl)
		if(document.info){
			document.info.inputType.value = "URL coords"
			document.info.io.value = 'url values->Flash'
			document.info.xPercent.value = xUrl
			document.info.yPercent.value = yUrl
			document.info.zoom.value = zUrl
			document.info.title.value = newTitle
		}
	} else {
//		info.debug3.value = 'awaiting values (' + valueWaitCount + ')';
		valueWaitCount++;
		setTimeout("flashReady()", 1000);
	}
}
function urlCoords(x,y,z,pageTitle){
	receivedUrlCoords = true;
	xUrl = x;
	yUrl = y;
	zUrl = z;
	newTitle = pageTitle;
	if (newTitle == ''){
		newTitle = '[title]';
	}
}
function checkNum(arg){
	var validchars = '0123456789.';
	for (var i=0; i < arg.length; i++) {
		
/*		var chara = num.charAt(i).toLowerCase();
		if (validchars.indexOf(chara) != -1){
		} else {
		alert("Invalid character: " + char);
		validNum = false;
		break;
		}*/
	}
}

function setUrl(){
	validNum = true;
	var xIn = document.info.xPercent.value;
	var yIn = document.info.yPercent.value;
	var zIn = document.info.zoom.value;
	titleOut = document.info.title.value;
	checkNum(xIn)
	checkNum(yIn);
	checkNum(zIn);
	if(validNum){
		urlCurrent = document.location.href;
		urlQMark = urlCurrent.indexOf('?');
		urlStrip = urlCurrent.substring(0,urlQMark);
		xRounded = Math.round(xIn*1000)/1000;
		yRounded = Math.round(yIn*1000)/1000;
		urlAppend = '';
		urlAppend += '?x=' + xRounded
		+ '&y=' + yRounded
		+ '&z=' + document.info.zoom.value;
		if (titleOut != '[title]' && titleOut != ''){
			urlAppend += '&title=' + titleOut;
		}
		document.location = urlStrip + urlAppend;
	}
}

function urlAppendOutput(){
	var xIn = document.info.xPercent.value;
	var yIn = document.info.yPercent.value;
	var zIn = document.info.zoom.value;
	titleOut = document.info.title.value;
	xRounded = Math.round(xIn*1000)/1000;
	yRounded = Math.round(yIn*1000)/1000;

	urlAppend = '';
	urlAppend += '?x=' + xRounded
	+ '&y=' + yRounded
	+ '&z=' + document.info.zoom.value;
	if (titleOut != '[title]' && titleOut != ''){
		urlAppend += '&title=' + titleOut;
	}
	
	document.info.urlAppend.value = urlAppend;
	window.clipboardData.setData('Text', urlAppend);
//	alert(urlAppend);
}

function clearUrl(){
	urlCurrent = document.location.href;
	urlQMark = urlCurrent.indexOf('?');
	urlStrip = urlCurrent.substring(0,urlQMark);
	document.location = urlStrip;
}

function coords(x,y,z){
	xPercentNew = x; yPercentNew = y; zoomNew = z;
	setCookie("xPercent", xPercentNew); setCookie("yPercent", yPercentNew); setCookie("zoom", zoomNew);
	if(document.info){
		document.info.xPercent.value = xPercentNew; document.info.yPercent.value = yPercentNew; document.info.zoom.value = zoomNew;
		document.info.debug3.value = "cookie set (times: " + ++cookieSetCount + ")";
	}
	if(document.location.href.indexOf('?') != -1){
		clearUrl();
	}
	document.info.xPercent.value = Math.round(document.info.xPercent.value*1000)/1000;
	document.info.yPercent.value = Math.round(document.info.yPercent.value*1000)/1000;
}

function jsToFlash(){
	window.document.map.SetVariable("newCoords", true);
	document.info.inputType.value = "form input";
	document.info.io.value = 'form values->Flash (' + ++formCoordCount +')';
	window.document.map.SetVariable("newCoords", "true")
	window.document.map.SetVariable("xPercentIn", document.info.xPercent.value);
	window.document.map.SetVariable("yPercentIn", document.info.yPercent.value);
	window.document.map.SetVariable("scaleIn", document.info.zoom.value);
}

toggleDivA = new Array();
var visibleA = new Array();
var displayA = new Array();
visibleA[-1] = 'hidden';
visibleA[1] = 'visible';
displayA[-1] = 'none';
displayA[1] = 'block';

function toggleVis(divID){
	checkCookie = getCookie(divID);
	if(getCookie(divID)){
	}
	if(!toggleDivA[divID]){
		toggleDivA[divID] = -1;
	}
	toggleDivA[divID] = toggleDivA[divID] * -1;
	visibleTo = visibleA[toggleDivA[divID]];
	displayTo = displayA[toggleDivA[divID]];
	setCookie(divID, toggleDivA[divID]);
	if (isDOM){
		document.getElementById(divID).style.visibility=visibleTo;
		document.getElementById(divID).style.display=displayTo;
	} else if (isIE) {
		 eval("document.all."+_w+".style.visibility="+visibleTo+";");
		 eval("document.all."+_w+".style.display="+displayTo+";");
	}
}
function checkSettings(){
	showAdvanced = getCookie('advanced');
	if(showAdvanced == 1) {
		toggleVis('advanced');
		document.settings.ast.checked = true;
	}
	showIO = getCookie('iopanel');
	if(showIO == 1) {
		toggleVis('iopanel');
		document.settings.iot.checked = true;
	}
}
