var winadd;
var janelasminimizadas = 0;
var primeiravez = false;
var main;
var main_count = 0;

// Overide WindowUtilities getPageSize to remove dock height (for maximized windows)
WindowUtilities._oldGetPageSize = WindowUtilities.getPageSize;
WindowUtilities.getPageSize = function() {
  var size = WindowUtilities._oldGetPageSize();
  var dockHeight = $('dock').getHeight();
  
  size.pageHeight -= dockHeight;
  size.windowHeight -= dockHeight;
  return size;
};    


// Overide Windows minimize to move window inside dock  
Object.extend(Windows, {
  // Overide minimize function
  minimize: function(id, event) {
    var win = this.getWindow(id)
    if (win && win.visible) {
      // Hide current window
      win.hide();            
    
      // Create a dock element
	  $('dock').style.visibility="visible";
	  janelasminimizadas++;
      var element = document.createElement("span");
      element.className = "dock_icon"; 
      element.style.display = "none";
	  element.style.cursor = "pointer";
      element.win = win;
      $('dock').appendChild(element);
      Event.observe(element, "mouseup", Windows.restore);
      $(element).update(win.getTitle());
    
      new Effect.Appear(element)
    }
    Event.stop(event);
  },                 
  
  // Restore function
  restore: function(event) { 
    var element = Event.element(event);
    // Show window
    element.win.show();
    //Windows.focus(element.win.getId());                    
    element.win.toFront();
    // Fade and destroy icon
    new Effect.Fade(element, {afterFinish: function() {element.remove()}})
	janelasminimizadas--;
	if (janelasminimizadas<1) { $('dock').style.visibility="hidden";}
	{
	}
  }
})

// blur focused window if click on document
Event.observe(document, "click", function(event) {   
  var e = Event.element(event);
  var win = e.up(".dialog");
  var dock = e == $('dock') || e.up("#dock"); 
  if (!win && !dock && Windows.focusedWindow) {
    Windows.blur(Windows.focusedWindow.getId());                    
  }
})               

// Change theme callback
var currentTheme = 0;
function changeTheme(event) {
  var index = Event.element(event).selectedIndex;
  if (index == currentTheme)
    return;

  var theme, blurTheme;
  switch (index) {
    case 0:
      theme = "mac_os_x";
      blurTheme = "blur_os_x";
      break;
    case 1:
      theme = "bluelighting";
      blurTheme = "greylighting";
      break;
    case 2:
      theme = "greenlighting";
      blurTheme = "greylighting";
      break;
    case 3:
      theme = "alphacube";
      blurTheme = "alphacube";
      break;
    case 4:
      theme = "darkX";
      blurTheme = "darkX";
      break;
    case 5:
      theme = "spread";
      blurTheme = "spread";
      break;
    case 6:
      theme = "nuncio";
      blurTheme = "nuncio";
      break;
  }

  Windows.windows.each(function(win) {
    win.options.focusClassName = theme; 
    win.options.blurClassName = blurTheme;
    win.changeClassName(blurTheme)
  });
  Windows.focusedWindow.changeClassName(theme);
  currentTheme = index;
}


// Init webOS, create 3 windows
function initWebOS() {         
	if (primeiravez==false)
	{

  Windows.closeAll();
  // Create 3 windows
  //$R(1,3).each(function(index) {
  //  var win = new Window({className: "mac_os_x", blurClassName: "blur_os_x", title: "window #"+index, width:250, height:150, top: 100 + index*50, left:100 + index*50}); 
  //  win.getContent().update("<h1>Window #" + index + "</h1>");
  //  win.show();    
  //})                 
  //
  $$("#theme select").first().selectedIndex = currentTheme;
  Event.observe($$("#theme select").first(), "change", changeTheme);  

  left = new Window({id: "left", className: "mac_os_x", blurClassName: "blur_os_x", title: "Menu", width:250, height:450, top:1, left:1, closable:false});
  left.setURL("../indic/left.php");
  left.setDestroyOnClose();
  left.show();

  main = new Window({id: "main", className: "mac_os_x", blurClassName: "blur_os_x", title: "Principal", width:650, height:450, top: 1, left:260});
  main.setURL("../apresentacao.php");
  main.setDestroyOnClose();
  main.show();
  primeiravez = true;
  }
}

function ChecaLogin() {
  checa = new Window({id: "checa", className: "mac_os_x", blurClassName: "blur_os_x", title: "ChecaLogin", width:250, height:250, top:1, left:1});
  checa.setURL("../config/checaloginwindow.php");
  checa.setDestroyOnClose();
  //checa.show();
}

function LogMein() {
  Windows.closeAll();
  var login = new Window({id: "login", className: "mac_os_x", blurClassName: "blur_os_x", title: "Entrada", width:250, height:150, top:1, left:1, resizable: false, maximizable: false, minimizable:false, closable:false});
  login.setURL("../login.php");
  login.showCenter('true');
  login.setDestroyOnClose();
}

