var Class = {
  create: function() {
  	return function() {
  		if(this.destroy) Class.registerForDestruction(this);
      if(this.initialize) this.initialize.apply(this, arguments);
    }
  },
  
  extend: function(baseClassName) {
  	constructor = function() {
  		var i;
  		
			/*		
			var tmp = this.initialize;
			this.initialize = window[baseClassName].initialize;
  		window[baseClassName].apply(this, arguments);
  		this.initialize = tmp;
  		*/
  		this[baseClassName] = {}
  		for(i in window[baseClassName].prototype) {
  			if(!this[i]) this[i] = window[baseClassName].prototype[i];
  			if(typeof window[baseClassName].prototype[i] == 'function') {
  				this[baseClassName][i] = window[baseClassName].prototype[i].bind(this);
  			}
  		}
  		
  		if(window[baseClassName].getInheritedStuff) {
  			window[baseClassName].getInheritedStuff.apply(this);
  		}
  		
  		if(this.destroy) Class.registerForDestruction(this);
      if(this.initialize) this.initialize.apply(this, arguments);
  	}
  	constructor.getInheritedStuff = function() {
  		this[baseClassName] = {}
  		for(i in window[baseClassName].prototype) {
  			if(!this[i]) this[i] = window[baseClassName].prototype[i];
  			if(typeof window[baseClassName].prototype[i] == 'function') {
  				this[baseClassName][i] = window[baseClassName].prototype[i].bind(this);
  			}
  		}

  		if(window[baseClassName].getInheritedStuff) {
  			window[baseClassName].getInheritedStuff.apply(this);
  		}
  	}
  	
  	return constructor;
  	
  },
  
  objectsToDestroy : [],  
  registerForDestruction: function(obj) {
  	if(!Class.addedDestructionLoader) {
			Event.observe(window, 'unload', Class.destroyAllObjects);
  		Class.addedDestructionLoader = true;
  	}
  	Class.objectsToDestroy.push(obj);
  },
  
  destroyAllObjects: function() {
  	var i,item;
  	for(i=0;item=Class.objectsToDestroy[i];i++) {
  		if(item.destroy) item.destroy();
  	}
  	Class.objectsToDestroy = null;
  }  
}

/**
 * Extend function used in multiple inheritance
 */
Function.prototype.extend = function(baseClassName) {
	var parentFunc = this;
	
	var constructor = function() {
		this[baseClassName] = {}
		for(var i in window[baseClassName].prototype) {
			if(!this[i]) this[i] = window[baseClassName].prototype[i];
			this[baseClassName][i] = window[baseClassName].prototype[i].bind(this);
		}

		if(window[baseClassName].getInheritedStuff) {
			window[baseClassName].getInheritedStuff.apply(this);
		}
		if(parentFunc.getInheritedStuff) {
			parentFunc.getInheritedStuff.apply(this);
		}

		parentFunc.apply(this, arguments);
	}

	constructor.getInheritedStuff = function() {
		this[baseClassName] = {}
		for(i in window[baseClassName].prototype) {
			if(!this[i]) this[i] = window[baseClassName].prototype[i];
			this[baseClassName][i] = window[baseClassName].prototype[i].bind(this);
		}

		if(window[baseClassName].getInheritedStuff) {
			window[baseClassName].getInheritedStuff.apply(this);
		}
		if(parentFunc.getInheritedStuff) {
			parentFunc.getInheritedStuff.apply(this);
		}
	}
	
	return constructor;
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    return __method.call(object, event || window.event);
  }
}
Function.prototype.applyTo = function(cssSelector, arg1, arg2, arg3, arg4, arg5, arg6) {
	if(typeof cssSelector == 'string') {
		var registration = {}
		var targetClass = this;
		
		registration[cssSelector] = {
			initialise: function() {
				behaveAs(this, targetClass, arg1, arg2, arg3, arg4, arg5, arg6);
			}
		}
		
		if(Behaviour.alreadyApplied) {
			Behaviour.process(registration);
		} else {
			Behaviour.register(registration);
		}
	
	} else {
		behaveAs(cssSelector, this);		
	}
}

