//This module is doomed - will be replaced soon - do not touch it.
//Do not try to understand what happens here.
//It is an relic of some very early development with YUI - Panels and does no more
//fit into the framework.
//Später f. Window-Manager (Tile/Cascade)...


//Experimental Window Manager Function 
//Prevents the Content in a Window Panel from being not adjusted to the
//Windows new Height when new Height is bigger than it was before (in Cascade Windows).

xpRepos = function(myPanel, newWidth, newHeight, newX, newY)
{    
      var headerHeight = myPanel.header.offsetHeight;
      var bodyHeight = (newHeight - 10 - myPanel.footer.offsetHeight - myPanel.header.offsetHeight - 3);
		if (bodyHeight < 0) {
			bodyHeight = 0;
		}

    //resize only if dimension is positive. Negative values indicate that we should ignore the args
      if(newWidth > 0) 
      {
         myPanel.cfg.setProperty("width", newWidth + "px");
      }
      if(newHeight > 0)
      {
         myPanel.cfg.setProperty("height", newHeight + "px");
      }
         

		myPanel.body.style.height =  bodyHeight + "px";

		var innerHeight = myPanel.innerElement.offsetHeight;
		var innerWidth = myPanel.innerElement.offsetWidth;

		if (innerHeight < headerHeight) {
			myPanel.innerElement.style.height = headerHeight + "px";
		}

		if (innerWidth < 100) {
			myPanel.innerElement.style.width = "100px";
		}
      var myIFrame = document.getElementById(myPanel.WindowName + '_iFrame');
	   if( myIFrame != null )
	   {
         myIFrame.height  = '100%' ; 
		   myIFrame.width  = '100%' ;  
	   }


}//end experimental


window.NS2112 = window.NS2112 || {};

NS2112.namespace("windowManager");



