/*
 * jquery-util.js - JavaScript Library
 * Copyright (c) 2008 Lucas Ferreira (www.lucasferreira.com)
 *
 * Version: 0.5.1b
 */

/* Browser info class */
var Browser = {
	init: function()
	{
		this.os = (navigator.platform || "win").toString().toLowerCase();
		this.nav = (navigator.userAgent || "msie").toString().toLowerCase();
		this.nav_version = (navigator.appVersion || "1.0").toString();
		this.init_time = (new Date()).getTime();
		this.end_time = 0;
	},
	isIE: function(){ return jQuery.browser.msie; },
	isOpera: function(){ return jQuery.browser.opera; },
	isMozilla: function(){ return jQuery.browser.mozilla; },
	isSafari: function(){ return jQuery.browser.safari; },
	isWin: function(){ return (this.os.indexOf("win") > -1); },
	isLinux: function(){ return (this.os.indexOf("lin") > -1); },
	isMac: function(){ return (this.os.indexOf("mac") > -1); }
};
Browser.init();

/* For FireBug */
if(!console)
{
	var console = {
		created: false,
		create: function()
		{
			var d = jQuery($e("div", {"id": "debbug-win"}));
			d.css({"padding": "1em", "border": "1px solid black", "font": "12px Georgia1, Georgia, serif"});
			jQuery(document.body).append(d);
			console.created = true;
		},
		log: function(a)
		{
			if(!console.created) console.create();
			var d = document.getElementById("debbug-win");
			d.innerHTML += a + '<br />';
		}
			
	};
}
	
/* Delegate class */
function Delegate(obj, func, args)
{
	var f = function()
	{
		var target = arguments.callee.target, func = arguments.callee.func, args = arguments.callee.args;
		return func.apply(target, (args.length < 1 ? arguments : args));
	};
	extendObject(f, {
		args: (args != undefined && args.length > 0 ? args : new Array()),
		target: obj, func: func
	});
	return f;
}
var _d = Delegate.create = Delegate;
/* BodyLoad class */
var BodyLoad = {
	onloads: new Array(),
	add: function(f){ if(typeof f == "function") BodyLoad.onloads.push(f); },
	onLoad: function()
	{
		Browser.end_time = (new Date()).getTime();
		for(var i=0; i<BodyLoad.onloads.length; i++) BodyLoad.onloads[i].call(this);
	},
	init: function()
	{
		if(!window.onload)
		{
			window.onload = BodyLoad.onLoad;
		}
		else
		{
			var oldLoad = window.onload;
			window.onload = function()
			{
				oldLoad();
				Delegate.create(window, BodyLoad.onLoad)();
			}
		}
		if(window.initContentLoad != undefined) BodyLoad.onContent(window.initContentLoad);
	},
	onContent: function(f){ return jQuery(f); }
};
function initContentLoad()
{
	for(var i=0, a = document.links; i<a.length; i++)
		if(a[i].rel && a[i].rel == "blank") a[i].target = "_blank";
}
var _c = BodyLoad.onContent;
BodyLoad.init();

/* Prototypes Utils */
function extendObject(obj, ext, over)
{
	return jQuery.extend(obj, ext);
}
extendObject.elements = function(objs, ext, over)
{
	for(var i=0; i<objs.length; i++)
		extendObject(objs[i], ext, (over || false));
		
	return objs;
};
__extends__ = extendObject;

__extends__(String.prototype, {
	
	trim: function() { return jQuery.trim(this.toString()); },
	
	replaceAll: function(f, r){ return (this || "").split(f).join(r); },
	
	toNumber: function()
	{
		var s = this.toString().replaceAll(".", "").replace(",", ".").replaceAll(",", "");
		return isNaN(new Number(s)) ? 0 : new Number(s);
	},
	
	camelize: function()
	{
		for(var i=0, s=this.split("-"), ns=""; i<s.length; i++)
		{
			if(i < 1)
			{
				ns += s[i].toLowerCase();
			}
			else
			{
				ns += (ss=s[i]).substr(0, 1).toUpperCase() + ss.substr(1, ss.length).toLowerCase();
			}
		}
		return ns;
	},
	
	toObject: function(){ return eval('(' + this.toString() + ')');	}
	
});

if(!Array.prototype.push)
	Array.prototype.push = function(a){ this[this.length] = a; };
	
