var persisteduls=new Object()
var treemenu=new Object()
treemenu.closefolder="submenuclosed" 
treemenu.openfolder="submenuopen" 
var scrollStep=10;
var timerLeft="";
var timerRight="";
var pageTimer=new Date();
//-----------------------------------------------------------------------------------
function getWinWidth()
{
  var wdt=0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    wdt = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    wdt = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    wdt = document.body.clientWidth;
  }
	return wdt;
}
//-----------------------------------------------------------------------------------
function getWinHeight() {
  var hgt = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    hgt = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    hgt = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    hgt = document.body.clientHeight;
  }
	return hgt;
}
//-----------------------------------------------------------------------------------
function openBareWindow(strLink, wdt, hgt)
{
	var winParam = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,fullscreen=1,resizable=0,screenX=0,left=0,screenY=0";
	var newWin=window.open(strLink, 'barewindow', winParam);
	
//	newWin.moveTo(window.screenX, window.screenY);
	newWin.resizeTo(wdt == 0 ? screen.availWidth : wdt,hgt == 0 ? screen.availHeight : hgt);
	if ((wdt==0) && (hgt==0))
	{
		if (newWin.outerHeight<screen.availHeight | newWin.outerWidth<screen.availWidth)
		{
			newWin.outerHeight = screen.availHeight;
			newWin.outerWidth = screen.availWidth;
		}
	}
}		
//-----------------------------------------------------------------------------------
function fixHeights(c1, c2) 
{
	if (document.getElementById(c1)) 
	{
		var c1h=document.getElementById(c1).offsetHeight;
		var c2h=document.getElementById(c2).offsetHeight;
		var mxh = Math.max(c1h, c2h);
		document.getElementById(c1).style.height=mxh+"px";
		document.getElementById(c2).style.height=mxh+"px";
	}
}
//-----------------------------------------------------------------------------------
function fixHeights3(c1, c2, c3) 
{
	if (document.getElementById(c1)) 
	{
		var c1h=document.getElementById(c1).offsetHeight;
		var c2h=document.getElementById(c2).offsetHeight;
		var c3h=document.getElementById(c3).offsetHeight;
		var mxh = Math.max(c1h, c2h);
		mxh = Math.max(mxh, c3h); 
		document.getElementById(c1).style.height=mxh+"px";
		document.getElementById(c2).style.height=mxh+"px";
		document.getElementById(c3).style.height=mxh+"px";
	}
}
//-----------------------------------------------------------------------------------
function addToFavorites(title)
{
	if (document.all)
		window.external.AddFavorite("http://www.petersomos.com/",title);
	else if (window.sidebar) 
		window.sidebar.addPanel(title, "http://www.petersomos.com/", "");
}
//-----------------------------------------------------------------------------------
function ManageDivSwap() 
{
	if (arguments.length < 2) return;
    document.getElementById(arguments[0]).style.display = "block";
	for (var i = 1; i < arguments.length; i++) 
	{
		document.getElementById(arguments[i]).style.display = "none";
	}

}
//-----------------------------------------------------------------------------------
function toggleDisplay(div, controldiv, hiddencontrolclass, showncontrolclass)
{
    element = document.getElementById(div).style;
    controlelement = document.getElementById(controldiv).style;
    if (element.display == 'none')
	{
		element.display = 'block';
		controlelement.className = showcontrolclass;
	}
	else
	{
		element.display = 'none';
		controlelement.className = hiddencontrolclass;
	}
}
//-----------------------------------------------------------------------------------
function createCookie(name,value,days) 
{ 
	var expireDate = new Date()
	var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
	document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}
