
/**
 *	Popup class
 *
 *  Staitc part
 *  creates a popup from a div with a certain id. The header(visible part of the popup) has to be
 *  marked with suffix to the id. Default suffix is '_header', and so the header has to have the id
 *  <<id>>_header. For the Body you have to do the same, but the default suffix is '_body'.
 *
 *  All this values are configurable
 *
 *	@package bgoWidgets
 *  @author Opitz Tiberius
 *  @copyright BörseGo GmbH 2007
 *  @created 15.03.2007
 */
	var Popup = {
	  timer : [],
	  animation : [],
	  isOpen : [],
	  counter : 0,
	  popups : [],
	  initialOpen : '2000',
	  keepOpen : '2000',
	  close : '200',

/**
 *  the parameters are the id and the options hash
 *  this parameters are valid
 *	   showControl : 'yes',
 *     headerSuffix: '_header',
 *     bodySuffix : '_body',
 *     animationTime : '1.0',
 *     position: 'absolute',
 *     top: '100%',
 *     width: '200px',
 *     displayBody: 'none'
 */
	  create : function (id, options) {
	    var counter = ++ Popup.counter;
	    if (options) {
	      p_opt = new Hash(options);
	    } else {
	      p_opt =$H();
	    }
	    Popup.popups[counter] = new PopupInstance(id, p_opt);
	    Popup.animation[counter] = false;
	  },

	  getPopup : function(idx) {
	    return Popup.popups[idx];
	  },

	  getPopupById : function(idName) {
	    for(i=1; i<Popup.popups.length; i++) {
	      if (Popup.popups[i].getOption('mainClass') == idName) {
	        return Popup.popups[i];
	      }
	    }
	  },

	  doAction : function(ref, action) {
	    if (Popup.animation[ref]) return;
	    if (action == 'initialopen') {
	      eval(ref+'.open()');
	      eval("mytime = "+ref+".options['animationTime'] * 1000;");
	      mytime = Number(mytime) + Number(Popup.initialOpen);
	      Popup.timer[ref] = setTimeout("eval('"+ref+".close()')", mytime);
	    } else if (action == 'keepopen') {
	      clearTimeout(Popup.timer[ref]);
	      Popup.timer[ref] = setTimeout("eval('"+ref+".close()')", Popup.keepOpen);
	    } else if (action == 'close') {
	      clearTimeout(Popup.timer[ref]);
	      Popup.timer[ref] = setTimeout("eval('"+ref+".close()')", Popup.close);
	    } else {
	      eval(ref + '.' + action + '()');
	    }
	  }
	};



/**
 *	Popup Instance
 *
 *  object to control the popup
 *  the parameters are the id and the configurtation hash
 */
	var PopupInstance = Class.create();

  PopupInstance.prototype = {
      options_default : $H({
      showControl : 'yes',
      mainClass : '',
      refNr : 0,
      refName : '',
      headerSuffix: '_header',
      bodySuffix : '_body',
      animationTime : '1.0',
      position: 'absolute',
      top: '100%',
      left: '0px',
      width: '200px',
      displayBody: 'none'
    }),

    options : $H({
    }),

    initialize: function(id, opt) {
      this.options = Object.clone(this.options_default);
      this.options['mainClass'] = id;
      this.options['refNr'] = Popup.counter;
      this.options['refName'] = 'Popup.getPopup('+Popup.counter+')';
      var name = this.options['refName'];
      if (opt.values().length>0) {
        //alert(opt.invoke('join', ' = ').sort().join(', '));
        this.options.merge(opt);
      }
      //alert(this.options.invoke('join', ' = ').sort().join(', '));
      

      var header = $(id + this.options['headerSuffix']);

      header.observe('click', function() {
         Popup.doAction(name, 'initialopen');
      });

      var body = $(id + this.options['bodySuffix']);

       body.setStyle({
         position: this.options['position'],
         top: this.options['top'],
         left: this.options['left'],
         width: this.options['width'],
         display: this.options['displayBody']
       });

      body.observe('mousemove', function() {
         Popup.doAction(name, 'keepopen');
      });

      body.observe('mouseout', function() {
         Popup.doAction(name, 'close');
      });

      if (this.options['showControl'] == 'yes') {
        header.style.Float = 'left';

        new Insertion.After((id + this.options['headerSuffix']),'\
          <div style="float: none;">\
            <a href="javascript: void(0);" onClick="'+this.options['refName']+'.open()">open</a>\
            <a href="javascript: void(0)" onClick="'+this.options['refName']+'.close()">close</a>\
          </div>\
        ');
      }
    },

    getOption : function(name) {
      return this.options[name];
    },

	 /**
		* open function with effect
		*/
    open: function() {
      $(this.options['mainClass']).makePositioned();
      var body = $(this.options['mainClass']+this.options['bodySuffix']);
      body.setStyle({ zIndex: 100 });
      var ref = this.options['refNr'];
      //body.show();
      if (Popup.animation[ref] || Popup.isOpen[ref]) return;
      Popup.isOpen[ref] = true;
      Effect.Appear(body.id, {
        duration: this.options['animationTime'],
        beforeStart: function () {
          Popup.animation[ref] = true;
        },
        afterFinish: function () {
          Popup.animation[ref] = false;
        }
      });
    },

	 /**
		* close function
		*/
    close: function()	{
      var main = this.options['mainClass'];
      var body = $(this.options['mainClass']+this.options['bodySuffix']);
      body.setStyle({ zIndex: 0 });
      var ref = this.options['refNr'];
			//body.hide();
      if ( Popup.animation[ref]) return;
      Popup.isOpen[ref] = false;
      Effect.Fade(body.id, {
        duration: this.options['animationTime'],
        beforeStart: function () {
          Popup.animation[ref] = true;
        },
        afterFinish: function () {
          Popup.animation[ref] = false;
          $(main).undoPositioned();
        }
      });
    }

  };
