var popupStatus = 0

function openPopup(popup){
	if(popupStatus==0){
		$("#popup_background").css({"opacity": "0.7"});  
		$("#popup_background").fadeIn("slow");  
		$(popup).fadeIn("slow");  
		popupStatus = 1;  
	}
}

function closePopup(){  
//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#popup_background").fadeOut("slow");  
		$('#popup').fadeOut("fast");
		$('#popup').find('img').remove(); 
		popupStatus = 0;  
	}  
}

function centerPopup(popup){  
//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $(popup).height();  
	var popupWidth = $(popup).width();  
//centering  
	$(popup).css({  
		"position": "fixed",  
		"top": 20,  
		"left": windowWidth/2-popupWidth/2  
	});  
//only need force for IE6  
  
	$("#popup_background").css({  
		"height": windowHeight  
	});  
  
}

$(document).ready(function(){  
	$('.product a.popup_link').click(function (){
		var popup = $('#popup');
			popup.append('<img src="' + $(this).attr('href') + '" />');
			centerPopup(popup);
			openPopup(popup);
		return false;
	});

	//CLOSING POPUP
	//Click the x event!
	$("#popup .close").hover(function(){
		$(this).css('cursor', 'pointer');
	});
	
	$("#popup .close").click(function(){
		closePopup();
	});
	
	//Click out event!
	
	$("#popup_background").click(function(){
		closePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			closePopup();
	}
	});
	
});