/*
 * jquery-util-lite.js - JavaScript Support Library
 * Copyright (c) 2009 Diego Martins da Rocha
 * Version: 0.5.4
 */
 /* this path */
tmp = $.find('script')[0].src.toString().split(/(c)?js\//)[0];
var DEFAULT_URL = tmp.match('app/webroot') ? tmp.split(/app\/webroot/)[0]+'index.php/' : tmp;
delete tmp;
/* default value for empty value of a field */
$.fn.initValue = function(v){
	var c = 'initValue';
	$(this).each(function(){
		var o = $(this);
		if(o.val().length<1){
			if(v.length>0 && o.val().length<1) o.val(v);
			o.defaultValue = o.addClass(c).val();
			o.focus(function(){
				(o.val() == o.defaultValue) ? o.addClass(c).val('') : o.removeClass(c);
			}).blur(function(){
				(o.val().length<1) ? o.addClass(c).val(o.defaultValue) : o.removeClass(c);
			}).keypress(function(){
				o.removeClass(c);
			});
			o.parents('form:first').submit(function(){
				o.triggerHandler('focus');
			});
		}
	});
	return $(this);
};
/* fix menu hover */
$.fn.FixSFMenu = function(opened){
	var opened = (opened || false);
	if(opened) this.parent().find('li:nth-child('+opened+')').addClass('over');
	this.find("li").hover(function(){
		$(this).addClass('over').parent().find('.over').not($(this)).removeClass('over');
	}, function(){
		if(!opened)	$(this).removeClass('over');
	}).each(function(){
		if($(this).find('ul').length > 0){//acessibilidade - tab
			$(this).find('a:first').focus(function(){
				$(this).parent().addClass('over');
			});
			$(this).find('a:last').blur(function(){
				$(this).parent().parent().parent().removeClass('over');
				$(this).parent().parent().removeClass('over');
			});
		};
	});
	return $(this);
};
/* CSS position:fixed for ie6 */
$.fn.positionFixed = function(){
	if($.browser.msie && $.browser.version < '7.0'){
		var that = $(this);
		var thatTop = parseInt(that.css('top'));
		$(window).scroll(function(){
			that.css({'top':(document.documentElement.scrollTop || 0)+thatTop});
		});
		$(this).css({'position': 'absolute'});
	}else{
		$(this).css({'position': 'fixed'});
	};
	return $(this);
};
/* CakePHP Integration */
function $cake(a) {
	for(i=0,a=a.split('_').join('.').split(',');i<a.length&&(b=a[i])&&(a[i]='#');i++)
		for(j=0,c=b.split('.');j<c.length;(a[i]+=c[j].substr(0,1).toUpperCase()+c[j].substr(1)),j++);
	return $(a.join(","));
}
/* protect mail from spammers */
$.fn.pMail = function(){
	$(this).each(function(){
		$(this).html($(this).html().replace(/\[ponto\]/g,'.').replace(/\[arroba\]/g,'@').replace(/%5Bponto%5D/g,'.').replace(/%5Barroba%5D/g,'@'));
	});
	return $(this);
};
/* center the element */
$.fn.center = function(pos){
	var that = $(this);
	var h = function(){
		var x = -(that.outerWidth() - that.parent().width())/2;
		that.css({
			'margin-left': x+'px',
			'margin-right': x+'px' 
		});
	};
	var v = function(){
		var y = -(that.outerHeight() - that.parent().height())/2;
		that.css({
			'margin-top': y+'px',
			'margin-bottom': y+'px' 
		});
	};
	switch(pos){
		default: case 'h': h(); break;
		case 'v': v(); break;
		case 'both': h();v(); break;
	};
	return that;
};
/* normalize height of the real height of the lements: useful in lists */
/* normalize height of the real height of the lements: useful in lists */
$.fn.normalizeHeight = function(property){
	var property = property || 'padding';
	var maxHeight = 0;
	$(this).each(function(){
		switch (property) {
			case 'padding':
			default:
				maxHeight = (maxHeight > $(this).innerHeight()) ? maxHeight : $(this).innerHeight();
			break;
			case 'margin':
				maxHeight = (maxHeight > $(this).outerHeight()) ? maxHeight : $(this).outerHeight();
			break;
		}
	}).each(function(){
		var $cfg = {};
		switch (property) {
			case 'padding':
			default:
				$cfg[property+'-bottom'] = maxHeight-$(this).innerHeight()+parseInt($(this).css(property+'-bottom'))+'px';
			break;
			case 'margin':
				$cfg[property+'-bottom'] = maxHeight-$(this).outerHeight()+parseInt($(this).css(property+'-bottom'))+'px';
			break;
		}
		$(this).css($cfg);
	});
};
/* Add Plugins on the fly*/
$.addPlugin = function(func,link,asyncExec){
	if(!func) return false;
	if(eval('typeof $.'+func+' != "undefined" || typeof $.fn.'+func+' != "undefined"')){
		return true;
	}else{
		link = (!link) ? func+'/'+func : link;
		$return = false;
		$.ajax({
			async: asyncExec ? true : false,
			cache: true,
			dataType: 'script',
			url: DEFAULT_URL+'js/jquery/'+link.toLowerCase()+'.js',
			success: function(){
				$return = true;
			},
			error: function(){
				$return = false;
			},
			complete: asyncExec
		});
		return $return;
	}
};
/* simple Tooltip */
$.fn.tooltip = function(options){
	if($(this).length <1) return $(this);
	var o = {
		_class: 'jTooltip',
		text: false,
		x: 10,
		y: 30,
		opacity: 1,
		fade: 'slow',
		delay: 0
	};
	o = $.extend(o,options);
	var text = (o.text || $(this).attr('title'));
	if(text.length > 0){
		$(this).hover(function(e){;
			$(this).attr('title','');
			$('body').append('<p class="'+o._class+'">'+text+'</p>');
			$('.'+o._class).css({
				'top': (e.pageY - o.x) + 'px',
				'left': (e.pageX + o.y) + 'px'
			}).css({
				'position': 'absolute',
				'display': 'none',
				'opacity': o.opacity
			});
			
			setTimeout(function(){
				$('.'+o._class).fadeIn(o.fade);
			},o.delay);

	    },function(){
			$(this).attr('title',text);
			$('.'+o._class).remove();
	    })
		.mousemove(function(e){
			$('.'+o._class).css({
				'top': (e.pageY - o.x) + 'px',
				'left': (e.pageX + o.y) + 'px'
			});
		}).click(function(){
			$('.'+o._class).remove();
		});
	}
	return $(this);
};
$.UtilAjax = function(){
	$(window).load(function(){
		$(this).ajaxSend(function(){
			if($('#ajax-msg').length < 1){
				$('body').prepend('<div id="ajax-msg">Carregando Dados...</div>');
				$('#ajax-msg').positionFixed();
			}
			$('#ajax-msg').show().animate({
				'opacity': 1
			});
		}).ajaxComplete(function(){
			$('#ajax-msg').animate({
				'opacity': 0
			},'normal','swing',function(){
				$('#ajax-msg').hide();
			});
		});
	});
};
