/*
 +-----------------------------------------------------------------------+
 | sys/js/Window.js                                                      |
 |                                                                       |
 | This file is part of the Opensymap                                    |
 | Copyright (C) 2005-2008, Pietro Celeste - Italy                       |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Pietro Celeste <pietro.celeste@gmail.com>                     |
 |-----------------------------------------------------------------------|
 | Description : Desktop MDI generator                                   |
 |-----------------------------------------------------------------------|
 | Creation date : 2008-12-01                                            |
 +-----------------------------------------------------------------------+

 $Id:  $

*/
/*Window Prototype*/
function WindowPrototype(){
    this.Window           = document.createElement('div');
    this.Window.className = 'WinMdi';    
    this.Window.Desktop   = window.Desktop;
    this.Window.Ico       = Array();
    this.Window.Part      = Array();
    this.Window.Propertie = Array();
    this.Window.Propertie['Type']  = 'Normal';
    this.Window.Propertie['State']  = 'Normal';
    this.Window.Propertie['Debug']  = '';
    this.Window.Propertie['Dimension'] = new Array();
    this.Window.Propertie['Dimension']['Width'] = 640;
    this.Window.Propertie['Dimension']['Height'] = 480;
    this.Window.Propertie['Position'] = new Array();
    this.Window.Propertie['Position']['Top'] = '';
    this.Window.Propertie['Position']['Left'] = '';
    
    this.Window.Maximize = function(){		
        this.Propertie['State']        = 'Maximized';
        this.Propertie['Position']     = getPositionNew(this);
		this.style.width               = '100%';
        this.style.height              = this.parentNode.clientHeight;
        this.Desk.style.height = this.parentNode.clientHeight - (this.Part['TitleBar'].offsetHeight + this.Part['StatusBar'].offsetHeight);
        this.style.top                 = '0px';
        this.style.left                = '0px';                
	}       
    
    this.Window.Minimize = function(){
        this.style.visibility    = 'hidden';
        this.Propertie['State']  = 'Minimized';        
        this.Minimized           = document.createElement('div');
        this.Minimized.Parent    = this;
        this.Minimized.className = 'WinMdiMinimized';
        this.Minimized.innerHTML =  this.Part['TitleBar'].firstChild.innerHTML;
        this.Minimized.onclick   = function(){
            this.Parent.style.visibility = '';
            this.Parent.Desktop.GetFocus(this.Parent.Propertie['Name']);
            this.Parent.Desktop.WinMinCnt.removeChild(this);
            this.Parent.Minimized     = null;
        }
        this.Desktop.WinMinCnt.appendChild(this.Minimized);        
    }   
    
    this.Window.OpenChild = function(wp,wn,ww,wh,dg){
         if(!dg) var dg = '';
         var w = this.Desktop.OpenWindow(wp,wn,ww,wh,dg);
             w.SetParent(this);
    }
    
	this.Window.ParentRefresh = function(){
        var PDoc = this.GetParentDocument();
        if (PDoc){
            PDoc.forms[0].submit();
            return true;            
        }        
    }
	
    this.Window.GetParentDocument = function(){
        if (this.Propertie['Parent']){
          return this.Propertie['Parent'].Desk.contentWindow.document;  
        } else {
          return false;
        }
    }
    
    this.Window.RemoveMaskDesk = function(p){
        if (this.MaskDesk){           
            this.removeChild(this.MaskDesk);
            this.MaskDesk = null;
        }
    }
    
    this.Window.Resize      = function (w,h){
        if (w){
            this.style.width           = w;
        }
        if (h){
            this.style.height          = h;    
            this.Desk.style.height     = h-50;         
        }
    }
    
    this.Window.SetParent = function(p){
        this.Propertie['Parent'] = p;
    }
    
    this.Window.SetPosition = function(t,l){
        switch(t){
           case 'center':
                          t = 400;
                          l = 200;
                          break;                 
        }
        this.Propertie['Position']['Top'] = t;
        this.Propertie['Position']['Left'] = l;
        this.style.top  = t+'px';
        this.style.left = l+'px';
    }
    
    this.Window.SetTitle    = function (t){
        this.Part['TitleBar'].firstChild.innerHTML = t;
    } 
    
    this.Window.ShowMaskDesk = function(){
        //if (this.MaskDesk==null && this.Propertie['Type'] == 'Normal'){
        if (this.MaskDesk==null){
		    this.MaskDesk                = document.createElement('div');
            this.MaskDesk.className      = 'WinMdiMaskDesk';
            this.MaskDesk.style.width    = this.offsetWidth -4;
            this.MaskDesk.style.height   = this.offsetHeight-34;
            //this.MaskDesk.style.border = '1px solid red';
            this.MaskDesk.onclick        = function(){
                this.parentNode.parentNode.GetFocus(this.parentNode.Propertie['Name']);
                //this.RemoveMaskDesk();
            }
            this.insertBefore(this.MaskDesk,this.Desk);
        }
	}	
    
    this.Window.UnMaximize = function(){
        this.style.left          = this.Propertie['Position']['Top'];
        this.style.top           = this.Propertie['Position']['Left'];
		this.style.width         = this.Propertie['Dimension']['Width'];
        this.style.height        = this.Propertie['Dimension']['Height'];
        this.Desk.style.height   = this.Propertie['Dimension']['Height'] - (this.Part['TitleBar'].offsetHeight + this.Part['StatusBar'].offsetHeight);
        this.Propertie['State']  = 'Normal';
	}
    
    this.Window.close = function (){  
          if (this.Propertie['Parent']){
              window.Desktop.GetFocus(this.Propertie['Parent'].Propertie['Name']);
              this.ParentRefresh();
          }                
          this.parentNode.CloseWindow(this.Propertie['Name']);   
    }
    
    this.Window.Show = function(){
        window.Desktop.appendChild(this);
    }    
    
    return this.Window;
}

