// Port of Seattle JavaScript Document
// Phinney Bischoff Design House - www.pbdh.com


// stylesheet switcher by Paul Sowden/A List Apart
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return "default";
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return "default";
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  setFooter(); 
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}


// function to clear default text from search box
//
function clearText(thefield){
	if (thefield.defaultValue == thefield.value)
	thefield.value = ""
} 


// image popup script
// this works in conjunction with the file /include/popup.html
//
// to pop up an image that automatically sizes to the image size, call the function like so:
// <p><a href="javascript:imgPop('/images/business/realestate/Pier_481010.jpg','Pier%2048')"><img src="/images/business/realestate/Pier_481010_small.jpg" width="300" height="224" alt="Pier 48, Central Business District in Seattle, Washington" border="0" /></a></p>
// 
// of course, the image can also be popped up from a text link
// <p><a href="javascript:imgPop('/images/business/realestate/Pier_481010.jpg','Pier%2048')">View Photo</a></p>
//
// like any url, the value for the title should be encoded, e.g. %20 stands for a space.
// quotes and apostrophes must be encoded as well, or the script won't work ( quote: &quot; apostrophe: &#039; )
//
// note that some browsers are set to not allow window rezizing, so we start with a good generous size
//
function imagePop(imageUrl,windowTitle) {
	window.open( "/include/popup.html?"+imageUrl+"?"+windowTitle, "", "scrollbars=1,resizable=1,height=420,width=780" );
} 


// hack to allow IE6 to select text in absolutely-positioned div

if (window.createPopup && document.compatMode && document.compatMode=="CSS1Compat")
{
  document.onreadystatechange = onresize = function fixIE6AbsPos()
  {
    if (!document.body) return;
    if (document.body.style.margin != "0px") document.body.style.margin = 0;
    onresize = null;
    document.body.style.height = 0;
    setTimeout(function(){ document.body.style.height = document.documentElement.scrollHeight+'px'; }, 1);
    setTimeout(function(){ onresize = fixIE6AbsPos; }, 100);
  }
}


// determines height of columns and sets footer top
// the starting top is hard-coded, so there will be a little trouble 
// if the height of the pagetop stuff changes
//
function setFooter() {
	var topHeight = 266;

	var bodyDiv = document.getElementById('bodytext-content');
	maxHeight = bodyDiv.offsetHeight;

	var sidenavDiv = document.getElementById('sidelinks-content');
	if (sidenavDiv) {
		if (sidenavDiv.offsetHeight > maxHeight ) { maxHeight = sidenavDiv.offsetHeight; }
	}
	
	var sidebarDiv = document.getElementById('sidebar-content');
	if (sidebarDiv) {
		if (sidebarDiv.offsetHeight > maxHeight ) { maxHeight = sidebarDiv.offsetHeight; }
	}
	
	var footerDiv = document.getElementById('pagebottom');
	footerDiv.style.display = 'block'; // was 'none' in the stylesheet
	footerDiv.style.top = maxHeight + topHeight + 'px'; // was 2000px in the stylesheet
}
