/**
 * Arquivo com pequenos plugins para jQuery e funções úteis.
 * File with little jQuery plugins e funcitions useful.
 *
 * @author Estevão Lucas <estevao[dot]lucas[at]gmail[dot]com>
 * 
 * Copyright(c) Todos os direitos reservados a Kava Soluções
 */
 
 
/**
 * Implementation to fadeToggle, this plugin allow the same fadeIn and fadeOut's parameters.
 *
 * @name jQuery.fn.fadeToggle();
 * @exemple $( "div" ).fadeToggle();
 * @param {String|Number} speed An velocity that you want. By default is "normal". Possible values are "slow", "normal" and "fast" 					 *                        to string and any number in miliseconds.
 * @param {Function} callback A function to be invoked when animation over.
 *
 */ 
$.fn.fadeToggle = function( speed, callback )
{
	callback = callback || null;
	
	typeof velocity == "function"
		&& ( callback = speed ) && ( speed = "normal" )
		
	return this.each( function()
	{
		var $this = $( this ),
			opacity = $this.is( ":visible" )
				? "hide"
				: "show";
			
		$this.animate( {opacity: opacity }, speed, callback );
	});
}

/**
 * jQuery resetDefaultValue plugin
 *
 * @name jQuery.fn.resetDefaultValue
 * @version 0.9.1
 * @author Leandro Vieira Pinho
 * @example $( "input[@type='text']" ).resetDefaltValue()
 */

$.fn.resetDefaultValue = function( now )
{
	return this.each( function()
	{
		var d = this.defaultValue,
			clear = function()
			{
				this.value == d && ( this.value = '' );
			},	
			reset = function()
			{
				this.value == '' && ( this.value = d );
			}
		
		$( this )
			.click( clear )
			.focus( clear )
			.blur( reset );
	});
}



// Faz um Ajax via method GET e chama uma funao apos o envio
$.envioAjax = function( url, callback )
{
	var url = typeof url == "object"
		? url.href
		: url;
		
	$.ajax({
		type: "GET", 
		url: url,
		success: function( response )
		{
			callback && callback( response );
		}
	});
}

// Envio AJAX do formulrios
$.fn.submitAjax = function( callback )
{
	this.submit( function()
	{
		// Envia o formulário totam
		$( this ).ajaxSubmit(
		{
			resetForm: true,
			success: function( response )
			{
				$[ callback ]( response );
			}
		});
	
		return false;
	});
}

$.fn.onlyNumber = function( settings )
{
	var defaults = {
			except: "-.A"	
		},
		settings = $.extend( {}, defaults, settings || {}),
		except = [];
		
	for( var i = 0; i < settings.except.length; i++ )
	{
		except.push( settings.except.charCodeAt( i ) );
	}
	
	return this.each( function()
	{
		$( this ).keypress( function( e )
		{
			if( e.which!=8 && e.which!=0 && ( e.which<48 || e.which>57 ) && $.inArray( e.which, except ) == -1 )
				return false;
		});
	});
}

// Simular um Include, mas é necessário passar o local onde será incluido
include = function( file, place )
{
	$.ajax({
		type: "GET",
		url: file,
		success: function( response )
		{
			$( response ).appendTo( $( place ) ).show();
		}
	})
}


/*dojo.keys = {
	// summary: definitions for common key values
	BACKSPACE: 8,
	TAB: 9,
	CLEAR: 12,
	ENTER: 13,
	SHIFT: 16,
	CTRL: 17,
	ALT: 18,
	PAUSE: 19,
	CAPS_LOCK: 20,
	ESCAPE: 27,
	SPACE: 32,
	PAGE_UP: 33,
	PAGE_DOWN: 34,
	END: 35,
	HOME: 36,
	LEFT_ARROW: 37,
	UP_ARROW: 38,
	RIGHT_ARROW: 39,
	DOWN_ARROW: 40,
	INSERT: 45,
	DELETE: 46,
	HELP: 47,
	LEFT_WINDOW: 91,
	RIGHT_WINDOW: 92,
	SELECT: 93,
	NUMPAD_0: 96,
	NUMPAD_1: 97,
	NUMPAD_2: 98,
	NUMPAD_3: 99,
	NUMPAD_4: 100,
	NUMPAD_5: 101,
	NUMPAD_6: 102,
	NUMPAD_7: 103,
	NUMPAD_8: 104,
	NUMPAD_9: 105,
	NUMPAD_MULTIPLY: 106,
	NUMPAD_PLUS: 107,
	NUMPAD_ENTER: 108,
	NUMPAD_MINUS: 109,
	NUMPAD_PERIOD: 110,
	NUMPAD_DIVIDE: 111,
	F1: 112,
	F2: 113,
	F3: 114,
	F4: 115,
	F5: 116,
	F6: 117,
	F7: 118,
	F8: 119,
	F9: 120,
	F10: 121,
	F11: 122,
	F12: 123,
	F13: 124,
	F14: 125,
	F15: 126,
	NUM_LOCK: 144,
	SCROLL_LOCK: 145
};*/