__extends__(Array.prototype, {
	first: function(){ return (this.length > 0) ? this[0] : null; },
	last: function(){ return (this.length > 0) ? this[this.length-1] : null; },
	read: function(){ Delegate.create(a=(this || []), arguments[0], [a.length])(); },
	each: function(f){ for(var i=0, a=(this || []); i<a.length; i++) Delegate.create(a, f, [i])(); return a; },
	run: function(f){ for(var i=0, a=(this || []); i<a.length; i++) Delegate.create(a[i], f, [i])(); return a; },
	find: function(e){ return jQuery.inArray(e, (this||[])); },
	merge: function(a){ return this.concat(a); },
	uniqueValues: function(){ for(var i=0, a=[], b=(this||[]); i < b.length; i++) if(a.find(b[i]) < 0) a.push(b[i]); return a; },	apply: function()
	{
		if((a=arguments).length < 1) return false;

		for(var i=1,f=a[0],args=[]; i<a.length; i++) args.push(a[i]);
		
		this.run(function(i){
			if(!empty(this[f]) && typeof this[f] == "function")
			{
				this[f].apply(this, args);
			}
		});
		
		return this;
	},
	remove: function(i)
	{
		this.splice(i, 1);
		return this;
	}
});

/* Event class */
var Event = {
	
	add: function(obj, evType, fn)
	{
		if(typeof obj == "array")
		{
			obj.each(function(i){ Event.add(this[i], evType, fn); });
			return true;
		}
		var obj = (typeof obj == "string") ? $(obj) : obj;
		if(empty(obj)) return false;
		
		obj.bind(evType, fn);
	},

	remove: function(obj, evType, fn)
	{
		if(typeof obj == "array")
		{
			obj.each(function(i){ Event.remove(this[i], evType, fn); });
			return true;
		}
		var obj = (typeof obj == "string") ? $(obj) : obj;
		if(empty(obj)) return false;	

		obj.unbind(evType, fn);
	},
	
	cancel: function(evt)
	{
		try {
			evt.preventDefault();
		} catch(e) {
			window.event.returnValue=false;
		}
	}
	
};

/* MISCELANIOUS FUNCTIONS */
function initValue(o)
{
	var o = $target(o);
	if(o.initVal == undefined)
	{
		o.initVal = o.value;
		jQuery(o).blur(function(e){
			if(this.value.length < 1) this.value = this.initVal;
		});
	}
	if(o.value == o.initVal) o.value = "";
}

function getStyle(e, p)
{
	var e = (typeof e == "string") ? $(e) : e;
	return e.css(p);
}

function chr(n){ return (String.fromCharCode(n) || null); }

function empty(o){ return ((typeof o == "undefined") || (o==null) || (o.length < 1) || false); }

/* FixSFMenu CLASS */
function FixSFMenu(menu)
{
	$t("LI", (typeof menu == "string") ? $(menu) : menu).each(function()
	{
		if(!empty(ul = $t("UL", this))) FixSFMenu.registerEvents(this, ul.last());
	});
}
FixSFMenu.registerEvents = function(li, ul)
{
	var li = jQuery(li), ul = jQuery(ul);	
	li.e("mouseover", function(){ jQuery(this).addClass("over"); });
	li.e("mouseout", function(){ jQuery(this).remClass("over"); });
	li.find("a").first().e("focus", function(){
		var li = jQuery(this).parent(), a = li.find("ul li a").last();
		a.get().first().parentLI = li;
		a.e("blur", function(){	this.parentLI.remClass("over");	});
		li.addClass("over");
	});
}

/* DOM functions */
var $target = getTargetByEvent = function(e){ return (e=(e||window.event)).target ? e.target : e.srcElement; };

function $t(t, p){ return $(p).find(t); }

function $e(el, t)
{
	var nEl = document.createElement(el.toString());
	if(typeof t == "object" && !t.nodeType)
	{
		for(var a in t)
		{
			if(a == "text")
			{
				nEl.appendChild(document.createTextNode(t[a]));
			}
			else if(a == "name")
			{
				if(Browser.isIE()) nEl = document.createElement('<' + el + ' name="' + t[a] + '">');
				nEl.setAttribute("name", t[a]);
			}
			else
			{
				nEl[(a == "class" ? "className" : a)] = t[a];
			}
		}
	}
	else if(typeof t == "string")
		nEl.appendChild(document.createTextNode(t));
		
	else if(typeof t == "undefined");
	
	else
		nEl.appendChild(t);

	return nEl;
}

function absLeft(obj)
{
	var obj = typeof obj == "string" ? $(obj) : (obj || this);	
	return obj.offset().left;
}

function absTop(obj)
{
	var obj = typeof obj == "string" ? $(obj) : (obj || this);	
	return obj.offset().top;
}

/* jQuery */
__extends__(jQuery.prototype, {
	e: function(evt, f){ return this.bind(evt, f); },
	remClass: function(c){ return this.removeClass(c); },
	first: function(){ return jQuery(this.get().first()); },
	last: function(){ return jQuery(this.get().last()); },
	fixsf: function(){ return FixSFMenu( this ); }
});