function Win(){        
    this.Window = WindowPrototype();
    
    this.Build = function(){
        if (!this.Window.Propertie['Position']['Top']){
            var c = window.Desktop.GetNewWinCoord();
            this.SetPosition(c['y'],c['x']);
        }
        this.SetDimension();
        this.BuildTitleBar();        
        this.BuildBody();   
        this.BuildStatusBar();
        return this.Window;
    }
            
    this.BuildBody = function (){
        try{
            this.Window.Desk =  document.createElement('<iframe name="'+this.Window.Propertie['Name']+'"></iframe>');
        } catch (e) {
            this.Window.Desk =  this.Window.ownerDocument.createElement('iframe');
            this.Window.Desk.setAttribute('name',this.Window.Propertie['Name']);
            this.Window.Desk.setAttribute('id',this.Window.Propertie['Name']);
        }
        this.Window.Desk.style.cssText = 'clear: both';
        this.Window.Desk.className    = 'WinDesk';
        this.Window.Desk.style.height = (this.Window.Propertie['Dimension']['Height'] - (this.Window.Propertie['Type'] == 'Normal' ? 48 : 31)) + 'px';     
        //this.win.Desk.style.height =  this.win.Height - (this.titlebar.offsetHeight + this.StatusBar.offsetHeight);
        this.Window.Desk.scrolling    = 'no';
        this.Window.Desk.src          = this.Window.Propertie['PageToLoad'];    
        this.Window.Desk.frameBorder  = '0';    
        this.Window.appendChild(this.Window.Desk);
        this.Window.Desk.Win       = this.Window;            		
    }
    
    this.BuildIcone = function(){
        switch(this.Window.Propertie['Type']){
            case 'Menu':
                          this.SetCommand('../img/osy/WinMdiCloseBody.gif',function(){
                            //this.SetCommand('/img/WinMdiClose.gif', function(){
                                var p = this.parentNode.parentNode;                                     
                                if (p.style.height == '30px'){
                                    p.style.height =  p.Propertie['Dimension']['Height']+'px';
                                    p.Desk.style.visibility = 'visible';
                                    p.Desk.style.height = (p.Propertie['Dimension']['Height']-30)+'px';
                                } else {
                                    p.style.height = '30px';
                                    p.Desk.style.visibility = 'hidden';
                                    p.Desk.style.height = '1px';
                                }
                          });
                          break;
            case 'Normal':
                          this.SetCommand('../img/osy/WinMdiClose.gif', function(){
                               this.parentNode.parentNode.close();
                          });
                            
                          this.SetCommand('../img/osy/WinMdiMaximize.gif', function(){
                               var p = this.parentNode.parentNode;
                               if (p.Propertie['State'] == 'Normal'){
                                   this.src = '../img/osy/WinMdiUnMaximize.gif';
                                   p.Maximize();
                               } else {
                                   this.src = '../img/osy/WinMdiMaximize.gif';
                                   p.UnMaximize();
                               }
                           });
                            
                           this.SetCommand('../img/osy/WinMdiMinimize.gif', function(){
                                var p = this.parentNode.parentNode;
                                p.Minimize();
                            });
                           
                           if (this.Window.Propertie['Debug']){
                              this.SetCommand('../img/osy/WinMdiDebug.gif', function(){
                                var p = this.parentNode.parentNode;
                                    p.OpenChild(p.Propertie['Debug'],'FormDebug',800,600,null);
                            });
                           }
                           break;
           
        }
        /*var c = document.createElement('div')
            c.style.cssText = 'clear: both';
        this.win.titlebar.appendChild(c);*/
    }
    
    this.BuildTitleBar = function (){
        this.Window.Part['TitleBar']     = document.createElement('div');
        this.Window.Part['TitleBar'].className   = 'WinMdiTitleBar';
        this.Window.Part['TitleBar'].onmousedown = function(){
             if (this.parentNode.Propertie['Type'] != 'Modal'){
        	    this.parentNode.parentNode.GetFocus(this.parentNode.Propertie['Name']);
             }
             this.parentNode.Desktop.MakeDraggable(this.parentNode);             
             this.parentNode.ShowMaskDesk();
        }
        if (this.Window.Propertie['Type']=='Normal'){
            this.Window.Part['TitleBar'].ondblclick = function(){            
                if (this.parentNode.State == 'normal'){               
                    this.parentNode.Maximize();
                } else {
                    this.parentNode.Minimize();
                }
            }
        }
        var titcnt = document.createElement('div');
            titcnt.style.cssText  = 'float: left; font-size: 12px; font-weight: bold;';
			titcnt.innerHTML      = this.Window.Propertie['Name'];
        this.Window.Part['TitleBar'].appendChild(titcnt);
        this.Window.appendChild(this.Window.Part['TitleBar']);	
        this.BuildIcone();	     
    }
    
    this.BuildStatusBar = function(){
        if (this.Window.Propertie['Type'] == 'Normal'){
            this.Window.Part['StatusBar'] = document.createElement('div');
            this.Window.Part['StatusBar'].className   = 'WinMdiStatusBar';
            this.Window.appendChild(this.Window.Part['StatusBar']);
            this.Window.Part['Resizer'] = document.createElement('div');
            this.Window.Part['Resizer'].className = 'WinMdiResizeCorner';
            this.Window.Part['Resizer'].onmousedown = function(ev){    
            	    resizeObject  = this.parentNode.parentNode;
                    this.parentNode.parentNode.ShowMaskDesk();
            	    return false;
                }
            this.Window.Part['Resizer'].style.margin = '0px';
            this.Window.Part['StatusBar'].appendChild(this.Window.Part['Resizer']); 
        }
    }
            
    this.SetCommand = function(p,f){
        var i = this.Window.Ico.length;
        this.Window.Ico[i] = new Image()
        this.Window.Ico[i].src = p;
        this.Window.Ico[i].align = 'right';
        this.Window.Ico[i].onmouseover = function (){  this.style.backgroundColor = 'red'; }
        this.Window.Ico[i].onmouseout  = function (){  this.style.backgroundColor = ''; }
        this.Window.Ico[i].onclick     = f;
        this.Window.Ico[i].style.margin = '2px';
        this.Window.Part['TitleBar'].appendChild(this.Window.Ico[i]);
    }        
    
    this.SetDimension  = function(w,h){
        if (w){
           this.Window.Propertie['Dimension']['Width']  = w;                   
        }
        if (h){
            this.Window.Propertie['Dimension']['Height'] = h;
        }
        this.Window.style.width  = this.Window.Propertie['Dimension']['Width']+'px';
        this.Window.style.height = this.Window.Propertie['Dimension']['Height']+'px';        
    }
        
    this.SetName = function(n){
        this.Window.Propertie['Name'] = n;
    }   
    
    this.SetPage = function(p){
        this.Window.Propertie['PageToLoad'] = p;
    }
    
    this.SetPosition = function(t,l){
        switch(t){
           case 'center':
                          t = (window.innerHeight - this.Window.Propertie['Dimension']['Height']) / 2;
                          l = (window.innerWidth - this.Window.Propertie['Dimension']['Width']) / 2;
                          break;                 
        }
        this.Window.Propertie['Position']['Top'] = t;
        this.Window.Propertie['Position']['Left'] = l;
        this.Window.style.top = t+'px';
        this.Window.style.left = l+'px';
    }
    
    this.SetDebug = function(Url){
        this.Window.Propertie['Debug'] = Url;
    }
    this.SetState = function(s){
        this.Window.Propertie['State'] = s;
    }
    
    this.SetType = function(t){
        switch(t){
            case 'Modal':
            case 'Menu' :
            case 'Normal':
                            this.Window.Propertie['Type'] = t;
                            break;
            default:
                    alert('Il tipo '+ t +' non e\' un tipo di finestra ammesso');
                    break;
        }
    }        
    
    this.SetVisibilityLevel = function(l){
        this.Window.style.zIndex = l;
    }   
}  
//window.onresize = function (){alert('resize');}

