//Show dialg box - THIS IS UNOBTRUSIVE. THIS IS MADNESS!!!!!!!!!!!
/*Usage:
  new DialogBox2({
    link_id: '1', //- obvious
    handle_id: '1', //- optional, where box will be attached 
    text: 'text', //- text to put in to dialog box 
    action_link_name: 'name', //- name for action link
    cancel_link_name: 'name', //- optional, name for action link (default: 'Cancel')
    x: 20, //- optional
    y: 1 //- optional
  }).show();
*/
function DialogBox2(options) {
  this.link = $("#" + options["link_id"]);
  if(options["handle_id"]) {
    this.handle = $("#" + options["handle_id"]);
  }
  this.text = options["text"];
  this.action_link_name = options["action_link_name"];
  this.cancel_link_name = options["cancel_link_name"] || "Cancel";
  this.x = parseInt(options['x']) || 0;
  this.y = parseInt(options['y']) || 0;
  this.show = function() {
    this.bind_to_link();
  }

  this.bind_to_link = function() {
    var _this = this;
    this.link.click(function(e){
      if(_this.text){
        $('#dialog-box').remove();
        _this.create_dialog_box();
      } else {
        _this.show_dialog_box();
      }
      e.preventDefault();
    });
  };

  this.show_dialog_box = function() {
    this.handle.children(".dialog-box").show();
  }

  this.create_dialog_box = function() {
    frame = $(document.createElement('div'))
      .addClass('dialog-box')
      .attr('id', 'dialog-box');
    if(!this.handle){
      frame.appendTo(this.link.parent());
    } else {
      frame.appendTo(this.handle);
    }
    content = $(document.createElement('div'))
      .addClass('content')
      .css('left', -415 + this.x)
      .css('top', -22 + this.y)
      .appendTo(frame);
    $(document.createElement('div'))
      .addClass('right-arrow')
      .css('left', -8 + this.x)
      .css('top', -12 + this.y)
      .appendTo(frame);
    $(document.createElement('div'))
      .html(this.text)
      .addClass('text')
      .appendTo(content);
    buttons = $(document.createElement('div'))
      .addClass('buttons')
      .appendTo(content);
    links = $(document.createElement('div'))
      .addClass('links')
      .appendTo(buttons);
    action_button = $(document.createElement('a'))
      .addClass('button')
      .html(this.action_link_name)
      .attr("href", this.link.attr("href"))
      .appendTo(links);
    cancel_button = $(document.createElement('a'))
      .addClass('cancel')
      .html(this.cancel_link_name)
      .appendTo(links);
    cancel_button.click(function(e){
      $('.dialog-box').empty();
      e.preventDefault();
    })
  };
}


/*Usage:
  ToolTip({
    tool_tip_id: 'id',
    link_id: 'id',
    x: 1,
    y: 2
  });
*/

function ToolTip() {
  this.show = function(options) {
    this.tool_tip = $("#" + options['tool_tip_id']);
    this.link = $("#" + options['link_id']);
    this.x = options['x'];
    this.y = options['y'];
    this.arrow = options['arrow'];

    var _this = this
    this.link.mousemove(function(){_this.tool_tip.show();});
    this.link.mouseleave(function(){_this.tool_tip.hide();});
    this.tool_tip.mousemove(function(){_this.tool_tip.show()});
    this.tool_tip.mouseleave(function(){_this.tool_tip.hide()});
    this.tool_tip.css('left', this.x);
    this.tool_tip.css('top', this.y);

    $(document.createElement('div'))
      .html('')
      .addClass('arrow-' + this.arrow)
      .appendTo(this.tool_tip);
  }
}

$.fn.delay = function(time, callback){
  // Empty function:
  jQuery.fx.step.delay = function(){};
  // Return meaningless animation, (will be added to queue)
  return this.animate({delay:1}, time, callback);
}
