function closePreview(){
	$('#smokeScreen,#preview,#preview_image,#preview_image_div').fadeOut(300).unbind('click');
	$('OBJECT').css('visibility','visible');
};
function showPic(path,picWidth,picHeight,desc){
	function sTop() {
	return window.pageYOffset
	|| document.documentElement && document.documentElement.scrollTop
	|| document.body.scrollTop;
	};
	function wHeight() {
	return window.innerHeight
	|| document.documentElement && document.documentElement.clientHeight
	|| document.body.clientHeight;
	};
	
	if($('#preview_image').length == 0){
		var img = '<a href="javascript:closePreview()" >';
		img += '<img title="Sulje" id="preview_image" style="border:0" />';
		img += '</a>';
		$('#preview_image_div').prepend(img);
	}
	$('OBJECT').css('visibility','hidden');
	//$('#preview_image').removeAttr('src');
	$('#preview_image').hide().attr('src',path);
	$('#preview_description').hide().html(desc);

	$('#preview_image_div').hide().width(80).height(80);	
	$('#smokeScreen').css('filter', 'alpha(opacity=80)').click(closePreview);	
	$('#preview').queue(function() {
		$('#smokeScreen').fadeIn(500);
		$('#preview').show(0, function(){
			$('#preview_image_div').show(200);
			$('#preview_loading').show(100);
			
			$('#preview_image').ready(function(){
				$('#preview_image').attr('width',picWidth).attr('height',picHeight);
				var halfWidth = Math.round(picWidth/2);
				var halfHeight = Math.round(picHeight/2);
				if(picWidth<30){ picWidth = 80; }
				var elTop = Math.round(sTop() + (wHeight() / 2) - (picHeight / 2));
				if(picHeight<wHeight()){
					$('#preview').css({marginTop:'0',top:elTop});									
				}else{
					if(elTop<0 || ($(document).height()-picHeight)>0){
						$('#preview').css('marginTop','20px').css('top',sTop()); // start from the top
					}else{
						$('#preview').css('marginTop','20px').css('top',0); // start from the top
					}
				}
				if(desc!=null){
					picHeight=(parseInt(picHeight)+parseInt($('#preview_description').width(picWidth+'px').height()+10));
				}
				
				$('#preview').css('marginLeft','-' + halfWidth + 'px' ); // center horizontally				
				$('#preview_image_div').animate({width:picWidth}, 250).animate({height:picHeight}, 250);
				$('#preview_image_div').queue(function() {
					$('#preview_loading').hide();
					$('#preview_image').fadeIn(500);
					if(desc!=null)$('#preview_description').fadeIn(500);
					$(this).dequeue();
				});
					
			});
		});
		$(this).dequeue();
	});
};
$(document).ready(function(){
	if($('#smokeScreen').length==0)	$('body').prepend('<div id="smokeScreen"></div><div id="preview"><div id="preview_image_div"><div id="preview_description"></div></div></div>');
	$('a.img_link').click(function(e){
		e.preventDefault();
		try{
		var val = $(this).attr('rel').split('[');
		val = val[1].split(']');
		val = val[0].split(',');
		var desc = ($(this).find('img') && $(this).find('img').attr('title')) ? $(this).find('img').attr('title') : '';
		showPic($(this).attr('href'),val[0],val[1],desc);
		}catch(e){}
	});
});


