var load_funcs=new Array();
var unload_funcs=new Array();

function register_onload_handler(func) {
    load_funcs.push(func);
}

function register_onunload_handler(func) {
    unload_funcs.push(func);
}

function body_onload() {
    rollover_setup();
	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") == "ext") 
			anchor.target = "_blank";
	}
    for(var i=0; i < load_funcs.length; ++i) {
        eval('load_funcs[i]()');
    }
}

function body_onunload() {
    for(var i=0; i < unload_funcs.length; ++i) {
        eval('unload_funcs[i]()');
    }
}

/* loosely based on youngpup.net's soopa-rollovers.js */

var imghash=new Array();

function rollover_setup() {
    var rlist=document.getElementsByTagName('img');
    for(var i=0; i<rlist.length; ++i) {
        var img=rlist[i];
        var id=img.id;
        if(!id || id.substring(0,9)!='rollover_') continue;
        var dp=img.src.lastIndexOf('.');
        imghash[id + '_h']=new Image();
        imghash[id + '_h'].src=img.src.substring(0,dp) + '_h' + img.src.substring(dp,img.src.length);
        imghash[id + '_n']=new Image();
        imghash[id + '_n'].src=img.src;
        img.onmouseover=rollover_on;
        img.onmouseout=rollover_off;
    }
}

function rollover_on() {
	this.src=imghash[this.id + '_h'].src;
}

function rollover_off() {
	this.src=imghash[this.id + '_n'].src;
}

function toggle_div(on_id) {
  if(on_id) {
    var on_elt=document.getElementById(on_id);
    on_elt.style.display='block';
  }
  for(var i=1; i<toggle_div.arguments.length; ++i) {
    var off_id=toggle_div.arguments[i];
    if(off_id) {
      var off_elt=document.getElementById(off_id);
      off_elt.style.display='none';
    }
  }
  return false;
}

function is_ie6() {
  var i=eval('navigator.appName.indexOf(\'Microsoft\')!=-1 && parseInt(navigator.appVersion)<7');
  return i;
}

// Automatically building and adding an iframe under the popup to work
// around the bug in IE that shows selects through overlaying divs
//
var popup_fmask;
function fmask_show(popup) {
  if(!is_ie6()) return;
  if(!popup_fmask) {
    popup_fmask=document.createElement('iframe');
    popup_fmask.style.position='absolute';
    popup_fmask.style.border='none';
    popup_fmask.style.visibility='hidden';

    // This takes care of the warning on https: pages because the       
    // default is "about:blank" which is considered a non-secure        
    // protocol in IE                                                   
    //
    if(location.protocol=='https:') popup_fmask.src='/blank.html';
  }
  popup.parentNode.appendChild(popup_fmask);
  popup_fmask.style.width=popup.offsetWidth+'px';
  popup_fmask.style.height=popup.offsetHeight+'px';
  popup_fmask.style.top=popup.style.top ? popup.style.top : (popup.offsetTop ? popup.offsetTop+'px' : '');
  popup_fmask.style.left=popup.style.left ? popup.style.left : (popup.offsetLeft ? popup.offsetLeft+'px' : '');
  popup_fmask.style.zIndex=popup.style.zIndex ? (popup.style.zIndex-1) : 1;
  popup_fmask.style.visibility='visible';
}

function fmask_hide() {
  if(popup_fmask) {
    popup_fmask.style.visibility='hidden';
    if(popup_fmask.parentNode) popup_fmask.parentNode.removeChild(popup_fmask);
  }
}

// Show/hide a pre-built popup with an automatic iframe mask under it
//
var shown_popup_id;
function popup_show(id,anim_len,anim_steps) {
  var popup=document.getElementById(id);
  if(!popup) return;

  if(shown_popup_id) popup_hide(shown_popup_id);

  /// if(anim_len) {
  ///   dprint("Doing the animation");
  ///   var w=popup.offsetWidth;
  ///   var h=popup.offsetHeight;
  /// }

  popup.style.display='block';
  shown_popup_id=id;
  fmask_show(popup);
  return false;
}

