﻿// JScript File

var popupThumbnailViewer={

    // Define local variables
    mouseOffSet: 10,

    createThumbBox:function(cssStyle) {
        document.write('<div id="previewerThumbBox" onClick="popupThumbnailViewer.closeit()" class="' + cssStyle + '"><div id="previewerThumbBoxImage"></div></div>')
        this.thumbBox = document.getElementById("previewerThumbBox")                                    // Refrence to the thumbnail container
        this.thumbImage = document.getElementById("previewerThumbBoxImage")                             // Refrence to the image to be displayed
        this.standardBody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body // create refrence to standard "body" across doctype
    },
    
    loadImage:function(imageLocation) {
    
        // If thumbnail image currently visible then hide it
        if (this.thumbBox.style.visibility=="visible") {                                                // If thumbox is visible on the page already
            this.closeit()                                                                              // Hide it first (not doing so causes triggers some positioning bug in Firefox
        }
        
        var imageHTML='<img src="' + imageLocation + '" />'                                             // Construct HTML for shown image
        
        this.thumbImage.innerHTML=imageHTML                                                             // Populate thumbImage div with shown image's HTML (while still hidden)
        this.featureImage=this.thumbImage.getElementsByTagName("img")[0]                                // Refrence shown image it self
        
        this.featureImage.onload=function() {
            popupThumbnailViewer.showThumbBox()                                                         // Display "thumbbox" div to the world!
        }
        
        if (document.all && !window.createPopup) {                                                      // Target IE5.0 browsers only. Address IE image cache not firing onload bug: panoramio.com/blog/onload-event/
	        this.featureImage.src=link.getAttribute("href")
	    }
    },


    
    showThumbBox:function() {
        this.thumbBox.style.visibility="visible"
    },
    
    closeit:function(){
        this.thumbBox.style.visibility="hidden"
        this.thumbImage.innerHTML=""
        this.thumbBox.style.left="-2000px"
        this.thumbBox.style.top="-2000px"
    },
    
    positionViewer:function(evt) {
    
        // Ensure we have an event object
        if (evt == null)
            evt = window.event
        
        // Get scrolling factors
        var scrollTop = this.standardBody.scrollTop
        var scrollLeft = this.standardBody.scrollLeft;
         
        // Position the preview image to be offset to the current mouse position
        this.thumbBox.style.left=(evt.clientX + scrollLeft + this.mouseOffSet) + 'px'
        this.thumbBox.style.top=(evt.clientY + scrollTop + this.mouseOffSet) + 'px'

    },
    
    cleanup:function(){
        if (this.featureImage) {
            this.featureImage.onload=null 
        }
	    this.featureImage=null
        this.thumbImage=null
        this.thumbBox=null
    },
    
    dotask:function(target, functionref, tasktype){
        var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
        if (target.addEventListener) {
	        target.addEventListener(tasktype, functionref, false)		        
        } else if (target.attachEvent) {
	        target.attachEvent(tasktype, functionref)
	    }
    }
}


