// 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 - 64;
  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
      var element = document.createElement("span");
      element.className = "dock_icon"; 
      element.style.display = "none";
      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()}})
  }
})

// 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());                    
  }
})               

// Chnage 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;
  }
  Windows.windows.each(function(win) {
    win.options.focusClassName = theme; 
    win.options.blurClassName = blurTheme;
    win.changeClassName(blurTheme)
  });
  Windows.focusedWindow.changeClassName(theme);
  currentTheme = index;
}

WinW = window.innerWidth;
WinH = window.innerHeight;

function openwin(q,i) {         
	
	t = q;
	
	if (i != "") { q = q + "&i=" + i }

if (!$("t" + t)) {	
	var win = new Window({
		className: 			"mac_os_x",
		blurClassName: 		"mac_os_x",
		id:					"t" + t,
		title: 				t.toUpperCase(),
		closable:       	true,
		minimizable:       	true,
		maximizable:       	true,
		width:				380,
		height:				500
		});
    win.setURL("pagina.asp?q="+q)
    win.show(); 
	}
}  


function twitter(q) {
	if (!$("q" + q)) {
    var win = new Window({
		className: 			"mac_os_x",
		blurClassName: 		"mac_os_x",
		id:					"q" + q,
		title: 				"@" + q.toUpperCase(),
		closable:       	true,
		minimizable:       	true,
		maximizable:       	true,
		width:				380,
		height:				500
		}); 
    win.setURL("twitter.asp?q=from%3A"+q)
    win.show(); 
	}
}


function info(u) {
	if (!$("u" + u)) {
    var win = new Window({
		className: 			"mac_os_x",
		blurClassName: 		"mac_os_x",
		id:					"u" + u,
		title: 				"INFO @" + u.toUpperCase(),
		closable:       	true,
		minimizable:       	false,
		maximizable:       	false,
		width:				512,
		height:				256
		}); 
    win.setURL("info.asp?u="+u)
    win.showCenter(); 
	}
}

function wellcome() {         
    var win = new Window({
		className: 			"mac_os_x",
		blurClassName: 		"mac_os_x",
		title: 				"Bem vindo!",
		closable:       	true,
		minimizable:       	false,
        maximizable:      	false,
		resizable:			false,
		width:				640,
		height:				430,
		top:				100
		}); 
    win.setURL("wellcome.asp")
    win.showCenter(); 
	}


function calendario() {
	if (!$("calendario")) {
    var win = new Window({
		className: 			"mac_os_x",
		blurClassName: 		"mac_os_x",
		id:					"calendario",
		title: 				"CALENDÁRIO",
		url:				"calendario/calendario.asp",
		closable:       	true,
		minimizable:       	true,
		maximizable:       	true,
		width:				WinW - 140,
		height:				WinH - 140,
		top:				64,
		}); 
    win.showCenter();
	}
}


function estatisticas() {
	if (!$("estatisticas")) {
    var win = new Window({
		className: 			"mac_os_x",
		blurClassName: 		"mac_os_x",
		id:					"estatisticas",
		title: 				"ESTATÍSTICAS",
		url:				"estatisticas.asp",
		closable:       	true,
		minimizable:       	true,
		maximizable:       	true,
		width:				600,
		height:				500,
		top:				64,
		}); 
    win.show();
	}
}
function janelao(endereco,titulo) {
    var win = new Window({
		className: 			"mac_os_x",
		blurClassName: 		"mac_os_x",
		title: 				titulo.toUpperCase(),
		url:				endereco,
		closable:       	true,
		minimizable:       	false,
		maximizable:       	false,
		width:				WinW - 140,
		height:				WinH - 140,
		top:				64,
		}); 
    win.showCenter();
	}
	
function shorty() {
if (!$("shorty")) {	
    var win = new Window({
		id:					"shorty",
		className: 			"mac_os_x",
		blurClassName: 		"mac_os_x",
		title: 				"SHORTY AWARDS",
		url:				"shorty/default.asp",
		closable:       	true,
		minimizable:       	true,
		maximizable:       	true,
		width:				780,
		height:				300,
		top:				64
		}); 
    win.show();
	}
}
