// initial idea from Shutter by Andrew Sutherland, http://code.jalenack.com/
function mkShutter( shlink, setid, inset ) {
  var sh_newInfo, sh_newShutter, shfile, sh_title, prevlink, nextlink, previmg, nextimg;
  
  this.hideShutter = function() {
    sh_newInfo = document.getElementById('sh_newInfo');
    sh_newInfo.parentNode.removeChild(sh_newInfo);
    shutter = document.getElementById('sh_newShutter');
    shutter.parentNode.removeChild(shutter);
    showSelectBoxes();
    showFlash();
  }

  this.showImg = function() {
    if ( document.getElementById('sh_newShutter') ) {
      var sh_topImg = document.getElementById('sh_topImg');
      var sh_txtwrap = document.getElementById('sh_txtwrap');
      var sh_waitbar = document.getElementById('sh_waitbar');
      var sh_wrap = document.getElementById('sh_wrap');

      if ( sh_waitbar ) sh_waitbar.style.display = 'none';  
      if ( sh_wrap.style.visibility == 'visible' ) return;

      var deH = document.documentElement ? document.documentElement.clientHeight : 0;
      var dbH = document.body.clientHeight;
      var wheight = ( deH > 0 && ( deH < dbH || dbH < 1 ) ) ? deH : dbH;

      var deW = document.documentElement ? document.documentElement.clientWidth : 0;
      var dbW = document.body.clientWidth;
      var wwidth = ( deW > 0 ) ? deW : dbW;

      if ( sh_topImg.height > (wheight - 20 - sh_txtwrap.clientHeight) ) {
        sh_topImg.width = sh_topImg.width * ((wheight - 20 - sh_txtwrap.clientHeight) / sh_topImg.height);
        sh_topImg.height = wheight - 20 - sh_txtwrap.clientHeight;
      }

      if ( sh_topImg.width > (wwidth - 16) ) {
        sh_topImg.height = sh_topImg.height * ((wwidth - 16) / sh_topImg.width);
        sh_topImg.width = wwidth - 16;
      }
      
      var mtop = ( ( (wheight - sh_topImg.height - sh_txtwrap.clientHeight - 20) / 100 ) * 45 ) > 3 ? Math.floor( ( (wheight - sh_topImg.height - sh_txtwrap.clientHeight - 20) / 100 ) * 45 ) : 3;

      sh_newInfo.style.margin = mtop + 'px auto auto auto';
      sh_wrap.style.visibility = 'visible';
    }
  }

  // from lightbox by Lokesh Dhakar - http://www.huddletogether.com
  this.showSelectBoxes = function() {
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
  }

  this.hideSelectBoxes = function() {
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
  }

  this.showFlash = function() {
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
  }

  this.hideFlash = function() {
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}
  }

  if ( document.getElementById('sh_newShutter') ) hideShutter();

  shfile = shutterLinks[shlink].slice(shutterLinks[shlink].lastIndexOf('/')+1);
  if ( document.links[shlink].title && document.links[shlink].title != shfile ) sh_title = document.links[shlink].title;
  else sh_title = '&nbsp;';

  shUrl = shutterLinks[shlink];

  if ( inset != -1 ) {
    if ( inset > 1 ) prevlink = 'javascript:mkShutter(' + shutterSets[setid][inset - 2] + ',' + setid + ',' + (inset - 1) +')';
    else prevlink = '';

    if ( inset < (shutterSets[setid].length) ) nextlink = 'javascript:mkShutter(' + shutterSets[setid][inset] + ',' + setid + ',' + (inset + 1) +')';
    else nextlink = '';
  }

  sh_newShutter = document.createElement('div');
  sh_newShutter.setAttribute('id','sh_newShutter');
  document.getElementsByTagName('body')[0].appendChild(sh_newShutter);
  hideSelectBoxes();
  hideFlash();
  sh_newShutter.onclick = hideShutter;

  sh_newInfo = document.createElement('div');
  sh_newInfo.setAttribute('id','sh_newInfo');
  sh_newInfo.innerHTML = '<div id="sh_waitbar">L O A D I N G</div><table id="sh_wrap" style="visibility:hidden;"><tr><td colspan="3"><img src="' + shUrl + '" id="sh_topImg" onload="showImg();" onclick="hideShutter();" title=" X " /></td></tr><tr id="sh_txtwrap"><td class="sh_arrows"><a href="' + prevlink + '" id="sh_prev">&lt;&lt;</a></td><td id="sh_title">' + sh_title + '</td><td class="sh_arrows"><a href="' + nextlink + '" id="sh_next">&gt;&gt;</a></td></tr></table>';
  sh_newInfo.innerHTML += '<div style="display:none">-----------------------------</div>'; // ugly ie6 html comments/dub. characters fix

  document.getElementsByTagName('body')[0].appendChild(sh_newInfo);
    
  // preload
  if ( prevlink ) {
    previmg = new Image();
    previmg.src = shutterLinks[shutterSets[setid][inset - 2]];
  } else { document.getElementById('sh_prev').style.visibility = 'hidden'; }

  if ( nextlink ) {
    nextimg = new Image();
    nextimg.src = shutterLinks[shutterSets[setid][inset]];
  } else { document.getElementById('sh_next').style.visibility = 'hidden'; }
    
  window.setTimeout(function(){if(document.getElementById('sh_waitbar'))document.getElementById('sh_waitbar').style.visibility = 'visible'},2000);
}
