/**
 * Extensão das funcionalidades da jQuery
 * 
 * @author Jackson Caset
 */
var $j = jQuery.noConflict();

(function($j) {
	/**
	 * Corrige previousSibling
	 * 
	 * @param {Object} objNode
	 */
	$j.previousObject = function(objNode) {
		for (objNode = objNode.previousSibling; ( objNode && objNode.nodeType == 3 ); objNode = objNode.previousSibling){}
	
		return objNode;
	},
	/**
	 * Corrige nextSibling
	 * 
	 * @param {Object} objNode
	 */
	$j.nextObject = function(objNode) {
		for (objNode = objNode.nextSibling; ( objNode && objNode.nodeType == 3 ); objNode = objNode.nextSibling){}
	
		return objNode;
	},
	/**
	 * Método loading incorporado a jQuery.
	 * 
	 * @examples
	 * 		$j.loading({display:1, html:'Conteúdo'});
	 * 		$j.ajax({}).loading(0);
	 * 
	 * @param {display:1/0, content/html:'conteúdo do loading'} obj
	 */
	$j.loading = function(obj) {
		var mask = $j('#loading-mask');
		var loading = $j('#loading')
		var frase = $j('#loading #frase');
		var content = '';
		
		if (obj.display == 1) {
			content = obj.html != '' ? obj.html : obj.content;
			if (content != '') {
				$j(frase).html(content);
			}
			$j(loading).fadeIn();
		} else if (obj.display == 0 || obj == 0) {
			$j(mask).fadeOut();
			$j(loading).fadeOut('fast');
		}
	}
	$j.fn.loading = function(obj) {
		return this.each(function() { $j.loading(obj); });
	}
	/**
	 * Método alert incorporado a jQuery.
	 * 
	 * @param {id:'#id-elemento', type:'success/alert/error', html:'Conteúdo', timeout:'tempo para a msgm sumir'} obj
	 */
	$j.alert = function(obj) {
		var el = typeof(obj.id) == 'undefined' ? '#aux-mensagens' : obj.id;
		var type = typeof(obj.type) == 'undefined' ? 'success' : obj.type;
		var html = typeof(obj.html) == 'undefined' ? '&nbsp;' : obj.html;
		var timeout = typeof(obj.timeout) == 'undefined' ? 3000 : obj.timeout;
		
		var el = $j(el).html() != null ? $j(el) : parent.$j(el);
		
		el.removeClass('success alert error');
		el.addClass(type).html(html).slideDown();
		setTimeout(function() { el.slideUp(); }, timeout);
	}
	$j.fn.alert = function(obj) {
		return this.each(function() { $j.alert(obj); });
	}
	/**
	 * Método incorporado a jQuery que substitui o padrão write do objeto document. Pode ser mais personalizado no futuro.
	 * 
	 */
	$j.write = function(content) {
		document.write(content);
	}
	/**
	 * Exibiçao de popups. Incorporado a jQuery
	 * 
	 * @param {url:'url-para-popup.php', width:'500px', height:'500px', scroll:true, resize:true} obj
	 */
	$j.popup = function(obj) {
		return void(
			open(
				 ''+obj.url+'','','width='+(obj.width || obj.w)+',height='+(obj.height || obj.h)+', scrollbars='+(obj.scroll || null)+', resizable='+(obj.resize || null)+''
			)
		);
	}
	/**
	 * Adiciona js em documentos html
	 * 
	 * @param ['arquivo1.js', 'arquivo2.js'] scripts
	 */
	$j.js = function(scripts) {		
		if(scripts.length > 0) {
			var s='';
			$j.each(scripts, function(i) {
				s += "<script type=\"text/javascript\" src=\""+scripts[i]+"\"></script>";
			});
			$j.write(s);
		}
	}
})(jQuery);