// Global variables for sothai.com
var strCaption;

// Global functions for sothai.com

// Generic onload function
window.onload = function () {
  SetupGlobe();
  AdjustWidth();
  SetupPhotos();
}

function SetupGlobe() {
  var el = document.getElementById("globe");
  if (el == undefined) return;
  // attach event handlers to image, not link
  el = getFirstChildImg(el);
  if (el == undefined) return;
  el.onmouseover = ShowClearMouseover;
  el.onmouseout = ShowClearMouseover;
  el.onclick = ShowClearMouseover;
  // data structure: blnPersist|restoreSrc
  el.setAttribute("parstorage", "F|" + el.src);
}

function AdjustWidth() {
  var el = document.getElementById("picpg");
  if (el == undefined) return;
  if (el.className.indexOf("col3") < 0) return;
  // Check for extra available width for thumbnails
  var avail = document.getElementsByTagName("body")[0].offsetWidth - el.offsetLeft;
  if (avail >= 500) el.style.width = "500px";
  if (avail >= 750) el.style.width = "750px";
}

function SetupPhotos() {
  var el, els, i;
  els = getElementsByClassName("div.thumbs", "");
  if (els.length == 0) return;
  // add event handlers to parent divs, event bubbles up
  for (i=0; i<els.length; i++) els[i].onclick = ShowPhoto;
}

// Expand-O-Globe Functions
function ShowClearMouseover(e) {
  // find target
  if (!e) var e = window.event;
  if (e.target) var el = e.target;
  else if (e.srcElement) var el = e.srcElement;
  // retrieve any stored parameters
  // data structure: blnPersist|restoreSrc
  var par = el.getAttribute("parstorage");
  if (par == "undefined") {
    par = "";
    el.setAttribute("parstorage", par);
  }
  // handle event
  if (e.type == "mouseover") {
    // If not persistently modified, switch to href image
    if (par.substring(0,2) == "F|") {
      el.src = el.parentNode.href;
    }
  }
  if (e.type == "mouseout") {
    // If not persistently modified, switch back to original src
    if (par.substring(0,2) == "F|") el.src = par.substring(2);
  }
  if (e.type == "click") {
    // Toggle between images, set persistent with href image
    if (par.substring(0,2) == "F|") {
      el.src = el.parentNode.href;
      el.setAttribute("parstorage", "T|" + par.substring(2));
      return false;
    } else {
      el.src = par.substring(2);
      el.setAttribute("parstorage", "F|" + par.substring(2));
      return false;
    }
  }
}

// Photo functions
function ShowPhoto(e) {
  var e, el, doc, targ
  // find target (target is image in <a>, not the <a> itself)
  if (!e) e = window.event;
  if (e.target) el = e.target;
  else if (e.srcElement) el = e.srcElement;
  // handle event
  if (e.type == "click" && el.nodeName.toUpperCase() == "IMG"
      && el.parentNode.nodeName.toUpperCase() == "A") {
    // Update page in iframe
    doc = getNamedIFrame(el.parentNode.getAttribute("target")) || 
      getSibIFrameDoc(el);  // Returns named or nearest sibling iframed doc
    if (doc == "null") return true; // Execute default link action
    strCaption = el.getAttribute("alt"); // Read by the iframe
    doc.location.replace(el.parentNode.href); //Navigate iframe w/o history
    return false;   // cancel the default link action
  }
}
// This powers the "^ Taller" control
function resize(par) {
  var pDiv=par.parentNode.parentNode;
  pDiv.getElementsByTagName('IFRAME')[0].style.height='700px';
  pDiv.getElementsByTagName('DIV')[0].style.height='700px';
  par.parentNode.style.display='none';  
}

// Utility functions
function getFirstChildImg(aNode) {
  var imgs = aNode.getElementsByTagName("img");
  if (imgs.length == 0) return;
  else return imgs[0];
}
function getElementsByClassName(strClass, strID) {
  var strTagName, strClassName, els, i, j
  if (strClass.indexOf(".") > 0) {   // e.g., div.myclass
    strTagName = strClass.split(".")[0];
    strClassName = strClass.split(".")[1];  
  } else {                          // e.g., myclass
    strTagName = "*";
    strClassName = strClass;
  }
  var elArray = new Array();
  if (strID.length > 0) { // limited to children of element
    els = document.getElementById(strID).getElementsByTagName(strTagName);
  } else {                // global
    els = document.getElementsByTagName(strTagName);
  }
  for (i=els.length-1; i>=0; i--) { // collect els that match
    if (els[i].className.indexOf(strClassName) > -1) j = elArray.push(els[i]);
  }
  return elArray;
}
function getNamedIFrame(sName) { // locate iframe by name
  var ifs, i; 
  ifs = document.getElementsByTagName("iframe");
  for (i=0;i<ifs.length;i++) {
    if (ifs[i].getAttribute("name") == sName) return getIFrameDoc(ifs[i]);
  }
  return null;
}
function getSibIFrameDoc(aNode) {
  var sibs, i
  while (aNode.nodeName.toUpperCase() != "BODY") {
    sibs = aNode.parentNode.childNodes;
    for (i=0; i<sibs.length; i++) {
      if (sibs[i].nodeName.toUpperCase() == "IFRAME") {
        return getIFrameDoc(sibs[i]);
      }
    }
    aNode = aNode.parentNode;
  }
  return null;
}
function getIFrameDoc(aNode) {
  if (aNode.contentDocument) return aNode.contentDocument;
  if (aNode.contentWindow) return aNode.contentWindow.document;
  if (aNode.document) return aNode.document;
  return null;
}

