var $log = (function() {
	var ob = false, fn = false, q = [], qq;

	return function() {
		if(!ob) {
			try { ob = $pick(Window.opera, console, false); }catch(e) {}
			if(ob) { fn = $pick(ob.postError,ob.log,false); }
		}
		if(fn) {
			if(q.length) {
				qq = q;
				q = [];
				qq.each(function(i) { $log(i); });
			}
			fn.apply(ob, $A(arguments));
		}else {
			q.push.apply(q, $A(arguments));
		}
	};
}());


//
//	Suppresses Error display in IE, writes errors to console if available
//
window.onerror = function(a,b,c) {
    if($type(a) == 'string') {
        if(a.contains('el.main.environment')) return true; // firebug lite error, who cares
        if(a.contains('pngbehavior.htc')) return true; // can't get access to that file, don't care
    }
	try{
        if($type(a) != 'string') {
            $log(a);
        }else {
			var err = a+' in file: '+b+' on line: '+c;
            if($chk(console.error)) console.error(err);
            else $log('Error - '+err);
        }
	}catch(e) {
		// no console, do nothing
	}

	return true;
};


//
// Splits up a query string into key/value pairs
//
String.implement({
	toQueryObject: function(){ 
		var o = {}; 
		$A(decodeURIComponent(this.replace(/^\?/,'')).split('&')).each(function(p){
			p = p.split("="); 
			o[p[0]] = p[1];
		}); 
		return o; 
	}
});

//
// Conveniently stores the current query string as a Hash on the document
//
document.queryHash = $H(document.location.search.toQueryObject());

//
// Loads Firebug Lite by request (for IE mainly)
//
window.addEvent('domready', function(){
	var prefs = $H(JSON.decode(Cookie.read('prefs'))); 
	prefs.set('fbl', $pick(document.queryHash.get('fbl'), prefs.get('fbl'), false));
	if(prefs.get('fbl') == 'false') prefs.set('fbl', false);
	if(prefs.get('fbl') == 'true') prefs.set('fbl', true);
	Cookie.write('prefs', JSON.encode(prefs));
	
	if(prefs.get('fbl')) {
		var firebug = 'https://getfirebug.com/firebug-lite.js#saveCookies,startOpened';
		new Asset.javascript(firebug, { onload:function(){ $log('Firebug Lite Started'); } });
	}
});
