﻿//---------------------------------------------------------------------------------
//  (c) 2007 Guardian Networks, Inc
//---------------------------------------------------------------------------------
GNgui.GNWindow = Class.create ( );

GNgui.GNWindow.prototype = 
{
	initialize : function ( ObjNameBase , Parent , X , Y , Wide , High, BorderSize, WindowType)
	{
		this.HeaderDiv = document.createElement ( "div" );
        this.ContentDiv = document.createElement ( "div" );
        this.ContainerDiv = document.createElement ( "div" );
        
        //Element.extend ( this.ContainerDiv );
        
        this.ParentElement = $e( Parent );

        this.Width = parseInt ( Wide );
        this.Height = parseInt ( High );
        this.Xpos = parseInt ( X );
        this.Ypos = parseInt ( Y );
        this.Border = parseInt ( BorderSize );

        this.HeaderDiv.id = ObjNameBase + "HeaderDiv";
        this.ContentDiv.id = ObjNameBase + "ContentDiv";
        this.ContainerDiv.id = ObjNameBase + "ContainerDiv";

        this.ContainerDiv.unselectable = "on";
        this.HeaderDiv.unselectable = "on";
  
        this.HeaderDiv.GNWindow = this;
        this.ContentDiv.GNWindow = this;
        this.ContainerDiv.GNWindow = this;

        this.ContainerDiv.style.height = this.Height;
        this.ContainerDiv.style.width = this.Width;

        this.HeaderDiv.style.height = 18;
        this.HeaderDiv.style.width = this.Width - ( this.Border * 2 ); 
        this.HeaderDiv.style.top = this.Border;
        this.HeaderDiv.style.left = this.Border;

        this.ContentDiv.style.height = this.Height - 18 - ( this.Border * 2 );
        this.ContentDiv.style.width = this.Width - ( this.Border * 2 );
        this.ContentDiv.style.top = this.Border + 18;
        this.ContentDiv.style.left = this.Border;

        this.ContainerDiv.appendChild ( this.HeaderDiv );
        this.ContainerDiv.appendChild ( this.ContentDiv );
        this.ParentElement.appendChild ( this.ContainerDiv );

        this.HeaderDiv.className = "GNWindowHeader";
        this.ContentDiv.className = "GNWindowContent";
        this.ContainerDiv.className = "GNWindowContainer";

        //WindowType is bit-wise key deciding what functions a given window has, or behaviors
        //1 - Movable
        //2 - Sizable
        //4 - Dockable
        //8 - Minimizable
        //16 - Semi-transparent until mouseover
        //32 - Closable
        //64 - Unused
        //128 - Unused
        this.WindowType = parseInt(WindowType);

        this.MinWidth = 25;
        this.MinHeight = 25;

        this.ContainerDiv.style.left = this.Xpos;
        this.ContainerDiv.style.top = this.Ypos;
 
        Event.observe ( this.HeaderDiv , 'mousedown' , this.GUIHeaderGrab.bindAsEventListener ( this ) );
        Event.observe ( this.ContainerDiv , 'mouseover' , this.GUIHover.bindAsEventListener ( this ) );
        Event.observe ( this.ContainerDiv , 'mousedown' , this.GUIGrabEdge.bindAsEventListener ( this ) );
        
        Event.observe(document, 'mouseup', this.WindowMouseUp.bindAsEventListener ( this )  );
        //Event.observe(document, 'mousemove', this.GhostOutline.bindAsEventListener ( this ) );
        if ( this.WindowType & 16 ) 
        {
            Event.observe(this.ContainerDiv, 'mouseover', this.OpacMouseOver.bindAsEventListener ( this ));
            Event.observe(this.ContainerDiv, 'mouseout', this.OpacMouseOut.bindAsEventListener ( this ));
        }
        window.currentDrag = "";
        window.resizeId = "";
	},
	
	
  Catch : function( event )
  {

	  return false;
  },
	
	GUIHeaderGrab : function ( event )
    {
        if ( this.WindowType & 1 )
        {
            window.currentDrag = this.HeaderDiv.id;
            window.dragX = event.clientX;
            window.dragY = event.clientY;
            this.BaseX = event.clientX - event.offsetX - this.Border;
            this.BaseY = event.clientY - event.offsetY - this.Border;
        }
        return false;
    },
           
    GUIHover : function ( event )
    {
        this.ContainerDiv.style.cursor = "default"
        if( this.WindowType & 2 )
        {
            if ( ( event.offsetX > ( this.Width - this.Border * 2 ) ) || ( event.offsetY > ( this.Height - this.Border * 2 ) ) )
            {
                this.ContainerDiv.style.cursor = "nw-resize";
            }
        }
        return false;
    },
    
    GUIGrabEdge : function ( event )
    {
        this.ContainerDiv.style.cursor = "default"
        if( this.WindowType & 2 )
        {
            if ( ( event.offsetX > ( this.Width  - this.Border * 2 ) ) || ( event.offsetY > ( this.Height - this.Border * 2 ) ) )
            {
               this.BaseX = event.clientX - event.offsetX - this.Border;
               this.BaseY = event.clientY - event.offsetY - this.Border;
               this.ContainerDiv.style.cursor = "nw-resize";
               window.resizeId = this.ContainerDiv.id;
               window.resizeX = event.clientX;
               window.resizeY = event.clientY;
            }
        }
        return false;
    },
    
    Reposition : function ( X , Y )
    {
        this.ContainerDiv.style.left = parseInt( this.ContainerDiv.getStyle("left") ) + X;
        this.ContainerDiv.style.top = parseInt( this.ContainerDiv.getStyle("top") ) + Y;
        
        this.Xpos = parseInt( this.ContainerDiv.getStyle("left")  );
        this.Ypos = parseInt(  this.ContainerDiv.getStyle("top")  ); 
        return false;
    },
    
    Resize : function ( X , Y )
    {
        var newX = parseInt( $e( this.ContainerDiv ).getWidth ( ) ) + X;
        var newY = parseInt( $e( this.ContainerDiv ).getHeight ( ) ) + Y;
        if ( newX > 0 && newY > 0 )
        {       
            if( newX < this.MinWidth )
            {
                newX = this.MinWidth;
            }
            if( newY < this.MinHeight )
            {
                newY = this.MinHeight;
            }     
            this.ContainerDiv.style.width = newX;
            this.ContainerDiv.style.height = newY;
            
            this.HeaderDiv.style.width = newX - ( this.Border * 2 ) ;
            this.ContentDiv.style.width = newX - ( this.Border * 2 ) ;
            this.ContentDiv.style.height = ( newY - 18 ) - ( this.Border * 2 );
            this.Width = newX;
            this.Height = newY;            
        }
        return false;
    },
    
    SetContent : function ( Content )
    {
        this.ContentDiv.innerHTML = Content;
    },
    
    SetTitle : function ( Title )
    {
        this.HeaderDiv.innerHTML = Title;
        if ( this.WindowType & 32 )
        {
					this.HeaderDiv.innerHTML += "<img alt='' src='../images/x.gif' onclick='$e(\"" + this.ContainerDiv.id + "\").hide();' style='width:15px;height:15px;position:absolute;right:0px;top:0px;' />";
        }
    },
    
    OpacMouseOver : function( event )
    {
        this.setOpacity( 0.8 );
    },

    OpacMouseOut : function( event )
    {
        this.setOpacity( 0.33 );
    },
  
    setOpacity : function ( Level )
    {   
        this.ContainerDiv.setStyle( { opacity: Level } );
    },
    
    Hide : function ()
    {
      $hide(this.ContainerDiv);
    },
    
    Show : function ( X , Y)
    {
      if ( X )
      {
        $el(this.ContainerDiv,X);
      }
      if ( Y )
      {
        $et(this.ContainerDiv,Y);
      }
      
      $show(this.ContainerDiv);
    },
    
    WindowMouseUp : function ( event )
    {
        if( window.currentDrag != "" )
        {
           var newLeft = ( event.clientX - window.dragX );
           var newTop = ( event.clientY - window.dragY );
           this.Reposition ( newLeft, newTop );
           window.dragX = "";
           window.dragY = "";
           window.currentDrag = "";
           $e( 'GhostDiv' ).style.visibility = "hidden";
        }
        else if ( window.resizeId != "" )
        {
           var newLeft = ( event.clientX - window.resizeX );
           var newTop = ( event.clientY - window.resizeY );
           this.Resize ( newLeft, newTop );
           window.resizeX = "";
           window.resizeY = "";
           window.resizeId = "";
           $e( 'GhostDiv' ).style.visibility = "hidden";
        }
        return false;
    },
    
    GhostOutline : function ( event )
    {
        if ( window.currentDrag != "" )
        {
            $e( 'GhostDiv' ).style.width = $e( window.currentDrag ).GNWindow.Width;
            $e( 'GhostDiv' ).style.left = $e( window.currentDrag ).GNWindow.BaseX + ( event.clientX - window.dragX ) - $e( window.currentDrag ).GNWindow.Border;
            $e( 'GhostDiv' ).style.height = $e( window.currentDrag ).GNWindow.Height;
            $e( 'GhostDiv' ).style.top = $e( window.currentDrag ).GNWindow.BaseY + ( event.clientY - window.dragY ) - $e( window.currentDrag ).GNWindow.Border;
            $e( 'GhostDiv' ).style.visibility = "visible";            
        }
        if ( window.resizeId != "" )
        {
            if ( $e( window.resizeId ).GNWindow.Height + ( event.clientY - window.resizeY ) > $e( window.resizeId ).GNWindow.MinHeight )
            {
                $e( 'GhostDiv' ).style.height = $e( window.resizeId ).GNWindow.Height + ( event.clientY - window.resizeY );
                $e( 'GhostDiv' ).style.top = $e( window.resizeId ).GNWindow.BaseY;
            }
            if ( $e( window.resizeId ).GNWindow.Width + ( event.clientX - window.resizeX ) > $e( window.resizeId ).GNWindow.MinWidth )
            {
                $e( 'GhostDiv' ).style.width = $e( window.resizeId ).GNWindow.Width + ( event.clientX - window.resizeX );
                $e( 'GhostDiv' ).style.left = $e( window.resizeId ).GNWindow.BaseX;
                if ( $e( window.resizeId ).GNWindow.Height + ( event.clientY - window.resizeY ) > $e( window.resizeId ).GNWindow.MinHeight )
                {
                    $e( 'GhostDiv' ).style.visibility = "visible";
                }
            }        
        }
    }
}

