/* 
  Algo: a dekstop environment look-a-like in your web browser
  Copyright (C) 2009  Niels Serup
  
	This file is part of Algo.

	Algo is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Algo is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Algo.  If not, see <http://www.gnu.org/licenses/>.
*/

window.onload = function() {
  // Get the head element
  head_con = document.getElementsByTagName('head')[0]
  
  // Theme support - .href will be added at the end of the onload function
  theme = document.createElement('link')
  theme.rel = 'stylesheet'
  theme.type = 'text/css'
  head_con.appendChild(theme)
  
  
  // wrapper holds everything
  wrapper = document.createElement('div')
  wrapper.style.display = 'none'
  document.body.appendChild(wrapper)
  
    
  // A lot of globals used in various functions
  global_directions = ['top', 'right', 'bottom', 'left']
  global_directions_length = global_directions.length
  global_directions_dt = ['tl', 't', 'tr', 'r', 'br', 'b', 'bl', 'l']
  global_directions_dt_length = global_directions.length
  global_options = ['minimize', 'maximize', 'close']
  hiding_drops = false
  posis = {}
  wnds = new Array()
  
  x = 0
  y = 0
  moving_window = -1
  adjusting_window = -1
  adjusting_dir = -1
  start_x = -1
  start_y = -1
  diff_x = 0
  diff_y = 0
  
  width_min = 150
  height_min = 100
  
  abs_scroller_width = 10
  appbox_padding = 2
  appbox_margin = 2
  appbox_border = 1
  abs_scrolled = 0
  
  theme_set = false
  
  // Ew
  if (navigator.appName == 'Microsoft Internet Explorer') ie = true
  else ie = false
  
  
  /* Prepares fixing a highligthing problem that occurs whenever
     you've moved or adjusted a window. */
  highlightfix = document.createElement('textarea')
  highlightfix.className = 'highlightfix'
  highlightfix.value = 'fix'
  wrapper.appendChild(highlightfix)
  
  // Extra security for some elements that just don't exist
  fallback = document.createElement('div')
  
  
  // navi = navigation bar = navbar. Here 2 are created.
  navi = []
  navi[0] = new navbar([[0, ''], -1, -1, [0, '']])
  navi[1] = new navbar([-1, -1, [0, ''], [0, '']])
  
  // Where to put different stuff..
  navi[0].support('dropdowns')
  navi[0].support('clock')
  navi[1].support('appboxes')
  
  // ..and actually putting it there
  navi[posis['dropdowns']].box('dropdowns', 'left')
  navi[posis['clock']].box('clock')
  navi[posis['appboxes']].box('appboxes', 'left')  
  navi[posis['appboxes']].box('abs_scroller')
  
  
  
  /* Create dropdown menus. You can change the way a window opens
     by writing a "#" after the title, followed by your variables */
     
  navi[posis['dropdowns']].drop('Programs', [
  ['Math', [
    'Calculator'
  ]], 
  ['Office', [
    'Notes#true,-1,-1,400,400,w-405,sh-405'
  ]]
  ])
  
  navi[posis['dropdowns']].drop('Algo', [
  'About',
  'Help',
  ])
  
  navi[posis['dropdowns']].drop('Test', [
  'A',
  ['B', [
  'C',
  'D',
  ['E', [
  ['F', [
  'G',
  'H'
  ]]
  ]],
  'I'
  ]]
  ])
  
  // Create the screen area (the area on which the windows can move freely)
  scr = new screen()
  scr.wh()
  
  // Event actions
  document.onmousemove = scr.set_coor
  
  window.onresize = function() {
    scr.update()
    for (var i = 0; i < scr.len; i++) {
      if (!wnds[i].normal) {
        scr.wh()
        wnds[i].width = w + scr.margin_g * 2
        wnds[i].height = sh + scr.margin_g
        wnds[i].update()
      }
    }
    // Check if scrollbars are needed to make all appboxes visible
    navi[posis['appboxes']].check_abs_height(false)
  }
  
  document.onmouseup = function() {
    // End various stuff
    if (moving_window > -1)
      scr.move_end()
    if (adjusting_window > -1)
      scr.adjust_end()
  }
  
  // Set the theme (will default to the default if you specify an unexisting theme)
  settheme('Default')
}