/*
 * jquery-util-lite.js - JavaScript Support Library
 * Copyright (c) 2009 Diego Martins da Rocha
 * Version: 0.5.0
 */
 /* this path */
var defaultUrl = $.find('script')[0].src.toString().split(/(c)?js\//)[0];
var defaultUrl2 = (defaultUrl.match('app/webroot')) ? defaultUrl.split(/app\/webroot/)[0]+'index.php/' : defaultUrl;
/* default value for empty value of a field */
$.fn.initValue = function(){
	this.focus(function(){
		if($(this).val() == this.defaultValue){
			$(this).val('');
		}
	}).blur(function(){
		if(!$(this).val()) {
			$(this).val(this.defaultValue);
		}
	});
	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.positionFix = function(){
	if($.browser.msie && parseInt($.browser.version.substr(0,1)) < 7){
		var that = $(this);
		var thatTop = parseInt(that.css('top'));
		$(window).scroll(function(e){
			that.css({'top':(document.documentElement.scrollTop || 0)+thatTop});
		});
		$(this).css({'position': 'absolute'});
	}else{
		$(this).css({'position': 'fixed'});
	}
	return $(this);
}
	
/* CakePHP Integration */
function $cake(s){
	var s = s.split('_').join('.').split('.');
	for(var i=1; i<s.length; i++){
		s[i] = s[i].substr(0, 1).toUpperCase() + s[i].substr(1);
	}
	s = s.join('');
	return $('#'+s);
};

/* 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){
	switch(pos){
		default:
		case 'h':
			var x = -($(this).outerWidth() - $(this).parent().width())/2;
			$(this).css({
				'margin-left': x+'px',
				'margin-right': x+'px' 
			});
			break;
		case 'v':
			var y = -($(this).outerHeight() - $(this).parent().height())/2;
			$(this).css({
				'margin-top': y+'px',
				'margin-bottom': y+'px' 
			});
		break;
	}
	return $(this);
	
};
/* Add Plugins on the fly*/
$.addPlugin = function(func,link){
	if(!func) return;
	if(!link) link = func;
	if(link.indexOf('/') < 0){
		link = link+'/'+link;
	}
	if(!eval('$.'+func) && !eval('$.fn.'+func)){
		$.ajax({
			async: false,
			cache: true,
			dataType: 'script',
			url: defaultUrl+'js/jquery/'+link.toLowerCase()+'.js'
		});
	}
}
/* simple Tooltip */
$.fn.tooltip = function(options){
	if($(this).length <1){
		return $(this);
	}
	var o = {
		id: '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 id="'+o.id+'">'+text+'</p>');
			$('#'+o.id).css({
				'top': (e.pageY - o.x) + 'px',
				'left': (e.pageX + o.y) + 'px'
			}).css({
				'position': 'absolute',
				'display': 'none',
				'opacity': o.opacity
			});
			
			setTimeout(function(){
				$('#'+o.id).fadeIn(o.fade);
			},o.delay);

	    },function(){
			$(this).attr('title',text);
			$('#'+o.id).remove();
	    })
		.mousemove(function(e){
			$('#'+o.id).css({
				'top': (e.pageY - o.x) + 'px',
				'left': (e.pageX + o.y) + 'px'
			});
		}).click(function(){
			$('#'+o.id).remove();
		});
	}
	return $(this);
};