/**
    simpleLightBox.

 */
jQuery.fn.simpleLightbox = function(_options){
	// defaults options
	var _options = jQuery.extend({
		lightboxContentBlock: '.lightbox',
		faderOpacity: 0.8,
		faderBackground: '#ffffff',
		closeLink:'a.close-btn, a.cancel',
		href:true,
		onClick: null
	},_options);

	var _popupCounter = 1;

	/* initialize all available refs to lightboxes in the page */
	return this.each(function(i, _this){
		var _this = jQuery(_this);	/* wrap _this dom element into jQuery */
		if (!_options.href)			/* fill lightboxContentBlock from options or href */
			_this.lightboxContentBlock = _options.lightboxContentBlock;
		else _this.lightboxContentBlock = _this.attr('href');
		
		if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {  // do we have some content?
			_this.faderOpacity = _options.faderOpacity;			/* set options */
			_this.faderBackground = _options.faderBackground;
			_this.closeLink = _options.closeLink;
			var _fader;
			var _lightbox = $(_this.lightboxContentBlock);
			
			if (!jQuery('div.lightbox-fader').length)			/* do we have a lightbox-fader div? If not, create one and append */
				_fader = $('body').append('<div class="lightbox-fader"></div>');
			_fader = jQuery('div.lightbox-fader');
			
			_lightbox.css({		/* place the lightbox on top, 999 should be ok */
				'zIndex':999
			});
			_fader.css({		/* init fader-div, this div will gray out the background */
				opacity:_this.faderOpacity,
				backgroundColor:_this.faderBackground,
				display:'none',
				position:'absolute',
				top:0,
				left:0,
				zIndex:998,
				textIndent: -9999
			}).text('&nbsp;');
			
			_lightbox.shownFlag = false;
			
			/* onClick function for lightbox reference */
			_this.click(function(){
				if (jQuery.isFunction(_options.onClick)) {		/* do we have a custom onClick, apply it */
					_options.onClick.apply(_this);
				}

				var _popupURL = _this.attr('href');						/* get the href, we need to use for the lightbox */
				if(jQuery('div[rel*="'+_popupURL+'"]').length == 0) {	/* first check if the content isn't already loaded */
					$.ajax({											/* make an AJAX-call, to laod the html to show */
						url: _popupURL,
						global: false,
						type: "GET",
						dataType: "html",
						success: function(msg){						/* we received the output from the call, let's process it */
							// append loaded popup
							_lightbox = $(msg);
							_lightbox.attr('rel',_popupURL).css({		/* place it on top, and out of reach - not visible, so we can reach it */
								zIndex:999,
								position:'absolute',
								display:'block',
								top: -9999,
								left: -9999
							});
							_lightbox.attr('id','ajaxpopup'+_popupCounter);		/* add a unique id, so we can identify it */
							jQuery('body').append(_lightbox);					/* inject into page */

							// init js for lightbox
							if(typeof initTabs == "function"){					/* run initialisation for tabs in the page */
								initTabs(_lightbox.find('.tabset'));
							}
							if(typeof sIFR == "function"){						/* any font-replacement needed? sIFR is setup in sifr.js */
								sIFR.replaceElement(named({sSelector:"#ajaxpopup" + _popupCounter + " h1 span", sFlashSrc:"swf/legacysans.swf", sWmode:"transparent", sColor:"#002663", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0"}));
							};
							_popupCounter++;

							// attach close event
							jQuery(_this.closeLink, _lightbox).click(function(){		/* append click-handler to close-buton */
								_lightbox.fadeOut(400, function(){
									_fader.fadeOut(300);
									_scroll = false;
								});
								return false;
							});

							// show lightbox
							_lightbox.hide();											/* hide it, so we can fade it in */
							_lightbox.shownFlag = true;
							jQuery.fn.simpleLightbox.positionLightbox(_lightbox);		/* center box, see extension-function positionLightBox() */
							_fader.fadeIn(300, function(){							/* and let it shine in... */
								_lightbox.fadeIn(400);
								jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
							});
						},
						error: function(msg){									/* oeps, error, if custom msg needed, add it here */
							alert('ajax error');
							return false;
						}
					});
				} else {
					_lightbox.hide();											/* html was already loaded, just fade it in */
					_lightbox.shownFlag = true;
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					_fader.fadeIn(300, function(){
						_lightbox.fadeIn(400);
						jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					});
				}
				return false;
			});
			jQuery(_this.closeLink).click(function(){
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
					_scroll = false;
				});
				return false;
			});
			_fader.click(function(){
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
				});
				return false;
			});
			
			
			var _scroll = false;		/* what to do with scroll-behaviour? */
			
			jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
				if(!_lbox.shownFlag) return false;
				var _height = 0;
				var _width = 0;
				var _minWidth = $('body > div:eq(0)').outerWidth();	/* get the width of the first div in the body */
				if (window.innerHeight) {								/* determine current width/height of browserwindow */
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				var _thisHeight = _lbox.outerHeight();					/* what is the height of the popup-lightbox? */
				var _page = $('body');
				if (_lbox.length) {										/* let's position top and left */
				
					if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}		/* change the width/height for fader-div */
					if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
					
					if (_height > _thisHeight) {									/* position lightbox, it fits into the window */
						if ($.browser.msie && $.browser.version < 7) {
							_lbox.css({
								position:'absolute',
								top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
							});
						} else {
							_lbox.css({
								position:'fixed',
								top: ((_height - _lbox.outerHeight()) / 2)+"px"
							});
						}
					}
					else {
						var _fh = parseInt(_fader.css('height'));					/* position lightbox, it seems to be to large for the window */
						if (!_scroll) {
						if (_fh - _thisHeight > parseInt($(document).scrollTop())) {	/* is the preferred position on the page out of range, compared to scrollTop() */
								_fh = parseInt($(document).scrollTop())				/* probably zero... */
								_scroll = _fh;
							} else {
								_scroll = _fh - _thisHeight;							/* ok, we have a new top-position! */
							}
						}
						_lbox.css({													/* absolute positioning lightbox top-attribute */
							position:'absolute',
							top: _scroll
						});
					}
					
					if (_width > _lbox.outerWidth()) _lbox.css({left:((_width - _lbox.outerWidth()) / 2 + 10) + "px"});		/* width is so much simpler..., if it fits: calculate center, otherwise ultimate left */
					else _lbox.css({position:'absolute',left: 0});
				}
			}
			
			jQuery(window).resize(function(){			/* reposition if window resizes */
				if (_lightbox.is(':visible'))
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});	
			jQuery(window).scroll(function(){			/* reposition if user scrolls */
				if (_lightbox.is(':visible'))
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			
			jQuery.fn.simpleLightbox.positionLightbox(_lightbox);		/* handle esc-key */
				$(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.fadeOut(400, function(){
						_fader.fadeOut(300);
					});
				}
			});
		}
	});
}