/* 
 * FDesktop Functions
 * @author Fernando Hernandez - Freelance Soft
 * www.freelancesoft.com.ar
 *
 * DESCRIPCION:
 * Conjunto de funciones para el FDesktop.
 * 
 * 
 * REQUERIMIENTOS:
 * Clase FWindows, Dialog Functions, HTML con los elementos del escritorio FDesktop
 *
 * Testeado en Opera 9, Firefox 2 e IE 6, 7
 */
  var Windows_Open_Limit=12;
  var FWindows=new Array();
  
  function initFDesktop(){
     StartClock();
	 moveWindows_setTopLimit(30);
	  
	 //init array of fwindows 
     for(i=1; i<=Windows_Open_Limit; i++){  
	   FWindows[i]=null;
     }
   }
  
  function showDesktop(){
     for(i=1; i<(FWindows.length); i++){
	    if(FWindows[i]!=null){
			minimizeWindow(i);
		}	
	  }
   }
  
  function newWindow(title,content){
   	 var insert=false;
	 
	 for(i=1; i<=Windows_Open_Limit; i++){
	 	   if(FWindows[i]==null && !insert){
	  	      FWindows[i]=new FWindow(i,title,content,'desktop');   
			  insert=true;
			  num=i;
	       }
	 } 
	if(!insert){
	   if(i>=Windows_Open_Limit){
	      openAlertDialog(0); //El alert dialog 0 debe estar configurado para mostrar el mensaje de error
	   }else{
	   	  FWindows[i+1]=new FWindow((i+1),title,content,'desktop'); 
		  num=i+1; 
	   }
	}
	   update_menu_items();
	   return num;
   }
  
  function openWindow(num){
      var FWindow=FWindows[num];
	  
	  if(FWindow.isWindowOpen()){
	  	toFront(num);
	  }else{
	  	FWindow.openWindow();
		toFront(num);
	  }	  
  }	 
  function closeWindow(num){
	  var FWindow=FWindows[num];
	 
	  FWindows[num]=null;
	  
	  FWindow.closeWindow();
	  update_menu_items();
  }	 
  
  function minimizeWindow(num){
  	  FWindows[num].minimizeWindow();
  }
  
  function toFront(num){
  	for(i=1; i<(FWindows.length); i++){
	    if(FWindows[i]!=null){
			if(i==num){
				FWindows[i].toFront();
			}else{
				FWindows[i].toBack();
			}
		}	
	  }
  }
  
  function changeWindowsSize(num){
  	 if(FWindows[num].isMaximized()){
	 	FWindows[num].restaureWindow(); 
	 } else {
	 	FWindows[num].maximizeWindow();
	 }
  }
	 
  function open_menu(){
     var menu=document.getElementById('menu_desplegable');
	 
	 menu.style.visibility= "visible";
	 menu.style.zIndex= "1";
  }	 
  
  function close_menu(){
     var menu=document.getElementById('menu_desplegable');
	 
	 menu.style.visibility= "hidden";
	 menu.style.zIndex= "0";
  }	 
  
  function setDesktopBackground(image){
   	  var desktop=document.getElementById('body');
	  
	  desktop.style.backgroundImage="url("+image+")";
   }
   
   function setDesktopColor(color){
   	  var desktop=document.getElementById('body');
	  
	  desktop.style.backgroundColor=color;
   }
   
   function update_menu_items(){
      var items=document.getElementById('menu_bar_items');
	  var i; 
	  
	  items.innerHTML="";
	  for(i=1; i<(FWindows.length); i++){
	  	if(FWindows[i]!=null){
	  	  items.innerHTML=items.innerHTML+" | <a href=\"javascript:openWindow("+i+")\" class=\"link_menu_item\">"+FWindows[i].getTitle()+"</a> <a href=\"javascript:closeWindow("+i+")\" class=\"link_menu_item\">(X)</a>";
	    }
	  }
   }
  
  
//----------------------------------- CLOCK FUNCTIONS
  function UpdateClock() {
    var reloj=document.getElementById('reloj');
   
    if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
     }

    var tDate = new Date();

    var hora = (tDate.getHours()>9) ? tDate.getHours() : "0"+tDate.getHours();
    var minutos = (tDate.getMinutes()>9) ? tDate.getMinutes() : "0"+tDate.getMinutes();
   
    reloj.innerHTML = hora + ":" + minutos;
   
    clockID = setTimeout("UpdateClock()", 5000);
   }
   
   function StartClock() {
     clockID = setTimeout("UpdateClock()", 500);
   }
//--------------------------------- END CLOCK FUNCTIONS