/*-------------------------------------------------------------+
| Global Document Variables 								   |
+-------------------------------------------------------------*/
// Establsih base path for JScript debugging
//var baseURL = "http://gis.mcphersoncountyks.us/svg/";
var baseURL = "./";

// "MouseOver" and "MouseOut" colors for hyperlinks.
var mouse_over_fill = 'yellow';
var mouse_over_opacity = '0.625';
var mouse_over_width = '3px';
var mouse_over_stroke = 'yellow';
var mouse_out_fill = 'white';
var mouse_out_opacity = '0';
var mouse_out_width = '0';
var mouse_out_stroke = 'white';
var optAerial = false;
var optRollover = false;
var svgloading = false;

// "onClick" event Storage variable.
var lastPropertyID;
var section_lcid;
var tshp_lcid;
var locMapSrc = "";
var svgMapSrc = "";
var section = 1;
var township = 17;
var range = 1;
var expLevel = 0;
var mouseoverSVG = true;

// Query the client for SVG Support (IE Only!)
var hasSVGSupport = false;

// Establish templates for the Information Box below the Location Map
var template1 = "<p class=MsoNormal>Township:<b>%1</b> S Range:<b>%2</b> W</p>";
var template2 = "<p class=MsoNormal>Township:<b>%1</b> S Range:<b>%2</b> W Section:<b>%3</b></p>";
var svgMapTemplate = "<embed name='SVGMAP' wmode='opaque' src='%1' pluginspage='http://www.adobe.com/svg/viewer/install/'>";

// Establish variables to set hilite on entry of svg document
var hilite_id = null;

// Set display options on successful SVG load
function setOptions () {
//window.alert ("SVG Load complete.\nYou may begin navigating the map!");
//Hilite current object in SVGMAP in Location Map
var svgdoc = document.LOCMAP.getSVGDocument();
if (hilite_id != null) showSVG (svgdoc, hilite_id, false);
}

function seeResult() {
  Database.style.display="none";
  Result.style.display="";
}

function seeData() {
  Database.style.display="";
  Result.style.display="none";
}

function svgLoaded () {
//Hilite current object in SVGMAP in Location Map
var svgdoc = document.LOCMAP.getSVGDocument();
if (hilite_id != null) showSVG (svgdoc, hilite_id, false);
svgloading = false;
//window.alert ("SVG Load complete.");
showRaster ();
   loadMsgOff();
return true;
}

// Modify the location of the document elements based on the current window size
function placeDivs () {
		var x;
		var y;
		var h;

		x = parent.document.body.offsetWidth - 246;
		y = parent.document.body.offsetHeight - 158;
		h = y - 144;

		LocationMap.style.left = x;
		LocInfo.style.left =x;
		Tabs.style.left = x;
		SCH_DATA.style.left = x;
		SVG_DATA.style.left = x;
		LGD_DATA.style.left = x;
		HELP_DATA.style.left = x;
		OPT_DATA.style.left = x;
		SCH_DATA.style.height = h;
		SVG_DATA.style.height = h;
		LGD_DATA.style.height = h;
		HELP_DATA.style.height = h;
		OPT_DATA.style.height = h;
		MainMap.style.width = x - 1;
		MainMap.style.height = y - 1;
		parent.document.embeds.SVGMAP.width = x - 1;
		parent.document.embeds.SVGMAP.height = y - 1;
		Database.style.width = x;
		Database.style.top = y;
                Result.style.width = x;
                Result.style.top = y;
}

function loadMsgOn() {
   Load.style.width = Database.style.width;
   Load.style.top = Database.style.top;
   Database.style.display="none";
   Load.style.display="";
}

function loadMsgOff()  { 
   Load.style.display="none";
   Database.style.display=""
}

// Determine what SVG is currently loaded and set the global
// document variables to correspond to the loaded page.
function getSVGLocation () {
		svgMapSrc = document.SVGMAP.src;
		locMapSrc = document.LOCMAP.src;

	if (svgMapSrc.indexOf("MCPHERSON_CO") != -1)
				{expLevel = 0;}
	else {
		if (locMapSrc.indexOf("MCPHERSON_CO") != -1)
						{expLevel = 2;}
		else
						{expLevel = 1;}
		}

	if (expLevel > 0) {
		var iT = svgMapSrc.indexOf("T");
		township = eval(svgMapSrc.substr (iT + 1, 2));
		var iR = svgMapSrc.indexOf("R");
		range = eval(svgMapSrc.substr (iR + 1, 1));
		var twrs = township + '-' + range + '-';
				var i = svgMapSrc.indexOf(twrs);
				if (i > -1) {
						var l = svgMapSrc.indexOf(".svg");
						var strS = svgMapSrc.substr(i + 5, l - (i + 5));
				section = eval(strS);
						}
		}
		showData ('Called from getSVGLocation');
}

