// Check if anything's supported
var supported=false;
if(document.createElement && document.write) 
 // This latter is a check for XHTML mode which
 // doesn't work for other reasons, not just document.write
{
  supported=true;  
}

// Either 800 or 1024
var extraWidth;

var jsOKInterval;
if(supported)
{
  document.write('<link rel="stylesheet" href="../jsenabled09.css" type="text/css" />');
  // Check screen size
  var size=/[?&]size=([0-9]+)/.exec(window.location);
  if(size && size[1]==1024)
  {
    extraWidth=200;
  }
  else if(size && size[1]==800)
  {
    extraWidth=0;
  }
  else
  {
    if(getWidth()>900) 
      extraWidth=200;
    else
      extraWidth=0;
  }

  if(extraWidth==200)
  {
    document.write('<link rel="stylesheet" href="../width1024.css" type="text/css" />');
  }  
}

function getWidth() 
{
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    // Netscape-compatible
    return window.innerWidth;
  } 
  else if( document.documentElement && document.documentElement.clientWidth) 
  {
    // IE 6+, 'standards compliant' mode
    return document.documentElement.clientWidth;
  } 
  else if( document.body && document.body.clientWidth) 
  {
    // IE 4
    return document.body.clientWidth;
  }
  return 0;
}

function findChild(node,name)
{
  var list=node.childNodes;
  for(var i=0;i<list.length;i++)
  {
    if(list.item(i).nodeName == name) return list.item(i);
  }
  zwargle;
}

function fixNoJS()
{
  var nojs=document.getElementById("nojs");
  if(nojs)
  {
    nojs.style.display="none";
    window.clearInterval(jsOKInterval);
  }
}

var iepc=
  (navigator.appVersion.indexOf("MSIE")!=-1) &&
  (navigator.appVersion.indexOf("Windows")!=-1);

var currentImg,currentInner,currentPlaceholder;

// IE-only code to handle lack of fixed support
///////////////////////////////////////////////

// Returns current Y position of the view, cross-browser (ugh!)
function getViewY() 
{
  // Mozilla
  if(window.pageYOffset || window.pageYOffset==0)
    return window.pageYOffset;

  // IE 5/6
  if(document.body.scrollTop || document.body.scrollTop==0) 
  {
    // IE 6
    if(document.documentElement.scrollTop || document.documentElement.scrollTop==0) 
      return document.body.scrollTop + document.documentElement.scrollTop;
    // IE 5
    else
      return document.body.scrollTop;
  }

  // Unknown browser
  return null;
}

// Returns height of the view
function getViewHeight()
{
  // Mozilla
  if(window && window.innerHeight)
    return window.innerHeight;

  // IE 5/6
  if(document.documentElement.clientHeight)
    return document.documentElement.clientHeight;
  if(document.body.clientHeight)
    return document.body.clientHeight;

  // Unknown browser
  return null;
}

var lastY;

// Fix scroll position for IE
function fixIEScroll()
{
  var viewY=getViewY();
  if(viewY==lastY) return;
  lastY=viewY;

  // Get rid of images when user scrolls
  if(currentImg) 
  {
    document.getElementById("pic").removeChild(currentImg);
    currentImg=null;
  }
}

// end IE-only code

// Called when the page loads
function loadedBody()
{
//  document.getElementById("nojs").style.display="none";

  // Show size options
  var sizeChooser=document.createElement("div");
  sizeChooser.setAttribute("id","sizelink");
  var currentSize,targetSize,targetNumber;
  if(extraWidth==200)
  {
    currentSize="full";
    targetSize="reduced";
    targetNumber="800";
  }
  else
  {
    currentSize="reduced";
    targetSize="full";
    targetNumber="1024";
  }
  
  sizeChooser.appendChild(document.createTextNode(currentSize+" size ("));
  var sizelink=document.createElement("a");
  sizeChooser.appendChild(sizelink);
  if(location.href.indexOf('index.html')!=-1)
    sizelink.setAttribute("href","index.html?size="+targetNumber);
  else
    sizelink.setAttribute("href","./?size="+targetNumber);
  sizelink.appendChild(document.createTextNode(
    "view at "+targetSize+" width"));
  sizeChooser.appendChild(document.createTextNode(")"));    
 
  document.body.appendChild(sizeChooser);

  if(iepc)
  {
    document.getElementsByTagName("h1").item(0).style.position="absolute";
    sizeChooser.style.position="absolute";
    setInterval(fixIEScroll,100);

    var picBG=document.createElement("div");
    picBG.style.position="absolute";
    picBG.style.left="0px";    
    picBG.style.width="434px";
    picBG.style.top="44px";
    picBG.style.height=document.getElementById("story").clientHeight+"px";
    picBG.style.backgroundColor="#909090";
    document.getElementsByTagName("body").item(0).appendChild(picBG);

    var plug=document.getElementById("plug");
    plug.style.position="absolute";
    plug.style.top="50px";    
  }
  
}

// Called when the image loads
function imageLoaded()
{
  currentPlaceholder.style.visibility="hidden";
  currentInner.style.visibility="visible";
}

