/*
 * jquery-util-lite.js - JavaScript Support Library
 * Copyright (c) 2010 Diego Martins da Rocha
 * Version: 1.0.1
 */
 /* 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;
/* 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(','));
};
/* default value for empty value of a field */
$.fn.initValue = function()
{
	this.focus(function(){
		if(typeof this.defaultValue == "undefined")
		{
			this.defaultValue = this.val();
		}
		if(this.defaultValue == $(this).val()) $(this).val('');
	}).blur(function(){
		if($(this).val().length < 1) $(this).val(this.defaultValue);
	});
}

/* 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 && parseInt($.browser.version) < 7){
		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);
};
/* protect the 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 = -parseInt((that.outerWidth() - that.parent().width())/2);
		that.css({
			'margin-left': x+'px',
			'margin-right': x+'px' 
		});
	};
	var v = function(){
		var y = -parseInt((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 elements: useful in lists */
$.fn.normalizeHeight = function(property,el_per_time){
	var that = $(this);
	var property = property || 'height';
	var el_per_time = el_per_time || that.length;
	
	var start = 0;
	var end = el_per_time;
	
	for(i=0;i<that.length;i++){
		if(start < i && end > i){
			start = end;
			end += el_per_time;
		}
		var maxHeight = 0;
		
		that.slice(start,end).each(function(){
			var those = $(this);
			switch (property) {
				case 'padding':
				case 'height':
				default:
					maxHeight = (maxHeight > those.innerHeight()) ? maxHeight : those.innerHeight();
				break;
				case 'margin':
					maxHeight = (maxHeight > those.outerHeight()) ? maxHeight : those.outerHeight();
				break;
			}
		}).each(function(){
			var those = $(this);
			var cfg = {};
			switch (property) {
				case 'padding':
					cfg[property+'-bottom'] = maxHeight-those.innerHeight()+parseInt(those.css(property+'-bottom'))+'px';
				break;
				case 'margin':
					cfg[property+'-bottom'] = maxHeight-those.outerHeight()+parseInt(those.css(property+'-bottom'))+'px';
				break;
				default:
				case 'height':
					cfg['height'] = maxHeight-parseInt(those.css('padding-bottom'))+'px';
				break;
			}
			those.css(cfg);
		});
	}
};
/* Add Plugins on the fly */
$.addPlugin = function(func,link,asyncExec){
	if(func && !eval('typeof $.'+func+' != "undefined" || typeof $.fn.'+func+' != "undefined"')){
		$.ajax({
			async: asyncExec ? true : false,
			cache: true,
			dataType: 'script',
			url: DEFAULT_URL+'js/jquery/'+((!link) ? func+'/'+func : link).toLowerCase()+'.js',
			complete: asyncExec
		});
	}
};
/* get 'get' variables*/
$.getQueryString = function(){
	var vars=window.location.href.toString(),results={}; 
	if((vars=vars.split('?')).length>1){
		$.each(vars[1].split('#')[0].split('&'),function(k,v){
			temp=value.split('=');
			results[temp[0]]=temp[1];
		});
	}
	return results;
};
/* ajax progress msg */
$.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();
			});
		});
	});
};
