
jQuery.fn.simpleLightbox = function(_options){
	// defaults options
	var _options = jQuery.extend({
		lightboxContentBlock: '.lightbox',
		faderOpacity: 0.5,
		faderBackground: '#676767',
		closeLink:'a.btn-close',
		href:true,
		onClick: null
	},_options);

	var _popupCounter = 1;

	return this.each(function(i, _this){
		var _this = jQuery(_this);
		//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;');
			_lightbox.shownFlag = false;
			_this.click(function(){
				if (jQuery.isFunction(_options.onClick)) {
					_options.onClick.apply(_this);
				}

				var _popupURL = _this.attr('href');							
				//deletes the div already created for popuup if exists and creates new
				jQuery('div[rel*="'+_popupURL+'"]').remove();
				
				if(jQuery('div[rel*="'+_popupURL+'"]').length == 0) {
					$.ajax({
						url: _popupURL,
						global: false,
						type: "GET",
						dataType: "html",
						success: function(msg){
							// append loaded popup
							_lightbox = $(msg);
							_lightbox.attr('rel',_popupURL).css({
								zIndex:999,
								position:'absolute',
								display:'block',
								top: -9999,
								left: -9999
							});
							//_lightbox.attr('id','ajaxpopup'+_popupCounter);
							_lightbox.attr('id','ajaxpopup');
							
							jQuery('body').append(_lightbox);

							/*if(typeof sIFR == "function"){
								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(){
								//alert("Temporary test message: lightbox close signal.");
								killOrphanDropdowns();
								_lightbox.fadeOut(400, function(){
									_fader.fadeOut(300, function(){
										setTimeout("destroyPopup()", 100);
									});
									_scroll = false;
									//if contains swf player clear the player html div
									if($(".player"))
									{	
									    $(".player").html("");									  
									}
									//$('ajaxpopup'+_popupCounter).remove();
								});
								return false;
							});

							// show lightbox
							_lightbox.hide();
							_lightbox.shownFlag = true;
							jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
							_fader.fadeIn(300, function(){
								_lightbox.fadeIn(400);
								if(typeof initCastomForms == "function"){
									initCastomForms(_lightbox.get(0));
								}
								if(typeof VSA_initScrollbars == "function"){
									VSA_initScrollbars(_lightbox.get(0));
								}
								if(typeof initPopup == "function"){
									initPopup({
										openEvent:'click'
									});
									initPopup({
										popupHolderClass:'popup-hover'
									});
								}
								jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
							});
						},
						error: function(request, error){
							alert('ajax error: ' + request.status + ' / ' + request.responseText + ' / ' + error);
							return false;
						}
					});
				} else {
					_lightbox.hide();
					_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(){
				killOrphanDropdowns();
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
					//if contains swf player clear the player html div
					if($(".player"))
				   {
					 $(".player").html("");
				   }
				   
				});
				return false;
			});
			var _scroll = 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();
				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 (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
					if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
					if (_height > _thisHeight) {
						_lbox.css({
							position:'absolute',
							top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
						});
					}
					else {
						var _fh = parseInt(_fader.css('height'));
						if (!_scroll) {
						if (_fh - _thisHeight > parseInt($(document).scrollTop())) {
								_fh = parseInt($(document).scrollTop())
								_scroll = _fh;
							} else {
								_scroll = _fh - _thisHeight;
							}
						}
						_lbox.css({
							position:'absolute',
							top: _scroll
						});
					}
				}
			}
			jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				$(document).keydown(function (e){
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.fadeOut(400, function(){
						_fader.fadeOut(300);
					});
				}
			});
		}
	});
}
function destroyPopup() {
	//alert("test. Please disregard");
	//objPopup = document.getElementById('ajaxpopup');
	//alert("test: " + objPopup.parent.id);
	//document.removeChild(objPopup);
	$('#ajaxpopup').remove();
}
function killOrphanDropdowns (){
	if(document.getElementById('optionsDiv2')){
		selects.pop()
		$('#optionsDiv2').remove();
	}
}