var _APPLYTOCHILDREN_GENERATED_IDS = 0;
Function.prototype.applyToChildren = function(parentNode, cssSelector, arg1, arg2, arg3, arg4, arg5, arg6) {
	if(!parentNode.id) {
		_APPLYTOCHILDREN_GENERATED_IDS++;
		parentNode.id = 'atc-gen-id-' + _APPLYTOCHILDREN_GENERATED_IDS;
	}
	this.applyTo('#' + parentNode.id + ' ' + cssSelector);
}


if(typeof Behaviour == 'undefined') {
var Behaviour = {
	isEventHandler : { onclick : true, onfocus : true, onblur : true, onmousedown : true, onmouseup : true, onmouseover: true, onmouseout: true, onclick : true },
	
	list : new Array,

	namedList : {},
	isDebugging : false,
	
	register : function(name, sheet){
		if(typeof name == 'object') {
			Behaviour.list.push(name);
		} else {
			Behaviour.list.push(sheet);
			Behaviour.namedList[name] = sheet;
		}
	},
	
	start : function(){
		Behaviour.addLoader(function() {Behaviour.apply();});
	},
	
	debug : function() {
		Behaviour.isDebugging = true;
	},
	
	apply : function(parentNode, applyToParent){
		if(Behaviour.isDebugging) console.time('Behaviour: apply took');
		
		if(typeof parentNode == 'string') parentNode = document.getElementById(parentNode);
		var h;
		for (h=0;sheet=Behaviour.list[h];h++){
			//console.log(parentNode);
			
			Behaviour.process(sheet, parentNode, applyToParent);
		}
		
		if(Behaviour.isDebugging) console.timeEnd('Behaviour: apply took');

		Behaviour.alreadyApplied = true;
	},
	
	reapply : function(name) {
		if(Behaviour.namedList[name]) Behaviour.process(Behaviour.namedList[name]);
	},
	
	process : function(sheet, parentNode, applyToParent) {
		var i;
		var debugText = "";
		for (selector in sheet){
			if(!sheet[selector]) continue;
			if(Behaviour.isDebugging) console.time('Behaviour: ' + selector);
			list = document.getElementsBySelector(selector, parentNode);

			if (list && list.length > 0) {
				if(Behaviour.isDebugging) console.log("Behaviour: %s: %d items, %o", selector, list.length, list);
			
				for (i=0;element=list[i];i++){
					if(parentNode == element && applyToParent != true) continue;

					// lastSelectorApplied is a duplicate checker.  getElementsBySelector sometimes returns duplicates
					if(element.lastSelectorApplied != sheet[selector]) {
						element.lastSelectorApplied = sheet[selector];
						if(sheet[selector].prototype) {
							behaveAs(element, sheet[selector]);
						} else {
							var x;
							for(x in sheet[selector]) {
								if(element[x] && !element['old_' + x]) element['old_' + x] = element[x];
							
								if(sheet[selector][x]) {
									if(Behaviour.isEventHandler[x]) {
										element[x] = sheet[selector][x].bindAsEventListener(element);
										// Event.observe(element, x.substr(2), sheet[selector][x]);
									} else {
										element[x] = sheet[selector][x];
									}
								}
							}
							// Two diferent ways of spelling initialize depending on your version of the English language
				            if(sheet[selector].initialise) {
				            	element.initialise();
				            } else if(sheet[selector].initialize) {
				            	element.initialize();
				            }
				
							// Sometimes applyToChildren classes cause sheet[selector] to die in initialise().  Why?
							if(typeof sheet[selector] == 'undefined') break;
							
				        	if(sheet[selector].destroy) Class.registerForDestruction(element);
						}
					}
				}
			}
			
			if(Behaviour.isDebugging) console.timeEnd('Behaviour: ' + selector);
		}
	},
	
	/**
	 * Add a window.onload function.
	 */
	addLoader : function(func){
		Behaviour.addEvent(window,'load', func);
	},
	
	/**
	 * Attach an event listener to the given object
	 */
	addEvent: function(obj, evType, fn, useCapture){
		if (obj.addEventListener){
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} else if (obj.attachEvent){
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		} else {
			alert("Handler could not be attached");
		}
	}
}

Behaviour.start();
}

