(function($) {
	$.fn.lightBox = function(settings) {
		// Settings to configure the jQuery lightBox plugin how you like
		settings = jQuery.extend({
                    overlayBgColor:         '#000',
                    overlayOpacity:         0.2,
                    fixedNavigation:        false,
                    imageLoading:           '/jquery/lightbox-ico-loading.gif',
                    imageBtnClose:          '/jquery/lightbox-btn-close.gif',
                    imageBlank:             '/jquery/lightbox-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)
                    $(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
                        $('embed, object, select').css({ 'visibility' : 'hidden' });
                        var target = $('#' + objClicked.rel);
                        _set_interface();
                        _set_image_to_view(target);
                        $("#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><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"><img src="' + settings.imageBtnClose + '"></a></div></div></div></div>');	
                    var arrPageSizes = ___getPageSize();
                    $('#jquery-overlay').css({
                        backgroundColor:    settings.overlayBgColor,
                        opacity:            settings.overlayOpacity,
                        width:              arrPageSizes[0],
                        height:             arrPageSizes[1]
                    }).fadeIn();
                    var arrPageScroll = ___getPageScroll();
                    $('#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(target) {
                    $('#lightbox-image,#lightbox-nav,#lightbox-nav-btnPrev,#lightbox-nav-btnNext,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
                    $("#lightbox-image").html(target.html())
                    //$(settings.placeHolder).appendTo($("#lightbox-image"))
                    _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: 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(); });
                    $('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
