$(document).ready(function(){
	initLightbox();
})
function initLightbox(){
	var _fader, _lightbox;
	if (!jQuery('div.lightbox-content').length)
		jQuery('body').append('<div class="lightbox-content"></div>');
	if (!jQuery('div.lightbox-fader').length)
		jQuery('body').append('<div class="lightbox-fader"></div>');
	if (!jQuery('div.lightbox-error').length)
		jQuery('body').append('<div class="lightbox-loader"><img src="/buildpeople/images/loader.gif" alt="image"></div>');

	_lightbox = jQuery('div.lightbox-content');
	_loader = jQuery('div.lightbox-loader');
	_fader = jQuery('div.lightbox-fader');
	_fader.opacity= 0.75;
	_lightbox.showTime = 300;
		if ($.browser.msie && $.browser.version > 6) {_lightbox.showTime = 0;}
	_lightbox.css({
		opacity:0
	}); 
	_loader.css({
		display:'none',
		position:'absolute',
		top: '50%',
		left:'50%',
		zIndex:998
	})
	_fader.css({
		backgroundColor: '#2b4542',
		display:'none',
		position:'fixed',
		top:0,
		left:0,
		zIndex:990,
		textIndent: -9999
	}).text('&nbsp;');
	if(typeof(document.body.style.maxHeight) == 'undefined'){
		var _frame = $('<iframe src="javascript:false" />');
		_fader.html(_frame);
		_frame.css({
			opacity: 0,
			clear: 'both',
			position: 'absolute',
			left: 0,
			top: 0,
			width: "100%",
			height: "100%",
			zIndex: 0
		});
	}

	var _links = $('a.open-popup');
	_links.click(function(){
		var _this = jQuery(this);
		if (_this.attr('href').indexOf('#')==-1){
			_this.href = _this.attr('href');
			$.ajax({
				url: _this.href,
				cache: false,
				success: function(msg){
					_lightbox.html(msg);
					_loader.show();
					var _images = _lightbox.find('img');
					if (_images.length){
						var _imgCount = _images.length;
						var _imgLoaded = 0;
						_images.each(function(){
							var _img = new Image();
							_img.onload = function(){
								_imgLoaded =_imgLoaded+1;
								if(_imgLoaded >= _imgCount) {
									showLightbox();
								};
							};
							_img.src = $(this).attr('src');
						});
					} else showLightbox();
				},
				error: function(msg){
					_this.flag = true;
					_loader.show().html('Error loading');

					jQuery(document).keydown(function (e) {
						if (!e) evt = window.event;
						if (e.keyCode == 27) {
							_loader.fadeOut(300);
						}
					});
				}
			});
		}else{
			_lightbox = $(_this.attr('href'));
			_loader.show();
			var _images = _lightbox.find('img');
			if (_images.length){
				var _imgCount = _images.length;
				var _imgLoaded = 0;
				_images.each(function(){
					var _img = new Image();
					_img.onload = function(){
						_imgLoaded =_imgLoaded+1;
						if(_imgLoaded >= _imgCount) {
							showLightbox();
						};
					};
					_img.src = $(this).attr('src');
				});
			} else showLightbox();
		}

		function showLightbox(){
			var _close = _lightbox.find('a.close');
			lightboxPosition();
			_loader.hide();
			_fader.css({opacity:0, display:'block'}).animate({opacity: _fader.opacity},400,function(){
				if ($.browser.safari) {
					_lightbox.children(':eq(0)').css({
						top: document.body.scrollTop + 25
					});
				} else {
					_lightbox.children(':eq(0)').css({
						top: document.documentElement.scrollTop + 25
					});
				}
				_lightbox.css({opacity:0, display:'block'}).animate({opacity: 1},_lightbox.showTime,function(){
					_lightbox.css({opacity: 'auto'});
					var _btnAssistance = $('a.btn-assistance');
					var _btnSuccessfully = $('a.btn-successfully');
					_btnAssistance.click(function(){
						hideLightbox();
						_this.removeClass('active');
						return false;
					});
					_btnSuccessfully.click(function(){
						hideLightbox();
						_this.addClass('active');
						return false;
					})
				});
			});

			//close lightbox
			_close.click(function(){
				hideLightbox();
				return false;
			})
		}
		function hideLightbox(){
			_fader.animate({opacity:0},400,function(){
				_fader.css({display: 'none'});
			});
			_lightbox.animate({opacity: 0},_lightbox.showTime,function(){
				_lightbox.css({display: 'none'});
			});
		}

		_fader.click(function(){
			hideLightbox();
		})
		
		jQuery(window).resize(function(){
			lightboxPosition();
		});
		function lightboxPosition(){
			var _height = 0;
			var _width = 0;
			var _minWidth = jQuery('body > div:eq(0)').outerWidth();
			var _page = jQuery('body > div:eq(0)');
			if (window.innerHeight) {
				_height = window.innerHeight;
				_width = window.innerWidth;
			} else {
				_height = document.documentElement.clientHeight;
				_width = document.documentElement.clientWidth;
			}
			var _thisHeight = _lightbox.outerHeight();
			if (_lightbox.length) {
				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) {
						if (!window.innerHeight) {
							_fader.css({
								position:'absolute',
								top: 0
							});
						};
					};
			}
			if (_width < _minWidth || _width < _lightbox.width()){
				_lightbox.children().css({
					left: 0,
					marginLeft: 0
				});
			}else{
				_lightbox.children().css({
					left: '',
					marginLeft: ''
				});
			}

			jQuery(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.fadeOut(_lightbox.showTime, function(){
						_fader.fadeOut(300);
					});
				}
			});
		}
		return false;
	})

}