//
// MaineTownship.com JavaScript
// Copyright © 2007 Maine Township
//

function show(obj,layer)
{
	var l;
	if(document.all) {
		l=document.all[layer];
	}
	else if(document.getElementById) {
		l=document.getElementById(layer);
	}
	//alert("Top:" + getY(obj) + ",Left:" + getX(obj));
	l.style.top = getY(obj)+"px";
	l.style.left = getX(obj)+"px";
	l.style.display = 'block';
	//alert("Top:"+l.style.top+",Left:"+l.style.left);
}

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
	iReturnValue += oElement.offsetTop;
	oElement = oElement.offsetParent;
	}
return iReturnValue;
}

function getX( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
	iReturnValue += oElement.offsetLeft;
	oElement = oElement.offsetParent;
	}
return iReturnValue;
}

// user-agent identity
	var version = parseInt(navigator.appVersion);
	var isNS  = (navigator.appName.indexOf('Netscape') >= 0);
	var isNS4 = (isNS && version == 4);
	var isNS5 = (isNS && version > 4);
	var isFF  = (navigator.userAgent.indexOf('Firefox') >= 0);
	var isIE  = !isNS;
	var isIE4 = (isIE && version == 4);
	var isIE5 = (isIE && version > 4);
	var isMac = (navigator.appVersion.indexOf('Macintosh') >= 0);
	var isWin = !isMac;
	var isAOL = (navigator.userAgent.indexOf('AOL') >= 0);

// events
	var clickURL;
	var mouseX = 0;
	var mouseY = 0;
	var pageWidth = 0;
	var pageHeight = 0;
	
// settings
	var menuDelay = 100;
	var allowFrame = true;

// page tools
	var windowArray = new Array();
	var pageURL = (window != top) ? document.referrer : location.href;
	var emailURL = '/utilities/email.html?title=%%title%%&url=%%url%%';
	var printURL = '/utilities/print.html?id=%%id%%';
	var similarURL = '/utilities/search.html?similar=%%url%%';
	
// cookie management
	function readCookie(name)
	{
		var cookieArray = document.cookie.split('; ');
		for (var i = 0; i < cookieArray.length; i++)
		{
			var nameValueArray = cookieArray[i].split('=', 2);
			if (nameValueArray[0] == name) return(nameValueArray[1]);
		}
		return(null);
	}

	function writeCookie(name, value, expires)
	{
		if (expires)
		{
			document.cookie = name + '=' + value + '; expires=' + expires.toGMTString();
		}
		else
		{
			document.cookie = name + '=' + value;
		}
	}

// channel load
	function channelLoad()
	{
		if (window.advancePanel) advancePanel();
	}

// event handlers
	function pageLoad(evt)
	{
		// frame break
		if (window != top && allowFrame != true) top.location.replace(location.href);
	}

	function pageUnload(evt)
	{
		// close child windows
		for (var i = 0; i < windowArray.length; i++)
		{
			if (typeof windowArray[i] == 'object' && !windowArray[i].closed) windowArray[i].close();
		}

		if (window.channelUnload) channelUnload();
	}

	function mouseOver(evt)
	{
		obj = (evt.target) ? evt.target : evt.srcElement;
		if (obj.over) obj.src = obj.over.src;
	}

	function mouseOut(evt)
	{
		obj = (evt.target) ? evt.target : evt.srcElement;
		if (obj.out) obj.src = obj.out.src;
	}

	function mouseClick(evt)
	{
		if (isNS4)
		{
			clickURL = evt.target.href;
		}
		else if (isNS5)
		{
			node = evt.target;
			while (node.tagName != 'A' && node.tagName != 'HTML')
			{
				node = node.parentNode;
			}
			clickURL = node.href;
		}
		else if (isIE)
		{
			element = evt.srcElement;
			while (element.tagName != 'A' && element.tagName != 'HTML')
			{
				element = element.parentElement;
			}
			clickURL = element.href;
		}

		if (clickURL == null) clickURL = '';
	}

	function addMouseEvents(obj)
	{
		if (isNS5)
		{
			obj.addEventListener('mouseover', mouseOver, false);
			obj.addEventListener('mouseout', mouseOut, false);
		}
		if (isIE)
		{
			obj.detachEvent('onmouseover', mouseOver);
			obj.attachEvent('onmouseover', mouseOver);
			obj.detachEvent('onmouseout', mouseOut);
			obj.attachEvent('onmouseout', mouseOut);
		}
	}