// Change the currently highlighted tab and show the corresponding tab page
function setTab (tabID, divID) {
		var oldTab;
		var newTab;
                
            
                if (tabID.name == "SCH") {
                   
                   seeResult();
                   }
                else  {
                   
                   seeData();
                   }

		oldTab = './art/' + lastTab.name + '_INA.GIF';
		lastTab.src = oldTab;

		newTab = './art/' + tabID.name + '_ACT.GIF';
		tabID.src = newTab;

		lastDiv.style.display="none";
		divID.style.display="";

		lastTab = tabID;
		lastDiv = divID;
		return true;
}

// Display the current SVG Map location in the Location Map Information Box
function showLocation (template) {
		var infolabel = template;

		infolabel = infolabel.replace ('%1', township);
		infolabel = infolabel.replace ('%2', range);
		infolabel = infolabel.replace ('%3', section);

		LocInfo.innerHTML = infolabel;
		showData ('Called from getSVGLocation');
		return true;
}

// Function to load the specified SVG document in the MainMap Division
function loadSVGMap (mapId) {
		var mapHtml = svgMapTemplate;
		mapHtml = mapHtml.replace ("%1", mapId);
		MainMap.innerHTML = mapHtml;
		placeDivs ();
		return true;
}

//Function to load the specified SVG document in the Location Map Area
function loadLocMap () {
		parent.document.embeds['LOCMAP'].setAttribute ("SRC", locMapSrc);
		return true;
}

function hideError () {
	return true;
}

function showRaster () {

	window.onerror = hideError;

	//if (!optAerial) return true;

	var 	 thisMap = document.SVGMAP.getSVGDocument();

	// Check for invalid svgdoc variable
	if (thisMap == null) return true;

	var svgobj = thisMap.getElementById('rasterData');

	// Check for valid svgobj variable
	if (svgobj == null) {
		alert("*** No Raster Data! ***");
		return true;
	  }

	var svgstyle = svgobj.getStyle();
	if (optAerial)	{
		svgstyle.setProperty ('display', 'visible');
		svgstyle.setProperty ('visibility', 'visible');
		}
	else  {
		svgstyle.setProperty ('display', 'none');
		svgstyle.setProperty ('visibility', 'hidden');
		}

	return true;

}

// Function to display the provided SVG document
function showSVG (svgdoc, target_id, ovrColor) {
		// Check for invalid svgdoc variable
		if (svgdoc == null) return false;
		var count = 0;
		var svgobj = null;
		while ((svgobj == null) && (count < 10000)) {
		  svgobj = svgdoc.getElementById(target_id);
		  // Check for valid svgobj variable
		  count++;
		  }
		if (svgobj == null) {
		//alert("Invalid ID: **" + target_id + "**");
		return true;};
		var svgstyle = svgobj.getStyle();
		if (ovrColor) svgstyle.setProperty ('fill', mouse_over_fill);
		svgstyle.setProperty ('fill-opacity', mouse_over_opacity);
		svgstyle.setProperty ('stroke-width', mouse_over_width);
		if (ovrColor) svgstyle.setProperty ('stroke', mouse_over_stroke);
		return true;
}

// Function to display the provided SVG document
function hideSVG (svgdoc, target_id, ovrColor) {
		// Check for invalid svgdoc variable
		if (svgdoc == null) return false;
		var svgobj = svgdoc.getElementById(target_id);
		// Check for valid svgobj variable
		if (svgobj == null) return false;

		var svgstyle = svgobj.getStyle();
		if (ovrColor) svgstyle.setProperty ('fill', mouse_out_fill);
		svgstyle.setProperty ('fill-opacity', mouse_out_opacity);
		svgstyle.setProperty ('stroke-width', mouse_out_width);
		if (ovrColor) svgstyle.setProperty ('stroke', mouse_out_stroke);
		return true;
}

function showData (message) {

		return true;

		var textOutput = '<p class=MsoNormal>Expand Level:' + expLevel + '<br>';
		textOutput = textOutput + 'Section:' + section + '<br>';
		textOutput = textOutput + 'Township:' + township  + '<br>';
		textOutput = textOutput + 'Range:' + range + '<br>';
		textOutput = textOutput + 'Message:' + message + '</p>';

		OPT_DATA.innerHTML = textOutput;
}

// Function to retrun the correct SVG Document based on Mouseover status
function getSVGDoc () {
		if (mouseoverSVG)
				{var svgdoc = document.SVGMAP.getSVGDocument();}
	else
				{var svgdoc = document.LOCMAP.getSVGDocument();}
		return svgdoc;
}
