	

	var mouseProduct = []; 
	var currentPopUp;
	var closedPopUp;
	var loading = false; 
		


      var mouseX = 0;

      var mouseY = 0;



	
	
	
		$(document).ready(function() {
			
 $().mousemove( function(e) {
      mouseX = e.pageX;
      mouseY = e.pageY;
  });

			
				
			$('.shop-pop').bind("click",function(){
				if ($(this).attr('id').length == 0) 
						return false;
				if (loading)
					return false; 
				var productId = $(this).attr('id').replace('product-',''); 
				setUpPopup(productId); 
				return false; 
		});
				

		});
		
		
		function redir(link_){
			$.get("/shopup.php",{action:'logClick',id:$(link_).attr('id').replace("click-",'')});
			return true; 
		}
		 

		function setUpPopup(productId){
			if (currentPopUp == productId)
				return; 
			loading = true; 			

		
			mouseProduct[productId]=true;
			
			if ($('#popup-temp-'+productId).length == 0) {
				$.get("/shopup.php",{id:productId}, function(data){
								$('#product-'+productId).prepend("<div class='popup-window' id='popup-temp-"+productId+"'>"+data+"</div>");
								var img = $('#popup-temp-'+productId).find('.popup-photo').find('img');
								$(img).load(function(){
//									$('#test').append("<br/>"+$(this).width()); 
									showPopup(productId); 
								}).error(function () {}); 
							
								}); 
				}
				else {
							showPopup(productId);		
				}
		
		}
			
function closePopUp(productId) {
		$('#popup-temp-'+productId).css("display","none");
		closedPopUp = productId; 
		currentPopUp = ""; 
		}
		
		
function repositionPop(productId) {

	var pos = FindXYWH(document.getElementById('product-'+productId));

	
	var win_width = $(window).width(); 
	var win_height = $(window).height(); 
	
	var frombottom = win_height - pos.y; 
	

// top 	
	if (pos.y < 0)
		$('#popup-temp-'+productId).css("top",  mouseY);	
// too far right		
	if (pos.x+300 > win_width)
		$('#popup-temp-'+productId).css("left",  mouseX-300);	
// too low
	if (frombottom < 500)
		$('#popup-temp-'+productId).css("top",  Math.max($(window).scrollTop()+10 ,mouseY - 600 ));

	 
		
	var x_offset = 0;
	var y_offset = 0; 

}




		
		
function FindXY(obj){
	var x=0,y=0;
	while (obj!=null){
		x+=obj.offsetLeft-obj.scrollLeft;
		y+=obj.offsetTop-obj.scrollTop;
		obj=obj.offsetParent;
	}
	return {x:x,y:y};
}

// Find the x,y location in pixels for a relatively positioned object
// returns an object with .x, .y, .w (width) and .h (height) properties.
function FindXYWH(obj){
	var objXY = FindXY(obj);
	return objXY?{ x:objXY.x, y:objXY.y, w:obj.offsetWidth, h:obj.offsetHeight }:{ x:0, y:0, w:0, h:0 };
}


		
				
		
		function showPopup(productId) {

			
			
			if ($('#popup-temp-'+productId).length > 0){

					currentPopUp = productId;
					loading = false; 
						
				$('.popup-window').css("display","none");
				$('#popup-temp-'+productId).css("z-index", parseInt( new Date().getTime()/1000 ));
				$('#popup-temp-'+productId).easydrag().fadeIn() 

				//	$('#product-'+productId).draggable(); 
			
				var width_ = $('#popup-temp-'+productId).find('.popup-photo').find('img').width();

	
				if (width_ == null || width_ < 150)
							width_ = 200; 
				$('#popup-temp-'+productId).width(width_+60); 
				
				$('#popup-temp-'+productId).find('.popup-text').css("width",width_+"px");
				$('#popup-temp-'+productId).find('.popup-text').css("min-width",width_+"px");
				
				$('#popup-temp-'+productId).css("min-width",width_+60+"px"); 
				$('#popup-temp-'+productId).css("width",width_+60+"px"); 
				repositionPop(productId);
				
				$('.log-click').click(function(){
												return redir($(this)); 
									});
				}
				
		}
		
		
			