function popup_hide(id) {
  fmask_hide();
  document.getElementById(id).style.display='none';
  shown_popup_id=null;
  return false;
}

// Our handy dprint :)
// CSS:
// .logframe { position: absolute; top: 5px; left: 5px; width: 95%; height: 130px;
//             overflow: scroll; padding: .5em; font-size: 9pt; border: 1px solid #888;
//             background-color: #ffe; opacity: 0.90; filter: alpha(opacity=90) }
// (c) Andrew Maltsev

function dprint(text) {
  var elt=document.getElementById('logframe');
  if(!elt) {
    elt=document.createElement('div');
    elt.className='logframe';
    elt.style.position='absolute';
    elt.id='logframe';
    document.body.appendChild(elt);
  }
  elt.insertBefore(document.createElement('br'),elt.firstChild);
  elt.insertBefore(document.createTextNode(text),elt.firstChild);
}

// Automatic zooming into images of classes zoom\d+
// Needs some CSS support:
//  .zoomed { position: absolute; top: -50px; left: 10px; display: block; background: #fff; padding: 8px; opacity: 0.96; filter: alpha(opacity=96) }
//  .zoomed .inner { background: #ffe; border: 1px solid #aa8; padding: 10px }
//  .zoomed .close { border-top: 1px solid #aaa; margin-top: 0 }
// All that's needed in HTML is: <img class="zoom500" src="/images/foo-100.jpg">
// An image called /images/foo-500.jpg should also exist.
// (c) Andrew Maltsev

var zoom_active;

function zoom_hide() {
  if(!zoom_active) return;
  fmask_hide();
  document.body.removeChild(zoom_active);
  zoom_active=false;
  return false;
}

function zoom_show() {
  if(zoom_active) {
    zoom_hide();
  }

  var div=document.body;

  var zouter=document.createElement('div');
  zouter.className='zoomed';
  var zinner=document.createElement('div');
  zinner.className='inner';
  var close=document.createElement('div');
  close.className='close';
  var a=document.createElement('a');
  a.href='#';
  a.onclick=zoom_hide;
  a.appendChild(document.createTextNode('Close'));
  close.appendChild(a);

  var zimg=new Image();
  zimg.src=this.zsrc;

  zinner.appendChild(zimg);
  zinner.appendChild(close);
 
  zouter.appendChild(zinner);
  zouter.onclick=a.onclick;
  zouter.title='Click to close';
  zouter.style.zIndex=99;

  zoom_active=zouter;

  var width=zouter.offsetWidth;
  if(!width || width<this.zwidth+40) width=this.zwidth+40;
  zouter.style.top=((document.body.parentNode.scrollTop || 0)+100)+'px';
  zouter.style.left=(width>1000 ? 50 : Math.floor((1000-width)/2))+'px';

  div.insertBefore(zouter,div.firstChild);

  fmask_show(zouter);

  return false;
}

function zoom_setup() {
  var imgs=document.getElementsByTagName('img');
  for(var i=0; i<imgs.length; ++i) {
    var img=imgs[i];
  
    var clist=img.className.split(' ');
    var zwidth=false;
    for(var j=0; j<clist.length; ++j) {
      if(clist[j].substr(0,4)=='zoom' && !isNaN(parseInt(clist[j].substr(4)))) {
        zwidth=parseInt(clist[j].substr(4));
        break;
      }
    }
    if(!zwidth) continue;
  
    var zsrc=img.src;
    var zpos=zsrc.search(/-\d+\.\w+$/);
    if(zpos<0) continue;
    zsrc=zsrc.substr(0,zpos+1)+zwidth+zsrc.substr(zsrc.indexOf('.',zpos));

    img.zwidth=zwidth;
    img.zsrc=zsrc;
    img.style.cursor='pointer';
    img.onclick=zoom_show;
  }
}

var pageTracker;
function urchinTracker(uri) {
  if(pageTracker) pageTracker._trackPageview(uri);
}
