String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/, ''); 
};

function openWindow( content, popup, link, offsetY, offsetX ) {
	var w = $( link ).get(0);
	var offsetY = ( offsetY == undefined ? 0 : offsetY );
	var offsetX = ( offsetX == undefined ? 0 : offsetX );
	var position = $( link ).offset();
	$( content ).block();
	$( popup ).css({position: 'absolute', cursor: 'default', left:(position.left - offsetX), top:(position.top - offsetY), 'z-index':2000 } ).show();
};

function closeWindow( content, popup ) {
	$( content ).unblock( { fadeOut:false } );
	$( popup ).hide();	
};
//***************************************************************************
// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
// http://javascript.about.com/od/guidesscriptindex/a/screen.htm
function pageWidth() {	
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?  document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} ;
function pageHeight() { 
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
};
function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
};
function posTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
};
function posRight() {
	return posLeft()+pageWidth();
};
function posBottom() {
	return posTop() + pageHeight();
};
//////////////////////
// firebugx.js      //
//////////////////////
if (!window.console || !console.firebug){
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml","group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {};
	for (var i = 0; i < names.length; ++i)
		window.console[names[i]] = function() {};
}
function createPackage(name){
	var t		= window;
	var pkgs	= name.split(".");
	for(p in pkgs){
		if(!t[pkgs[p]]){
			t[pkgs[p]] = {};
		}else{
			if(typeof t[pkgs[p]] == 'function'){
				alert(name+': '+pkgs[p]+' is a Class and cannot be package name!');
			}
		};
		t = t[pkgs[p]];
	};
	return t;
};
jQuery.fn.extend({
	disable	: function(){
		return $(this).attr('disabled', 'disabled');
	},
	enable	: function(){
		return $(this).attr('disabled', '');
	}	
});
jQuery.toJSON = function (obj, depth){
	depth = isNaN(depth)?0:depth;
	if(depth>3){
		return null;
	}
    var type = typeof obj;
    switch(type) {
      case 'undefined':
      case 'function':
      case 'unknown': return;
    }
    if (obj === null) return 'null';
    if (obj.toJSON) return obj.toJSON();
    if (obj.ownerDocument === document) return;
	if(obj.constructor == Array || (obj.constructor == Object && obj.length>0)){
		var ret = [];
		for (k=0; k<obj.length;k++){
			var val = $.toJSON(obj[k], depth+1);
			if(val === undefined) continue;			
			ret.push(val);
		};		
		return "["+ret.join(",")+"]";
	}else if (obj.constructor == Object){
		var ret = []
		for (k in obj){
			var key = k;
			var val = $.toJSON(obj[k], depth+1);
			if(val === undefined) continue;						
			var str = '"'+key+'":'+val;
			ret.push(str);
		};
		ret = "{"+ret.join(",")+"}";
		return ret;
	}else if(obj.constructor == String){
		return '"'+obj+'"';
	}else if(obj.constructor == Number){
		return obj;
	}else if(obj.constructor == Boolean){
		return obj?'true':'false';
	};
};
function createClass(pkgName, constr, impl, extend){
	constr	= constr || function(){};
	impl	= impl||{};
	var pkgNameAr = pkgName.split('.');
	var className = pkgNameAr.pop();
	var extended = extend?(extend.prototype?extend.prototype:{}):{};
	
	var pkg = createPackage(pkgNameAr.join('.'));
	pkg[className] = constr;
	pkg[className].prototype = $.extend({}, pkg[className].prototype, extended, impl, true);
	return pkg[className];
};

DEBUG = {
		console		: null,
		options 	: null,	
		LVL_ALL		: 0,	
		LVL_INFO	: 10,
		LVL_LOG		: 20,
		LVL_WARN	: 30,
		LVL_ERROR	: 40,
		LVL_NONE	: 999,

		__call		: function(func, arguments, level){
			if(level >= this.options.logLevel){
				func.apply(this.console, arguments);
			}
		},

		info		: function(){return this.__call(this.console.info,	arguments, DEBUG.LVL_INFO);},
		log			: function(){
			return this.__call(this.console.log,	arguments, DEBUG.LVL_LOG);
		},
		debug		: function(){return this.__call(this.console.debug,	arguments, DEBUG.LVL_LOG);},
		warn		: function(){return this.__call(this.console.warn,	arguments, DEBUG.LVL_WARN);},
		error		: function(){return this.__call(this.console.error,	arguments, DEBUG.LVL_ERROR);},
		
		time		: function(){return this.__call(this.console.time,		arguments, DEBUG.LVL_LOG);},
		timeEnd		: function(){return this.__call(this.console.timeEnd,	arguments, DEBUG.LVL_LOG);}	
};

createClass(
	'marketplace.Debugger',
	function(options){
		this.options = options;
		this.console = console;
	},
	DEBUG
);

Debug = new marketplace.Debugger({
	logLevel:DEBUG.LVL_WARN
});