$(document).ready(function(){

  // settings
  timeout_delay = 1000;
  x_adjust = 20;
  y_adjust = 20;

  open_dialog = null;

  $('.mouseover_dialog').each( function(i){

    $(this).attr('dialog_id','#dialog-' + i);
    $(this).attr('loaded','false');
    $(this).attr('open','false');

    $('body').append('<div style="text-align: left;" id="dialog-' + i + '" title="' + $(this).text() + '"></div>');


    $(this).bind('mouseover', function(e){

      if( !( $( $(this).attr('dialog_id') ).dialog( 'isOpen' ) ) ) {

        if (open_dialog != null && open_dialog != $(this).attr('dialog_id') ){
          $( open_dialog ).parent().stopTime();
          $( open_dialog ).dialog('close');
        }
        open_dialog = $(this).attr('dialog_id');

        this.position = [e.clientX + x_adjust, e.clientY + y_adjust];

        if ( $(this).attr('loaded') == 'false') {

          $( $(this).attr('dialog_id') ).load( $(this).attr('src'),
            function (responseText, textStatus, XMLHttpRequest) {
              $( $(this).attr('dialog_id') ).dialog({
                position: this.position,
                draggable: false
              });
              $(this).attr('loaded','true');
            }.call( this )
          );

        } else {
          $( $(this).attr('dialog_id') ).dialog('option', 'position', this.position);
          $( $(this).attr('dialog_id') ).dialog('open');
        }

        $( $(this).attr('dialog_id') ).parent().bind('mouseover', function(e){
          $(this).stopTime();
        });

        if ( timeout_delay !== false ) {
          $( $(this).attr('dialog_id') ).parent().bind('mouseout', function(e){
            $(this).oneTime(timeout_delay, function() {
              $(this).children().dialog('close');
              open_dialog = null;
            });
          });
        }

      }
    });

    if ( timeout_delay !== false ) {
      $(this).bind('mouseout', function(e){
        $( $(this).attr('dialog_id') ).parent().oneTime(timeout_delay, function() {
          $(this).children().dialog('close');
          open_dialog = null;
        });
      });
    }

  });
});