//-----------------------------------------------------------------------------------
function readCookie(name) 
{ 
	var re=new RegExp(name+"=[^;]+", "i"); 
	if (document.cookie.match(re)) 
		return document.cookie.match(re)[0].split("=")[1]; 
	return "";
}
//-----------------------------------------------------------------------------------
treemenu.createTree=function(treeid, enablepersist, persistdays)
{
	var ultags=document.getElementById(treeid).getElementsByTagName("ul")
	if (typeof persisteduls[treeid]=="undefined") persisteduls[treeid]=(enablepersist==true && readCookie(treeid)!="")? readCookie(treeid).split(",") : ""
	for (var i=0; i<ultags.length; i++)	treemenu.buildSubTree(treeid, ultags[i], i)
	if (enablepersist==true)
	{ //if enable persist feature
		var durationdays=(typeof persistdays=="undefined")? 1 : parseInt(persistdays)
		treemenu.dotask(window, function(){treemenu.rememberstate(treeid, durationdays)}, "unload") //save opened UL indexes on body unload
	}
}
//-----------------------------------------------------------------------------------
treemenu.buildSubTree=function(treeid, ulelement, index)
{
	ulelement.parentNode.className="submenu"
	if (typeof persisteduls[treeid]=="object")
	{ //if cookie exists (persisteduls[treeid] is an array versus "" string)
		if (treemenu.searcharray(persisteduls[treeid], index))
		{
			ulelement.setAttribute("rel", "open")
			ulelement.style.display="block"
			ulelement.parentNode.className=treemenu.openfolder
		}
		else
			ulelement.setAttribute("rel", "closed")
	} //end cookie persist code
	else if (ulelement.getAttribute("rel")==null || ulelement.getAttribute("rel")==false) //if no cookie and UL has NO rel attribute explicted added by user
		ulelement.setAttribute("rel", "closed")
	else if (ulelement.getAttribute("rel")=="open") //else if no cookie and this UL has an explicit rel value of "open"
		treemenu.expandSubTree(treeid, ulelement) //expand this UL plus all parent ULs (so the most inner UL is revealed!)

	ulelement.parentNode.onclick=function(e)
	{
		var submenu=this.getElementsByTagName("ul")[0]
		if (submenu.getAttribute("rel")=="closed")
		{
			submenu.style.display="block"
			submenu.setAttribute("rel", "open")
			ulelement.parentNode.className=treemenu.openfolder
		}
		else if (submenu.getAttribute("rel")=="open")
		{
			submenu.style.display="none"
			submenu.setAttribute("rel", "closed")
			ulelement.parentNode.className=treemenu.closefolder
//			submenu.className=treemenu.closefolder
		}
		treemenu.preventpropagate(e)
	}
	ulelement.onclick=function(e)
	{
		treemenu.preventpropagate(e)
	}
}
//-----------------------------------------------------------------------------------
treemenu.expandSubTree=function(treeid, ulelement)
{ //expand a UL element and any of its parent ULs
	var rootnode=document.getElementById(treeid);
	var currentnode=ulelement;
	currentnode.style.display="block";
	currentnode.parentNode.className=treemenu.openfolder;
	while (currentnode!=rootnode)
	{
		if (currentnode.tagName=="UL")
		{ //if parent node is a UL, expand it too
			currentnode.style.display="block";
			currentnode.setAttribute("rel", "open"); //indicate it's open
			currentnode.parentNode.className=treemenu.openfolder;
		}
		currentnode=currentnode.parentNode;
	}
}
//-----------------------------------------------------------------------------------
treemenu.flatten=function(treeid, expand)
{ //expand or contract all UL elements
	var ultags=document.getElementById(treeid).getElementsByTagName("ul")
	for (var i=0; i<ultags.length; i++)
	{
		ultags[i].style.display=expand ? "block" : "none"
		var relvalue=expand ? "open" : "closed"
		ultags[i].setAttribute("rel", relvalue)
		ultags[i].parentNode.className=expand ? treemenu.openfolder : treemenu.closefolder
	}
}
//-----------------------------------------------------------------------------------
treemenu.rememberstate=function(treeid, durationdays)
{ //store index of opened ULs relative to other ULs in Tree into cookie
	var ultags=document.getElementById(treeid).getElementsByTagName("ul")
	var openuls=new Array()
	for (var i=0; i<ultags.length; i++)
	{
		if (ultags[i].getAttribute("rel")=="open")
		openuls[openuls.length]=i //save the index of the opened UL (relative to the entire list of ULs) as an array element
	}
	if (openuls.length==0) //if there are no opened ULs to save/persist
	openuls[0]="none open" //set array value to string to simply indicate all ULs should persist with state being closed
	createCookie(treeid, openuls.join(","), durationdays) //populate cookie with value treeid=1,2,3 etc (where 1,2... are the indexes of the opened ULs)
}
//-----------------------------------------------------------------------------------
treemenu.searcharray=function(thearray, value)
{ //searches an array for the entered value. If found, delete value from array
	var isfound=false;
	for (var i=0; i<thearray.length; i++)
	{
		if (thearray[i]==value)
		{
			isfound=true;
			thearray.shift(); //delete this element from array for efficiency sake
			break;
		}
	}
	return isfound;
}
//-----------------------------------------------------------------------------------
treemenu.preventpropagate=function(e)
{ //prevent action from bubbling upwards
	if (typeof e!="undefined") e.stopPropagation()
		else event.cancelBubble=true
}
//-----------------------------------------------------------------------------------
treemenu.dotask=function(target, functionref, tasktype)
{ //assign a function to execute to an event handler (ie: onunload)
	var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
	if (target.addEventListener) target.addEventListener(tasktype, functionref, false)
		else if (target.attachEvent) target.attachEvent(tasktype, functionref)
}
//-----------------------------------------------------------------------------------
function toLeft(id)
{
  	document.getElementById(id).scrollLeft=0;
}
//-----------------------------------------------------------------------------------
function scrollDivLeft(id)
{
  	clearTimeout(timerRight) ;
  	document.getElementById(id).scrollLeft+=scrollStep;
  	timerRight=setTimeout("scrollDivLeft('"+id+"')",10);
}
//-----------------------------------------------------------------------------------
function scrollDivRight(id)
{
  	clearTimeout(timerLeft)
  	document.getElementById(id).scrollLeft-=scrollStep;
  	timerLeft=setTimeout("scrollDivRight('"+id+"')",10)
}
//-----------------------------------------------------------------------------------
function toRight(id)
{
  	document.getElementById(id).scrollLeft=document.getElementById(id).scrollWidth;
}
//-----------------------------------------------------------------------------------
function stopMe()
{
 	clearTimeout(timerRight); 
  	clearTimeout(timerLeft);
}
//-----------------------------------------------------------------------------------
function setImageoftheDayWidth(c, offset) 
{
	if (document.getElementById(c)) 
	{
		var children=document.getElementById(c).getElementsByTagName("div")
		var wdt = 0;
		for (var i=0; i<children.length; i+=2) 
		{
			obj=document.getElementById(c).getElementsByTagName("div")[i];
			wdt+=obj.offsetWidth
		}
		document.getElementById(c).style.width=wdt+offset+"px";
	}
}
//-----------------------------------------------------------------------------------
document.onreadystatechange=function() 
{
	if (document.readyState=="complete") 
	{
    	if (sp=document.getElementById("debugpagetimerdisplay")) 
			sp.innerText=((new Date() - pageTimer)/1000) + " sec";
	}
}
//-----------------------------------------------------------------------------------
function externalLinks() 
{ 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) 
	{ 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
			anchor.target = "_blank"; 
	} 
} 
//-----------------------------------------------------------------------------------
function doGMapUnload(map)
{
	if (map)
	{
		var cookieStr = '';
		var center = map.getCenter();
	
		cookieStr += 'lat=' + center.y + ',';
		cookieStr += 'lon=' + center.x + ',';
		cookieStr += 'z=' + map.getZoom(); // + ',';
//		cookieStr += 'mType=' + mType + ',';
	
		createCookie('pgmap',cookieStr, 365);
	}
	GUnload();
}
//-----------------------------------------------------------------------------------
function getMapState(map, lat, lon, zoom, mType) 
{
	var mapState=new Array();
	qString=getCookie('pgmap');
	separator = ',';
	if (qString) 
	{
		var nvPairs = qString.split(separator);
		for (var n = 0 ; n < nvPairs.length ; n++ )	
		{
			var nv = nvPairs[n].split('=');
			mapState[nv[0]] = nv[1];
		}
		if (nvPairs.length>0)
		{
			centerPoint = new GLatLng(parseFloat(mapState['lat']), parseFloat(mapState['lon']));
			if (mapState['z']) 
			{
				zoom = parseInt(mapState['z']);
			}
			if (mapState['mType']) 
			{
				mType = parseInt(mapState['mType']);
			}
		}
	}
	else
	{
		centerPoint = new GLatLng(lat, lon);
	}
	map.setCenter(centerPoint, zoom);
	map.setMapType(map.getMapTypes()[mType]);
}
//-----------------------------------------------------------------------------------
function getCookie(cookieName) 
{
	if (document.cookie.length > 0) 
	{
		var cIndex = document.cookie.indexOf(cookieName+"=");
		if (cIndex != -1) 
		{
			cIndex += cookieName.length + 1;
			var cLength = document.cookie.indexOf(";", cIndex);
			if (cLength == -1) 
			{
				cLength = document.cookie.length;
			}
			return unescape(document.cookie.substring(cIndex, cLength)); 
		}
	}
	return null;
}
//-----------------------------------------------------------------------------------
