$(document).ready(function(){
	$('.pan-preview img').click(function(el){
		var img=$(this);
		var src=img.attr('data');
		var pop=$(".pan-preview-popup").length;
		var sTop=$(window).scrollTop();
		var winW=$(window).width();
		var winH=$(window).height();
		
		var pi=img.closest(".postimg");
		if(!$("span").is(".pan-prew-loading") && !$("div").is(".pan-preview-main")){
			pi.css({"position":"relative"});
			pi.append('<span class="pan-prew-loading">LOADING...</span>');
		}
		
		if(!pop){
			$('body').append('<div class="pan-preview-main"><div class="pan-preview-popup prew-pos"><img src="'+src+'" /></div></div>');
		}
		
		$(".pan-preview-popup img").load(function(){
			$(".pan-preview-main").fadeIn(300);
			$(".pan-prew-loading").remove();
			var imgW=$(this).width();
			var imgH=$(this).height()+5;
			var perW=((winW-imgW)*100/winW)/2;
			var perH=((winH-imgH)*100/winH)/2;
			$(".prew-pos").css({"left":perW+'%',"margin-top":"2%"});
		});

		$(".pan-preview img").css({"cursor":"not-allowed"});
		$(".pan-preview-popup").css({"position":"absolute","top":sTop});
		
//		$(".pan-preview-popup img").mousemovie(function(e){
//			if(e.which == 2)
//				alert();
//		});
		
		$(".pan-preview-popup").click(function(e){
			$(".pan-preview img").css("cursor", "-webkit-zoom-in");
			var div = $('.pan-preview-main');
			if(!div.is(e.target)){
				$(".pan-preview-main").fadeOut(300);
				setTimeout(function() {
					$(".pan-preview-main").remove();
				}, 300);
			}
		});
	});
});



