$(document).ready(function(){
	$('body').append('<div id="paravanLightbox" class="lightboxParavan" ></div>');
	
	$('body').append('<div id="popupLightbox" ></div>');
	$('#popupLightbox').css('display','none');	
	
	$('body').append('<div id="shadowLightbox" ></div>');
	$('#shadowLightbox').css('display','none');		
});

function showLightbox(popupWidth, popupHeight, title, className)
{
	if(className == undefined){
		className = 'lightboxContainer';
	}		
	
	var _left = parseInt($(document).scrollLeft());
	var _top = parseInt($(document).scrollTop());
	
	hideLightbox(className);

	$(document).scrollTop( _top );
	$(document).scrollLeft( _left );

	var cWidth=$(window).width();
	var cHeight=$(window).height();	
	var left=Math.floor((cWidth-popupWidth)/2) + _left;
	var top= parseInt((cHeight - popupHeight)/2) + _top;
	
	

	if($.browser.msie && $.browser.version < 7)
	{
		$('select').css('visibility', 'hidden');
	}

	try
	{		
		$('#shadowLightbox')[0].runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../images/paravan.png', sizingMethod='scale')";
	}
	catch(e)
	{
		$('#shadowLightbox').css('backgroundImage','url(../images/paravan.png)');
	}	
	$('#shadowLightbox').css({
		'position': 'absolute',
		'top': top + 6,
		'left': left + 6,
		'width': popupWidth,
		'height': popupHeight,
		'zIndex': '20',
		'display': ''
	});

	$('#popupLightbox').css({
		'position': 'absolute',
		'top': top,
		'left': left,
		'width': popupWidth,
		'height': popupHeight,
		'zIndex': '30',
		'display': 'block' //,
		//'backgroundColor': '#CCC'
	});
	
	$('#popupLightbox').append(
		'<div class="' + className + '" style="width:' + popupWidth + 'px;height:' + popupHeight + 'px;">' +
			'<div class="lightboxCaption popup_header">' + 
				'<div style="float:left;margin-top:10px; margin-left:10px;">' +
				title +
				'</div>' + 
				'<div style="float:right; margin-top:10px; margin-right:10px;"><a href="javascript:void(0);" title="Close window" onclick="hideLightbox(\'' + className + '\')">'+translation["%%CLOSE%%"]+' [x]</a></div>' + 
			'</div>' + 
			//'<a class="lightboxClose" href="javascript:void(0);" title="Close window" onclick="hideLightbox(\'' + className + '\')"></a>' +
			'<div class="lightboxContent">' +
				'<div id="lightboxContentHTML"></div>' +
			'</div>' +
			'<div class="popup_footer"><span>&nbsp;</span></div>' +
		'</div>');
		
	$('.' + className + ' .lightboxContent')
		.height(popupHeight - $('.lightboxCaption').outerHeight())
		.width($('#popupLightbox').outerWidth());

	
	$('body').css('overflow', 'hidden');
	
	if($.browser.msie && $.browser.version <= 7)
	{
		$('html').css('overflow', 'hidden');
	}	
		
	$('#paravanLightbox')
		.css({
			'width' : $(document).width(), 
			'height' : $(document).height(),
			'overflow': 'hidden'
		})
		.show();	
		
	
	
	return $('popupLightbox')[0];
}
this.onresize=function()
{
	if(parseInt($('#popupLightbox').css('width')) > 0 && parseInt($('#popupLightbox').css('height')) > 0)
	{
		popupWidth = parseInt($('#popupLightbox').css('width')); 
		popupHeight = parseInt($('#popupLightbox').css('height'));
	}
	else
	{
		return;
	}
	
	var cWidth=$(window).width();	
	var _left = parseInt($(document).scrollLeft());		
	var left=Math.floor((cWidth-popupWidth)/2) + _left;

	$('#popupLightbox').css({
			'left': left
		});

		$('#shadowLightbox').css({
			'left': left+6
		});
		
		$('#paravanLightbox').css({
		'width' : $(document).width()
	});	
	
	var cHeight=$(window).height();	
	var _top = parseInt($(document).scrollTop());	
	var top = parseInt((cHeight - popupHeight)/2) + _top;
		
	$('#popupLightbox').css({
		'top': top 
	});

	$('#shadowLightbox').css({
		'top': top+6 
	});
	
	$('#paravanLightbox').css({
		'height' : $(document).height()
	});	
}
$(document).keydown(		
	function(e)
	{	
		e = $.event.fix(e);
				
		switch(e.which)
		{
			case 27://ESC
				hideLightbox();						
			break;
		}
	}
);
function hideLightbox(className)
{
	if(className == undefined){
		className = 'lightboxContainer';
	}	
	try{
		if($.browser.msie && $.browser.version <= 7)
		{
			$('select').css('visibility', 'visible');
		}
		
		$('.' + className).remove();
		
		$('#popupLightbox').hide();
		$('#shadowLightbox').hide();
		$('#paravanLightbox').hide();
		$('body').css('overflow','auto');
		if($.browser.msie && $.browser.version <= 7)
		{
			$('html').css('overflow', '');
		}
	}
	catch(e){}
}
function lightboxSetContent(content)
{
	$('#lightboxContentHTML')[0].innerHTML = content;
}
function lightboxShow(width, height, title, type, source, afterFunction, className)
{
	if(className == undefined)
	{
		className = 'lightboxContainer';
	}		
		
		
	if(type == 'dynamic' && source.length > 0)
	{
		showLightbox(width, height, title, className);
		lightboxBusy(className);
		$.get(source, {}, function(data){
			lightboxSetContent(data);
			lightboxFree(className);
			
			if(afterFunction != undefined)
			{
				afterFunction();
			}
		});
		
	}
	else if(type == 'static')
	{
		if($('#' + source)[0] != undefined)
		{
			showLightbox(width, height, title, className);						
			$('#lightboxContentHTML')[0].innerHTML = $('#' + source)[0].innerHTML;
		}
		else
		{
			return;
		}
	}
	
	return;
}
function lightboxBusy(className)
{
	//$('.' + className + ' .lightboxContent').css('backgroundImage','url(../images/anim-loading.gif)');
	$('#lightboxContentHTML').hide();
}
function lightboxFree(className)
{	
	//$('.' + className + ' .lightboxContent').css('backgroundImage','none');
	$('#lightboxContentHTML').show();
	
}

