jQuery( function( $ )
{	
	/**
	 *
	 * Ativa a exibição do menu
	 *
	 **/
	$( 'div#main-menu ul' ).superfish( );
	
	
	/**
	 *
	 * Formulário de contato
	 * http://www.tradecall.com.br/contato.php
	 *
	 **/
	if( exists( 'form#form-contato' ) )
	{

		/**
		 *
		 * Máscaras
		 *
		 **/
		$( 'input#telefone' ).setMask( 'phone' );



		/**
		 *
		 * Contador de caracteres
		 *
		 **/
		$( 'textarea#mensagem' )
			.css( 'margin', 0 )
			.charactersCounter(
			{
				counter : 'p',
				limit	: 5000
			});



		/**
		 *
		 * Validação do formulário de contato
		 *
		 **/
		$( 'form#form-contato' ).submit( function( )
		{

			// Nome
			if( is_empty( 'input#nome' ) )
			{
				alert( 'Informe seu nome!' );
				$( 'input#nome' ).focus( );
				return false;
			}

			// E-mail
			if( is_empty( 'input#email' ) )
			{
				alert( 'Informe seu e-mail!' );
				$( 'input#email' ).focus( );
				return false;
			}
			else if( !validate_field( 'email', $( 'input#email' ).val( ) ) )
			{
				alert( 'E-mail inválido! Verifique o e-mail informado' );
				$( 'input#email' ).focus( );
				return false;
			}

			// Telefone
			if( !is_empty( 'input#telefone' ) )
			{
				if( !validate_field( 'telefone', $( 'input#telefone' ).val( ) ) )
				{
					alert( 'Telefone inválido! Verifique o telefone informado' );
					$( 'input#telefone' ).focus( );
					return false;
				}
			}

			// Mensagem
			if( is_empty( 'textarea#mensagem' ) )
			{
				alert( 'Escreva sua mensagem!' );
				$( 'textarea#mensagem' ).focus( );
				return false;
			}

		});

	}



	/**
	 *
	 * Abre o link em uma nova janela
	 *
	 **/
	$( 'a.window-open' ).live( 'click', function( )
	{

		window_open( $( this ).attr( 'href' ), $( this ).attr( 'rel' ) );
		return false;

	});
	
	
	
	/**
	 *
	 * Carrega a função que evita abertura de janelas com o mesmo contéudo
	 *
	 **/
	prevent_duplicated_windows;




});




/**
 *
 * Abre o link em uma nova janela
 *
 * @param url: URL que será aberta
 * @param name: Nome da janela
 * @return void
 *
 **/
function window_open( url, name )
{

	/**
	 *
	 * Identifica se a URL informada já está com o "http"
	 *
	 **/
	var absolutePath = ( $( 'base' ).length == 1 ) ? $( 'base' ).attr( 'href' ) : '';
	var filter = /^(http)/;
	var url = ( filter.test( url ) ) ? url : absolutePath + url;

	window_name = window.open( url, name );

}



/**
 *
 * Evita que várias janelas com o mesmo nome sejam abertas
 *
 * @return void
 *
 **/
function prevent_duplicated_windows( )
{

	if( window.window_name && window.window_name.open && !window.window_name.closed )
	{
		window.window_name.opener = null;
	}

}



/**
 *
 * Verifica se o campo está vazio
 *
 * @param element: Elemento que será validado
 * @return Boolean
 *
**/
function is_empty( element )
{
	return ( $( element ).val( ) === '' ) ? true : false;
}



/**
 *
 * Verifica se o valor do input/textarea é igual ao valor informado
 *
 * @param element: Elemento que será validado
 * @param comparare_with: Valor que será utilizado na comparação
 * @return Boolean
 *
**/
function is_equals( element, comparare_with )
{
	return ( $( element ).val( ) == comparare_with ) ? true : false;
}



/**
 *
 * Verifica se o elemento existe no DOM
 *
 * @param element: Elemento que será verificado
 * @return Boolean
 *
**/
function exists( element )
{
	return ( $( element ).length >= 1 ) ? true : false;
}



/**
 *
 * Valida o valor do campo informado, utilizando Expressões Regulares
 *
 * @param type: Tipo de campo que será validado
 * @param value: Valor informado no campo que será validado
 * @return Boolean
 *
 **/
function validate_field( type, value )
{

	var filter;


	switch( type )
	{
		case 'email':
			filter = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
			break;

		case 'url':
			filter = /^(http(s)?\:\/\/)?(localhost|[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,3})((\/\S*)|(\/)[a-zA-Z0-9\.\?\/\{\}_%=&+-]+)?$/;
			break;

		case 'telefone':
			filter = /^\([0-9]{2}\) [0-9]{4}-[0-9]{4}$/;
			break;

		case '0800':
			filter = /^(0800) [0-9]{6}[0-9]?$/;
			break;

		case '0300':
			filter = /^(0300) [0-9]{6}[0-9]?$/;
			break;

		case 'servico-ddg':
			filter = /^(0[38]00) [0-9]{6}[0-9]?$/;
			break;

		case 'data':
			filter = /^(((0[1-9]|[12][0-9]|3[01])(\/)(0[13578]|1[02])(\/)[12][0-9]{3})|((0[1-9]|[12][0-9]|30)(\/)(0[469]|11)(\/)[12][0-9]{3})|((29)(\/)(02)(\/)(1804|1808|1812|1816|1820|1824|1828|1832|1836|1840|1844|1848|1852|1856|1860|1864|1868|1872|1876|1880|1884|1888|1892|1896|1904|1908|1912|1916|1920|1924|1928|1932|1936|1940|1944|1948|1952|1956|1960|1964|1968|1972|1976|1980|1984|1988|1992|1996|2000|2004|2008|2012|2016|2020|2024|2028|2032|2036|2040|2044|2048|2052|2056|2060|2064|2068|2072|2076|2080|2084|2088|2092|2096|2104|2108|2112|2116|2120|2124|2128|2132|2136|2140|2144|2148|2152|2156|2160|2164|2168|2172|2176|2180|2184|2188|2192|2196))|((0[1-9]|[12][0-8])(\/)(02)(\/)[12][0-9]{3}))$/;
			break;

		case 'mes-ano':
			filter = /^(0[1-9]|1[012])(\/)[12][0-9]{3}$/;
			break;

		case 'hora':
			filter = /^([0-1][0-9]|2[0-3]):([0-5][0-9])$/;
			break;

		case 'cep':
			filter = /^[0-9]{5}-[0-9]{3}$/;
			break;

		case 'estado':
			filter = /^(AC|AL|AP|AM|BA|CE|DF|ES|GO|MA|MT|MS|MG|PA|PB|PR|PE|PI|RJ|RN|RS|RO|RR|SC|SP|SE|TO)$/;
			break;

		default:
			return false;
	}

	return ( filter.test( value ) ) ? true : false;

}