// When user clicks to view a picture
function pictureClicked(id,w,h)
{
  // If browser doesn't support DOM, just follow link normally
  if(!supported)
  {
    return true;
  }
  
  // Get rid of plug
  document.getElementById("plug").style.display="none";
  
  if(extraWidth==0)
  {
    if(w>=h)
    {
      var newWidth=480;
	  h=(h*newWidth)/w;
	  w=newWidth;
    }
    else
    {
      var newHeight=400;
      w=(w*newHeight)/h;
      h=newHeight;
    }
  }

  var panel=document.getElementById("pic");
  panel.style.zIndex=5;

  // Handle lack of fixed support in IE/PC
  if(iepc)
  {
    panel.style.position="absolute";
    var start=getViewY();
    if(start < 28) start=28;
    panel.style.top=start+"px";
    panel.style.bottom="";//(getViewY()+getViewHeight())+"px";
    panel.style.height=getViewHeight();
  }

  // Replace image section
  if(currentImg) panel.removeChild(currentImg);
  currentImg=document.createElement("div");

  // Image itself (hidden until it loads)
  var img=document.createElement("img");
  currentImg.appendChild(img);
  currentInner=img;
  currentInner.style.visibility="hidden";
  img.style.position="absolute";
  img.style.top="10px";
  img.style.left="10px";
  img.style.width=w+"px";
  img.style.height=h+"px";
  img.style.border="2px solid black";

  // 'Loading' placeholder
  currentPlaceholder=document.createElement("div");
  currentPlaceholder.style.position="absolute";
  currentPlaceholder.style.top="10px";
  currentPlaceholder.style.left="10px";
  currentPlaceholder.style.width=(w-20)+"px";
  currentPlaceholder.style.height=(h-20)+"px";
  currentPlaceholder.style.zIndex="10";
  currentPlaceholder.style.border="2px solid black";
  currentPlaceholder.style.padding="20px 0 0 20px";
  currentPlaceholder.appendChild(
    document.createTextNode("Loading, please wait..."));
  currentImg.appendChild(currentPlaceholder);

  // Drop shadow

  var bottom=document.createElement("div");
  bottom.style.position="absolute";
  bottom.style.top=(h+4+10)+"px";
  bottom.style.left="30px";
  bottom.style.width=(w+4-20)+"px";
  bottom.style.height="20px";
  bottom.style.backgroundImage="url(../drop-b08.gif)";
  bottom.style.zIndex="5";
  currentImg.appendChild(bottom);

  var right=document.createElement("div");
  right.style.position="absolute";
  right.style.top="40px";
  right.style.left=(w+4+10)+"px";
  right.style.width="20px";
  right.style.height=(h+4-30)+"px";
  right.style.zIndex="6";
  right.style.backgroundImage="url(../drop-r08.gif)";
  currentImg.appendChild(right);

  var corner=document.createElement("img");
  corner.style.position="absolute";
  corner.style.top="20px";
  corner.style.left=(w+4+10)+"px";
  corner.style.zIndex="7";
  corner.src="../drop-tr08.gif";
  currentImg.appendChild(corner);

  corner=document.createElement("img");
  corner.style.position="absolute";
  corner.style.top=(h+4+10)+"px";
  corner.style.left=(w+4+10)+"px";
  corner.style.zIndex="8";
  corner.src="../drop-br08.gif";
  currentImg.appendChild(corner);

  corner=document.createElement("img");
  corner.style.position="absolute";
  corner.style.top=(h+4+10)+"px";
  corner.style.left="10px";
  corner.style.zIndex="9";
  corner.src="../drop-bl08.gif";
  currentImg.appendChild(corner);

  // Copy caption
  var caption=document.getElementById("c_"+id).cloneNode(true);
  currentImg.appendChild(caption);
  caption.style.position="absolute";
  caption.style.background="#f0f0f0";
  caption.style.display="block";
  caption.style.padding="4px 4px 4px 4px";
  caption.style.zIndex="15";
  
  // Is there room to put the caption at the right?
  var rightSpace=462+extraWidth-w;
  if(rightSpace > 100)
  {
    caption.style.left=w+25+"px";
    caption.style.top="0px";
    //caption.style.height=(h-4)+"px";
    caption.style.width=rightSpace+"px";
    caption.style.paddingTop="8px";    
  }
  else
  {
    caption.style.left="0px";
    caption.style.top=(h+4+30)+"px";
    caption.style.width=(476+extraWidth)+"px";
    caption.style.paddingLeft="14px";
    
    // Work out available height for caption box
    var availableHeight;
    if(iepc)
    { 
      availableHeight=getViewHeight()-h-55;
	  var start=getViewY();
      if(start < 28) availableHeight-=(28-start);
    }
    else
      availableHeight=getViewHeight()-h-82;
    if(availableHeight<50) availableHeight=50;
    
    if(!iepc)
      caption.style.maxHeight=availableHeight+"px";
    else
      caption.style.height=availableHeight+"px";
  
    caption.style.overflow="auto";
  }
 
  panel.appendChild(currentImg);  
  

  // Notice when image loads

  img.onload=imageLoaded;
  img.setAttribute("src",id+".jpg");

  // Don't follow link
  return false;
}


