var mouse_position={x:0,y:0};
jQuery(document).mousemove(function(e)
{
  if (document.all) {
    if (document.documentElement && document.documentElement.scrollTop) {
      mouse_position.x=e.clientX+document.documentElement.scrollLeft;
      mouse_position.y=e.clientY+document.documentElement.scrollTop;
    }
    else if (document.body) {
      mouse_position.x=e.clientX+document.body.scrollLeft;
      mouse_position.y=e.clientY+document.body.scrollTop;
    }
  }
  else {
    mouse_position.x=e.pageX;
    mouse_position.y=e.pageY;
  }
});

(function($) {

  function setCreditsPosition(obj)
  {
    var credits_top  = mouse_position.y - obj.credits.h;
    var credits_left = mouse_position.x - 3;
    var win_width    = jQuery(window).width();

    if ( (credits_left + obj.credits.w) > win_width ) {
      credits_left = win_width - obj.credits.w;
    }

    obj.credits.css({ top: credits_top, left: credits_left });
  }

  function setCreditsEvents(obj)
  {
    obj.hotspot.hover( function() { showCredits(obj); }, function() {});
  }

  function showCredits(obj)
  {
    obj.hotspot.unbind('hover')
               .parent().find('*').removeAttr('title').removeAttr('alt');

    if ( !obj.credits.isOpen ) {
      obj.credits.isOpen = true;
      setCreditsPosition(obj);
    }

    setTimeout(function() {

      if (!jQuery.browser.msie) {
        jQuery('#' + obj.credits.attr('id') + ':hidden')
          .css({ width: 0, opacity: 0, display: 'block' })
          .animate({ opacity: 1, width: obj.credits.w }, 500, function() {
            setTimeout(function() { hideCredits(obj) }, 1500);
          });
      } else {
        jQuery('#' + obj.credits.attr('id') + ':hidden')
          .css({ width: 0 })
          .show()
          .animate({ width: obj.credits.w }, 500, function() {
            setTimeout(function() { hideCredits(obj) }, 1500);
          });
      }

    }, 700);
  }

  function hideCredits(obj)
  {
    var e = mouse_position;
    isOver = ( e.x > obj.hotspot.offset().left
               && e.x < obj.hotspot.offset().left + obj.hotspot.width()
               && e.y > obj.hotspot.offset().top
               && e.y < obj.hotspot.offset().top + obj.hotspot.height()
             )
             ||
             ( e.x > obj.credits.offset().left
               && e.x < obj.credits.offset().left + obj.credits.width()
               && e.y > obj.credits.offset().top
               && e.y < obj.credits.offset().top + obj.credits.height()
             );
    if (isOver) {
      setTimeout(function() { hideCredits(obj) }, 1500);
    }
    else {
      if (!jQuery.browser.msie) {
        obj.credits.animate({ width: 0, opacity: 0 }, 400, function() {
          obj.credits.isOpen = false;
          obj.credits.hide();
          setCreditsEvents(obj);
        });
      } else {
        obj.credits.animate({ width: 0 }, 400, function() {
          obj.credits.isOpen = false;
          obj.credits.hide();
          setCreditsEvents(obj);
        });
      }
    }
  }

  $.fn.extend({

    credits: function(obj) {

      var version = "1.0";
      var obj = jQuery.extend({ hotspot: jQuery(this)
                              , credits: jQuery('#deA-credits-box')
                              });

      obj.hotspot        = jQuery(obj.hotspot);
      obj.hotspot.h      = obj.hotspot.height();
      obj.hotspot.w      = obj.hotspot.width();
      obj.credits        = jQuery(obj.credits);
      obj.credits.h      = obj.credits.height();
      obj.credits.w      = obj.credits.width();
      obj.credits.isOpen = false;
      obj.credits.appendTo('body');

      setCreditsEvents(obj);

//      jQuery(window).resize(function() { setCreditsPosition(obj) });
//      jQuery(document).ready(function() { setCreditsPosition(obj) });

    }

  });

})(jQuery);

jQuery(document).ready(function () {
  jQuery('#deA-credits').credits();
});
