jQuery.fn.simpleLightbox = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		lightboxContentBlock: '.nonelight',
		faderOpacity: 1,
		faderBackground: '#595959',
		closeLink:'a.btn-close, .close',
		href:true,
		onShow: null,
		onClick: null
	},_options);

	return this.each(function(i, _this){
		var _this = jQuery(_this);
		var _d = $.browser.msie ? 0 : 300;
		var _posTimer = null , _posTimer2 = null;
		if (!_options.href)
			_this.lightboxContentBlock = _options.lightboxContentBlock;
		else _this.lightboxContentBlock = _this.attr('href');
		if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
			_this.faderOpacity = _options.faderOpacity;
			_this.faderBackground = _options.faderBackground;
			_this.closeLink = _options.closeLink;
			var _fader;
			var _lightbox = $(_this.lightboxContentBlock);
			if (!jQuery('div.lightbox-fader').length)
				_fader = $('body').append('<div class="lightbox-fader"></div>');
			
			_fader = jQuery('div.lightbox-fader');
			_lightbox.css('zIndex',999);
			_fader.css({
				opacity:_this.faderOpacity,
				backgroundColor:_this.faderBackground,
				display:'none',
				position:'absolute',
				top:0,
				left:0,
				zIndex:998,
				textIndent: -9999
			}).text('$nbsp');
			if(typeof(document.body.style.maxHeight) == 'undefined'){
				var _frame = $('<iframe />');
				_fader.html(_frame);
				_frame.css({
					opacity: 0,
					clear: 'both',
					position: 'absolute',
					left: 0,
					top: 0,
					width: "100%",
					height: "100%",
					zIndex: 0
				});
				var _newdiv = $('<div />');
				_newdiv.css({
					background: '#f00',
					opacity:0,
					clear: 'both',
					position: 'absolute',
					left: 0,
					top: 0,
					width: "100%",
					height: "100%",
					zIndex: '90'
				});
				_fader.append(_newdiv);
			}
			_lightbox.shownFlag = false;

			_this.click(function(){
				if (jQuery.isFunction(_options.onClick)) {
					_options.onClick.apply(_this);
				}
				if (_fader.is(":visible")) {
					$(".lightbox").each(function(){
						$(this).css("display","none");
					});
					_lightbox.shownFlag = true;
					_lightbox.fadeIn(0);
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				} else {
				_fader.fadeIn(10, function(){
					_lightbox.shownFlag = true;
					_fader.shownFlag = true; 
					_lightbox.fadeIn(20, function(){
						if (jQuery.isFunction(_options.onShow)) {
							_options.onShow.apply(_this);
						}
					});
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				});
				}
				return false;
			});
			_lightbox.bind('lbclose',function(){
				_lightbox.fadeOut(0, function(){
					_fader.fadeOut(0);
					$(this).css('top','');
				});
			})
			jQuery(_this.closeLink, _lightbox).click(function(){
				_lightbox.trigger('lbclose');
				return false;
			});
			_fader.click(function(){
				_lightbox.trigger('lbclose');
				return false;
			});

			jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
				if(!_lbox.shownFlag) return false;
				var _height = 0;
				var _width = 0;
				var _minWidth = $('body > div:eq(0)').outerWidth();
				_minWidth = 1000;
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				var _thisHeight = _lbox.outerHeight();
				var _page = $('body > div:eq(0)');
				if (_lbox.length) {
					if(_thisHeight > _height){
						if (_thisHeight > _page.outerHeight(true)) _fader.css('height',_thisHeight); else _fader.css('height',_page.innerHeight())
					}else if (_height > _page.outerHeight(true)) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
					if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
					if (_height > _thisHeight){
						_lbox.css({position:'absolute'});
						var _top = document.documentElement.scrollTop + document.body.scrollTop;
						if(_posTimer) clearTimeout(_posTimer);
						_posTimer = setTimeout(function(){
							_lbox.animate({top: (_top + (_height - _thisHeight) / 2)}, 200);
						},300);
					}
					else {
						_lbox.css({
							position:'absolute',
							top: 0
						});
					}
					if(_posTimer2) clearTimeout(_posTimer2);
					_posTimer2 = setTimeout(function(){
/* 						if (_width > _lbox.outerWidth()) _lbox.css({left:(_width - _lbox.outerWidth()) / 2 + "px"});
						else _lbox.css({position:'absolute',left: 0}); */
					},300);
				}
			}
			
			jQuery(window).resize(function(){
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			jQuery(window).scroll(function(){
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			
			jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				$(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.trigger('lbclose');
				}
			});
		}
	});
}

$(function(){
	$('.gallery-main a').click(function(){return false;})
	jQuery('a.enlarge').simpleLightbox({
		lightboxContentBlock: '#lightbox',
		href: false,
		faderOpacity: 1,
		faderBackground: '#595959',
		closeLink:'a.btn-close, .close',
		onShow: function(){
			$('#lightbox .image-holder img').attr('src', this.attr('href')).parent().width($('#lightbox .image-holder img').width());
			var _w = $('#lightbox .image-holder img').width() < 700 ? 700 : $('#lightbox .image-holder img').width();
		}
	});
});
