/**
* Przemek Sobstel (http://sobstel.org/)
*/

$(document).ready(function(){
	$('.preview').click(function(e){		
		
		var img1 = new Image();img1.src = 'img/top_ref_bg.jpg';
		var img2 = new Image();img2.src = 'img/nav.jpg';	
						
		function open(){		
			$('#top').css('background-image', 'url(img/top_ref_bg.jpg)');
			$('#nav').css('background-image', 'url(img/nav.jpg)');
			$('object').hide();
			$('body').append(box);
		}
		
		function close(){
			box.remove();
			$('#top').css('background-image', 'none');
			$('#nav').css('background-image', 'none');
			$('object').show();
			return false;
		}
		
		function cursorOn(){
			$(this).css('cursor', 'pointer');
		}
		
		function cursorOff(){
			$(this).css('cursor', 'default');
		}
				
		var head = $(document.createElement('div')).						
			append(
				$('<img src="img/close.png" width="16" height="16" style="float:right"/>').
				click(close).
				mouseover(cursorOn).
				mouseout(cursorOff)
			).
			append('<div style="float:left"><b>'+this.title+'</b></div>').
			append('<div style="clear:both;height:8px">&nbsp;</div>');

		var img = new Image();		
		img.src = this.href;
		
		var width = img.width == 0 ? 500 : img.width;
		var height = img.height == 0 ? 300 : img.height + 60;
		
		var pic = $(img).
			attr('title', 'Kliknij, aby zamknąć pogląd').			
			click(close).
			mouseover(cursorOn).
			mouseout(cursorOff).
			load(function(){
				box.
					width(this.width).
					height(this.height+60).
					css('margin-left', '-'+Math.round(this.width/2)+'px').
					append(this).
					append('<br /><br />').
					append(link);
			});
			
		var link = $(document.createElement('a')).
			attr('href', '#').			
			html('zamknij podgląd').
			click(close);								
		
		var box = $(document.createElement('div')).
			css('z-index', 1).
			css('position', 'absolute').
			css('left', '50%').			
			css('top', 15).
			css('background', '#111 url(img/loadingAnimation.gif) center center no-repeat').
			css('margin-left', -(Math.round(width/2))).
			css('margin-top', 10).
			width(width).
			height(height).
			css('border', '10px solid #000').
			css('text-align', 'center').
			css('color', '#fff').
			append(head);
			// append(pic);
		
		open();					
						
		return false;
	});

});
