/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
function imagePreview(){	
	initW = 0;
	initH = 0;
	var close=1;
	$("a.preview").click(function(e){
		if(close==1){
			close=0;
			//zwart vlak
			$("#previewcontent").css('display','inherit');
			$('#preview').css('display','inherit');
			$("#previewcontent").append('<img src="images/preloader.gif" class="prevpreloader" alt="" />');
			// new image object
			var img = new Image();
			// image onload
			$(img).addClass('prentie');
			$(img).load(function () {
				// hide first
				$(".prevpreloader").remove();
				//$(this).css('display','none'); 
				//since .hide() failed in safari
				$("#preview").append($(this));
				
				initW = $('.prentie').width();
				initH = $('.prentie').height();	
				$('.prentie').css('display','none');
				var imgSize = scaleSize($("body").width(),$("body").height(),initW,initH);
				
				
				$(".prentie, #preview").width(imgSize[0]);
				$(".prentie, #preview").height(imgSize[1]);
				
				var left = -imgSize[0]/2;
				var top = - imgSize[1]/2;
				$("#preview").css('margin-left',left);
				$("#preview").css('margin-top',top);
				$(".prentie").fadeIn('normal');
			}).attr({title:'click to close', alt:'click to close', src:this.href});
			
			//$("#previewcont").html("<div id='preview'><img class='prentie' src='"+ this.href +"' alt='click to close' title='click to close' /></div>");
			return false;	
		}					
    });	
    
	$("#preview, #previewcontent").click(function(e){
		$("#previewcontent").css('display','none');
		$(".prentie").remove();
		$(".prevpreloader").remove();
		close=1;
	});
	
	$(window).resize(function(){
		var imgSize = scaleSize($("body").width(),$("body").height(),initW,initH);
		$(".prentie, #preview").width(imgSize[0]);
		$(".prentie, #preview").height(imgSize[1]);
		var left = -imgSize[0]/2;
		var top = - imgSize[1]/2;
		$("#preview").css('margin-left',left);
		$("#preview").css('margin-top',top);
	});
	
}
function scaleSize(maxW, maxH, currW, currH){
      var ratio = currH / currW;

      if(currW >= maxW){
            currW = maxW - 50;
            currH = currW * ratio;
      } else if(currH >= maxH){
            currH = maxH - 50;
            currW = currH / ratio;
      }
      return [currW, currH];
}


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});
