var base = function () {
  for (var k in this) {
    if (typeof this[k] == 'object' && typeof this[k].init == 'function') {
      this[k].init();
    }
  }
};

base.message = base.prototype.message = {
  init : function () {
    this.bindBlink();
  },
  bindBlink : function () {
    jQuery('#message').fadeIn();
    var to = window.setTimeout(function () {
      jQuery('#message').fadeOut();
    }, 5000);
    jQuery('#message').hover(function () {
      window.clearTimeout(to);
    }, function () {
      to = window.setTimeout(function () {
        jQuery('#message').fadeOut();
      }, 5000);
    });
  }
};

base.modal = base.prototype.modal = {
  init : function () {
    this.bindOpen();
    this.bindClose();
  },
  open : function (id) {
    var modal = jQuery(id);
    var height = modal.outerHeight();
    if (height >= jQuery(window).height()) {
      modal.css({
        'position'   : 'absolute',
        'top'        : '100px',
        'margin-top' : '0'
      });
      jQuery('html, body').animate({ scrollTop : 0 }, 'slow');
    } else {
      modal.css('margin-top', height / -2);
    }
    modal.fadeIn();
    jQuery(document.body).click(function (event) {
      var target = jQuery(event.target);
      if (!target.is('div.modal') && target.parents('div.modal').length == 0) {
        modal.fadeOut();
      }
    });
  },
  close : function (id) {
    var modal = typeof id == 'string' ? jQuery(id) : jQuery('div.modal');
    modal.fadeOut();
  },
  bindOpen : function () {
    var modal = this;
    jQuery('a.modal').live('click', function (event) {
      event.preventDefault();
      modal.close();
      modal.open(base.getHash(this.href));
    });
  },
  bindClose : function () {
    var modal = this;
    jQuery('a[href$=#modal-close]').live('click', function (event) {
      event.preventDefault();
      modal.close();
    });
  }
};

base.layout = base.prototype.layout = {
  init : function () {
    this.cssHacks();
  },
  cssHacks : function () {
  }
};

base.getHash = function (url) {
  return url.replace(/^.*(#.*)$/, '$1');
};

jQuery(document).ready(function () {
  new base();
});

