
var thumbview = {
    
    //Should "title" attribute of link be used as description?
    enableTitle: false, 
    
    //Enable fading transition in IE?
    enableTransition: true, 
    
    //Hide enlarged image when mouse moves out of anchor link? (if enlarged image is hyperlinked, always set to false!)
    hideimgmouseout: false, 
    
    //Limit the large image size?
    limitSize: true,
    //if limitSize = true, specify the limited size
    widthLimit: 500,
    heightLimit: 350,

    //IE specific multimedia filter string
    iefilterstring: 'progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0 Duration=0.7)', 
    
    //Detect browser support for IE filters
    iefiltercapable: document.compatMode && window.createPopup? true : false, 
    
    //array to preload enlarged images (ones set to display "onmouseover"
    preloadedimages:[], 
    
    //array to hold participating links (those with rel="enlargeimage:initType")
    targetlinks:[], 
    
    //flag to indicate whether init() function has been run already come window.onload
    alreadyrunflag: false, 

    loadimage:function(linkobj)
    {
        
        //Get URL to enlarged image
        var imagepath = linkobj.getAttribute("href") 
        
        //Reference container on page to show enlarged image in
        var showcontainer = document.getElementById(linkobj.getAttribute("rev").split("::")[0]) 
        
        //Get URL enlarged image should be linked to, if any
        var dest=linkobj.getAttribute("rev").split("::")[1] 

        //Get title attr        
        var description = (thumbview.enableTitle && linkobj.getAttribute("title")) ? linkobj.getAttribute("title") : "" 
        
        if (this.limitSize == true)
        {
            //Construct HTML for enlarged image
            var imageHTML='<img src="' + imagepath + '" style="border-width: 0" width="' +
                this.widthLimit + '" height="' + this.heightLimit + '" />' 
        }
        else
        {
            //Construct HTML for enlarged image
            var imageHTML='<img src="' + imagepath + '" style="border-width: 0" />' 
        }

        //Hyperlink the enlarged image?
        if (typeof dest != "undefined")
            imageHTML='<a href="' + dest + '">' + imageHTML + '</a>'
        
        //Use title attr of the link as description?
        if (description != "") 
            imageHTML += '<br />' + description
            
        //Is this an IE browser that supports filters?            
        if (this.iefiltercapable)
        { 
            showcontainer.style.filter = this.iefilterstring
            showcontainer.filters[0].Apply()
        }

        showcontainer.innerHTML = imageHTML
        
        //Reference enlarged image itself
        this.featureImage = showcontainer.getElementsByTagName("img")[0]
        
        //When enlarged image has completely loaded
        this.featureImage.onload = 
            function()
            { 
                //Is this an IE browser that supports filters?
                if (thumbview.iefiltercapable) 
                    showcontainer.filters[0].Play()
            }   

        //If an error has occurred while loading the image to show
        this.featureImage.onerror = 
            function()
            { 
                //Is this an IE browser that supports filters?
                if (thumbview.iefiltercapable) 
                    showcontainer.filters[0].Stop()
            }
    },

    hideimage:function(linkobj)
    {
        //Reference container on page to show enlarged image in
        var showcontainer = document.getElementById(linkobj.getAttribute("rev").split("::")[0]) 
            
        showcontainer.innerHTML = ""
    },


    cleanup:function()
    { 
        //Clean up routine on page unload
        if (this.featureImage)
        {
            this.featureImage.onload = null; 
            this.featureImage.onerror = null; 
            this.featureImage = null
        }
        
        this.showcontainer = null
        for (var i=0; i<this.targetlinks.length; i++)
        {
            this.targetlinks[i].onclick = null
            this.targetlinks[i].onmouseover = null
            this.targetlinks[i].onmouseout = null
        }
    },

    //assign a function to execute to an event handler (ie: onunload)
    addEvent: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)
        }
    },

    //Initialize thumb view script
    init:function()
    {
        //True or false: IE filters supported and is enabled by user
        this.iefiltercapable = (this.iefiltercapable && this.enableTransition) 
        
        var pagelinks = document.getElementsByTagName("a")
    
        for (var i = 0; i < pagelinks.length; i++)
        { 
            //Begin if statement: Test for rel="enlargeimage"
            if (pagelinks[i].getAttribute("rel") && /enlargeimage:/i.test(pagelinks[i].getAttribute("rel")))
            { 
                //Get display type of enlarged image ("click" or "mouseover")
                var initType = pagelinks[i].getAttribute("rel").split("::")[1] 
            
                //If type is "mouseover", preload the enlarged image for quicker display
                if (initType == "mouseover")
                { 
                    this.preloadedimages[this.preloadedimages.length] = new Image()
                    this.preloadedimages[this.preloadedimages.length-1].src = pagelinks[i].href
                    
                    //Cancel default click action
                    pagelinks[i]["onclick"] = 
                        function()
                        { 
                            return false
                        }
                }
            
                //Load enlarged image based on the specified display type (event)
                pagelinks[i]["on"+initType] = 
                    function()
                    { 
                        //Load image
                        thumbview.loadimage(this) 
                        return false
                    }
        
                if (this.hideimgmouseout)
                {
                    pagelinks[i]["onmouseout"] = 
                        function()
                        {
                            //Hide image
                            thumbview.hideimage(this)
                        }
                }

                //store reference to target link
                this.targetlinks[this.targetlinks.length] = pagelinks[i] 

            } //END if statement

        } //END for loop

    } //END init() function

}


//Take advantage of "DOMContentLoaded" event in select Mozilla/ Opera browsers for faster init
if (document.addEventListener) 
{
    //Initialize script on page load
    thumbview.addEvent(document, 
        function()
        {
            thumbview.alreadyrunflag = 1; 
            thumbview.init()
        }
        , "DOMContentLoaded") 
}
else 
{
    //Take advantage of "defer" attr inside SCRIPT tag in IE for instant init
    if (document.all && document.getElementsByTagName("a").length > 0)
    { 
        thumbview.alreadyrunflag = 1
        thumbview.init()
    }
}

//Default init method: window.onload
thumbview.addEvent(window, 
    function()
    {
        if (!thumbview.alreadyrunflag) 
            thumbview.init()
    }
    , "load") 

thumbview.addEvent(window, 
    function()
    {
        thumbview.cleanup()
    }
    , "unload")



