/**
 * MasterListener Object
 * Wrapper for resize/scroll events
 * 
 * Requires jQuery
 * 
 * @author Avery Brooks
 * @copyright 2007
 */
 
function MasterListener(name) {
	
	this.actionQueue = new Array();
	this.eventTypes  = new Array(
		"both",
		"scroll",
		"resize"
	);
	this.isAlert = false;
	// this.myName = name;

	/**
	 * startListening
	 */
	this.startListening = function() {
		$(window).bind('scroll',this,function(e){
			e.data.fireActions(1);
		});
		$(window).bind('resize',this,function(e){
			e.data.fireActions(2);
		});
		this.isAlert = true;
	}

	/**
	 * stopListening
	 */
	this.stopListening = function() {
		$(window).unbind("scroll");
		$(window).unbind("resize");
		this.isAlert = false;
		return true;
	}

	/**
	 * fireActions
	 */
	this.fireActions = function(eventType) {
		if (this.actionQueue.length == 0) return false;
		for (var i=0;i<this.actionQueue.length;i++) {
			if (eventType == this.actionQueue[i][1]) {
				setTimeout(this.actionQueue[i][0],3);
			}
		}
		return true;
	}

	/**
	 * addAction
	 */
	this.addAction = function(action, eventType) {
		if (eventType == 0) {
			this.actionQueue.push(Array(action,1));
			this.actionQueue.push(Array(action,2));
		} else {
			this.actionQueue.push(Array(action,eventType));
		}
		return true;
	}

	/**
	 * removeAction
	 * a little dicey b/c it clears and rebuilds, but c'est la vie
	 * alternative is to rewrite actionqueue as as object
	 */
	this.removeAction = function(action, eventType) {

		var arrayTemp = this.actionQueue;
		var actionTemp = null;
		this.clearQueue();

		while(arrayTemp.length > 0) {
			actionTemp = arrayTemp.pop();
			if (actionTemp[0] == action && (actionTemp[1] == eventType || eventType == 0)) {
				// alert(actionTemp[0] + " removed!");
			} else {
				// alert(actionTemp[0] + " ok!")
				this.addAction(actionTemp[0],actionTemp[1]);
			}
		}
	}

	this.clearQueue = function() {
		this.actionQueue = Array();
	}

	this.isAwake = function() {
		return this.isAlert;		
	}

} // End MasterListener object