/*
 * Force elemnt to "behave like" the given class
 * The constructor will be called an all of the methods attached
 * Think of it as dynamic multiple inheritance... welcome to the messed up
 * yet delightful world of JavaScript
 */
function behaveAs(element, behaviourClass, arg1, arg2, arg3, arg4, arg5, arg6) {
	if(!element) return;
	
	// You can get into icky situations if behaveAs is called twice - the first class passed *has* initialize,
	// and the 2nd class passed *doesn't have it*.  The first initialize is called twice, without this delete.
	element.initialize = null;

	var x;
	for(x in behaviourClass.prototype) {
		element[x] = behaviourClass.prototype[x];
		if(x == 'onclick' && element[x]) {
			element[x] = element[x].bindAsEventListener(element);
		}
	}
	
	behaviourClass.apply(element, [arg1, arg2, arg3, arg4, arg5, arg6]);

	return element;
}

/*
   The following code is Copyright (C) Simon Willison 2004.

   document.getElementsBySelector(selector)
   - returns an array of element objects from the current document
     matching the CSS selector. Selectors can contain element names, 
     class names and ids and can be nested. For example:
     
       elements = document.getElementsBySelect('div#main p a.external')
     
     Will return an array of all 'a' elements with 'external' in their 
     class attribute that are contained inside 'p' elements that are 
     contained inside the 'div' element which has id="main"

   New in version 0.4: Support for CSS2 and CSS3 attribute selectors:
   See http://www.w3.org/TR/css3-selectors/#attribute-selectors

   Version 0.4 - Simon Willison, March 25th 2003
   -- Works in Phoenix 0.5, Mozilla 1.3, Opera 7, Internet Explorer 6, Internet Explorer 5 on Windows
   -- Opera 7 fails 
   
   
   ***NOTE***: This function will sometimes return duplicates.  Sam decided that rather than slow
   down the code with uniqueness checks, it was up to the code that uses this to do so.
*/

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector, parentNode) {
  // Attempt to fail gracefully in lesser browsers
  if (!document.getElementsByTagName) {
    return new Array();
  }
  // Split selector in to tokens
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    
    if (token.indexOf('#') > -1) {
      // Token is an ID selector
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (!element || (tagName && element.nodeName.toLowerCase() != tagName)) {
        // tag with that ID not found, return false
        return new Array();
      }

			// Parent node limitation      
      if(parentNode && !hasAncestor(element, parentNode) && !hasAncestor(parentNode, element)) {
        return new Array();
       }


      // Set currentContext to contain just this element
      currentContext = new Array(element);
      continue; // Skip to next token
    }
    
    if (token.indexOf('.') > -1) {
      // Token contains a class selector
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      
      if (!tagName) {
        tagName = '*';
      }
      // Get elements matching tag, filter them for class selector
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if(currentContext[h]) {
	        if (tagName == '*') {
	            elements = getAllChildren(currentContext[h]);
	        } else {
	            elements = currentContext[h].getElementsByTagName(tagName);
	        }
	        for (var j = 0; j < elements.length; j++) found[foundCount++] = elements[j];
	      }
      }
      currentContext = new Array;
      var currentContextIndex = 0;

      // Single class
      if(bits.length == 2) {
	      for (var k = 0; k < found.length; k++) {
	        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
						// Parent node limitation      
			      if(!parentNode || hasAncestor(found[k], parentNode) || hasAncestor(parentNode, found[k])) {
		          currentContext[currentContextIndex++] = found[k];
						}
	        }
	      }
	      
	    // Multiple classes
	    } else {
	      var classNameMatcher = function(el) {
	      	var i;
	      	if(!el.className) return false;
	      	for(i=1;i<bits.length;i++) if(!el.className.match(new RegExp('\\b'+bits[i]+'\\b'))) return false;
	      	return true;
	      }
	      for (var k = 0; k < found.length; k++) {
	        if (classNameMatcher(found[k])) {
						// Parent node limitation      
			      if(!parentNode || hasAncestor(found[k], parentNode) || hasAncestor(parentNode, found[k])) {
		          currentContext[currentContextIndex++] = found[k];
						}
	        }
	      }
	    	
	    }

      continue; // Skip to next token
    }
    
    // Code to deal with attribute selectors
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      // Grab all of the tagName elements within current context
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
      	if(currentContext[h]){
	        var elements;
	        if (tagName == '*') {
	            elements = getAllChildren(currentContext[h]);
	        } else {
	            elements = currentContext[h].getElementsByTagName(tagName);
	        }
	        for (var j = 0; j < elements.length; j++) {
						// Parent node limitation      
			      if(!parentNode || hasAncestor(elements[j], parentNode) || hasAncestor(parentNode, elements[j])) {
		          found[foundCount++] = elements[j];
						}
	        }
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction; // This function will be used to filter the elements

      switch (attrOperator) {
        case '=': // Equality
          checkFunction = function(candAttrValue) { return (candAttrValue == attrValue); };
          break;
        case '~': // Match one of space seperated words 
          checkFunction = function(candAttrValue) { return (candAttrValue.match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;
        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(candAttrValue) { return (candAttrValue.match(new RegExp('^'+attrValue+'-?'))); };
          break;
        case '^': // Match starts with value
          checkFunction = function(candAttrValue) { return (candAttrValue.indexOf(attrValue) == 0); };
          break;
        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(candAttrValue) { return (candAttrValue.lastIndexOf(attrValue) == candAttrValue.length - attrValue.length); };
          break;
        case '*': // Match ends with value
          checkFunction = function(candAttrValue) { return (candAttrValue.indexOf(attrValue) > -1); };
          break;
        default :
          // Just test for existence of attribute
          checkFunction = function(candAttrValue) { return candAttrValue; };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
				// Class needs special handling      
	      var candAttrValue = attrName == 'class' ? found[k].className : found[k].getAttribute(attrName);
        if (checkFunction(candAttrValue)) {
		      if(!parentNode || hasAncestor(found[k], parentNode) || hasAncestor(parentNode, found[k])) {
		         currentContext[currentContextIndex++] = found[k];
					}
        }
      }
      // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
      continue; // Skip to next token
    }
    
    if (!currentContext[0]){
    	return;
    }
    
    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
				// Parent node limitation      
	      if(!parentNode || hasAncestor(elements[j], parentNode) || hasAncestor(parentNode, elements[j])) {
	        found[foundCount++] = elements[j];
	      }
      }
    }
    currentContext = found;
  }
  
  if(parentNode) {
  	var i;
  	for(i=0;i<currentContext.length;i++) {
  		if(!hasAncestor(currentContext[i], parentNode)) currentContext.splice( i, 1 );
  	}
  }
  
  return currentContext;
}

