
function Film_Tooltip ()
{
  this.cachedItems = [];
  this.ajaxURLStart = '/ajax/product/'
  this.ajaxURLEnding = '.html';
  this.tooltipCssID = 'js_film_tooltip';
  this.tooltipHandler = null;
  this.offset = {
    'left': 20,
    'top': 20
  }
  this.loadingImage = '<img src="/images/ovloading.gif" alt="loading" style="margin: 10px" />';
}

Film_Tooltip.prototype.init = function ()
{
  jQuery('body').append('<div id="'+this.tooltipCssID+'"></div>');
  this.tooltipHandler = jQuery('#'+this.tooltipCssID);
  this.tooltipHandler.hide();
  
  this.registerHoverHandler();
}


Film_Tooltip.prototype.registerHoverHandler = function ()
{
  var objectHandler = this;
  
  jQuery('a.film-tooltip').hover(
    function () // mouseover
    {
      objectHandler.showTooltip( jQuery(this).attr("rel") );
    },
    function () // mouseout
    {
      objectHandler.hideTooltip();
    }
  ).mousemove ( // register move handler
    function ( eventObject )
    {
      objectHandler.tooltipHandler.css("top", (eventObject.pageY + objectHandler.offset.top)+"px").css("left", (eventObject.pageX + objectHandler.offset.left)+"px");
      //objectHandler.tooltipHandler.css("top", "100px").css("left", "100px");
    }
  );


}

Film_Tooltip.prototype.showTooltip = function ( filmID )
{
  if (filmID == undefined)
  {
    return;
  }

  // loading Image
  this.insertContentInTooltip( this.loadingImage );
  this.tooltipHandler.show();
  this.getFilmData(filmID);
}

Film_Tooltip.prototype.hideTooltip = function ()
{
  this.tooltipHandler.hide();
}

Film_Tooltip.prototype.getFilmData = function ( filmID )
{
  // first check cache
  if (this.cachedItems[filmID] == undefined)
  {
    this.loadFilmData(filmID);
    return;
  }

  // otherwise fetch content and insert when loaded
  this.insertContentInTooltip(this.cachedItems[filmID]);
}

Film_Tooltip.prototype.insertContentInTooltip = function (content)
{
  this.tooltipHandler.html(content);
}

Film_Tooltip.prototype.loadFilmData = function ( filmID )
{
  var objectHandler = this;
  
  jQuery.post(
    this.ajaxURLStart + filmID + this.ajaxURLEnding, // url
    '', // no parameters
    function (data) // callback
    {
      objectHandler.cachedItems[filmID] = data;
      objectHandler.insertContentInTooltip( objectHandler.cachedItems[filmID] );
    }
  );
}

jQuery('a.film-tooltip').ready(
  function ()
  {
    var filmTooltipHandler = new Film_Tooltip();
    filmTooltipHandler.init();
  }
);