function alerta(alertar,titulo) {
		Dialog.alert("<center><br>"+ alertar + "</center><br><br>", 
				        {className: "mac_os_x_dialog", title: titulo, windowParameters: {width:300, height:100}, okLabel: "Ok", 
						    ok:function(win) {debug("validate alert panel"); return true}
						    });
}

function ModalUpXLS(banco) {
 // debug($('modal_window_content'))
 winadd = new Window('upxls_window', {className: "dialog", title: "Adicionar dados a banco existente",top:100, left:100,  width:360, height:188, zIndex:150, opacity:1, resizable: false, maximizable: false, minimizable:false})
 winadd.setURL('../indic/upxls-htm.php?nome='+banco);
 winadd.setDestroyOnClose();
 //winadd.show('true');
 winadd.showCenter('true');
}


function ModalAdd(processo,nodeX) {
 // debug($('modal_window_content'))
 winadd = new Window('modal_window', {className: "dialog", title: "Adicionar Elemento",top:100, left:100,  width:360, height:288, zIndex:150, opacity:1, resizable: false, maximizable: false, minimizable:false})
 winadd.setURL('../indic/add.php?proc='+processo+'&cod='+nodeX);
 winadd.setDestroyOnClose();
 //winadd.show('true');
 winadd.showCenter('true');
}


function geraDados() {
		mygrid = new dhtmlXGridObject('gridboxDados');
		mygrid.setImagePath("../image/");
		mygrid.setHeader("id, variável");
		mygrid.setInitWidths("200,150")
		mygrid.setColAlign("left,right")
		mygrid.setColTypes("ro,ro");
		mygrid.setColSorting("str,int")

		mygrid2 = new dhtmlXGridObject('gridboxEstatistica');
		mygrid2.setImagePath("../image/");
		mygrid2.setHeader("índices, valor");
		mygrid2.setInitWidths("105,105")
		mygrid2.setColAlign("right,left")
		mygrid2.setColTypes("ro,ro");
		mygrid2.setColSorting("str,int");
}



function atualizaDados(sql) {
		mygrid.setXMLAutoLoading("../mapa/dados_xml.php?sql="+sql);
		mygrid.init();
		mygrid2.init();
		mygrid2.loadXML("../mapa/estatistica_xml.php?sql="+sql);
		mygrid.loadXML("../mapa/dados_xml.php?sql="+sql);
		prompt('',"../mapa/dados_xml.php?sql="+sql);
}


function AbreDados(sql,titulo) {
  if (!$("dados_content"))
  {
  dados = new Window({id: "dados", className: "mac_os_x", blurClassName: "blur_os_x", title: "Tabela: "+titulo, width:650, height:450, top: 10, left:290});
  dados.getContent().update('<table><tr><td valign=top><div id="gridboxDados" width="370px" height="450px" style="overflow:hidden"> </div></td><td valign=top><div id="gridboxEstatistica" width="210px" height="230px" style="background-color:white;overflow:hidden"></div></td></tr></table>');
  geraDados();
  dados.setDestroyOnClose();
  }
  atualizaDados(sql);
  dados.show();
  f = $("dados_content");

}

function atualizaGrafico(sql)
{
var nomehost = window.location.hostname;
	grafx=$("graf").value;
	$("graficoimg").src = 'http://'+nomehost+'/mapserver/mapa/grafico.php?graf='+grafx+'&sql=' + sql;
}

function CloseWinAdd() {
    winadd.close();
}

function AbreGraf(sql,titulo) {
  if (!$("graf_content"))
  {
  graf = new Window({id: "graf", className: "mac_os_x", blurClassName: "blur_os_x", title: "Grafico: "+titulo, width:650, height:450, top: 10, left:290});
  graf.getContent().update('<table class=tab><tr><td><img id=graficoimg src="/cgi-bin/owtchart.exe?YLabel2Fmt=%.0f&Type=3DBar&W=500&H=400&NumSets=1&NumPts=1&Vals=1&XLabels=Reload"></td></tr></table><BR><form action="no" name="tabelas" method="post"><SELECT NAME="graf" ID="graf" onchange="javascript: atualizaGrafico("'+sql+'");">	<option value=line>Linha</option><option value=3DLine>Linha 3D</option>	<option value=area>Área</option>	<option value=3DArea>Área 3D</option>	<option value=bar>Barras</option>	<option value=3DBar selected>Barras 3D</option>	<option value=Pie>Torta</option>	<option value=3DPie>Torta 3D</option>	<option	value=HBar>Barras Horizontais</option>	<option value=3DHBar>Barras Horizontais 3D</option></SELECT></form>');
  graf.setDestroyOnClose();
 }
  graf.show();
  atualizaGrafico(sql);
  f = $("dados_content");

}




Event.observe(window, "load", ChecaLogin)
               
               