function hasAncestor(child, ancestor) {
	if(ancestor) {
		if(ancestor.contains) return ancestor == child || ancestor.contains(child);
		
		var p = child;
		while(p) {
			if(p == ancestor) return true;
			p = p.parentNode;
		}
	}
	
	return false;	
}

// TODO we should use prototypes built-in $()-function,
// which returns Prototype-compatible arrays
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}


/* That revolting regular expression explained 
/^(\w+)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/
  \---/  \---/\-------------/    \-------/
    |      |         |               |
    |      |         |           The value
    |      |    ~,|,^,$,* or =
    |   Attribute 
   Tag
*/

/**
 * Simple observer pattern
 * 
 * Call $('sitetree').observe('SelectionChanged', this)
 *   -> this.onSelectionChanged(newNode) will be called whenever the selection changes
 * Call $('sitetree').observeMethod('SelectionChanged', this.updateDropdown.bind(this))
 *   -> this.updateDropdown(newNode) will be called whenever the selection changes
 * Call $('sitetree').notify('SelectionChanged', newNode)
 *   -> The SelectionChanged event will be sent to all observers
 */
Observable = Class.create();
Observable.prototype = {
	observe : function(event, observer) {
		return this.observeMethod(event, observer['on' + Event].bind(observer));
	},
	observeMethod : function(event, method) {
		if(!this.observers) this.observers = {};
		if(!this.observers[event]) this.observers[event] = [];
		
		var nextIdx = this.observers[event].length;
		this.observers[event][nextIdx] = method;
		return event + '|' + nextIdx;
	},
	stopObserving : function(observerCode) {
		var parts = observerCode.split('|');
		if(this.observers && this.observers[parts[0]] && this.observers[parts[0]][parts[1]])
			this.observers[parts[0]][parts[1]] = null;
		else
			throw("Observeable.stopObserving: couldn't find '" + observerCode + "'");
	},
	notify : function(event, arg) {
		var i, returnVal = true;
		if(this.observers && this.observers[event]) {
			for(i=0;i<this.observers[event].length;i++) {
				if(this.observers[event][i]) {
					if(this.observers[event][i](arg) == false) returnVal = false;
				}
			}
		}
		return returnVal;
	}
};

