var init_funcs=new Array();

function register_onload_handler(func) {
    init_funcs.push(func);
}

function init() {
    rollover_setup();

    eval('zoom_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 < init_funcs.length; ++i) {
        init_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;
}

// 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.appendChild(document.createTextNode(text));
  elt.appendChild(document.createElement('br'));
}

// 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;
  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;
  }
}

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_fmask.style.left=popup.style.left;
  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) {
  var popup=document.getElementById(id);
  if(!popup) return;

  if(shown_popup_id) popup_hide(shown_popup_id);

  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;
}

function blanket_show() {
    var vph;
    var blanketHeight;

	if(typeof window.innerWidth != 'undefined') vph = window.innerHeight;
	else vph = document.documentElement.clientHeight;

	if((vph > document.body.parentNode.scrollHeight) && (vph > document.body.parentNode.clientHeight))
		blanketHeight = vph;
	else if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight)
        blanketHeight = document.body.parentNode.clientHeight;
    else
        blanketHeight = document.body.parentNode.scrollHeight;

	var blanket=document.getElementById('blanket');
    if(blanket==null) {
        blanket=document.createElement('div');
        blanket.id="blanket";
        document.body.insertBefore(blanket,document.body.firstChild);
    }
	blanket.style.height=blanketHeight + 'px';
    blanket.style.display="block";
}

function blanket_hide() {
	var blanket=document.getElementById('blanket');
    if(blanket!=null)
        blanket.style.display="none";
}

function new_window(url,title,w,h,scroll,pos){
  var pleft;
  var ptop;
  if(pos=="random") {
    pleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
    ptop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
  }
  else if(pos=="center") {
    pleft=(screen.width)?(screen.width-w)/2:100;
    ptop=(screen.height)?(screen.height-h-20)/2:100;
  }
  else {
    pleft=100;
    ptop=100;
  }
  if(pleft<0) pleft=0;
  if(ptop<0) ptop=0;
  var settings='width='+w+',height='+h+',top='+ptop+',left='+pleft+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
  var win;
  eval('win=window.open(url,title,settings);win.focus();');
  return (win==null); // false means success
}

function new_iframe(url,id,w,h,l,t,pclass){
  var pleft=l;
  var ptop=t;
  // if(pos=="random") {
  //   pleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
  //   ptop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
  // }
  // else if(pos=="center") {
  //   pleft=(screen.width)?(screen.width-w)/2:100;
  //   ptop=(screen.height)?(screen.height-h-20)/2:100;
  // }
  // else {
  //   pleft=100;
  //   ptop=100;
  // }
  // if(pleft<0) pleft=0;
  // if(ptop<0) ptop=0;

  if(pclass==null || pclass=="")
    pclass="popup";

  // Outer frame. It may exist if we created it before.
  //
  var eltdiv=document.getElementById(id);
  if(eltdiv==null) {
    eltdiv=document.createElement('div');
    eltdiv.id=id;
    eltdiv.className=pclass;
    eltdiv.style.top=ptop+'px';
    eltdiv.style.left=pleft+'px';
    eltdiv.style.display='none';

    // Close icon
    //
    var eltclimg=document.createElement('img');
    eltclimg.src="/images/close.png";
    var eltclose=document.createElement('a');
    eltclose.className='popupClose';
    eltclose.style.top="-12px";
    eltclose.style.right="-12px";
    eltclose.href="#";
    eltclose.title="Close";
    eltclose.appendChild(eltclimg);
    eltdiv.appendChild(eltclose);

    // Iframe inside
    //
    var eltiframe=document.createElement('iframe');
    eltiframe.style.border='none';
    eltiframe.frameborder=0;
    eltiframe.scrolling='no';
    eltiframe.src=url;
    eltiframe.style.width=w+'px';
    eltiframe.style.height=h+'px';
    eltdiv.appendChild(eltiframe);

    // IE needs to have iframe switched away to dispose of the flash
    // Other browsers need the element to be removed for video to stop
    //
    eltclose.onclick=function() {
      eltiframe.src="about:blank";
      document.body.removeChild(eltdiv);
      blanket_hide();
      return false;
    };

    document.body.appendChild(eltdiv);
  }

  blanket_show();
  eltdiv.style.display='block';
  eltdiv.style.visibility='visible';

  return false;
}

