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.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');
		
		parentNode = $(parentNode);
		var h;
		for (h=0;sheet=Behaviour.list[h];h++){
			//console.log(parentNode);
			try {
				Behaviour.process(sheet, parentNode, applyToParent);
			} catch(e) {
				if("console" in window) console.log(e);
				else throw e;
			}
		}
		
		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 = $$(selector);

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

document.getElementsBySelector = $$;

/*
 * 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;
}

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

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;	
}

/* 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) {
	  if(Behaviour.isDebugging) console.log("Notify %s", event);
		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();

Element.addMethods({
  set: function(element, att, value) {
    element = $(element);
    element.att = value;
  }
});
