// Need the prototype js framework

// =====================================================
// GLOBALS            
// =====================================================
// Global vars
var PREVIOUS_WINDOW_SIZE = {height: 0, width: 0}

// =====================================================
// COMMON OBJECT EXTENSION
// =====================================================
document.getAncestorsByTagName = function(tag_name, element) {
  element = $(element).parentNode
  tag_name = tag_name.toUpperCase()
  var ancestors = new Array()
  while (element && element.tagName) {
    if (element.tagName.toUpperCase() == tag_name)
      ancestors.push(element)
    element = element.parentNode
  }
  return ancestors
}

Array.prototype.apply = function(func_name){
  for (var i=0; i<this.length; i++) {
    func_name(this[i])
  }
}

// =====================================================
// WINDOW MANAGER                              
// =====================================================
// Resize the main window to fit the screen.
var WindowManager = {
  size: function(element) {
    var size = {}
    element = $(element)
    size.width = element.clientWidth
    size.height = element.clientHeight
    if(size.width == 0) size.width = element.scrollWidth
    if(size.height == 0) size.height = element.scrollHeight
    return size
  },  
  offset: function(element) {
    var offset = {}
    element = $(element)
    offset.top = element.offsetTop
    offset.left = element.offsetLeft
    while(element = element.offsetParent) {
      offset.top += element.offsetTop
      offset.left += element.offsetLeft
    }
    return offset
  },
  window_size: function() {
    var size = {}
    size.height = window.innerHeight
    size.width = window.innerWidth
    if(!size.width) size.width = document.documentElement && document.documentElement.clientWidth
    if(!size.height) size.height = document.documentElement && document.documentElement.clientHeight
    return size
  }
}

// =====================================================
// MISCEALENOUS                                    
// =====================================================
// Various utilities to render page
var Misc = {  
  // Add a function to the onload event
  add_to_onload: function(string_to_eval) {
    var loading = window.onload;
    window.onload = function() {
      if (loading) loading();
      eval(string_to_eval)
    }
  },
  add_to_resize: function(string_to_eval) {
    var loading = window.onresize;
    window.onresize = function() {
      if (loading) loading();
      eval(string_to_eval)
    }
  },
  add_to_onchange: function(string_to_eval, element) {
    element = $(element)
    var changing = element.onchange
    element.onchange = function() {
      if (changing) changing()
      eval(string_to_eval)
    }
  },
  resized: function(obj) {
    if (PREVIOUS_WINDOW_SIZE.height != obj.height || 
        PREVIOUS_WINDOW_SIZE.width != obj.width) {
      PREVIOUS_WINDOW_SIZE.height = obj.height
      PREVIOUS_WINDOW_SIZE.width = obj.width
      return true;
    } else {
      return false
    }
  },
  // Display footer at the bottom of the page if it is to short.
  display_footer: function(element) {
    element = $(element)
    window_size = WindowManager.window_size()
    if(Misc.resized(window_size)) {
      Misc.refresh_footer(element)
    }
  },
  refresh_footer: function(element){
    element = $(element)
    window_size = WindowManager.window_size()
    offset = WindowManager.offset(element)
  size = WindowManager.size(element)
  
  if(element.style.position=='absolute')
    var max_size = WindowManager.size(element.parentNode).height + size.height
  else
    var max_size = WindowManager.size(element.parentNode).height
  
  //alert(max_size + ' ,' + window_size.height + ' ,' + (max_size <= window_size.height).toString())
  
    if(max_size <= window_size.height)
      Misc.absolute_footer(element)
    else //if(Element.getStyle(element, 'position')=='absolute' && (max_size > window_size.height))
      Misc.relative_footer(element)
  },
  absolute_footer: function(element) {
    new_height = WindowManager.window_size().height - WindowManager.size(element.parentNode).height
    Element.setStyle(element, {'position':'relative', 'top':new_height.toString() + 'px'})
  },
  relative_footer: function(element) {
    Element.setStyle(element, {'position':'relative', 'top':'0px'})
  },
  // Submit a form given is id
  form_submit: function(element) {
    element = $(element)
    if(element) {
      element.submit()
    }
  },
  url_for: function(url, options){
    options = $H(options)
		return url.replace(/action/, options.action)
	},
	query_string: function(){
		return window.location.search.substring(1)
	}
}
// ==========================
// TABBED FORM
// ==========================
var LForm = {
  active_fieldset: function(element, link_object){
    element = $(element)
    form_tag = document.getAncestorsByTagName("form", element)[0]
    $A(document.getElementsByClassName("slot", form_tag)).apply(Element.hide)
    Element.show(element)
    link_object = $(link_object)
    if( link_object != undefined){
      ul = document.getAncestorsByTagName("ul", link_object)[0]
      lis = ul.getElementsByTagName("li")
      for (var i=0; i<lis.length; i++){
        lis[i].className="item"
      }
      link_object.parentNode.className="current"
    }
  }
}
