// preload "close" image, otherwise safari lays out tech box incorrectly
var pic1 = new Image(66,22); pic1.src = "/kswiss/images/nav/lightbox/lightbox-btn-close.gif";

(function($) {
    $.fn.lightBox = function(settings) {
        // Settings to configure the jQuery lightBox plugin how you like

        settings = jQuery.extend({
            overlayBgColor: '#000',
            overlayOpacity: 0.5,
            fixedNavigation: false,
            containerBorderSize: 10,
            containerResizeSpeed: 400,
            txtImage: 'Image',
            txtOf: 'of',
            keyToClose: 'c',
            placeHolder: null,
            timeout: -1,
            hideOnClickOutside: true,
            imageLoading: '/kswiss/images/nav/lightbox/lightbox-ico-loading.gif',
            // (string) Path and the name of the loading icon
            imageBtnPrev: '/kswiss/images/nav/lightbox/lightbox-btn-prev.gif',
            // (string) Path and the name of the prev button image
            imageBtnNext: '/kswiss/images/nav/lightbox/lightbox-btn-next.gif',
            // (string) Path and the name of the next button image
            imageBtnClose: '/kswiss/images/nav/lightbox/lightbox-btn-close.gif',
            // (string) Path and the name of the close btn
            imageBlank: '/kswiss/images/nav/lightbox/lightbox-blank.gif',
            // (string) Path and the name of a blank image (one pixel)
            alreadyOpened: false
        },
        settings);

        var jQueryMatchedObj = this;

        function _initialize() {
            _start(this, jQueryMatchedObj)
            $(document).keydown(function(objEvent) {
                _keyboard_action(objEvent)
            })
            if (settings.timeout != -1) setTimeout("$('#lightbox-secNav-btnClose').click()", settings.timeout)
            return false
        }

        function _start(objClicked, jQueryMatchedObj) {
            if (!settings.alreadyOpened) {
                settings.alreadyOpened = true

                /* this was causing problems -- underlying flash components have wmode set so this is not nec.
                $('embed, object, select').css({
                    'visibility': 'hidden',
                });
                */

                _set_interface();
                settings.placeHolder = objClicked
                _set_image_to_view();
                $("#lightbox-image object").css("visibility", "visible")
                $("#lightbox-image embed").css("visibility", "visible")
                $("#lightbox-image object embed").css("visibility", "visible")

            }
        }

        function _set_interface() {
            $('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><div id="lightbox-image" style="position:relative;z-index:10001"></div><div style="" id="lightbox-nav"><a href="#" id="lightbox-nav-btnPrev"></a><a href="#" id="lightbox-nav-btnNext"></a><div id="lightbox-secNav"></div></div><div id="lightbox-loading"><a href="#" id="lightbox-loading-link"><img src="' + settings.imageLoading + '"></a></div></div></div><div id="lightbox-container-image-data-box"><div id="lightbox-container-image-data"><div id="lightbox-image-details"><span id="lightbox-image-details-caption"></span><span id="lightbox-image-details-currentNumber"></span></div></div></div></div>');
            var arrPageSizes = ___getPageSize();
            var arrPageScroll = ___getPageScroll();
            $('#jquery-overlay').css({
                backgroundColor: settings.overlayBgColor,
                opacity: settings.overlayOpacity,
                width: arrPageSizes[0],
                height: arrPageSizes[1]
            }).fadeIn();
            $('#jquery-lightbox').css({
                top: arrPageScroll[1] + (arrPageSizes[3] / 10),
                left: arrPageScroll[0]
            }).show();
            if (settings.hideOnClickOutside) $('#jquery-overlay,#jquery-lightbox').click(function() {
                _finish();
            });
            $('#lightbox-loading-link,#lightbox-secNav-btnClose').click(function() {
                _finish();
                return false;
            });
            $(window).resize(function() {
                // Get page sizes
                var arrPageSizes = ___getPageSize();
                // Style overlay and show it
                $('#jquery-overlay').css({
                    width: arrPageSizes[0],
                    height: arrPageSizes[1]
                });
                var arrPageScroll = ___getPageScroll();
                $('#jquery-lightbox').css({
                    top: arrPageScroll[1] + (arrPageSizes[3] / 10),
                    left: arrPageScroll[0]
                });
            });
        }

        function _set_image_to_view() {
            $('#lightbox-image,#lightbox-nav,#lightbox-nav-btnPrev,#lightbox-nav-btnNext,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
            //$("#lightbox-image").html(settings.placeHolder.innerHTML)
//            $(settings.placeHolder).appendTo( $("#lightbox-image") );

//            document.getElementById( "lightboxTable" ).style.display = "block";


            $("#lightbox-image").html(
                '<a href="javascript:void(0)" id="lightbox-secNav-btnClose"><img src="' + settings.imageBtnClose + '"></a><br />'
                + document.getElementById("lightboxTable").innerHTML);

            _resize_container_image_box($("#lightbox-image").width(), $("#lightbox-image").height());
        }

        function _resize_container_image_box(intImageWidth, intImageHeight) {
            var intCurrentWidth = $('#lightbox-container-image-box').width();
            var intCurrentHeight = $('#lightbox-container-image-box').height();
            var intWidth = (intImageWidth + (settings.containerBorderSize * 2));
            var intHeight = (intImageHeight + (settings.containerBorderSize * 2));
            var intDiffW = intCurrentWidth - intWidth;
            var intDiffH = intCurrentHeight - intHeight;
            $('#lightbox-container-image-box').animate({
                width: intWidth,
                height: intHeight
            },
            settings.containerResizeSpeed,
            function() {
                _show_image()
            });
            if ((intDiffW == 0) && (intDiffH == 0)) {
                if ($.browser.msie) ___pause(250);
                else ___pause(100)
            }
            $('#lightbox-container-image-data-box').css({
                width: $.browser.msie & $.browser.version == "6.0" ? intWidth: intImageWidth
            })
            $('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css({
                height: intImageHeight + (settings.containerBorderSize * 2)
            })
        }

        function _show_image() {
            $('#lightbox-loading').hide();
            $('#lightbox-image').fadeIn(function() {
                _show_image_data();
            });
        }

        function _show_image_data() {
            $('#lightbox-container-image-data-box').slideDown('fast')
            $('#lightbox-image-details-caption').hide()
        }

        function _keyboard_action(objEvent) {
            if (objEvent == null) keycode = event.keyCode;
            else keycode = objEvent.keyCode
            if (keycode == 27) _finish()
            else {
                key = String.fromCharCode(keycode).toLowerCase();
                if ((key == settings.keyToClose) || (key == 'x')) _finish();
            }
        }

        function _finish() {
            settings.alreadyOpened = false
            $('#jquery-lightbox').remove();
            $('#jquery-overlay').fadeOut(function() {
                $('#jquery-overlay').remove();
            });
            /* other end of problem-causing piece; not needed since underlying flash has wmode set
            $('embed, object, select').css({
                'visibility': 'visible'
            });
            */
//            document.getElementById( "lightboxTable" ).style.display = "none";
        }
        /**
         / THIRD FUNCTION
         * getPageSize() by quirksmode.com
         *
         * @return Array Return an array with page width, height and window width, height
         */
        function ___getPageSize() {
            var xScroll,
            yScroll;
            if (window.innerHeight && window.scrollMaxY) {
                xScroll = window.innerWidth + window.scrollMaxX;
                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
                if (document.documentElement.clientWidth) {
                    windowWidth = document.documentElement.clientWidth;
                } else {
                    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 = xScroll;
            } else {
                pageWidth = windowWidth;
            }
            arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
            return arrayPageSize;
        };
        /**
         / THIRD FUNCTION
         * getPageScroll() by quirksmode.com
         *
         * @return Array Return an array with x,y page scroll values.
         */
        function ___getPageScroll() {
            var xScroll,
            yScroll;
            if (self.pageYOffset) {
                yScroll = self.pageYOffset;
                xScroll = self.pageXOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) {
                // Explorer 6 Strict
                yScroll = document.documentElement.scrollTop;
                xScroll = document.documentElement.scrollLeft;
            } else if (document.body) {
                // all other Explorers
                yScroll = document.body.scrollTop;
                xScroll = document.body.scrollLeft;
            }
            arrayPageScroll = new Array(xScroll, yScroll);
            return arrayPageScroll;
        };
        /**
          * Stop the code execution from a escified time in milisecond
          *
          */
        function ___pause(ms) {
            var date = new Date();
            curDate = null;
            do {
                var curDate = new Date();
            }
            while (curDate - date < ms);
        };
        // Return the jQuery object for chaining. The unbind method is used to avoid click conflict when the plugin is called more than once
        return this.unbind('click').click(_initialize);
    };
})(jQuery);
// Call and execute the function immediately passing the jQuery object
