/*
   based on Behaviour 1.1 by Ben Nolan http://www.bennolan.com/behaviour/
   modified by Chris Lewis 2007-09-05 to use Prototype functions instead

   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 BehaviourLog = $A();

var Behaviour = {
	//most Behaviour functions only need to be run once
	// if you want yours to run every Behaviour.apply() is called, add it to Behaviour.RUN_EVERY_TIME
	RUN_COUNT : $H(),
	RUN_EVERY_TIME: $A(),

	_startTime: null,
	_perfWidget: null,
	_documentLoaded: false,

	_running: false,
	_currentRule: null,
	_currentFunc: null,

	init: function() {
		Behaviour._startTime = Behaviour._getCurrentTime(); 
		Behaviour._perfWidget = $('performanceWidgetTable');
		Behaviour._documentLoaded = true;
	},

	list : new Array,
	
	register: function(sheet){
		this.list.push(sheet);
	},
	
	_getCurrentTime: function() {
		var temp = new Date();
		return temp.getTime();
	},

	_functionWrapper: function(el) {

		if (!this._currentRule || !this._currentFunc) return;

		var funcstart = 0;
		if (this._perfWidget)
			 funcstart = this._getCurrentTime();
		
		if (typeof(el.id) == 'undefined' || el.id == null || el.id == '')
			el.id = 'myfonts_random_id_' + Math.random();

		if (typeof(this.RUN_COUNT[this._currentRule][el.id]) == 'undefined')
			this.RUN_COUNT[this._currentRule][el.id] = 0;

		if (!this.RUN_EVERY_TIME.include(this._currentRule)
			&& this.RUN_COUNT[this._currentRule][el.id] > 0)
			return;

		this._currentFunc(el);

		if (this._perfWidget)
		{
			var start = Math.round(funcstart - this._startTime);
			var delta = Math.round(this._getCurrentTime() - funcstart);

			BehaviourLog.push({
				"start": start,
				"duration": delta,
				"rule": this._currentRule,
				"element": el.id
			});
		}

		++this.RUN_COUNT[this._currentRule][el.id];
	},

	_runThisOnIndividualRules: function(pair) {

		this._currentRule = pair.key;
		this._currentFunc = pair.value;
		
		if (typeof(this.RUN_COUNT[pair.key]) == 'undefined')
			this.RUN_COUNT[pair.key] = $H();

		$$(pair.key).each(this._functionWrapper.bind(this));
		
		this._currentRule = null;
		this._currentFunc = null;
	}, 
	
	_runThisOnRuleList: function(rules) {
		$H(rules).each(this._runThisOnIndividualRules.bind(this));
	},

	apply : function() {

		if (!this._documentLoaded) return false;
		if (this._running) return false; //avoid loops

		this._running = true;
		this.list.each(this._runThisOnRuleList.bind(this));
		this._running = false;
	}
};

Event.observe(document,"dom:loaded", function() { 
	Behaviour.init();
	Behaviour.apply();
});
