/*
 * jquery-util-lite.js - JavaScript Support Library
 * Copyright (c) 2008 Diego Martins da Rocha
 * Version: 0.3.4
 */
 /* 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(e){
	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(e){
	if($.browser.msie && $.browser.version < '7.0'){
		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);
};

/* fake css columns */
function equalColumns(param1,param2,adjust){
	var adjust = (adjust || 0);
	if(param1.outerHeight() > param2.outerHeight()){
		param2.height(param1.outerHeight()-param2.outerHeight()+param2.height()+adjust);
	}else if(param1.outerHeight() < param2.outerHeight()){
		param1.height(param2.outerHeight()-param1.outerHeight()+param1.height()+adjust);
	}
}
/* 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 */
$.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);
	
}
