function $(expr, context) {
	return iQ(expr, context);
};

function _$(id) {
	var result = iQ('#' + id);
	return result.length == 0 ? null : result[0];
}

function $$(expr) {
	return document.querySelectorAll(expr);
}

var _$t = contentAPI.tracker;
// Element.prototype can not be accessed in SandBox
//Element.prototype.$$ = function(expr) {
//	return this.querySelectorAll(expr);
//}
//
//Element.prototype.remove = function() {
//	if (this.parentNode) {
//		this.parentNode.removeChild(this);
//	}
//}

/**
 * USAGE:
 * 	function abc (arg1, arg2, arg3, arg4) {
 * 		..
 *  }
 * 
 * 	var context = 'abcd';
 * 	abc.bind(context, 'arg1', 'arg2')('arg3', 'arg4');
 * 
 */
Function.prototype.bind = function (context) {
	var __method = this, args = Array.prototype.slice.call(arguments, 1);
	return function() {
		return __method.apply(context, args.concat(Array.prototype.slice.call(arguments, 0)));
	}
}

function offset(elem) {
	var box = elem.getBoundingClientRect();
	return {
		top: box.top,
		left: box.left
	}
}

function emptyFunction() { }

function escapeHTML(str) {
	return !str ? str : str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

function completeURL(url) {  
	if (!url)
		return url;
		
	if (url.indexOf('http://') != 0 && url.indexOf('https://') != 0) {
		url = 'http://' + url;
	}
	
	try {
		var ioService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
		var uri = ioService.newURI(url, null, null);
		
		// if url is 'http://', then spec will be http:///
		// so in order to prevent such case, test if uri.host exists.
		if (!uri.host) {
			return url;
		} else {
			return uri.spec;
		}
	} catch (e) {
		return url;
	}
}

(function() {
	function extend(destination, source) {
	    for (var property in source)
			destination[property] = source[property];
		return destination;
	}
	
	extend(Object, {
		extend: extend
	});
})();

var CSS = {
	is: function(node, cls) {
		var re = new RegExp('(^|\\s)' + cls + '(\\s|$)');
		if (re.test(node.className)) {
			return true;
		} else {
			return false;
		}
	},
	
	add: function(node, cls) {
		if (this.is(node, cls))
			return;
		var clss = node.className.split(' ');
		clss.push(cls);
		node.className = clss.join(' ');
	},
	
	del: function(node, cls) {
		var clss = node.className.split(' ');
		for (var i in clss) {
			if (clss[i] == cls) {
				clss.splice(i, 1);
				node.className = clss.join(' ');
				return;
			}
		}
	},
	
	find: function(cls, node) {
		if (!node) {
			node = document;
		}
		if (node.getElementsByClassName) {
			return node.getElementsByClassName(cls);
		} else if (node.querySelectorAll) {
			return node.querySelectorAll('.' + cls);
		} else if (document.getElementsByClassName) {
			return document.getElementsByClassName.call(node, cls);
		}
	},
	
	getRel: function(elem) {
        var rel = /rel-([^ ]+)/i.exec(elem.className);
        if (!!rel && rel.length == 2) {
            return rel[1];
        } else {
        	return null;
        }
    }
};

function quotefilter(str) {
	return !str ? str : str.replace(/'/g, '\\\'');
}

function xmlFilter(str) {
	return !str ? str : str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}