if(window.location.href.indexOf('debug_behaviour=') > -1) Behaviour.debug();

/**
 * Additions and improvements to Prototype-code.
 * Some if this is legacy code which is now present in Prototype as well,
 * but has to be kept for older scripts.
 * 
 * @author Silverstripe Ltd., http://www.silverstripe.com
 */

// Shortcut-function (until we update to Prototye v1.5)
if(typeof $$ != "Function") {
	$$ = document.getElementsBySelector;
}

var SS_DEFAULT_ISO = "en_GB";

Object.extend(Element, {
  setStyle: function(element, styles, camelized) {
    element = $(element);
    var elementStyle = element.style;

    for (var property in styles)
      if (property == 'opacity') element.setOpacity(styles[property])
      else
        elementStyle[(property == 'float' || property == 'cssFloat') ?
          (elementStyle.styleFloat === undefined ? 'cssFloat' : 'styleFloat') :
          (camelized ? property : property.camelize())] = styles[property];

    return element;
  }
});

// This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
function sprintf()
{
	if (!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				//alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			       if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			  else if (pPad) pad = pPad;
			var justifyRight = true;
			       if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
			       if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
			       if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
			       if (pType == 'b') subst = parseInt(param).toString(2);
			  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			  else if (pType == 'u') subst = Math.abs(param);
			  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			  else if (pType == 'o') subst = parseInt(param).toString(8);
			  else if (pType == 's') subst = param;
			  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

/**
 * Use Firebug-like debugging in non-Firefox-browsers
 * @see http://wish.hu/firebug-on-explorer
 */
if (!window.console) {
  window.console = {
    timers: {},
    openwin: function() {
      window.top.debugWindow =
          window.open("",
                      "Debug",
                      "left=0,top=0,width=300,height=700,scrollbars=yes,"
                      +"status=yes,resizable=yes");
      window.top.debugWindow.opener = self;
      window.top.debugWindow.document.open();
      window.top.debugWindow.document.write('<html><head><title>debug window</title></head><body><hr><pre>');
    },

	/**
	 * Caution: Excludes functions in listing
	 */
    log: function() {
		if(Debug.isLive()) return;
	
		if(!window.top.debugWindow) { 
			console.openwin(); 
		}
		
		var i = 0; content = "";
		if(arguments.length == 1 && typeof arguments[0] != "object") {
			content = arguments[0];
		} else if(arguments.length > 1 && typeof arguments[0] == "string"){
			content = sprintf(arguments[0], Array.prototype.slice.call(arguments, 1));
		}
		
		if(window.top.debugWindow.document) { 
			window.top.debugWindow.document.write(content+"\n");
		}
    },
    
    debug: this.log,

    time: function(title) {
      window.console.timers[title] = new Date().getTime();
    },

    timeEnd: function(title) {
      var time = new Date().getTime() - window.console.timers[title];
      console.log(['<strong>', title, '</strong>: ', time, 'ms'].join(''));
    }

  }
}

Number.prototype.CURRENCIES = {
	en_GB: '$ ###,###.##'
};

/**
 * Caution: Not finished!
 * @param iso string (Not used) Please use in combination with Number.CURRENCIES to achieve i18n
 * @return string
 * 
 * @see http://www.jibbering.com/faq/faq_notes/type_convert.html
 * @see http://www.rgagnon.com/jsdetails/js-0063.html
 * @see http://www.mredkj.com/javascript/nfdocs.html 
 */
Number.prototype.toCurrency = function(iso) {
	if(!iso) iso = SS_DEFAULT_ISO;
	// TODO stub, please implement properly
	return "$" + this.toFixed(2);
}

/**
 * Get first letter as uppercase
 */
String.prototype.ucfirst = function () {
   var firstLetter = this.substr(0,1).toUpperCase()
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}

/**
 * Show debug-information in the javascript-console or a popup.
 * Only shows output on dev- or test-environments.
 * Caution: Behaves like a static class (no prototype methods)
 */
Debug = Class.create();
Debug = {
	
	environment_type: "live",
	
	initialize: function() {
		if(window.location.href.match(/\?(.*)debug_javascript/)) {
			this.environment_type = "dev";
		}
		if(window.location.href.match(/\?(.*)debug_behaviour/)) {
			Behaviour.debug();
		}
	},
	
	/**
	 * @param type string "live", "test" or "dev"
	 */
	set_environment_type: function(type) {
		this.environment_type = type;
	},
	
	isDev: function() {
		return (window.location.href.match(/test\.|dev\./) || this.environment_type == "dev" || this.environment_type == "test");
	},
	
	isTest: function() {
		return (window.location.href.match(/test\./) || this.environment_type == "test");
	},

	isLive: function() {
		return !Debug.isDev();
	},

	show: function() {
		if(this.isDev() || this.isTest()) {
			console.debug.apply(console, arguments);
		}
	},

	debug: this.debug,

	log: function() {
		if(this.isDev() || this.isTest()) {
			console.log.apply(console, arguments);
		}
	}
}
Debug.initialize();

// Flash plugin version detection from SWFObject
// http://blog.deconcept.com/swfobject/
getFlashPlayerVersion = function(){
	var pv = new PlayerVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			pv = new PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}else if (navigator.userAgent && navigator.userAgent.indexOf("Windows CE") >= 0){ // if Windows CE
		var axo = 1;
		var counter = 3;
		while(axo) {
			try {
				counter++;
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+ counter);
//				document.write("player v: "+ counter);
				pv = new PlayerVersion([counter,0,0]);
			} catch (e) {
				axo = null;
			}
		}
	} else { // Win IE (non mobile)
		// do minor version lookup in IE, but avoid fp6 crashing issues
		// see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
		try{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		}catch(e){
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				pv = new PlayerVersion([6,0,21]);
				axo.AllowScriptAccess = "always"; // error if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
			} catch(e) {
				if (pv.major == 6) {
					return pv;
				}
			}
			try {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} catch(e) {}
		}
		if (axo != null) {
			pv = new PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
		}
	}
	return pv;
}

