/*
 +-----------------------------------------------------------------------+
 | sys/js/Desktop.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:  $

*/
var dragObject  = null;
var mouseOffset = null;
var resizeObject = null;

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function getPositionNew(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {Top:left, Left:top};
}

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);

	if(dragObject){
		dragObject.style.position = 'absolute';
		dragObject.style.top      = mousePos.y - mouseOffset.y;
		dragObject.style.left     = mousePos.x - mouseOffset.x;
		return false;
	}
    
    if(resizeObject){
        var p = getPosition(resizeObject);
        var w = mousePos.x-p['x'];
        var h = mousePos.y-p['y'];
        if (h < 100){	    	
            h = null;
        }
        if (w < 100){
            w = null;
        } 
        resizeObject.Resize(w,h);
		return false;
	}
}

function mouseUp(){
    if (dragObject){
        dragObject.RemoveMaskDesk('mouseup');	    
        dragObject.onmousedown = function(){};
    }       
    if (resizeObject){
        resizeObject.RemoveMaskDesk('mouseup');	    
    }
    dragObject = null;   
    resizeObject = null; 
}


function Desktop(){
   this.Desk           = document.createElement('div');
   this.Desk.className = 'FrmMdi';
   this.Desk.wins      = Array();
   this.Desk.WinMinCnt = document.createElement('div'); //Contenitore delle finestre minimizzate
   this.Desk.WinMinCnt.className = 'FormMdiWinMinCnt';
   this.Desk.appendChild(this.Desk.WinMinCnt);
   
   this.Desk.CreateInput = function(nam,typ,val,frm){
        var Tmp = document.createElement('input');
            Tmp.name  = nam;
            Tmp.type  = typ;
            Tmp.value = val;
            frm.appendChild(Tmp);
        return Tmp;        
   }
    /*Metodo che crea una form temporanea sulla pagina*/
   this.Desk.CreateForm = function(mtd,trg,act){
        var Frm = document.createElement('form');
            Frm.method = mtd;
            Frm.target = trg;
            Frm.action = act;
            document.body.appendChild(Frm);
        return Frm;
   } 
        
   this.Desk.GetFocus = function(wfocus){
        for (wname in this.wins){                               
           if (wname != wfocus && this.wins[wname] && this.wins[wname].Propertie['Type'] != 'Modal'){
              this.wins[wname].style.zIndex = '2';
              this.wins[wname].ShowMaskDesk();  
              this.wins[wname].className = 'WinMdiUnSelected';
           }
        }
        if (this.wins[wfocus].Propertie['Type'] != 'Modal'){
            this.wins[wfocus].style.zIndex = '3';
        }
        this.wins[wfocus].RemoveMaskDesk('getfocus');
        this.wins[wfocus].className = 'WinMdi';        
   }
   
   this.Desk.GetWinFromDesk = function(d){
        for (w in this.wins){
            if (this.wins[w].contentWindow.document = d){
                return this.wins[w];
            }
        } 
   }
   
   this.Desk.GetNewWinCoord = function(){
        var xmax = 0;
        var ymax = 0;
        for (wname in this.wins){                                
           if (this.wins[wname] && this.wins[wname].Propertie && this.wins[wname].Propertie['Type'] == 'Normal'){
               var p = getPosition(this.wins[wname]);
               if (p['x'] > xmax){
                  xmax = p['x'];
               }
               if (p['y'] > ymax){
                  ymax = p['y'];
               }
           }
/*           if (this.wins[wname] && this.wins[wname].className == 'WinMdi' && this.wins[wname].Propertie['Type'] == 'Normal'){
                var c = getPosition(this.wins[wname]);
                    c['x'] += 25;
                    c['y'] += 25;
                return c;
                //return {x:left,y:top};
           }*/
        }
        
        if (xmax > 0 && ymax > 0){
           return {x:xmax+25,y:ymax+25};
        }
        return {x:250,y:50};
   }   
   
  this.Desk.MakeDraggable = function(item,obj){    
       if(!item) return;
       item.onmousedown = function(ev){
           dragObject  = this;
           mouseOffset = getMouseOffset(this, ev);
           return false;
       }
  }
   
  this.Desk.OpenWindow = function (pag,tit,w,h,dbg){            
       if (this.wins[tit]){ 
           this.GetFocus(tit);
           return this.wins[tit];
       }       
       if(pag){
          var Page = pag.split('?');
          var TForm = this.CreateForm('post',tit,Page[0]);
          if (Page[1]){
             var DForm = '/osy/Form.php?AppID=OSymapSdk&FrmID=Form'; //Container Url Form Debug
             var LPar = Page[1].split('&');
             for (i in LPar){
                  var Par  = LPar[i].split('=');
                  this.CreateInput(Par[0],'hidden',Par[1],TForm);
                  switch(Par[0]){
                    case 'AppID':
                                  DForm += '&pkey[app_id]='+Par[1]; 
                                  break;
                    case 'FrmID':
                                  DForm += '&pkey[frm_id]='+Par[1];                             
                                  break;
                    case 'SesID':
                                  DForm += '&'+LPar[i];                             
                                  break;   
                  }
             }
          }       
       }
       this.wins[tit] = new Win();
       this.wins[tit].SetPage('');
       this.wins[tit].SetName(tit);
       this.wins[tit].SetDimension(w,h);
       if (dbg==1 && DForm){
          this.wins[tit].SetDebug(DForm);
       }
       this.wins[tit] = this.wins[tit].Build();
       this.appendChild(this.wins[tit]);  
       this.GetFocus(tit);

       if (TForm){
           TForm.submit();
           document.body.removeChild(TForm);
       }
       return this.wins[tit];
  }
   
  this.Desk.RegisterWindow = function (win){
   		if (!this.wins[win.Propertie['Name']]){
            win.Show(); 
            this.wins[win.Propertie['Name']] = win;
            this.appendChild(this.wins[win.Propertie['Name']]);  
            this.GetFocus(win.Propertie['Name']);
        } else {
            this.GetFocus(win.Propertie['Name']);
        }        
  }
        
  this.Desk.CloseWindow = function (winName){ 
        for (w in this.wins){
            if (this.wins[w] && (this.wins[w].Propertie['Parent'] == this.wins[winName])){
                this.GetFocus(w);
                return;
            }
        }              
        this.removeChild(this.wins[winName]);
        delete this.wins[winName];
  }
   
  this.Desk.ShowMenu = function(SesID){
        var WinMenu = new Win()
            WinMenu.SetName('Menu');
            WinMenu.SetType('Menu');
            WinMenu.SetPage('');
            //WinMenu.SetPage('/osy/System.Menu.php?SesID='+SesID);
            WinMenu.SetPosition(10,10);
            WinMenu.SetDimension(210,460);
        this.RegisterWindow(WinMenu.Build());            
            var TForm = this.CreateForm('post','Menu','/osy/System.Menu.php');
                        this.CreateInput('SesID','hidden',SesID,TForm);
                TForm.submit();
            //WinMenu.SetVisibilityLevel(10);

  }
             
   return this.Desk;
}

function InitDesktop(){                
    if (document.body.clientHeight){    
        window.innerHeight = document.body.clientHeight;
        window.innerWidth  = document.body.clientWidth;
    } else if(document.documentElement.clientHeight){
        window.innerHeight = document.documentElement.clientHeight;
        window.innerWidth  = document.documentElement.clientWidth;
    }
    window.Desktop = new Desktop();
    document.body.appendChild(window.Desktop);
    /*var l = parent.frmmdi.OpenWindow('log.php','Login',320,240);
        l.SetType('menu');*/
    //Serve a inizializzare il drang&drop delle finestre
    document.onmousemove = mouseMove;
    document.onmouseup   = mouseUp;
    var WinLogin = new Win()
        WinLogin.SetName('Login');
        WinLogin.SetType('Modal');
        WinLogin.SetPage('/osy/System.Login.php');
        WinLogin.SetDimension(320,240);
        WinLogin.SetPosition('center');
        WinLogin.SetVisibilityLevel('100');
        window.Desktop.RegisterWindow(WinLogin.Build());
}
