/*
   Behaviour v1.1 by Ben Nolan, June 2005. Based largely on the work
   of Simon Willison (see comments by Simon below).

   Description:
   	
   	Uses css selectors to apply javascript behaviours to enable
   	unobtrusive javascript in html documents.
   	
   Usage:   
   
	var myrules = {
		'b.someclass' : function(element){
			element.onclick = function(){
				alert(this.innerHTML);
			}
		},
		'#someid u' : function(element){
			element.onmouseover = function(){
				this.innerHTML = "BLAH!";
			}
		}
	};
	
	Behaviour.register(myrules);
	
	// Call Behaviour.apply() to re-apply the rules (if you
	// update the dom, etc).

   License:
   
   	This file is entirely BSD licensed.
   	
   More information:
   	
   	http://ripcord.co.nz/behaviour/
*/   

var Behaviour = {
	list : [],
	
	register : function(sheet){
		Behaviour.list.push(sheet);
	},
	
	start : function(){
		Behaviour.addLoadEvent(function(){
			Behaviour.apply();
		});
	},
	
	apply : function(){
		for (var h = 0; h < Behaviour.list.length; h++){
			var sheet = Behaviour.list[h];
         for (var selector in sheet){
				var list = document.getElementsBySelector(selector);
				
				if (!list){
					continue;
				}

				for (var i = 0; i < list.length; i++){
					sheet[selector](list[i]);
				}
			}
		}
	},
	
	addLoadEvent : function(func){
		if (window.attachEvent) {
         window.attachEvent('onload', func);
      }
      else if (window.addEventListener) {
         window.addEventListener('load', func, false);
      }
      else {
         var __onload = window.onload;
         window.onload = function() {
            func();
            if (typeof(__onload) == "function") {
               __onload();
            }
         };
      }
	}
};

Behaviour.start();

/*
   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 
*/

function getAllChildren(e) {
   // Returns all children of element. Workaround required for IE5/Windows. Ugh.
   return e.all? e.all: e.getElementsByTagName('*');
}

document.getElementsBySelector = cssQuery;