/**
* naUserWindowOverlay.js
* naUserWindowOverlay class
* @author goran
*/

// store references to all active windows
var activeUserWindowOverlays = [];

/**
* class naUserWindowOverlay
*/
var naUserWindowOverlay = Class.create();
naUserWindowOverlay.prototype = {

  /**
  * constructor
  */
  initialize: function(options) {
 
 
	// option parameters
	this.options = Object.extend({
      globalBaseUrl:        "http://www.nachtausgabe.de/",
      tuid:                 0,
      userHeaderToolboxId:	"toolbox_parent_element_[tuid]",
      toolboxActivatorId1:  "toolbox_activator_element_[tuid]"
 	}, options || {});

    //members
	this.toolbox = null;
	this.closeTimer = null;

	// Event bindings
	this.showToolbox = this._showToolbox.bindAsEventListener(this);	
	Event.observe(this.options.toolboxActivatorId1, "mouseover", this.showToolbox);

	this.setCloseTimer = this._setCloseTimer.bindAsEventListener(this);	
	this.clearCloseTimer = this._clearCloseTimer.bindAsEventListener(this);	
	Event.observe(this.options.userHeaderToolboxId, "mouseout", this.setCloseTimer);
	Event.observe(this.options.userHeaderToolboxId, "mouseover", this.clearCloseTimer);

  },

  /**
  * destructor
  */
  destroy: function() {
  	if(this.toolbox) this.toolbox.close();
  	
  	Event.stopObserving(this.options.toolboxActivatorId1, "mouseover", this.showToolbox);
  	Event.stopObserving(this.options.userHeaderToolboxId, "mouseout", this.setCloseTimer);
  	Event.stopObserving(this.options.userHeaderToolboxId, "mouseover", this.clearCloseTimer);
  },

  /**
  * private function showToolbox
  */
  _showToolbox: function() {
  	
  	//close all other user window overlays
	activeUserWindowOverlays.each(function(item) {
  		item.closeToolbox();
	});
  	
  	var thisRef = this;
  	
  	if(this.toolbox == null){
  	
  		this.toolbox = new Window({
  			className:         'naUserHeaderToolbox', 
  			height:            40, 
  			width:             200,
		  	resizable:         false, 
		  	title:		       false, 
		  	draggable:         false,
		  	wiredDrag:         false,
		  	closable:          false,
		    minimizable:       false,
		    maximizable:       false,
		    opacity:           1,
		    parent:            thisRef.options.userHeaderToolboxId,
		    destroyOnClose:    false,
		    closeCallback:     null, 
		  	zIndex:            100,
		  	showEffect:        Element.show,
		  	hideEffect:        Element.hide
		 });
  	
  		this.toolbox.setStatusBar('&nbsp;');
  		this.toolbox.setLocation(-90,-210);
  		this.toolbox.setAjaxContent(this.options.globalBaseUrl + '/userHeaderToolbox/userWindowOverlayContent?tuid=' + this.options.tuid, {}, false, false);

  		activeUserWindowOverlays.push(this);
  	}else{
  		this.toolbox.show();
  	}

  	this.closeTimer = setTimeout(this._closeToolbox.bind(this), 5000);

  },
  
  
  /**
  * public function closeToolbox
  */
  closeToolbox: function(event) {
  	if(this.toolbox) this.toolbox.close();
  },    
  
  
  /**
  * private function closeToolbox
  */
  _closeToolbox: function(event) {
    clearTimeout(this.closeTimer);
  	if(this.toolbox) this.toolbox.close();
  },  
  
  /**
  * private function clearCloseTimer
  */
  _clearCloseTimer: function(event) {
    clearTimeout(this.closeTimer);
  },    
  
  /**
  * private function clearCloseTimer
  */
  _setCloseTimer: function(event) {
    clearTimeout(this.closeTimer);
  	this.closeTimer = setTimeout(this._closeToolbox.bind(this), 100);
  }
 	
 	
}//class
