// Space_Delimited -------------------------------------- START

function SDString(SDstr)
{
  this.value = SDstr;
  this.trim = function () 
  {
    while (this.value.indexOf("  ")>-1)
    {
      this.value = this.value.replace("  ",' ');
    }
    if (this.value[0]==' ') 
      this.value = this.value.SubString(1,this.value.length);
    if (this.value[this.value.length-1]==' ') 
      this.value = this.value.SubString(0,this.value.length-1);
  };
  this.add = function (a) 
  {
    if(this.value[this.value.length-1]!=' ')
      this.value += ' ';
    this.value += a;
    this.trim();
  };
  this.del = function (d) 
  {
  this.trim();
    if (this.value.indexOf(d)!=0)
    {
      this.value = this.value.replace((' ' + d),'');
      this.trim();
    }
    else if (this.value != d)
    {
      this.value = this.value.replace((this.value + ' '),'');
      this.trim();
    }
    else this.value = null;
  };
} //end SDString

//end Space_Delimited



// Class_Management ------------------------------------- START

function appendClass(adC_obj,adC_val)
{
  if (adC_obj.getAttribute(ui.classRef))
  {
    var adC_currClass = new SDString(adC_obj.getAttribute(ui.classRef));
    adC_currClass.Add(adC_val);
    adC_obj.setAttribute(ui.classRef,adC_currClass.value);
  }
  else adC_obj.setAttribute(ui.classRef,adC_val);
  return null;
} //end appendClass

function removeClass(rmC_obj,rmC_val)
{
  if (rmC_obj.getAttribute(ui.classRef)) //exists
  {
    if (rmC_obj.getAttribute(ui.classRef)==rmC_val)  //matches
      rmC_obj.removeAttribute(ui.classRef);
    else if (rmC_obj.getAttribute(ui.classRef).indexOf(rmC_val)>-1) //contains
    {
      var rmC_currClass = new SDString(rmC_obj.getAttribute(ui.classRef));
      rmC_currClass.del(rmC_val);
      if (rmC_currClass.value==null)
        rmC_obj.removeAttribute(ui.classRef);
      else
        rmC_obj.setAttribute(ui.classRef,rmC_currClass.value);
    }
  } //end if
  return null;
} //end removeClass

function replaceClass(rpC_obj,rpC_val)
{
  rpC_obj.setAttribute(ui.classRef,rpC_val);
  return null;
} //end replaceClass

// end Class_Management



// Event_Management ------------------------------------- START
// Add/Remove Events
// Event Disposal

function defineBehaviour(dB_verb,dB_obj,dB_func) //event registration
{
  if (dB_func!=null && dB_obj!=null && dB_verb!=null)
  {
    if (ui.ie)
    {
      dB_verb = "on" + dB_verb;
      dB_obj.attachEvent(dB_verb,dB_func);
    }
    else dB_obj.addEventListener(dB_verb,dB_func,false);
  }
  return null;
} //end defineBehaviour


function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);


// Menu_Management -------------------------------------- START




function menu_set(obj)
{
  //TAGS: {UL, LI, A, SPAN}
  //classes {keepOpen,slideOut,dropDown,branchMenu}
  
  if (obj.hasChildNodes)
  {
    for (nc=0; nc<obj.childNodes.length; nc++)
    {
      if (obj.childNodes[nc].nodeName == "LI")
      {
        if (obj.childNodes[nc].hasChildNodes)
        {
          obj.childNodes[nc] 
        }
      }
    }
  }
  return null;
} //end setMenu


var menuShow;

function Menu_open(Mo_obj)
{
  appendClass(Mo_obj,"show");
}

function Menu_close(Mc_obj)
{
  removeClass(Mc_obj,"show");
}

var lastID = 0;
function newID()
{
  lastID++;
  return "GUID" + (lastID-1);
}

function MenuItem(MI_obj)
{
  this.Show = function () { appendClass(MI_obj,"Show");}
}
var wert = 0;

