jQuery.fn.customLightbox = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		thumbnail:'a',
		lightboxContentBlock: 'div.lightbox',
		lightboxImage:'img.big-image',
		next:'a.link-next',
		prev:'a.link-prev',
		title:'p',
		name:'h3.gallery-name',
		load:'img.load',
		galleryName:'LFH BILLEDGALLERI',
		faderOpacity: 0.9,
		faderBackground: '#000c1e',
		closeLink:'a.close',
		paddingLeft:17,
		paddingRight:17,
		paddingTop:37,
		paddingBottom:38,
		lbMinWidth:360,		
		onClick: null
	},_options);

	return this.each(function(i, _this){
		var _this = jQuery(_this);
		var _lightboxCode = '\
			<div class="popup lightbox"> \
				<div class="bgtop"><div>&nbsp;</div></div> \
				<div class="holder"> \
					<div class="title"> \
						<div> \
							<h3 class="gallery-name">'+_options.galleryName+'</h3> \
							<a class="close" href="#">LUKK VINDU</a> \
						</div> \
					</div> \
					<div class="imageblock"> \
						<div class="image"> \
							<img alt="" class="big-image" src="" /> \
							<img src="/gfx/load.gif" class="load" alt="" /> \
						</div> \
					</div> \
					<p>&nbsp;</p> \
					<div class="nav"> \
						<div> \
							<ul> \
								<li class="first"><a class="link-prev" href="#">FORRIGE BILDE</a></li> \
								<li><a class="link-next" href="#">NESTE BILDE</a></li> \
							</ul> \
						</div> \
					</div> \
				</div> \
				<div class="bgbot"><div>&nbsp;</div></div> \
			</div> \
		';
		if (!jQuery(_options.lightboxContentBlock).length)
				$('body').append(_lightboxCode);
		_lightbox = jQuery(_options.lightboxContentBlock);
		_lightbox.css({
			width:_options.lbMinWidth,
			display:'none',
			position:'absolute',
			top:0,
			left:0
		});
		
		if (!jQuery('div.lightbox-fader').length)
				$('body').append('<div class="lightbox-fader"></div>');
		_fader = jQuery('div.lightbox-fader');
		_fader.css({
			opacity:_options.faderOpacity,
			backgroundColor:_options.faderBackground,
			display:'none',
			position:'absolute',
			top:0,
			left:0,
			zIndex:998,
			textIndent: -9999
		}).text('$nbsp');
		
		var _currentImage = 0;
		var _thumbnails = jQuery(_options.thumbnail, _this);
		var _closeLink = jQuery(_options.closeLink, _lightbox);
		var _mainImage = jQuery(_options.lightboxImage, _lightbox);
		var _load = jQuery(_options.load, _lightbox);
		var _title = jQuery(_options.title, _lightbox);
		var _linkNext = jQuery(_options.next, _lightbox);
		var _linkPrev = jQuery(_options.prev, _lightbox);
		var _imageHolder = _mainImage.parent();
		var _image = {};
		var _top = 0;
		var _left = 0;
		
		_thumbnails.click(function(){
			if (jQuery.isFunction(_options.onClick)) {
				_options.onClick.apply(_this);
			}
			_currentImage = _thumbnails.index(jQuery(this));
			
			_fader.fadeIn(300, function(){
				_mainImage.hide();
				_title.hide();
				_lightbox.fadeIn(400, function(){
					onloadImage(_currentImage);
				});
				positionLightbox(_lightbox);
			});
			return false;
		});
		_linkNext.click(function(){
			_currentImage += 1;
			if (_currentImage >= _thumbnails.length) _currentImage = 0;
			onloadImage(_currentImage);			
			return false;
		});
		_linkPrev.click(function(){
			_currentImage -= 1;
			if (_currentImage < 0) _currentImage = _thumbnails.length-1;
			onloadImage(_currentImage);			
			return false;
		});
		_closeLink.click(function(){
			_lightbox.fadeOut(400, function(){
				_fader.fadeOut(300);
			});
			return false;
		});
			
		function onloadImage(_cur) {
			_mainImage.hide();
			_load.show();
			_title.slideUp(300);
			var _img = new Image();
			_img.onload = function(){
				_image = _img;
				_image.w = this.width;
				_image.h = 500;//this.height;
				showImage();
				_img.onload = function(){};
			}
			_img.src = _thumbnails.eq(_cur).attr('href');
		}
		function showImage(){
			var _dinWidth = _image.w + _options.paddingLeft + _options.paddingRight;
			var _dinHeight = _image.h;
			var _titleText = _thumbnails.eq(_currentImage).attr('title');
			if (_dinWidth < _options.lbMinWidth) _dinWidth = _options.lbMinWidth;
			var _pos = setLocation();			
			_imageHolder.animate({height:_dinHeight},{queue:false, duration:400});
			_lightbox.animate({
				width:_dinWidth,
				top:_pos.top,
				left:_pos.left
			},{queue:false, duration:400, complete:function(){
				_load.hide();
				_mainImage.attr('src',_image.src).fadeIn(300);
				if (_titleText != '') {
					_title.text(_titleText).slideDown(300);
				}
			}});
		}
		function setLocation(){
			var _height = 0;
			var _width = 0;
			var _offset = {};
			if (window.innerHeight) {
				_height = window.innerHeight;
				_width = window.innerWidth;
			} else {
				_height = document.documentElement.clientHeight;
				_width = document.documentElement.clientWidth;
			}
			var _thisHeight = _image.h + _options.paddingTop + _options.paddingBottom;
			var _thisWidth = _image.w + _options.paddingLeft + _options.paddingRight;
			if (_thisWidth < _options.lbMinWidth) _thisWidth = _options.lbMinWidth;
			var _page = $('body > div:eq(0)');
			if (_lightbox.length) {
				if (_height > _thisHeight) {
					if (!window.innerHeight) {
						_lightbox.css({position:'absolute'});
						_offset.top = (document.documentElement.scrollTop + (_height - _thisHeight) / 2)
					} else {
						_lightbox.css({position:'fixed'});
						_offset.top = ((_height - _thisHeight) / 2)
					}
				}
				else {
					_lightbox.css({position:'absolute'});
					_offset.top = 0
				}
				if (_width > _thisWidth) 
					_offset.left  = (_width - _thisWidth) / 2;
				else {
					_lightbox.css({position:'absolute'})
					_offset.left = 0;
				};
			}
			return _offset;
		}
		function positionLightbox (_lbox) {
			var _height = 0;
			var _width = 0;
			var _minWidth = $('body > div:eq(0)').outerWidth();
			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 (_height > _page.innerHeight()) _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) {
					if (!window.innerHeight) {
						_lbox.css({
							position:'absolute',
							top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
						});
					} else {
						_lbox.css({
							position:'fixed',
							top: ((_height - _lbox.outerHeight()) / 2)+"px"
						});
					}
				}
				else {
					_lbox.css({
						position:'absolute',
						top: 0
					});
				}
				if (_width > _lbox.outerWidth()) _lbox.css({left:(_width - _lbox.outerWidth()) / 2 + "px"});
				else _lbox.css({position:'absolute',left: 0});
			}
		}
			
		jQuery(window).resize(function(){
			positionLightbox(_lightbox);
		});
			
		$(document).keydown(function (e) {
			if (!e) evt = window.event;
			if (e.keyCode == 27) {
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
				});
			}
		});
	});
}
$(document).ready(function(){
	$('ul.gallist').customLightbox();
});



	
