/**
 *This is an Implementation of a simple, yes powerful lightbox
 *Copyright 2010 by Helbing IT and kjur.de
  
  Usage:
  castWindow([string]id, [string/DOMobj]content, [int]width(px), [int]height(min-px), [string:on|off]button(default=on));
  killWindow([string]id);
  
  Always open any new Windows before closing old to avoid flickering of the background-dimmer!
*/


var windowcount = 0;
function castLightbox(){
	$('<div>')
		.attr('id', 'kjur_lightbox_background')
		.addClass('kjur_lightbox_background')
		.css('opacity', '0.8')
		.fadeIn('100')
		.click(function(){
			$(this).remove();
			$('#kjur_lightbox_container').remove();
			
		})
		.appendTo('body');
}

function castWindow(id, content, width, height, button){
	if(windowcount == 0) castLightbox();
	windowcount++;
	if(!button) var button = 'yes'; //Kein Fehler!!
	$('<div>')
		.attr('id', id)
		.addClass('kjur_lightbox_container')
		.append(
			$('<div>')
				.addClass('kjur_lightbox_window')
				.css({'width' : + width + 'px', 'min-height': + height + 'px'})
				.append(content)
		)
		.fadeIn('100')
		.appendTo('body');
		
		$('#' + id).css('top', $(document).scrollTop() + 'px');
		$('.kjur_lightbox_background').css('top', $(document).scrollTop() + 'px');
		
		$(window).scroll(function(event){
			var y = $(this).scrollTop();
			$('#' + id).css('top', y + 'px');
			$('.kjur_lightbox_background').css('top', y + 'px');
		});
		
	if(button == 'yes'){
		$('#' + id)
			.find('.kjur_lightbox_window')
			.append(
				$('<div class="kjur_lightbox_xbtn_bg"></div>')
					.append(
						$('<div>')
							.addClass('kjur_lightbox_xbtn')
							.click(function(){
								killWindow(id);	
							})
							.mousemove(function(){
								$(this).addClass('kjur_lightbox_xbtn_h');
							})
							.mouseout(function(){
								$(this).removeClass('kjur_lightbox_xbtn_h');
							})
					)
			)
	}
}

function killLightbox(){
	$('.kjur_lightbox_background').fadeOut('100', function(){
		$(this).remove();
	});
}

function killWindow(id){
	$('#' + id).fadeOut('100', function(){
		$(this).remove();
		windowcount--;
		if(windowcount <= 0){
			killLightbox();
		}
	});
}
