var mpiutil = {};

//This is implemented as a simple linked list 
//(vs. a balanced tree) on the theory that we 
//will always be inserting near the head of the list
//in the cases where performance matters
mpiutil.PriorityQueue = function(compare) {
	this._compare = compare;
	this._head = null;
	this._itr = null;
	this.add = function(o) {
		if (!this._head) {
			this._head={value: o, next: null, prev: null};
			this._itr = this._head;
			return;
		} else {
			var prev = null;
			var node = this._head;
			while (node) {
				if (this._compare(o, node.value) < 0) {
					var newNode = {value: o, next: node, prev: prev};
					if (node)
						node.prev = newNode;
					if (prev) {
						prev.next = newNode;
					} else {
						this._head = newNode;
						this._itr = this._head;
					}
					return;
				}
				prev = node;
				node = node.next;
			}
			prev.next = {value: o, next: null, prev: prev};
		}
	};
	this.peek = function() {
		return this._itr ? this._itr.value : null;
	}
	
	this.take = function() {
		if (this._itr) {
			var ret = this._itr.value;
			this._itr = this._itr.next;
			return ret;
		}

		return null;
	}
	
	this.seek = function(o) {
		var node = this._itr;
		if (!node) {
			this._itr = this._head;
			while (this._itr.next)
				this._itr = this._itr.next;
			node = this._itr;
		}
		var iterate = null;
		if (this._compare(o, node.value) < 0) {
			
			iterate = function(o, node){ 
				if (!node.prev)
					return null;
				if (this._compare(o, node.prev.value) <= 0) {
					return node.prev;
				}
				return null;
			};

		} else {
			
			iterate = function(o, node){
				if (!node)
					return null;
				if (this._compare(o, node.value) >= 0) {
					return node.next;
				}
				return null;
			};
		}
		var ret = node; 
		while (node = iterate.call(this, o, node))
			ret = node;
		
		if (this._compare(o, ret.value) < 0 && ret.prev)
			ret = ret.prev;
		this._itr = ret;
	}
};


function get(id) {
    var o;
    if (document.getElementById && document.getElementById(id)!= null) 
        o = document.getElementById(id); 
    else if (document.layers && document.layers[id]!= null) 
        o = document.layers[id]; 
    else if (document.all && document.all[id] !=null) 
       o = document.all[id]; 
    return o;
}

function checkCode(x) {
	if (x!=null && x=="") {
		return false;
	}	
	return true;
}


// 2007-12-19 - matta
// used to popup a new browser without decorations
function popupWindow(URL, handleName, width, height) {

	var screenWidth = ( screen.availWidth || screen.width );
	var screenHeight = ( screen.availHeight || screen.height );

	// optional relative offset positioning
	var windowPositionLeft = ( window.screenLeft || window.screenX );
	var windowPositionTop = ( window.screenTop || window.screenY );

	var _l = ( screenWidth - width ) /2;
	//var _t = ( screenHeight - height ) /3;
	var _t = 0; // align top

	try {

		var _w = window.open( URL, handleName, "toolbar=no,scrollbars=no,location=no,status=no,directories=no,resizable=yes," + 'width=' + width + ',height=' + height + ',left=' + _l + ',top=' + _t );
		_w.moveTo(_l, _t);
		_w.focus();
		return _w;

	} catch (e) {
		// nada
	}
}

function getPageWidth() {return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;}

function getPageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}

