/**
 * jQuery Simple Dialog Plugin
 *   http://code.google.com/p/jquery-simpledialog/
 *
 * Copyright (c) 2009 Yusuke Horie
 *
 * Released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since  : 0.01 - 07/06/2009
 * Version: 0.01 - 07/06/2009
 */
(function(jQuery) {

  var
    _opts = {},
    _doc = null, _win = null, _docHeight, _winHeight, _winWidth,
    _content = null;

  jQuery.fn.simpleDialog =function (options) {
    _opts = jQuery.extend({}, $.fn.simpleDialog.defaults, options);

    return this.each(function(i, e) {
      jQuery(e)
        .bind('click.simpledialog', function (event) {
          event.preventDefault();

          _doc = $(document);
          _win = $(window);
          _docHeight = _doc.height();
          _winHeight = _win.height();
          _winWidth = _win.width();

          // show overlay
          _overlay();

          var $t = jQuery(this);

          if ($t.is('a')) {
            var href = $t.attr('href');
            if (href.match(/^#/)) {
              _show($('#' + $t.attr('rel')));
            } else if ($t.find('img').length > 0) {
              _load(href);
            } else {
              _request(href, {});
            }
          } else if ($t.is(':submit', ':button')) {
            var f = $t.parents('form');
            _request(f.attr('action'), f.serialize());
          } else { return false; }
        });
    });
  };

  jQuery.fn.simpleDialog.defaults = {
    opacity: 0.6,
    zIndex: 5000,
    duration: 400
  };

  /** private methods **/

  var _append = function (content) {
    jQuery('#sd_content').remove();
    return jQuery('body').append('<div id="sd_content" style="display:none;">' + content + '</div>').find('#sd_content');
  };

  var _request = function (url, data) {
    $.ajax({
      type: 'GET',
      url: url,
      data: data,
      dataType: 'html',
      success: function (content) {
        _show(_append(content));
      }
    });
  };

  var _load = function (url) {
    var img = _append('<img src="' + url + '" alt="" />');
    img
      .find('img')
      .load(function () {
        _show(img);
      });
  };

  var _overlay = function () {
    jQuery('body')
      .append('<div id="sd_overlay" style="display:none;"></div>')
      .find('#sd_overlay')
      .css({
        position: 'absolute',
        top: 0, left: 0,
        width: _winWidth,
        height: _docHeight,
        backgroundColor: '#222',
        opacity: _opts.opacity,
        zIndex: _opts.zIndex
      })
      .attr('class', 'sd_overlay')
      .fadeIn(_opts.duration)
      .click(function () {
        jQuery(this).remove();
        _content.hide();
      });
  };

  var _show = function (content) {
    _content = content;

    var w = content.width();
    var h = content.height();

    content
      .css({
        position: 'absolute',
        top: _doc.scrollTop() + _winHeight/2 - h/2 + 'px',
        left: (_docHeight > _winHeight) ? _winWidth/2 - w/2 - 18: _winWidth/2 - w/2 + 'px',
        backgroundColor: '#fff',
        border: 'solid 1px #ccc',
        zIndex: _opts.zIndex + 100
      })
      .fadeIn(_opts.duration*1.2);
  };

})(jQuery);