PlayerVersion = function(arrVersion) {
	this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
	this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
	this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
}

/*
 * common/js/loader.js
 * Common file, to be included before any of the other common/js scripts
 */
 
/*
 * Gracefully deal with a whole lot of modules wanting to set the onload() event handler
 */
if(typeof _LOADERS == 'undefined') _LOADERS = Array();

function callAllLoaders() {
	var i, loaderFunc;
	for(i=0;i<_LOADERS.length;i++) {
		loaderFunc = _LOADERS[i];
		if(loaderFunc != callAllLoaders) loaderFunc();
	}
}

function appendLoader(loaderFunc) {
	if(window.onload && window.onload != callAllLoaders)
		_LOADERS[_LOADERS.length] = window.onload;

	window.onload = callAllLoaders;

	_LOADERS[_LOADERS.length] = loaderFunc;
}


/*
 * Call the given function on any element of the given tag and class
 */
function setUpHandlers(tagName,className, handlerFunction) {	
	var allElements = document.getElementsByTagName(tagName);
	for(var i = 0;i<allElements.length;i++) {
		if(allElements[i].className) {
			tester = ' ' + allElements[i].className + ' ';
			if(tester.indexOf(' ' + className + ' ') != -1) {
				handlerFunction(allElements[i]);
			}
		}
	}
}

/*
 * Return an array of all elements
 */
function getAllElements() {
	var allElements = document.getElementsByTagName('*');
	if(allElements.length == 0) return document.all;
	else return allElements;
}	

/*
 * Functions to add and remove class names
 * Mac IE hates unnecessary spaces
 */