NS2112.windowManager = function(orgX, orgY, offsX, offsY, strLanguage)
{
   this.foo = 0 ; //a counter
   this.iWindowOriginX = orgX; //for cascade, where to start
   this.iWindowOriginY = orgY;
   this.iCascadeOffsetX = offsX;
   this.iCascadeOffsetY = offsY;
   this.m_bHideOnDrag = true ; //Hide Content of Dragged Windows by Default.
   this.m_iWindowExtX = NS2112.windowManager.constDefaultWindowWidth ;   //loading defaults
   this.m_iWindowExtY = NS2112.windowManager.constDefaultWindowHeight ; 
   this.myHiddenPanels = new Array(); //on hide all
   this.OverlayManager = new YAHOO.widget.OverlayManager();
   this.dragged = false ;
   this.unmanagedWindow = null;
    ;
   //debugger if breaklevel <= 3
   //NS2112.portalMan.break('WINDOWMAN', 3) ;
   //central storage
   NS2112.onConsoleOut.fire('windowMan - creating drag proxy object.') ; 
   NS2112.objectMan.insertObject(NS2112.windowManager.dragProxyName,  new NS2112.windowManager.dragPrx12()) ;



   this.selfTest = function()
   {
     alert('WindowManager') ;
   }
   
   this.getDragProxy = function(){
		var prx = NS2112.objectMan.getObject(NS2112.windowManager.dragProxyName) ;
		if(!prx) return null ;
		return prx.getElement() ;
   }
   
   this.getDragProxyObject = function(){
		var prx = NS2112.objectMan.getObject(NS2112.windowManager.dragProxyName) ;		
		return prx ;
   }

   this.register = function(myWindow){      
      var bRet = this.OverlayManager.register(myWindow) ;
      if( myWindow ) myWindow.style.zIndex = 1 ; //reset.
      return bRet ;
   }


//an array of snapIns (named)
   this.bringToTop = function(objSnapIns, myWindow){
      this.OverlayManager.blurAll() ;            
      this.OverlayManager.bringToTop(myWindow) ;
      myWindow.style.zIndex = 65534;  //->won't work on IE(undefined)NS2112.windowManager.constWndTopZIndex  ; //
   }

   this.blurAll = function(objSnapIns){
	  var window = null ;      
      for(var X in objSnapIns)
      {
         //this.OverlayManager.overlays[X]. 
         if(objSnapIns[X].m_Panel)  //can be null
            window = objSnapIns[X].m_Panel.getWindow() ;
         if( window ) window.style.zIndex = 1 ; //reset.
      }      
   }


   //this function and the resulting behaviour should be removed soon...
   this.setUnmanagedWindow = function(myWindow)
   {
      this.unmanagedWindow = myWindow ;
   }
   


   //language support
   this.myNLS = new NS2112.nlsProvider("DE") ;
   
   if( 0 == this.iCascadeOffsetX ) this.iCascadeOffsetX = 20 ;
   if( 0 == this.iCascadeOffsetY ) this.iCascadeOffsetY = 20 ;

   if( 0 == this.iWindowOriginY ) this.iWindowOriginY = 50 ;
   
   this.compareZIndexAsc = function(o1, o2) {
		var zIndex1 = o1.cfg.getProperty("zIndex");
		var zIndex2 = o2.cfg.getProperty("zIndex");

		if (zIndex1 < zIndex2) {
			return -1;
		} else if (zIndex1 > zIndex2) {
			return 1;
		} else {
			return 0;
		}
	}
   
   this.unlink = function(myPanel)
   {
   
      this.OverlayManager.remove(myPanel) ;
   
   }

   this.movePanel = function(myPanel, X, Y)
   {
      myOverlay = this.OverlayManager.find(myPanel) ;
      if( myOverlay != null)
         myOverlay.moveTo(X, Y) ;
   }

   this.cascadeWindows = function()
   {
      var iOffsY ;
      var iOffsX ;

      iOffsX = 0 ;
      iOffsY = 0 ;

      this.OverlayManager.overlays.sort(this.compareZIndexAsc) ;
      
      for(var X in this.OverlayManager.overlays)
      {
         iOffsX+= this.iCascadeOffsetX ;
         iOffsY+= this.iCascadeOffsetY;         
         this.OverlayManager.overlays[X].moveTo(this.iWindowOriginX+iOffsX, this.iWindowOriginY+iOffsY) ;    
         if( this.OverlayManager.overlays[X] == this.unmanagedWindow)  continue ; 
         this.OverlayManager.overlays[X].innerElement.style.width  = this.m_iWindowExtX + "px" ;
         this.OverlayManager.overlays[X].innerElement.style.height = this.m_iWindowExtY + "px";
         xpRepos(this.OverlayManager.overlays[X], this.m_iWindowExtX, 
                          this.m_iWindowExtY, this.iWindowOriginX+iOffsX, this.iWindowOriginY+iOffsY);
         this.setFocus(X) ;
         
      }
   }

  this.setFocus = function(strWindowName)
  {
    var myOverlay = this.OverlayManager.find(strWindowName) ;
    this.OverlayManager.focus(myOverlay) ;  
  }

//repelling the dreadful 'still reloading' effect in Firefox
  this.endDrag = function()
  {
      this.dragged = false ;
  }

  this.hideOnDrag =  function(myDraggedObject)
   {
         if( false == this.m_bHideOnDrag ) //only hide when set so.
            return ;
      if( this.dragged === true ) return ; //optimization, do not execute all mouseMove times.
    
      for(var X in this.OverlayManager.overlays)
      {
         //if(myDraggedObject.id == this.OverlayManager.overlays[X].WindowName) continue ; //do not hide my contents
         var myOuter = document.getElementById(this.OverlayManager.overlays[X].WindowName + '_outer');

	      if ( null != myOuter )
	      {
		      //myOuter.style.display = 'none' ;
           myOuter.style.visibility = 'hidden' ;
           
	      }
      }
      this.dragged = true ; //indicate drag action

   }
//not working - since overlaymanager would remove panels when calling hide...
   this.showAll = function()
   {
      //this.OverlayManager.showAll() ;
      for(var X in this.OverlayManager.overlays)
      {
         this.OverlayManager.overlays[X].show() ;
      }
   }

   this.hideAll = function()
   {
      //this.OverlayManager.hideAll() ;
       for(var X in this.OverlayManager.overlays)
      {
         this.OverlayManager.overlays[X].hide() ;
         
      }

   }
//repelling the dreadful 'still reloading' effect in Firefox
//this Method is called with event onBeforeHide for the panel
//it makes invisible the outer div of the panels body...
   this.hideContent = function(myDraggedObject) 
   {
      for(var X in this.OverlayManager.overlays)
      {
         if(myDraggedObject.id == this.OverlayManager.overlays[X].WindowName) //match
         { //do  hide my contents
          var myOuter = document.getElementById(this.OverlayManager.overlays[X].WindowName + '_outer');

	         if ( null != myOuter )
	         {
		         //myOuter.style.display = 'none' ;
               myOuter.style.visibility = 'hidden' ;
               break ;           
	         }
         }
      }
   }

   this.showContentAll =  function()
   {

      for(var X in this.OverlayManager.overlays)
      {
         var myOuter = document.getElementById(this.OverlayManager.overlays[X].WindowName + '_outer');
	      if ( null != myOuter )
	      {
/*
this statement causes the dreadful 'still reloading' effect in Firefox
but currently no other solution available...

*/            
		      myOuter.style.display = 'block' ;
            //myOuter.style.display = 'inline' ; 
            myOuter.style.visibility = 'visible' ;            
	      }
         
      }

   } //showContentAll
   //Performance optimization - hide unders while Dragging
   this.hideUnders = function(myPanel)
   {
      var Debug;
      for(var X in this.OverlayManager.overlays)
      {
         if(X == this.OverlayManager.overlays[X].WindowName) continue;
         var myOuter = document.getElementById(X + '_outer');
	      if ( null != myOuter )
	      {
		      myOuter.style.display = 'none' ;
	      }
      
      }
   }
   
   
   this.getProxy = function(){
	
   }
}