var Menu = {
  cursor : null,
  isOpen : false,
  KeepOpen : 1600,
  Timer : null,
  Current : function(newM) {
    if (Menu.cursor == newM)
    {
      if (Menu.Timer != null)
      {
        clearTimeout(Menu.Timer);
        Menu.Timer = null;
      }
    }
    else if (Menu.isOpen) Menu.Shut(Menu.cursor);
    Menu.cursor = newM;
    },
  Display : function () {
    Menu.Current(this.id);
    Menu.Show();
    },
  Show : function () { 
    if (!Menu.isOpen)
    {
      Menu_open(document.getElementById(Menu.cursor));
      Menu.isOpen = !Menu.isOpen;
    }
    },
  SoftShut : function() {
    Menu.Timer = setTimeout("Menu.Shut(Menu.cursor)",Menu.KeepOpen); 
    },
  Shut : function(oldM) {
    clearTimeout(Menu.Timer);
    Menu.Timer = null;
    if (Menu.isOpen)
    {
      Menu.isOpen=!Menu.isOpen;
      Menu.cursor = null;  
    }  
    Menu_close(document.getElementById(oldM)); 
    }  
  }


function Menu_makeSelectable(Mms_obj)
{
  var Mms_temp;
  if(Mms_obj.childNodes[(Mms_obj.childNodes.length-1)].nodeName == "UL")
  {
    if (Mms_obj.childNodes[(Mms_obj.childNodes.length-2)].nodeName == "A")
    {
      Mms_temp = document.createAttribute('id');
      Mms_temp.value = newID();
      Mms_obj.setAttributeNode(Mms_temp);
      addEvent(document.getElementById(Mms_obj.id),"mouseover", Menu.Display);
      addEvent(document.getElementById(Mms_obj.id),"mouseout", Menu.SoftShut);
    } //end if
  } //end for    
} //end makeMenuItem

function Menu_displayNested(obj)
{
  if (obj.hasChildNodes)
  {
    for (nc=0; nc<obj.childNodes.length; nc++)
    {
      if (obj.childNodes[nc].nodeName == "LI" && obj.childNodes[nc].hasChildNodes)
      {
        if (obj.childNodes[nc].getAttribute(ui.classRef))
        {
          if (obj.childNodes[nc].getAttribute(ui.classRef) != "selected") Menu_makeSelectable(obj.childNodes[nc]);
        }
        else
        {
          Menu_makeSelectable(obj.childNodes[nc]);
        }
      }
    } //end for
  } //end if
  return null;
} //end setShowMenu



function menu_init()
{

  Menu_displayNested(document.getElementById("menu"));
  return null;
} //end setMenu

function menu_exit()
{
//  alert("garbage collection");
}



// end Menu_Management


// user_info -------------------------------------------- START
// browser detect quirksmode.org  -- object ui should hold a set booleans and constants
var ui = { 
  init: function () { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "an unknown OS"; if (this.browser=="Explorer") {this.ie = true; this.classRef="className";} else {this.ie = false; this.classRef="class";} },
  searchString: function (data) { for (var i=0;i<data.length;i++) { var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if (dataString) { if (dataString.indexOf(data[i].subString) != -1) return data[i].identity; } else if (dataProp) return data[i].identity; } },
  searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == -1) return; return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); },
  dataBrowser: [ {string: navigator.userAgent, subString: "Chrome", identity: "Chrome" }, {string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" }, {string: navigator.vendor, subString: "Apple", identity: "Safari", versionSearch: "Version" }, {prop: window.opera, identity: "Opera" }, {string: navigator.vendor, subString: "iCab", identity: "iCab" }, {string: navigator.vendor, subString: "KDE", identity: "Konqueror" }, {string: navigator.userAgent, subString: "Firefox", identity: "Firefox" }, {string: navigator.vendor, subString: "Camino", identity: "Camino" }, {string: navigator.userAgent, subString: "Netscape", identity: "Netscape" }, {string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE" }, {string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" }, {string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla" } ],
  dataOS : [ {string: navigator.platform, subString: "Win", identity: "Windows" }, {string: navigator.platform, subString: "Mac", identity: "Mac" }, {string: navigator.userAgent,subString: "iPhone", identity: "iPhone/iPod"}, {string: navigator.platform,subString: "Linux",identity: "Linux"}]
};
 
function user_init() { ui.init(); return null; } //end user_init 

// end user_info


// page_init -------------------------------------------- START

function init()
{
  user_init();
  menu_init();
  //  if (document.getElementById("calculatorForm")) setCalculator();
} // end init


function exit()
{
  menu_exit();
} //end exit


// end page_init

// page_load -------------------------------------------- START

window.onload = init;
window.onunload = exit;

// end page_load




