// Modified for my purposes from:
// -------------------------------------------------------------------
// Image Thumbnail Viewer Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Last updated: July 7th, 2008- Fixed enlarged image not showing in IE sometimes
// -------------------------------------------------------------------

var popupControl={

targetLinks:[],

createPopup:function(){
   this.popupBox = document.createElement('div')
   this.popupImage = document.createElement('img')
//   this.popupCaption = document.createElement('p')
//   this.popupClose = document.createElement('h3')
   document.body.appendChild(this.popupBox)
   this.popupBox.appendChild(this.popupImage)
//   this.popupBox.appendChild(document.createElement('br'))
//   this.popupBox.appendChild(this.popupClose)
//   this.popupClose.appendChild(document.createTextNode('X'))
//   this.popupBox.appendChild(this.popupCaption)
//   this.popupCaption.appendChild(document.createTextNode(''))
   with (this.popupBox.style) {
      backgroundColor='#8B7355'
      border='5px solid #333333'
      padding='7px 7px 2px 7px'
      position='fixed'
      left='0px'
      top='0px'
      width='auto'
      //textAlign='center'
      visibility='hidden'
      zindex='49'
   }
//   with (this.popupClose.style){
//      backgroundColor='#333333'
//      position='relative'
//      color='#CCCCCC'
//      padding='0px 2px'
//      border='1px solid #DDDDDD'
//      cursor='pointer'
//   }
   this.popupBox.onclick=function(){popupControl.closeIt()}
   this.popupLoadBox=document.createElement('div')
   this.popupLoadImage=document.createElement('img')
   this.popupLoadText=document.createElement('h3')
   document.body.appendChild(this.popupLoadBox)
   this.popupLoadBox.appendChild(this.popupLoadImage)
   this.popupLoadBox.appendChild(this.popupLoadText)
   this.popupLoadText.appendChild(document.createTextNode('Loading Image...'))
   with (this.popupLoadBox.style) {
      position='fixed'
      visibility='hidden'
      zindex='50'
      border='5px solid #333333'
      padding='5px'
      backgroundColor='#8B7355'
   }
   this.popupLoadImage.src='images/loading.gif'
   this.popupLoadImage.style.paddingRight='15px'
}, //END createPopup()

getWidth:function(){
   if (window.innerWidth)
      return window.innerWidth;//-offset;
   if (document.documentElement.clientWidth>0)
      return document.documentElement.clientWidth;//-offset;
   return document.body.clientWidth;//-offset;
}, // END getWidth

getHeight:function(){
   if (window.innerHeight)
      return window.innerHeight-1;
   if (document.documentElement.clientHeight>0)
      return document.documentElement.clientHeight-1;
   return document.body.clientHeight-1;
}, // END getHeight

centerDiv:function(divobj){ //Centers a div element on the page
   var x = Math.floor((this.getWidth() / 2) - (divobj.offsetWidth / 2))
   var y = Math.floor((this.getHeight() / 2) - (divobj.offsetHeight / 2))
   with (divobj.style) {
      left = x + 'px'
      top = y + 'px'
      visibility='visible'
   }
//   if (divobj == this.popupBox){
//      with (this.popupClose.style){
//         left=(divobj.offsetWidth - 28) + 'px'
//         top='-'+(divobj.offsetHeight - 30) + 'px'
//      }
//   }
}, // END centerDiv()

showPopup:function(){
   this.popupLoadBox.style.visibility='hidden'
   this.centerDiv(this.popupBox)
}, // END showPopup()

loadImage:function(link){
   if (this.popupBox.style.visibility=='visible')
      this.closeIt()
   this.centerDiv(this.popupLoadBox)
   this.popupImage.src=link.href
//   if (link.getAttribute('title'))
//      this.popupCaption.childNodes[0].nodeValue=link.getAttribute('title')
//   if (this.popupImage.complete){
//      this.showPopup()
//   } else {
      this.popupImage.onload=function(){popupControl.showPopup()}
//   }
   this.popupImage.onerror=function(link){
      popupControl.closeIt()
//      popupControl.popupBox.style.visibility='hidden'
      window.open(link.href)
   }
   return false;
}, // END loadImage()

resizeToFit:function(){
   if (popupControl.popupBox.style.visibility=="visible")
      popupControl.centerDiv(popupControl.popupBox)
}, // END resizeToFit()

closeIt:function(){
   this.popupBox.style.visibility='hidden'
//   this.popupImage.src = null
//   this.popupCaption.childNodes[0].nodeValue=''
   this.popupBox.style.left='-2000px'
   this.popupBox.style.top='-2000px'
}, // END closeIt()

doTask:function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)
   var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
   if (target.addEventListener)
      target.addEventListener(tasktype, functionref, false)
   else if (target.attachEvent)
      target.attachEvent(tasktype, functionref)
}, // END doTask()

// TO DO: unattach event handlers
cleanup:function(){ //Clean up routine
   for (var i=0; i<this.targetLinks.length; i++){
      this.targetLinks[i].onclick=null
   }
   if (this.popupBox.style.visibility=='visible'){
      this.closeIt()
   }
//   this.popupCaption.removeChild(this.popupCaption.childNodes[0])
//   this.popupCaption=null
//   this.popupClose.removeChild(this.popupClose.childNodes[0])
//   this.popupClose=null
   if (this.popupImage){
      this.popupImage.onload=null
   }
   this.popupImage=null
   document.body.removeChild(this.popupBox)
   this.popupBox=null
},

init:function() {
   this.createPopup()
   var links=document.getElementsByTagName('a')
   for (var i=0; i<links.length; i++) {
      if (links[i].getAttribute('rel') == 'popup') {
         links[i].onclick=function(){
            try {
               return popupControl.loadImage(this)
            } catch (e) {
               return true;
            }
         } // END onclick()
         this.targetLinks[this.targetLinks.length] = links[i];
      } // END if
   } // END for
   if (this.targetLinks.length > 0) {
      this.doTask(window, function(){popupControl.resizeToFit()}, "resize")
   } else {
      this.cleanup()
   } // END if
} // END init()

} // END popupControl

popupControl.doTask(window, function(){popupControl.init()}, "load") //Initialize script on page load
popupControl.doTask(window, function(){popupControl.cleanup()}, "unload")
