

// ------------------------------------------------------
// ------------------------------------------------------
$(document).ready(function() {
	
		  var hideDelay = 700;  
		  var hideTimer = null;
		  
		  var showDelay = 300;
		  var showTimer = null;
		  
		  var currentID;
		  var scrollPos;
		  
		  var popupContainerPositionTop = 0;
		  var popupContainerHeightIE = 320;
		  
		  // One instance that's reused to show info for the current person
		  var container = $( ''
				  + '<div id="popupContainer">'
				  + '<div class="popupPopupTail"></div>'
				  + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="popupPopup">'
				  + '<tr>'
				  + '   <td class="corner topLeft"></td>'
				  + '   <td class="top"></td>'
				  + '   <td class="corner topRight"></td>'
				  + '</tr>'
				  + '<tr>'
				  + '   <td class="left">&nbsp;</td>'
				  + '   <td><div id="popupContent"></div></td>'
				  + '   <td class="right">&nbsp;</td>'
				  + '</tr>'
				  + '<tr>'
				  + '   <td class="corner bottomLeft">&nbsp;</td>'
				  + '   <td class="bottom">&nbsp;</td>'
				  + '   <td class="corner bottomRight"></td>'
				  + '</tr>'
				  + '</table>'
				  + '</div>' );
		  
		  

		  $('body').append(container);

		  $('.popupTrigger').live('mouseover', function()
		  {
		      // format of 'rel' tag: pageid,personguid
		      var settings = $(this).attr('rel').split(',');
		      var programID = settings[0];
		      
				// Window size
		      var windowHeight = parseInt($(window).height());
		      var windowWidth = parseInt($(window).width());
		      var popupTriggerPosition = $(this).offset();
		      var width = $(this).width();
		      var obliczenia = 0;
		      var popupPopupTailPositionTop = 0;
		      
		      if (hideTimer)
		      {
		          clearTimeout(hideTimer);
		      }

		      //-------------------------------
		      // POZYCJA OTWIERANEGO POPUP
		      scrollPos = _getScroll();
		      if( parseInt(scrollPos['scrollTop']) > (parseInt(popupTriggerPosition.top)-120) )
		      {
		    	  // ograniczenie okna od gory
		    	  popupContainerPositionTop = scrollPos['scrollTop'];
		      }
		      else if( windowHeight < parseInt(popupTriggerPosition.top-scrollPos['scrollTop'] + 250) )
		      {
		    	  // ograniczenie okna od dolu		    	  
		    	  if( isNaN(parseInt($('#popupContainer').css('height')))   )
		    	  {  
		    	  }
		    	  else
		    	  {
			    	  popupContainerHeightIE = parseInt($('#popupContainer').css('height')); // na IE7 nie dziala i trzeba zrobic wartosc na stale
		    	  }
		    	  
		    	  popupContainerPositionTop = windowHeight + scrollPos['scrollTop'] - parseInt(popupContainerHeightIE);
		      }
		      else
		      {
		    	  // wyswietlanie po srodku strony
		    	  popupContainerPositionTop = popupTriggerPosition.top - 120;
		      }
		      
		      container.css({
		          left: (popupTriggerPosition.left + width + 5) + 'px',
		          top: popupContainerPositionTop + 'px'
		      });	
		      //-------------------------------
		      

		      //-------------------------------
		      // POZYCJA WSKAZNIKA DLA POPUP DO OBRAZKA
		      popupPopupTailPositionTop = popupTriggerPosition.top - popupContainerPositionTop + 10;
		      
		      if( 0 > parseInt(popupPopupTailPositionTop) )
		      {
		    	  popupPopupTailPositionTop = 10;
		      }
		      else if ( parseInt(popupPopupTailPositionTop) > 250 )
		      {
		    	  popupPopupTailPositionTop = popupContainerHeightIE-50;
		      }
		      
		      $('.popupPopupTail').css({
		          left: '-11px',
		          top: popupPopupTailPositionTop + 'px'
		      });
		      //-------------------------------
		      
		      
		      $('#popupContent').html('&nbsp;');
		      
		      
		      $.ajax({
		          type: 'GET',
		          url: 'jquery/jquery.tooltip.ajax/ajax_opis.php',
		          data: 'programID=' + programID, 	
		          contentType: 'iso-8859-2',
		          beforeSend: function( xhr ) {
	              	  $('#popupContent').html('<div style="width:24px; margin: auto auto; padding: 15px;" ><img src="jquery/jquery.tooltip.ajax/images/loader.gif" alt="Czekaj..." ></div>');
		          },
		          success: function(data)
		          {
			  			$('#popupContent').html(data);	 
			        	// Calculate the opened top position of the pic holder
			  			//projectedTop = scrollPos['scrollTop'] + ((windowHeight/2) - (correctSizes['containerHeight']/2));
			  			//if(projectedTop < 0) projectedTop = 0;			
		        	    
		          }
		      });
		      
		      
		      if (showTimer)
		      {
		          clearTimeout(showTimer);
		      }
		     
		      showTimer = setTimeout(function()
		    		  		{
		    	  				container.css('display', 'block');
		    		  		}, showDelay);
		      
		      
		      
		      
		  });

		  $('.popupTrigger').live('mouseout', function()
		  {
		      if (hideTimer)
		      {
		          clearTimeout(hideTimer);
		      } 
		      
		      if (showTimer)
		      {
		          clearTimeout(showTimer);
		      }
		      
		      hideTimer = setTimeout(function()
					      {
					          container.css('display', 'none');
					      }, hideDelay);
		  });

		  // Allow mouse over of details without hiding details
		  $('#popupContainer').mouseover(function()
		  {		
			  if (showTimer)
		      {
		          clearTimeout(showTimer);
		      }
			  
		      if (hideTimer) 
		      {
		          clearTimeout(hideTimer);
		      }		      
		  });

		  // Hide after mouseout
		  $('#popupContainer').mouseout(function()
		  {			  
		      if (hideTimer)
		      {
		          clearTimeout(hideTimer);
		      }

		      if (showTimer)
		      {
		          clearTimeout(showTimer);
		      }
		      
		      hideTimer = setTimeout(function()
					      {
					          container.css('display', 'none');
					      }, hideDelay);
		      
		     
		  });
		  
});
//------------------------------------------------------
//------------------------------------------------------




//------------------------------------------------------
// odczytywanie polozenia scrola w przegladrce
function _getScroll(){
	if (self.pageYOffset) {
		return {scrollTop:self.pageYOffset,scrollLeft:self.pageXOffset};
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		return {scrollTop:document.documentElement.scrollTop,scrollLeft:document.documentElement.scrollLeft};
	} else if (document.body) {// all other Explorers
		return {scrollTop:document.body.scrollTop,scrollLeft:document.body.scrollLeft};
	};
};
//------------------------------------------------------



