window.onload = function(e) {
  var cookie = readCookie("pfstyle");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  setupAll();
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("pfstyle", title, 365);
}





// SET UP EVERYTHING!

function setupAll()
{
	
	// stylesheet event listeners
	
	if(window.addEventListener)
	{
	document.getElementById('styleswitchZoom').addEventListener("click", setActiveStyleSheetZoom, false);
	document.getElementById('styleswitchNorm').addEventListener("click", setActiveStyleSheetNorm, false);
	
	}
	else if (window.attachEvent)
	{
	document.getElementById('styleswitchZoom').attachEvent("onclick", setActiveStyleSheetZoom);
	document.getElementById('styleswitchNorm').attachEvent("onclick", setActiveStyleSheetNorm);
	}
	else
	{
		return null;
	}
	
	// set up google maps
	
	if(document.getElementById("map")) {gmapLoad();}
	
	// set up homepage image swap
	
	if(document.getElementById("imgswap")) {changeimg();}
	
	// set up beautiful world
	
	if(document.getElementById("beautMap")) {beautWorld();}
}

// END OF SET UP EVERYTHING!




// STYLESWITCHER STUFF

function setActiveStyleSheetZoom() {
	 setActiveStyleSheet('nostyles');
}

function setActiveStyleSheetNorm() {
	 setActiveStyleSheet('default');
}

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 null;
}

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 null;
}

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;
}

var cookie = readCookie("pfstyle");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


// ------------------------------------------------------------------


// BEAUTIFUL WORLD

function beautWorld()
{
// START set up scripts for map clicking - add events for all AREA tags on page and cancel default click action
var allAreaTags = document.getElementsByTagName('area');
var arrayLength = allAreaTags.length;
for (var i = 0; i < arrayLength; i++)
{
var thisAreaId = allAreaTags[i].id;
var thisArea = document.getElementById(thisAreaId);
if(window.addEventListener)
	{
	document.getElementById(thisAreaId).addEventListener( "click", showDetails, false);
	document.getElementById(thisAreaId).addEventListener( "click", cancelClick, false);
	}
	else if (window.attachEvent)
	{
	document.getElementById(thisAreaId).attachEvent("onclick", showDetails);
	document.getElementById(thisAreaId).attachEvent("onclick", cancelClick);
	}
	else
	{
		return null;
	}
	document.getElementById(thisAreaId).onclick = cancelClickSafari;
}
// END set up scripts for map clicking
}
// ------------------------------------------------------------------
function showDetails(e)
{
  var target = findTargetImageMap(e);
 var theurl = "beautiful_world_files/"+target.id+".html"
 newwindow=window.open(theurl,"popup","height=450,width=470,status=no,toolbar=no,menubar=no,location=no,resizable=yes");

	return false;
}

// ------------------------------------------------------------------
// STOP LINKS FROM GOING ANYWHERE - 2 functions here!
function cancelClick(e)
{
  if (window.event)
  {
	  window.event.returnValue = false;
  }
  if (e && e.preventDefault)
  {
	  e.preventDefault();
  }
}
function cancelClickSafari()
{
	return false;
}
// ------------------------------------------------------------------
// FIND OUT THE TARGET ON IMAGEMAP LINKS
function findTargetImageMap(e)
{
var target;
	if (window.event && window.event.srcElement)
	target = window.event.srcElement;
	else if (e && e.target)
	target = e.target;
	if(!target)
	return null;
	
	while (target != document.body && target.nodeName.toLowerCase() !='area')
	target = target.parentNode;
	
	if (target.nodeName.toLowerCase() != 'area')
	return null;
	
	return target;
}
// ------------------------------------------------------------------


// HP IMAGE SWAP

var startcount=(Math.round((Math.random()*19)+1));
function changeimg()
{
var imgdelay=10000;
window.setTimeout( "changeimg()", imgdelay);
document.getElementById("imgswap").src="img/hp_photos/"+startcount+".jpg";
startcount=(Math.round((Math.random()*19)+1));
}