NS2112.windowManager.constDefaultWindowWidth = 700 ;//obsolete
NS2112.windowManager.constDefaultWindowHeight = 450 ;//obsolete
NS2112.windowManager.constDefaultLogoWidth = 700 ;    //obsolete
NS2112.windowManager.constDefaultLogoHeight = 450 ;   //obsolete
NS2112.windowManager.constWndTopZIndex = 65534 ; //65535 is our dragShim therefore -1
NS2112.windowManager.dragProxyName = 'NS2112.windowManager.dragProxy' ;


NS2112.windowManager.getDragProxyObject = function(){ //a static shortcut
	 var wm = NS2112.portalMan.getWindowManager();	 
	 if(!wm) return null ;
	 return wm.getDragProxyObject() ;
}

NS2112.windowManager.getDragProxy = function(){ //a static shortcut
	 var wm = NS2112.portalMan.getWindowManager();	 
	 if(!wm) return null ;
	 return wm.getDragProxy() ;
}

NS2112.windowManager.setDragProxyXY = function(X,Y){
   prx =  NS2112.objectMan.getObject(NS2112.windowManager.dragProxyName) ;
   if(prx) prx.setXY(X, Y) ;
}

NS2112.windowManager.notifyUser = function(strWho, strWhat){
	alert("Systemmessage: " + strWho + "/" + strWhat) ;
}


NS2112.windowManager.addListener = function() {
    if ( window.addEventListener ) {
        return function(el, type, fn) {
            el.addEventListener(type, fn, false);
        };
    } else if ( window.attachEvent ) {
        return function(el, type, fn) {
            var f = function() {
                fn.call(el, window.event);
            };
            el.attachEvent('on'+type, f);
        };
    } else {
        return function(el, type, fn) {
            element['on'+type] = fn;
        }
    }
}();

/*the overall drag proxy element*/
NS2112.windowManager.dragPrx12 = function() {
		var prx_list_el = NS2112.windowManager.dragProxyName ;		
		var theElement = document.createElement("div");            
		a = document.createAttribute("id");
		a.nodeValue = prx_list_el ;
		theElement.setAttributeNode(a);
		theElement.style.display = 'none' ; //invisible at first
		theElement.style.border = '1px' ;
		theElement.style.border_style = 'solid' ;
		theElement.style.position = 'fixed' ;
		theElement.style.zIndex = 65535 ;
		theElement.style.width = '20px' ;
		theElement.style.height = '16px' ;		
		theElement.style.left = '72px' ;		
		theElement.style.background = 'url(' + NS2112.portalPath + 'assets/img/2112ddm.png) 0px 0px no-repeat';		
		document.body.appendChild ( theElement );  
		//theElement.style.visibility = 'hidden'; //do not use display:		
		//feature-discovery: fixed won't work on IE, absolute won't work on FF 
		//therefore check		
	    //offsetLeft will be 0 in IE.
		if( parseInt(theElement.offsetLeft) != 72 ) theElement.style.position = 'absolute' ;
		NS2112.onConsoleOut.fire('dragPrx12 - position Attribute is set to [ ' + theElement.style.position + ']') ; 
			//theElement.innerHTML = theElement.style.position + '/' + theElement.style.left;    		
		
		this.bringToTop = function(){
			theElement.style.zIndex = 65535 ;
		}    
		
		this.getElement = function(){
			return document.getElementById(NS2112.windowManager.dragProxyName) ;
		}
		this.setXY = function(X,Y){
		    theElement = this.getElement() ;
		    if(!theElement) return ;
			theElement.style.left = parseInt(X) + 'px' ;
		    theElement.style.top  = parseInt(Y) + 'px' ;
		}
		
		this.setVisible = function(bVisible){
		   var prx = this.getElement() ;
		   if(!prx) return ;
		   bVisible==true?prx.style.visibility = 'visible':prx.style.visibility = 'hidden' ;
		}
}   



