var zindex = 10;
var path;


var Janelas = {
  janelas: [],
  observers: [],
  
  
  register: function(janela){
  this.janelas.push(janela);
  },
  
  unregister: function(janela){
  	for (i=0;i<this.janelas.length;i++){
  		if (this.janelas[i] == janela){
    	this.janelas.splice(i,1);
    	}
 	}
  },
  
  setSkin: function(skin){
  	
  	new Insertion.Before(document.body, "<LINK REL='STYLESHEET' HREF='skins/"+ skin + "/estilo.css' TYPE='text/css' >");
  	path = "skins/" + skin + "/"; 
  //alert(path);  
  }
 
}    


var Janela = Class.create();
Janela.prototype = {
  initialize: function(element, options) {
  	
  	// op??es aplicadas diretamente ao div janela
    this.element = $(element);
    this.element.style.display = "inline";
  	this.element.style.position = "fixed";
	if (navigator.appName.substring(0,9) == "Microsoft"){
	this.element.style.position = "absolute";}  // ie hack
	this.element.style.margin = "0px";
	
    zindex = zindex + 2;
    this.element.style.zIndex = zindex;
    this.active = true;
  
     
    // Op??es de configura??o
    
    this.status = 'regular'; 
    this.width = options.width || '500px';
    this.title = options.title || element;
    this.height = options.height || "300px";
    this.modal = options.modal || false;
    
    //calculo como centralizar
    var vcenter =  window.screen.availHeight/2 - this.height.substring(0,3)/1 + "px"
	var hcenter =  window.screen.availWidth/2 - this.width.substring(0,3)/2 - 50 + "px";     	
    
    if (options.top == "center"){options.top = vcenter;}
	if (options.left == "center"){options.left = hcenter;}
	
	this.element.style.top = options.top || vcenter;
	this.element.style.left = options.left || hcenter;
	
	if(navigator.userAgent.indexOf("MSIE") != -1){
    	this.contentArea = this.element.firstChild;}
    else {this.contentArea = this.element.childNodes.item(1);}
    	
    this.contentArea.style.height = this.height;
   	Element.show(this.contentArea); 
	
	// registro a janela
	Janelas.register(this);
     
    //crio a barra de t?tulo
    this.createTopBar();
    
    this.contentArea.style.width = this.width;
    this.contentArea.className = "contentArea";
   
    // this.createStatusBar();
    // this.createToolBar();
    
    // crio os bot?es
    this.createButtonShow();
    this.createButtonHide();
    this.createButtonMinimize();
    this.createButtonRestaure();
    this.createButtonMaximize();
    this.createButtonClose();
   
    Element.hide(this.btShow);
    Element.hide(this.btRestaure);
    
    // crio o div modal 
    if (this.modal){
    	this.createDisabledArea();
    	Element.hide(this.btMaximize,this.btMinimize,this.btHide,this.btShow,this.btRestaure);
    }
     
    // chamo o eventos sobre o btClose
   	this.btClose.click = this.close.bindAsEventListener(this);
   	this.btClose.over = this.closeOver.bindAsEventListener(this);
    this.btClose.out = this.closeOut.bindAsEventListener(this);

    // chamo o eventos sobre o btMinimize     
    this.btMinimize.click = this.minimize.bindAsEventListener(this);
   	this.btMinimize.over = this.minimizeOver.bindAsEventListener(this);
    this.btMinimize.out = this.minimizeOut.bindAsEventListener(this);
     
    // chamo o eventos sobre o btMaximize     
    this.btMaximize.click = this.maximize.bindAsEventListener(this);
   	this.btMaximize.over = this.maximizeOver.bindAsEventListener(this);
    this.btMaximize.out = this.maximizeOut.bindAsEventListener(this);

	// chamo o eventos sobre o btShow
    this.btShow.click = this.show.bindAsEventListener(this);
   	this.btShow.over = this.showOver.bindAsEventListener(this);
    this.btShow.out = this.showOut.bindAsEventListener(this);
          
    // chamo o eventos sobre o btHide
    this.btHide.click = this.hide.bindAsEventListener(this);
   	this.btHide.over = this.hideOver.bindAsEventListener(this);
    this.btHide.out = this.hideOut.bindAsEventListener(this);
          
    // chamo o eventos sobre o btRestaure
    this.btRestaure.click = this.restaure.bindAsEventListener(this);
   	this.btRestaure.over = this.restaureOver.bindAsEventListener(this);
    this.btRestaure.out = this.restaureOut.bindAsEventListener(this);
          
    // crio eventos     
    this.keypress = this.keyPress.bindAsEventListener(this);
    this.element.click = this.activateWindow.bindAsEventListener(this);
    
    // observo os eventos do btClose
    Event.observe(this.btClose, "click", this.btClose.click);
    Event.observe(this.btClose, "mouseover", this.btClose.over);
    Event.observe(this.btClose, "mouseout", this.btClose.out);
    
    // observo os eventos do btMinimize
    Event.observe(this.btMinimize, "click", this.btMinimize.click);
    Event.observe(this.btMinimize, "mouseover", this.btMinimize.over);
    Event.observe(this.btMinimize, "mouseout", this.btMinimize.out);
    
    // observo os eventos do btMinimize
    Event.observe(this.btMaximize, "click", this.btMaximize.click);
    Event.observe(this.btMaximize, "mouseover", this.btMaximize.over);
    Event.observe(this.btMaximize, "mouseout", this.btMaximize.out);
    
    // observo os eventos do btShow
    Event.observe(this.btShow, "click", this.btShow.click);
    Event.observe(this.btShow, "mouseover", this.btShow.over);
    Event.observe(this.btShow, "mouseout", this.btShow.out);
    
    // observo os eventos do btHide
    Event.observe(this.btHide, "click", this.btHide.click);
    Event.observe(this.btHide, "mouseover", this.btHide.over);
    Event.observe(this.btHide, "mouseout", this.btHide.out);
    
    // observo os eventos do btHide
    Event.observe(this.btRestaure, "click", this.btRestaure.click);
    Event.observe(this.btRestaure, "mouseover", this.btRestaure.over);
    Event.observe(this.btRestaure, "mouseout", this.btRestaure.out);
    
    // outros eventos     
    Event.observe(document, "keypress", this.keypress);
    Event.observe(this.element, "click", this.element.click); 
  
   this.activateWindow();
  }, 
  
 close: function(){
   
  	if (this.active == true){
  	 Element.hide(this.element);
  	 Element.remove(this.btShow);
  	 Element.remove(this.btHide);
  	 Element.remove(this.btClose);
 	 Element.remove(this.btMinimize);
 	 Element.remove(this.btMaximize);
 	 Element.remove(this.topBar);
  	 if (this.modal){Element.remove(this.disabledArea);}
  	 Janelas.unregister(this);
  	 this.activateUpper();
  	 
  	 }
  	
  },
  
  closeOver: function(){
   	this.btClose.src = path + "closeOver.png";
  },
      
  closeOut: function(){
   	this.btClose.src = path + "close.png";
  },
  
  minimize: function(){
  	if (this.active == true){
	  	if (this.status=='maximized'){  
	   	Element.toggle(this.btMaximize,this.btMinimize);}
	   	if (this.status=='regular'){  
	   	Element.toggle(this.btRestaure,this.btMinimize);
	    this.top = this.element.style.top;
	  	this.left = this.element.style.left;
	  	this.width = this.topBar.style.width;
	  	this.height = this.topBar.style.height;  	 
	  	}  	 
	   	
	    Element.hide(this.contentArea, this.btHide, this.btShow);
	    this.element.style.top = '650px';
	    this.element.style.left = '2px';
	   	this.topBar.style.width = '150px';
	   	this.status = 'minimized';
   	}	
  },
  
  minimizeOver: function(){
   	this.btMinimize.src = path + "minimizeOver.png";
  },
  
  minimizeOut: function(){
   	this.btMinimize.src = path + "minimize.png";
  },
  
  show: function(){
   	Element.show(this.contentArea);
   	Element.toggle(this.btShow,this.btHide);
  },
  
  showOver: function(){
   	this.btShow.src = path + "showOver.png";
  },
  
  showOut: function(){
   	this.btShow.src = path + "show.png";
  },
  
  hide: function(){
   	Element.hide(this.contentArea);
   	Element.toggle(this.btShow,this.btHide);
  },
  
  hideOver: function(){
   	this.btHide.src = path + "hideOver.png";
  },
  
  hideOut: function(){
   	this.btHide.src = path + "hide.png";
  },
  
  maximize: function(){
	if (this.active == true){  
  
	  	if (this.status=='minimized'){
	  	Element.toggle(this.btMinimize,this.btMaximize);}
	  	if (this.status=='regular'){
	  	Element.toggle(this.btRestaure,this.btMaximize);
	  	this.top = this.element.style.top;
	  	this.left = this.element.style.left;
	  	this.width = this.topBar.style.width;
	  	this.height = this.topBar.style.height;  	 
	  	}
	  	Element.show(this.contentArea, this.btHide);
	  	Element.hide(this.btShow);	
	  	 
		this.topBar.style.width = window.screen.availWidth - 12;	  
		this.contentArea.style.width = "100%"; 
		if(navigator.userAgent.indexOf("MSIE") != -1){
		this.topBar.style.width = window.screen.availWidth - 25;
		} 
		this.element.style.position = "absolute";
		this.element.style.top = "0px";
		this.element.style.left = "0px";
		 
		this.status = 'maximized';
	}
  },
  
  maximizeOver: function(){
   	this.btMaximize.src = path + "maximizeOver.png";
  },
  
  maximizeOut: function(){
   	this.btMaximize.src = path + "maximize.png";
  },  
  
  restaure: function(){
  	if (this.active==true){
	  	 if (this.status=='maximized'){
	  	 Element.toggle(this.btMaximize,this.btRestaure);
	  	 this.contentArea.style.width = this.width;}
		 if (this.status=='minimized'){
	  	 Element.toggle(this.btMinimize,this.btRestaure);}
		 Element.show(this.contentArea, this.btHide);
	  	 Element.hide(this.btShow);	
		 this.topBar.style.width = this.width;
	  	 this.topBar.style.height = this.height;  	
		 this.element.style.top = this.top;
		 this.element.style.left = this.left;
		 this.status = 'regular';
  	}
  },
  
  restaureOver: function(){
   	this.btRestaure.src = path + "restaureOver.png";
  },
  
  restaureOut: function(){
   	this.btRestaure.src = path + "restaure.png";
  },  
        
  keyPress: function(event){
  	if(event.keyCode==Event.KEY_ESC) this.close();
  	if(event.keyCode==Event.KEY_RETURN) this.maximize();
    return;
  },
  
  activateWindow: function(){
  var i; 
 
    for (i=0;i<Janelas.janelas.length;i++){
   		if (Janelas.janelas[i] != this){
	   		Janelas.janelas[i].topBar.className = 'desactiveTopBar';
	  		Janelas.janelas[i].element.className = 'desactiveWindow';
	  		Janelas.janelas[i].active = false;
	  		//alert("Desativada: " +i+" : "+ Janelas.janelas[i].element.id);
	  	} else {
	  	  zindex = zindex + 2;
	  	  this.element.style.zIndex = zindex;
  	  	  this.topBar.className = 'activeTopBar';
  	  	  this.element.className = 'activeWindow';
  	  	  Janelas.janelas[i].active = true;
		  	  		//alert("Ativada: " +i+" : "+ Janelas.janelas[i].element.id);	
  	  	  }
   }
      
  }, 
 
  activateUpper: function(){
  var z; 
  z=0;
  var i;

    for (i=0;i<Janelas.janelas.length;i++){
   
   		if (Janelas.janelas[i].element.style.zIndex > z){
   		z = Janelas.janelas[i].element.style.zIndex;
   		objeto = Janelas.janelas[i];
   		}
	}
	//alert("Objeto Selecionado : " + objeto.element.id);
	objeto.topBar.className = 'activeTopBar';
  	objeto.element.className = 'activeWindow';
  	objeto.active = true;
  },     
       
  createButtonMinimize: function(){
    this.btMinimize = new Image();
    this.btMinimize.src = path + "minimize.png";
    this.btMinimize.className = "button";
    this.buttonBar.appendChild(this.btMinimize);
  },	
  
  createButtonMaximize: function(){
    this.btMaximize = new Image();
    this.btMaximize.src = path + "maximize.png";
    this.btMaximize.className = "button";
    this.buttonBar.appendChild(this.btMaximize);
  },	
   
  createButtonClose: function(){
    this.btClose = new Image();
    this.btClose.src = path + "close.png";
    this.btClose.className = "button";
    this.buttonBar.appendChild(this.btClose);
  },	
  
  createButtonShow: function(){
  	this.btShow = new Image();
    this.btShow.src = path + "show.png";
    this.btShow.className = "button";
    this.buttonBar.appendChild(this.btShow);
  },
  
  createButtonHide: function(){
  	this.btHide = new Image();
    this.btHide.src = path + "hide.png";
    this.btHide.className = "button";
    this.buttonBar.appendChild(this.btHide);
  },
  
  createButtonRestaure: function(){
  	this.btRestaure = new Image();
    this.btRestaure.src = path + "restaure.png";
    this.btRestaure.className = "button";
    this.buttonBar.appendChild(this.btRestaure);
  },
    
  createTopBar: function(){
    new Insertion.Top(this.element,"<div id='topBar'><div id='titleBar' style='float:left'>&nbsp;" + this.title + "</div><div id='buttonBar' style='float:right'></div></div>");
   
   	if(navigator.userAgent.indexOf("Firefox") != -1){
  		this.topBar = this.element.childNodes.item(0);
  		this.titleBar = this.topBar.childNodes.item(0);
  		this.buttonBar = this.topBar.childNodes.item(1);
  	}
	else if(navigator.userAgent.indexOf("MSIE") != -1){
	   	this.topBar = this.element.firstChild;
  		this.titleBar = this.topBar.firstChild;
  		this.buttonBar = this.topBar.lastChild;
	}
	else if(navigator.userAgent.indexOf("Netscape") != -1){
		this.topBar = this.element.childNodes.item(0);
  		this.titleBar = this.topBar.childNodes.item(0);
  		this.buttonBar = this.topBar.childNodes.item(1);}
	else{ 
		this.topBar = this.element.childNodes.item(0);
  		this.titleBar = this.topBar.childNodes.item(0);
  		this.buttonBar = this.topBar.childNodes.item(1); }
   	
    
   	this.buttonBar.style.cursor = "hand";
   	//this.titleBar.style.border = "1px solid blue";   	
   	this.topBar.className = "activeTopBar";
    this.titleBar.className = "titleBar";
    this.buttonBar.className = "buttonBar";
    //alert(this.buttonBar.className);
    
  	if(navigator.userAgent.indexOf("Firefox") != -1){
  		this.topBar.style.width = Number(this.width.substring(0,3)) + 2 + "px";}
	else if(navigator.userAgent.indexOf("MSIE") != -1){
	   	this.buttonBar.style.height = "20px";	
	   	this.topBar.style.width = this.width}
	else if(navigator.userAgent.indexOf("Netscape") != -1){
		this.topBar.style.width = this.width}
	else{ 
		this.topBar.style.width = this.width}
  },
  
  createDisabledArea : function(){   //n?o despon?vel para IE
  	this.disabledArea = document.createElement("div");
    document.body.appendChild(this.disabledArea);
    this.disabledArea.style.background = 'transparent';
    this.disabledArea.style.width = window.screen.availWidth;
    this.disabledArea.style.height = window.screen.availHeight;
    this.disabledArea.style.position = "fixed";
    if (navigator.appName.substring(0,9) == "Microsoft"){
       this.disabledArea.style.position = "absolute";
       //this.disabledArea.style.top = "0px";
    }
    this.disabledArea.style.top = "0px";
    this.disabledArea.style.left = "0px";
    this.disabledArea.style.zIndex = zindex - 1;
  }

}
 
    