// event capturing
	if (isNS4)
	{
		window.captureEvents(Event.ONLOAD);
		window.onLoad = pageLoad;
		window.captureEvents(Event.ONUNLOAD);
		window.onUnload = pageUnload;
		document.captureEvents(Event.CLICK);
		document.onClick = mouseClick;
	}

	if (isNS5)
	{
		window.addEventListener('load', pageLoad, false);
		window.addEventListener('unload', pageUnload, false);
		document.addEventListener('click', mouseClick, false);
	}

	if (isIE)
	{
		window.attachEvent('onload', pageLoad);
		window.attachEvent('onunload', pageUnload);
		document.attachEvent('onclick', mouseClick);
	}

// mouseovers; incompatible with NS4
	//
	// Usage:
	// <a href="LINK"><img src="IMAGE.EXT" onload="mouseLoad(this);" width="WIDTH" height="HEIGHT" title="ALT" /></a>
	// 
	// Note: mouseover image must be named IMAGE_over.EXT
	//
	function mouseLoad(obj)
	{
		if (isNS4 || obj.out) return;
		
		obj.out = new Image();
		obj.out.src = obj.src;
		obj.over = new Image();
		obj.over.src = obj.src.replace(/.gif$/, '_over.gif').replace(/.jpg$/, '_over.jpg');
		
		addMouseEvents(obj);
	}

// page tools
	//
	// email this page
	// Usage:
	// <a href="javascript:emailPage();">LINK</a>
	//
	function emailPage(url)
	{
		if (url != null)
		{
			windowArray[windowArray.length] = window.open(url);
		}
		else
		{			
			thisURL = emailURL.replace('%%title%%', escape(document.title)).replace('%%url%%', escape(pageURL));
			windowArray[windowArray.length] = window.open(thisURL, 'emailWindow', 'width=520,height=350,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0');
		}
	}

	//
	// print this page
	// Usage:
	// <a href="javascript:printPage(ID);">LINK</a>
	//
	function printPage(id)
	{
		windowArray[windowArray.length] = window.open(printURL.replace('%%id%%', id), 'printWindow', 'width=600,height=400,toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1');
	}
	
	//
	// bookmark this page (compatible with IE only)
	// Usage:
	// <a href="javascript:bookmarkPage();">LINK</a>
	//
	function bookmarkPage()
	{
		if (isMac || isAOL)
		{
			alert('Sorry, this feature is not supported by your browser.');
			return;
		}

		if (!isNS)
		{
			window.external.AddFavorite(this.location.href, document.title);
		}
		else
		{
			alert('Click "OK", then type CTRL-D to add this page to your list of bookmarks.');
		}
	}
	
	//
	// set as homepage (compatible with IE only)
	// Usage:
	// <a href="javascript:setHomepage();">LINK</a>
	//
	function setHomepage()
	{
		if (!isIE || isAOL)
		{
			alert('Sorry, this feature is not supported by your browser.');
			return;
		}
	
		document.body.style.behavior = 'url(#default#homePage)';
		document.body.setHomePage(pageURL);
	}

	//
	// adjust font size (incompatible with NS4)
	// Usage:
	// <a href="javascript:fontSize(-1|+1);">LINK</a>
	//
	function fontSize(amt)
	{
		if (isNS4)
		{
			alert('Sorry, this feature is not supported by your browser.');
			return;
		}

		if (amt == 1)
		{
			
		}
		else if (amt == -1)
		{
			
		}
	}

// panel settings	
	var panelCount = 4;
	var panelDelay = 6;

	var currentPanel = 1;
	var panelTimeout;

	function showPanel(panelNumber)
	{
		for (var panelIndex = 1; panelIndex <= panelCount; panelIndex++)
		{
			var myPanel = document.getElementById('panel_' + panelIndex);
			if (panelIndex == panelNumber)
			{
				currentPanel = panelIndex;
				myPanel.style.display = 'inline';
			}
			else
			{
				myPanel.style.display = 'none';
			}
		}

		pausePanel();
	}
	
	function advancePanel()
	{
		nextPanel();
		panelTimeout = setTimeout("advancePanel()", panelDelay * 1000);
	}
	
	function nextPanel()
	{
		currentPanel++;
		if (currentPanel > panelCount) currentPanel = 1;
		showPanel(currentPanel);
	}

	function previousPanel()
	{
		currentPanel--;
		if (currentPanel == 0) currentPanel = panelCount;
		showPanel(currentPanel);
	}
	
	function pausePanel()
	{
		if (panelTimeout) clearTimeout(panelTimeout);
	}
	
// IE hack for dropdown menus
    startList = function() {
			if (document.all&&document.getElementById) {
			navRoot = document.getElementById("nav");
			for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
			node.onmouseover=function() {
			this.className+=" over";
			  }
			  node.onmouseout=function() {
			  this.className=this.className.replace(" over", "");
			   }
			   }
			  }
			 }
			}
			window.onload=startList;
