(function(jQuery) {
	jQuery.fn.lightBox = function(settings) {
		// Settings to configure the jQuery lightBox plugin how you like
		settings = jQuery.extend({
                    overlayBgColor:         '#000',
                    overlayOpacity:         0.8,
                    fixedNavigation:        false,
                    imageLoading:           '/extension/ezwebin/design/fbi/images/gallery/loading.gif',
                    imageBtnClose:          '/extension/ezwebin/design/fbi/images/gallery/closelabel.gif',
                    imageBlank:             '/extension/ezwebin/design/fbi/images/blank.gif',
                    containerBorderSize:    10,
                    containerResizeSpeed:   400,
                    txtImage:               'Image',
                    txtOf:                  'of',
                    keyToClose:             'c',
                    placeHolder:            null,
                    timeout:                -1,
                    hideOnClickOutside:     true,
                    alreadyOpened:          false
		}, settings);

		var jQueryMatchedObj = this; 

		function _initialize() {
									   _start(this, jQueryMatchedObj)
                    jQuery(document).keydown(function(objEvent) {
                        _keyboard_action(objEvent)
                    })
                    if (settings.timeout != -1) setTimeout("jQuery('#lightbox-secNav-btnClose').click()", settings.timeout)
                    return false;
		}

		function _start(objClicked,jQueryMatchedObj) {
                    if (!settings.alreadyOpened) {
                        settings.alreadyOpened = true;
                        jQuery('embed, object, select').css({ 'visibility' : 'hidden' });
                        jQuery('.lightbox').show();
                        _set_interface();
                        settings.placeHolder = objClicked
                        _set_image_to_view();
                        jQuery("#lightbox-image object").css("visibility", "visible")
                        jQuery("#lightbox-image embed").css("visibility", "visible")
                        jQuery("#lightbox-image object embed").css("visibility", "visible")
                        
                    }
                     return false;
		}

		function _set_interface() {
                    jQuery('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><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 id="lightbox-secNav"><a href="#" id="lightbox-secNav-btnClose" class="button">Luk</a></div></div></div></div>');	
                    var arrPageSizes = ___getPageSize();
                    jQuery('#jquery-overlay').css({
                        backgroundColor:    settings.overlayBgColor,
                        opacity:            settings.overlayOpacity,
                        width:              arrPageSizes[0],
                        height:             arrPageSizes[3]
                    }).fadeIn();
                    var arrPageScroll = ___getPageScroll();
                    jQuery('#jquery-lightbox').css({
                        top:    arrPageScroll[1] + (arrPageSizes[3] / 10),
                        left:   arrPageScroll[0]
                    }).show();
                    if (settings.hideOnClickOutside) jQuery('#jquery-overlay,#jquery-lightbox').click(function() {
                        _finish();
                    });
                    jQuery('#lightbox-loading-link,#lightbox-secNav-btnClose').click(function() {
                        _finish();
                        return false;
                    });
                    jQuery(window).resize(function() {
                        // Get page sizes
                        var arrPageSizes = ___getPageSize();
                        // Style overlay and show it
                        jQuery('#jquery-overlay').css({
                            width:		arrPageSizes[0],
                            height:		arrPageSizes[1]
                        });
                        var arrPageScroll = ___getPageScroll();
                        jQuery('#jquery-lightbox').css({
                            top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
                            left:	arrPageScroll[0]
                        });
                    });
		}

		function _set_image_to_view() {
                    jQuery('#lightbox-image,#lightbox-nav,#lightbox-nav-btnPrev,#lightbox-nav-btnNext,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
                    //jQuery("#lightbox-image").html(settings.placeHolder.innerHTML)
                   jQuery(settings.placeHolder).appendTo(jQuery("#lightbox-image"))
                    _resize_container_image_box(jQuery("#lightbox-image").width(), jQuery("#lightbox-image").height())
		}

		function _resize_container_image_box(intImageWidth, intImageHeight) {
                    var intCurrentWidth = jQuery('#lightbox-container-image-box').width();
                    var intCurrentHeight = jQuery('#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;
                    jQuery('#lightbox-container-image-box').animate({ width: intWidth, height: intHeight }, settings.containerResizeSpeed, function() {
                        _show_image()
                    });
                    if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
                        if ( jQuery.browser.msie ) ___pause(250);
                        else ___pause(100)
                    }
                    jQuery('#lightbox-container-image-data-box').css({ width: jQuery.browser.msie & jQuery.browser.version == "6.0" ? intWidth : intImageWidth})
                    jQuery('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css({ height: intImageHeight + (settings.containerBorderSize * 2) })
		}

		function _show_image() {
                    jQuery('#lightbox-loading').hide();
                    jQuery('#lightbox-image').fadeIn(function() {
                        _show_image_data();
                    });
		}

		function _show_image_data() {
                    jQuery('#lightbox-container-image-data-box').slideDown('fast')
                    jQuery('#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('#jquery-lightbox').remove();
                    jQuery('#jquery-overlay').fadeOut(function() { jQuery('#jquery-overlay').remove(); });
                    jQuery('embed, object, select').css({ 'visibility' : 'visible' });
		}
		/**
		 / 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

