/*
 * ZejMedia PopUp
 *
 * Copyright (c) 2009 ZejMedia
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2009-05-30 $
 */
 
// Zej singleton
    window.Zej = new function(){
      // private functions and variables
      $ = jQuery;
      var _openAssociatedDialog = function(e) {
        // close active dialog first
        self.closeActiveDialog();

        // find neighboring div and open its dialog
//        var div = $(this).parents('.zej-info-popup').find('div')
        var div = $(this).parents('h2').get(0)._zejDialog;
        self.activeDialog = div;
        $(div).dialog('open');

        // stop event bubble
        return false;
      };

      // public methods and properties
      var self = {
        activeDialog: null,

        closeActiveDialog: function(e) {
          if (self.activeDialog !== null) {
            $(self.activeDialog).dialog('close');
          }
        },

        init: function() {
          // find and init all popups
          $('.zej-info-popup').each(function() {
            // init dialog
            var div = $(this).find('div').get(0);
            var header = $(this).find('h2').get(0);
            $(header).wrapInner('<a href="#"></a>').find('a').click(_openAssociatedDialog);
            header._zejDialog = div;
            var headerOffset = $(header).offset();
            $(div).attr('title', $(header).text()).dialog({
//              buttons: { 'Ok': self.closeActiveDialog },
              draggable: false,
//              position: [
//                headerOffset.left + $(header).width(),
//                headerOffset.top
//              ],
//              modal: true,
              height: 'auto',
              autoOpen: false
            });
//            $(div).dialog('option', 'height', $(div).height());
          });
        }
      };

      return self;
    };

    jQuery(function() {
      Zej.init();
    });