// -----------------------------------------------------------------------------------
//
//  Lightbox Lite
//  by Steven Pothoven - http://blog.pothoven.net
//  1/22/08
//
//  Derived from:
//
//  Lightbox v2.03.3 (http://huddletogether.com/projects/lightbox2/)
//  by Lokesh Dhakar - http://www.huddletogether.com
//  5/21/06
//
//  Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//  
//  Credit also due to those who have helped, inspired, and made their code available to the public.
//  Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), Thomas Fuchs(mir.aculo.us), and others.
//
// -----------------------------------------------------------------------------------

LightBoxLite = function() {

    //#######################################
    var mode = 'pic';
    
    
    var borderSize = 10;
    var resizeDuration = 0.6;
    
    /**
     * resizeImageContainer
     *
     * @param {Number} desired width
     * @param {Number} desired height
     */
    function resizeImageContainer( imgWidth, imgHeight) {

        // get current width and height
        var widthCurrent = $('outerImageContainer').getWidth();
        var heightCurrent = $('outerImageContainer').getHeight();

        // get new width and height
        var widthNew = (imgWidth  + (borderSize * 2));
        var heightNew = (imgHeight  + (borderSize * 2));

        // scalars based on change from old to new
        var xScale = ( widthNew / widthCurrent) * 100;
        var yScale = ( heightNew / heightCurrent) * 100;

        // calculate size difference between new and old image, and resize if necessary
        var wDiff = widthCurrent - widthNew;
        var hDiff = heightCurrent - heightNew;

        if (!( hDiff === 0)) {
            new Effect.Scale('outerImageContainer', yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); 
        }
        if (!( wDiff === 0)) {
            new Effect.Scale('outerImageContainer', xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); 
        }

        $('imageDataContainer').style.width = widthNew + "px";

        showImage();
    }
    
    /**
     * updateDetails
     * 
     */
    function updateDetails() {
        
        if(mode == 'flash' || mode == 'flashbig') {
            var caption = ' ';
        } else {
            //var caption = $('lightboxImage').src.split('/').pop().split('.')[0].gsub("%20", " ");
            
            var caption = ' ';
            
            
        }
        
        $('imagecaption').innerHTML = caption; 
        
        new Effect.Parallel(
            [ new Effect.SlideDown( 'imageDataContainer', { sync: true, duration: resizeDuration, from: 0.0, to: 1.0 }), 
              new Effect.Appear('imageDataContainer', { sync: true, duration: resizeDuration }) ] 
        );
    }
    
    /**
     * showImage
     * Display image and begin preloading neighbors.
     */
    function showImage() {
        

        $('overlay').show();
        new Effect.Opacity('overlay', { from: 0.0, to: 0.8, duration: 1.0 });


        if(mode == 'flash' || mode == 'flashbig') {
        
            new Effect.Appear('imageContainer', { duration: 2, queue: 'end', afterFinish: updateDetails });
            
        } else {

            $('imageloading').hide();
            //#######################################
            //new Effect.Appear('lightboxImage', { duration: resizeDuration, queue: 'end', afterFinish: updateDetails });
            
            new Effect.Appear('imageContainer', { duration: resizeDuration, queue: 'end', afterFinish: updateDetails });
            
            //$('photo').style.zIndex = "555";
            //$('lightbox').style.zIndex = "555";
            //$('outerImageContainer').style.zIndex = "555";

            //$('imageContainer').style.zIndex = "555";
            //$('lightboxImage').style.zIndex = "555";
        }
        
    }
    
    function checkForPreloadComplete(imgPreloader) {
    
        if(mode == 'flash') {
            resizeImageContainer(255, 210);
        } else if(mode == 'flashbig'){
            resizeImageContainer(522, 298);
        } else {
            if (imgPreloader.complete) {
                $('lightboxImage').src = imgPreloader.src;
                resizeImageContainer(imgPreloader.width, imgPreloader.height);
            } else {
                setTimeout(checkForPreloadComplete.bind(this, imgPreloader), 100);
            }
        }
    }
    
    
    return {
        /**
         * displayImage
         * display an image in a lightbox
         *
         * @param {String} URL of image
         */
        displayImage : function(imageUrl) {
        
            
            if(imageUrl.indexOf('film') != -1){
                mode = 'flash';
                if(imageUrl.indexOf('video') != -1){
                    mode = 'flashbig';
                }
                
            } else {
                mode = 'pic';
            } 
        
            if($('imageloading')) $('imageloading').show();
            
            //#######################################
            //$('lightboxImage').hide()
            $('imageContainer').hide()
            
            $('imageDataContainer').hide()
            $('photo').show();
  
  
            if(mode=='flash' || mode=='flashbig') {
                
                var flashUrl = imageUrl.split('/').pop().split('.')[0].gsub("%20", " ") + ".swf";
                
                var fwidth = 245;
                var fheight = 200;
                
                if(mode=='flashbig'){
                    fwidth = 512;
                    fheight = 288;
                }
 
                flashcode = '	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n';
                flashcode += '            codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"\n';
                flashcode += '            width="'+fwidth+'"\n';
                flashcode += '            height="'+fheight+'"\n';
                flashcode += '            id="' + flashUrl + '"\n';
                flashcode += '            align="middle">\n';
                flashcode += '    	<param name="allowScriptAccess" value="sameDomain" />\n';
                flashcode += '    	<param name="allowFullScreen" value="false" />\n';
                flashcode += '    	<param name="movie" value="' + flashUrl + '" />\n';
                flashcode += '        <param name="quality" value="high" />\n';
                flashcode += '        <param name="wmode" value="window" />\n';
                flashcode += '        <param name="bgcolor" value="#000000" />\n';
                flashcode += '\n';
                flashcode += '        <embed src="' + flashUrl + '"\n';
                flashcode += '               quality="high"\n';
                flashcode += '               wmode="window"\n';
                flashcode += '               bgcolor="#000000"\n';
                flashcode += '               width="'+fwidth+'"\n';
                flashcode += '               height="'+fheight+'"\n';
                flashcode += '               name="' + flashUrl + '"\n';
                flashcode += '               align="middle"\n';
                flashcode += '               allowScriptAccess="sameDomain"\n';
                flashcode += '               allowFullScreen="false"\n';
                flashcode += '               type="application/x-shockwave-flash"\n';
                flashcode += '               pluginspage="http://www.macromedia.com/go/getflashplayer" />\n';
                flashcode += '\n';
                flashcode += '    </object>\n';
                            
                //$('imageContainer').innerHTML =  flashcode;               
                setTimeout("$('imageContainer').innerHTML =  flashcode", 100);                
                
                
            } else {
                            
                piccode = '<img id="lightboxImage"></img>\n';
                piccode += '<div id="imageloading">\n';
                piccode += '    <img src="images/loadingImage.gif"></img>\n';
                piccode += '</div>\n\n';
                
                setTimeout("$('imageContainer').innerHTML =  piccode", 100);
            
            
                var imgPreloader = new Image();
                imgPreloader.src = imageUrl;            
                
            }
  

  
            // once image is preloaded, resize image container
            setTimeout(checkForPreloadComplete.bind(this, imgPreloader), 100);          
  
        },
        
        reset : function() {
        
            $('photo').hide();
            if($('imageloading')) $('imageloading').show();
            //#######################################
            //$('lightboxImage').hide()
            $('imageContainer').hide()
            $('imageDataContainer').hide()
            
            new Effect.Opacity('overlay', { from: 0.8, to: 0.0, duration: 0.5 });
            setTimeout("$('overlay').hide()", 500);

            
            
            if($('lightboxImage')) $('lightboxImage').src = "";
            $('outerImageContainer').style.width = "250px";
            $('outerImageContainer').style.height = "250px";
                      
        }
    };
}();