function addClass(el, cls, forceBefore) {
	if(!el.className) el.className = "";
	if(forceBefore != null && el.className.match(new RegExp('(^| )' + forceBefore))) {
		el.className = el.className.replace(new RegExp("( |^)" + forceBefore), '$1' + cls + ' ' + forceBefore);

	} else if(!el.className.match(new RegExp('(^| )' + cls + '($| )'))) {
		el.className += ' ' + cls;
		el.className = el.className.replace(/(^ +)|( +$)/g, '');
	}
}
function removeClass(el, cls) {
	var old = el.className;
	var newCls = ' ' + el.className + ' ';
	newCls = newCls.replace(new RegExp(' (' + cls + ' +)+','g'), ' ');
	el.className = newCls.replace(/(^ +)|( +$)/g, '');
}
function removeClasses(el, cls) {
	removeClass(el,cls);
	var items = el.getElementsByTagName('*');
	var i;
	for(i=0;i<items.length;i++) if(items[i].className) removeClass(items[i],cls);
} 

/*
 * Add an event handler, saving the existing one
 */
function addEventHandler(obj, evt, handler) {
	if(obj[evt]) obj['old_' + evt] = obj[evt];
	obj[evt] = handler;
}

function hover_over() {
	Element.addClassName(this, 'over');
}
function hover_out() {
	Element.removeClassName(this, 'over');
}

hover_behaviour = {
	onmouseover : hover_over,
	onmouseout : hover_out
}

/*
 * Stretch an object out to fill its parent.  Give space for siblings
 * If Mozilla didn't have a funky bug, we could *maybe* do this without JavaScript
 * 
 * A more robust stretchObject() replacement
 */
function fitToParent(el, tweakVal) {
	if(typeof el == "string") {
		var elName = el;
		el = document.getElementById(el);
		if(!el) /*throw("fitToParent: Can't find element '" + elName + "'")*/ return;
	}
	var height = getFittingHeight(el, tweakVal) + 'px';
	el.style.height = height;
}
	
function getFittingHeight(el, tweakVal, ignoreElements) {
	if(typeof el == "string") el = document.getElementById(el);

	// we set overflow = hidden so that large children don't muck things up
	if(el.parentNode && el.parentNode != document.body) {
		if(getDimension(el.parentNode,'overflow',true) != 'auto') el.parentNode.style.overflow = 'hidden';
	}
	
	var otherHeight = 0;
	
	var notAComment;
	if(el.parentNode) {
		for(var i=0;i<el.parentNode.childNodes.length;i++) {
			var sibling = el.parentNode.childNodes[i];
			
			if(sibling.tagName && el != sibling) {
				if(sibling.outerHTML == null) notAComment = true;
				else if(sibling.outerHTML.indexOf("<!--") != 0) notAComment = true;
				else notAComment = false;
				
				// notAComment used for other checking
				if(sibling.className && sibling.className.indexOf('fitToParent_ignore') != -1) notAComment = false;
	
				if(getDimension(sibling,'position',true) == 'absolute') notAComment = false;
				else if(getDimension(sibling,'display',true) == 'none') notAComment = false;
				else {
					var floatVal = getDimension(sibling,'float',true);
					if(floatVal == 'left' || floatVal == 'right') notAComment = false;
				}
				if(ignoreElements) {
					for(var j=0;j<ignoreElements.length;j++) {
						if(ignoreElements[j] == sibling) {
							notAComment = false;
							break;
						}
					}
				}
				/*
				if(el.id == 'Form_EditForm') {
				var x,y="";
				for(x in sibling.style) y += ", " + x + "=" + sibling.style[x];
				alert(y);
				}
				*/
				if(sibling.offsetHeight && notAComment && (sibling.tagName.toLowerCase() != 'script')) {
					// alert('adding height ' + sibling.tagName + '#' + sibling.id +  '.' + sibling.className);
					otherHeight += parseInt(sibling.offsetHeight);
				}
				
				// Hack for height of menu bar at top
				if(sibling.id == 'top') {
					otherHeight += 27;
				}
			}
		}
	}
	
	if(getDimension(el,'position',true) == 'relative') otherHeight += getDimension(el,'top');
	
	if(!tweakVal) tweakVal = 0;
	
//	if(el.clientHeight) tweakVal += el.offsetHeight - el.clientHeight;

	tweakVal += getDimension(el,'marginTop');
	tweakVal += getDimension(el,'marginBottom');
	tweakVal += getDimension(el,'paddingTop');
	tweakVal += getDimension(el,'paddingBottom');

	tweakVal += getDimension(el,'borderBottomWidth');
	tweakVal += getDimension(el,'borderTopWidth');

	//alert(getDimension(el.parentNode,'paddingTop', true));
	tweakVal += getDimension(el.parentNode,'paddingTop');
	tweakVal += getDimension(el.parentNode,'paddingBottom');
	// Body border fucks up in IE
	if(el.parentNode && el.parentNode.tagName != "BODY") {
		tweakVal += getDimension(el.parentNode,'borderTopWidth');
		tweakVal += getDimension(el.parentNode,'borderBottomWidth');
	}
	/*
	if(el.className == 'tabset') {
		el.parentNode.style.border = "1px blue solid";
		for(var i=0;i<el.parentNode.childNodes.length;i++) {
			var sibling = el.parentNode.childNodes[i];
			if(sibling.tagName)
				sibling.style.border = (el == sibling) ? "1px green solid" : "1px orange solid";
		}
				
		alert(el.id + ': ' + el.parentNode.offsetHeight + ', ' + tweakVal + ', ' + otherHeight 
			+ '\n' + getSiblingList(el) 
			+ '\n' + getDimension(el,'position',true) + ', ' + getDimension(el,'top')
		);
	
		el.parentNode.style.border = null;
		for(var i=0;i<el.parentNode.childNodes.length;i++) {
			var sibling = el.parentNode.childNodes[i];
			if(sibling.tagName)
				sibling.style.border = null;
		}
	}
	*/

	// alert(el.id + ' = ' + el.parentNode.offsetHeight + ', ' + tweakVal + ', ' + otherHeight);
	
	if(el.parentNode && el.parentNode.offsetHeight - otherHeight - tweakVal < 0) {
		return 0;
	}else{
		if(el.parentNode) {
			return (el.parentNode.offsetHeight - otherHeight - tweakVal);		
		}
	}
}



