
// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = '/shared/packages/lightbox/loading.gif';        
var closeButton = '/shared/packages/lightbox/close.gif';        

function LightBoxEventHandler()
{
    this.Captions=Array();
    function CaptionObject(ID,Caption)
    {
        this.ID=ID;
        this.Caption=Caption;
    }
    //
    // getPageScroll()
    // Returns array with x,y page scroll values.
    // Core code from - quirksmode.org
    //
    this.getPageScroll=function()
    {
        var yScroll;

        if (self.pageYOffset)
        {
            yScroll = self.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
        {
            // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
        }
        else if (document.body)
        {
            // all other Explorers
            yScroll = document.body.scrollTop;
        }

        arrayPageScroll = new Array('',yScroll) 
        return arrayPageScroll;
    }



    //
    // getPageSize()
    // Returns array with page width, height and window width, height
    // Core code from - quirksmode.org
    // Edit for Firefox by pHaez
    //
    this.getPageSize=function()
    {
        
        var xScroll, yScroll;
        
        if (window.innerHeight && window.scrollMaxY)
        {    
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
        }
        else if (document.body.scrollHeight > document.body.offsetHeight)
        {
            // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        }
        else
        {
            // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
        
        var windowWidth, windowHeight;
        if (self.innerHeight)
        {
            // all except Explorer
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
            // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
            // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }    
        
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight)        
            pageHeight = windowHeight;
        else
            pageHeight = yScroll;
       

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth)    
            pageWidth = windowWidth;
        else
            pageWidth = xScroll;

        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
        return arrayPageSize;
    }

    //
    // showLightbox()
    // Preloads images. Pleaces new image in lightbox then centers and displays.
    //
    this.showLightbox=function(objLink,Caption,Context)
    {
        // prep objects
        var objOverlay = document.getElementById('lightbox_overlay');
        var objLightbox = document.getElementById('lightbox');
        var objCaption = document.getElementById('lightboxCaption');
        var objImage = document.getElementById('lightboxImage');
        var objLoadingImage = document.getElementById('loadingImage');
        var objLightboxDetails = document.getElementById('lightboxDetails');
        var objVideoContainer   = document.getElementById('video_container');

        
        var arrayPageSize = this.getPageSize();
        var arrayPageScroll = this.getPageScroll();

        // center loadingImage if it exists
        if (objLoadingImage)
        {
            objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
            objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
            objLoadingImage.style.display = 'block';
        }

        // set height of Overlay to take up whole page and show
        objOverlay.style.height = (arrayPageSize[1] + 'px');
        objOverlay.style.display = 'block';
        
        if (objLink.href.match(/youtube\.com\/watch/i))
        {
            var hRef = objLink.href;
            var videoId = hRef.split('=');
            videoID = videoId[1];
            var width=500;
            var height=300;
            objVideoContainer.innerHTML='';
            objImage.style.display='none';
            
            so = new SWFObject("http://www.youtube.com/v/"+videoID, "flvvideo", width, height, "0");
            //so.addParam("wmode", "transparent");
            so.write(objVideoContainer);
             // center lightbox and make sure that the top and left values are not negative
                // and the image placed outside the viewport
                var lightboxTop     = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - height) / 2);
                var lightboxLeft    = ((arrayPageSize[0] - 20 - width) / 2);
                
                objLightbox.style.top   = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
                objLightbox.style.left  = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


                objLightboxDetails.style.width = width + 'px';
                
                if(objLink.getAttribute('title') || typeof(Caption) != "undefined")
                {
                    objCaption.style.display = 'block';
                    //objCaption.style.width = imgPreload.width + 'px';
                    if(typeof(Caption) != "undefined")
                        objCaption.innerHTML = Caption;
                    else
                        objCaption.innerHTML = objLink.getAttribute('title');
                }
                else         
                    objCaption.style.display = 'none';

                if (objLoadingImage)
                    objLoadingImage.style.display = 'none';
                objLightbox.style.display = 'block';

                // After image is loaded, update the overlay height as the new image might have
                // increased the overall page height.
                
                arrayPageSize = Context.getPageSize();
                objOverlay.style.height = (arrayPageSize[1] + 'px');
                
                document.onkeypress=function(e)
                {
                    if (e == null)
                    {
                        // ie
                        keycode = event.keyCode;
                    }
                    else
                    {
                        // mozilla
                        keycode = e.which;
                    }
                    key = String.fromCharCode(keycode).toLowerCase();
                    
                    if(key == 'x')
                        Context.hideLightbox.call(Context);                
                }
                return false;
                
        }
        else
        {
            // preload image
            objVideoContainer.innerHTML='';
            imgPreload = new Image();

            imgPreload.onload=function()
            {
                objImage.src = objLink.href;

                // center lightbox and make sure that the top and left values are not negative
                // and the image placed outside the viewport
                var lightboxTop     = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
                var lightboxLeft    = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
                
                objLightbox.style.top   = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
                objLightbox.style.left  = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


                objLightboxDetails.style.width = imgPreload.width + 'px';
                
                if(objLink.getAttribute('title') || typeof(Caption) != "undefined")
                {
                    objCaption.style.display = 'block';
                    //objCaption.style.width = imgPreload.width + 'px';
                    if(typeof(Caption) != "undefined")
                        objCaption.innerHTML = Caption;
                    else
                        objCaption.innerHTML = objLink.getAttribute('title');
                }
                else         
                    objCaption.style.display = 'none';

                if (objLoadingImage)
                    objLoadingImage.style.display = 'none';
                objLightbox.style.display = 'block';
                objImage.style.display='block';

                // After image is loaded, update the overlay height as the new image might have
                // increased the overall page height.
                
                arrayPageSize = Context.getPageSize();
                objOverlay.style.height = (arrayPageSize[1] + 'px');  
                
                
                // Check for 'x' keypress
                //Context.listenKey.call(Context,Context);
                document.onkeypress=function(e)
                {
                    if (e == null)
                    {
                        // ie
                        keycode = event.keyCode;
                    }
                    else
                    {
                        // mozilla
                        keycode = e.which;
                    }
                    key = String.fromCharCode(keycode).toLowerCase();
                    
                    if(key == 'x')
                        Context.hideLightbox.call(Context);                
                }
                return false;
            }
            imgPreload.src = objLink.href;
        }    
    }


    this.hideLightbox=function()
    {
        // get objects
        objOverlay = document.getElementById('lightbox_overlay');
        objLightbox = document.getElementById('lightbox');

        // hide lightbox and overlay
        objOverlay.style.display = 'none';
        objLightbox.style.display = 'none';
        
        // disable keypress listener
        document.onkeypress = '';
        return false;
    }
    
    this.initAnchor=function(id,caption,handler,context)
    {
        var obj=document.getElementById(id);
        if(obj==null)
            return;
        
        this.Captions.push(new CaptionObject(id,caption));    
        
        obj.onclick = function()
        {
            handler.call(context,obj,context);return false;
        };        
    }
    this.triggerLightBox=function(obj,context)
    {
        this.showLightbox(obj,this.FindCaptionById(obj.id),context);
    }
   
    this.FindCaptionById=function(ID)
    {
        for(var c=0;c<this.Captions.length;c++)
        {
            if(this.Captions[c].ID ==ID)
                return this.Captions[c].Caption;
        }
        return null;
    }
}