function getSiblingList(el) {
	var i,silbing,result=[];
	for(i=0;i<el.parentNode.childNodes.length;i++) {
		sibling = el.parentNode.childNodes[i];
		if(sibling.tagName) result.push(
			sibling.tagName
			+ (sibling.id ? ('#' + sibling.id) : '')
			+ (sibling.className ? ('.' + sibling.className) : '')
		);
	}
	return result.join(", ");
}

/*
 * Returns the value of the given dimension - marginBottom, paddingTop, etc
 * Checks both stylesheet and style="" attribute
 * Defaults to 0, always returns an integer
 */
function getDimension(el, dim, notAInteger) {
	if(!notAInteger) notAInteger = false;
	
	// This differs from browser to browser
	if(dim == 'float') {
		dim = (typeof el.style.cssFloat == 'string') ? 'cssFloat' : 'styleFloat';
	}
	
	var checkForNoneValue = {
		'borderBottomWidth' : 'borderBottomStyle',
		'borderTopWidth' : 'borderTopStyle',
		'borderLeftWidth' : 'borderLeftStyle',
		'borderRightWidth' : 'borderRightStyle'
	};
	
	// Handle hidden borders
	if(checkForNoneValue[dim]) {
		if(getDimension(el, checkForNoneValue[dim], true) == 'none') return 0;
	}
	
	if(el && el.style && el.style[dim]) {
		return notAInteger ? el.style[dim] : parseInt(el.style[dim]);
	
	} else if(el && el.currentStyle) {
		if(el.currentStyle[dim] != 'auto') {
			return notAInteger ? el.currentStyle[dim] : parseInt(el.currentStyle[dim]);
		}
		
	} else if(document.defaultView && document.defaultView.getComputedStyle) {
		var val;
		try {
			var s = document.defaultView.getComputedStyle(el, null);
			val = s[dim];
		} catch(er) {}
		if(val) {
			if(notAInteger) {
				return val;
			} else {
				var newVal = parseInt(val);
				if(isNaN(newVal)) {
					// throw("getDimension: Couldn't turn " + dim + " value '" + val + "' into an integer.");
					return 0;
				} else {
					return newVal;
				}
			}
		}
	}
	
	return notAInteger ? null : 0;
}

