jQuery( function( $ )
{

	/**
	 *
	 * Ativa a exibição do menu
	 *
	 **/
	$( 'ul#main-menu' ).superfish( );



	/**
	 *
	 * 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;



	/**
	 *
	 * Atualiza as dimensões do overlay ao redimensionar a janela
	 *
	 **/
	$( window ).resize( function( )
	{

		if( exists( 'div#overlay' ) )
		{

			/**
			 *
			 * Identifica as dimensões da página
			 *
			 **/
			var docElement	= ( document.documentElement );
			var page_width	= ( window.innerWidth ) || ( self.innerWidth ) || ( docElement && docElement.clientWidth ) || ( document.body.clientWidth );
			var page_height	= ( window.innerHeight ) || ( self.innerHeight ) || ( docElement && docElement.clientHeight ) || ( document.body.clientHeight );



			/**
			 *
			 * Atualiza as dimensões do overlay
			 *
			 **/
			$( 'div#overlay' )
				.css(
				{
					width	: page_width,
					height	: page_height
				});

		}

	});



	/**
	 *
	 * Fechar modal com a tecla "Esc"
	 *
	 **/
	$( '*' ).live( 'keydown', function( event )
	{

		/**
		 *
		 * Verifica se o overlay e a janela modal existem
		 *
		 **/
		if( exists( 'div#overlay' ) && exists( 'div#modal-box' ) )
		{

			/**
			 *
			 * Se o evento não foi capturado pela função, captura o evento do objeto "window"
			 *
			 **/
			if( !event )
			{
				event = window.event;
			}


	
			/**
			 *
			 * Identifica a tecla pressionada de acordo com o browser
			 *
			 **/
			if( event.keyCode )
			{
				key = event.keyCode;
			}
			else if( event.which )
			{
				key = event.which;
			}
			else if( event.charCode )
			{
				key = event.charCode;
			}


	
			/**
			 *
			 * Se a tecla pressionada for a tecla "Esc"...
			 *
			 **/
			if( key === 27 )
			{
	
				/**
				 *
				 * Remove a janela modal e o overlay
				 *
				 **/
				$( 'div#modal-box' ).fadeOut( 'fast', function( )
				{
	
					$( 'div#overlay' ).fadeOut( 'fast', function( )
					{
	
						$( 'div#modal-box' ).remove( );
						$( 'div#overlay' ).remove( );
	
					});
	
				});
	
				return false;
	
			}

		}

	});



	/**
	 *
	 * Soluções: Web Sites
	 * http://www.mbmarketing.com.br/criacao-de-sites
	 *
	 **/
	if( exists( 'img.tooltip' ) )
	{

		/**
		 *
		 * Adiciona o atributo "rel" nas imagens
		 *
		 **/
		$( 'img.tooltip' ).each( function( )
		{

			/**
			 *
			 * Identifica o ID do parágrafo
			 *
			 **/
			id = $( this ).next( 'p' ).attr( 'id' );



			/**
			 *
			 * Posição da imagem
			 *
			 **/
			var	pos_left = Math.ceil( $( this ).position( ).left ) + 50;
			var	pos_top  = Math.ceil( $( this ).position( ).top );



			/**
			 *
			 * Dimensões da imagem
			 *
			 **/
			var	span_width  = parseInt( $( this ).width( ), 10 ) - 100;
			var	span_height = parseInt( $( this ).height( ), 10 ) - 20;



			/**
			 *
			 * Cria um <span> sobre a imagem ( ele será utilizado para ativa o cluetip )
			 *
			 **/
			$( this ).before( '<span class="tooltip" id="tooltip-' + id + '">&nbsp;</span>' );



			/**
			 *
			 * Formata o <span>
			 *
			 **/
			$( 'span#tooltip-' + id )
				.attr( 'rel', '#' + id )
				.css(
				{
					width		: span_width,
					height		: span_height,
					zIndex		: 100000,
					display		: 'block',
					position	: 'absolute',
					top			: pos_top,
					left		: pos_left
				});



			/**
			 *
			 * Exibe os conteúdos em tool tips
			 *
			 **/
			$( 'span.tooltip' ).cluetip(
			{
		
				local		: true,
				showTitle	: false,
				width		: 300,
				positionBy	: 'bottomTop'
		
			});

		});

	}



	/**
	 *
	 * Dúvidas sobre Links Patrocinados
	 * http://www.mbmarketing.com.br/solucoes-links-parocinados-duvidas.php
	 *
	 **/
	if( exists( 'div#active-question' ) )
	{

		/**
		 *
		 * Esconde todas as respostas
		 *
		 **/
		$( 'dd' ).hide( );



		/**
		 *
		 * Exibe a resposta escolhida
		 *
		 **/
		$( 'div#active-question h1, dl dt' ).live( 'click', function( )
		{

			/**
			 *
			 * Verifica se é a pergunta ativa
			 *
			 **/
			if( $( this ).hasClass( 'active' ) )
			{

				/**
				 *
				 * Esconde a resposta e remove a identificação de pergunta ativa
				 *
				 **/
				$( this )
					.next( )
					.slideUp( 'fast', function( )
					{
						$( this ).removeClass( 'active' );
					})
					.end( )
					.removeClass( 'active' );

			}
			else
			{

				/**
				 *
				 * Esconde a pergunta que estava aberta
				 *
				 **/
				$( 'div#active-question div#resposta, dl dd' ).each( function( )
				{
					if( $( this ).hasClass( 'active' ) )
					{
						$( this ).slideUp( 'fast', function( )
						{
							$( this ).removeClass( 'active' );
						});
					}
				});



				/**
				 *
				 * Remove a identificação da pergunta que estava aberta
				 *
				 **/
				$( 'div#active-question h1, dl dt' ).each( function( )
				{
					if( $( this ).hasClass( 'active' ) )
					{
						$( this ).removeClass( 'active' );
					}
				});



				/**
				 *
				 * Exibe a resposta da pergunta selecionada
				 *
				 **/
				$( this )
					.next( )
					.slideDown( 'fast', function( )
					{
						$( this ).addClass( 'active' );
					})
					.end( )
					.addClass( 'active' );

			}

			return false;

		});

	}

	
	
	/**
	 *
	 * Portfolio: Web Sites
	 * http://www.mbmarketing.com.br/criacao-de-site
	 *
	 **/
	if( exists( 'div#portfolio-web-sites-content' ) )
	{

		$( 'div#web-sites-full' ).slideView( { uiBefore : true, allClickableArea : false } );

	}



	/**
	 *
	 * Portfolio: E-mail Marketing
	 * http://www.mbmarketing.com.br/arte-para-email-marketing
	 *
	 **/
	if( exists( 'div#portfolio-email-marketing-content' ) )
	{

		/**
		 *
		 * Amplia a arte do email marketing
		 *
		 **/
		$( 'div#portfolio-email-marketing-content ul li a' ).live( 'click', function( )
		{

			/**
			 *
			 * Identifica a posição da barra de rolagem atual ( ao fechar a janela modal, a tela será rolada até o ponto onde ele estava )
			 *
			 **/
			var	scroll_atual = $( document ).scrollTop( );



			/**
			 *
			 * Identifica a imagem que será ampliada
			 *
			 **/
			var	imagem = $( 'base' ).attr( 'href' ) + $( this ).attr( 'href' );



			/**
			 *
			 * Identifica o nome da arte
			 *
			 **/
			var	title = $( this ).attr( 'title' );



			/**
			 *
			 * Identifica as dimensões da página
			 *
			 **/
			var docElement	= ( document.documentElement );
			var page_width	= ( window.innerWidth ) || ( self.innerWidth ) || ( docElement && docElement.clientWidth ) || ( document.body.clientWidth );
			var page_height	= ( window.innerHeight ) || ( self.innerHeight ) || ( docElement && docElement.clientHeight ) || ( document.body.clientHeight );



			/**
			 *
			 * Cria o overlay
			 *
			 **/
			if( !exists( 'div#overlay' ) )
			{
				$( 'body' ).append( '<div id="overlay"></div>' );
			}



			/**
			 *
			 * Exibe a janela modal do formulário de contato
			 *
			 **/
			$( 'div#overlay' )
				.css(
				{
					opacity	: 0.8,
					width	: page_width,
					height	: page_height
				})
				.fadeIn( 'fast', function( )
				{

					/**
					 *
					 * Cria a janela modal
					 *
					 **/
					if( !exists( 'div#modal-box' ) )
					{
						$( 'div#modal-box' ).remove( );
					}



					/**
					 *
					 * Cria a janela modal
					 *
					 **/
					var box  = '<div id="modal-box">';
						box += '	<div id="content-modal">';
						box += '		<img src="' + imagem + '" alt="' + title + '" title="' + title + '" />';
						box += '	</div>';
						box += '</div>';
	
					$( 'body' ).append( box );



					/**
					 *
					 * Rola até o topo da página ( no Opera aparece uns bugs bizarros, e aplicando o scroll apenas no body, resolve... )
					 *
					 **/
					if( $.browser.opera )
					{
						$( 'body' ).animate( { scrollTop : 0 }, 1500 );
					}
					else
					{
						$( 'html, body' ).animate( { scrollTop : 0 }, 1000 );
					}



					/**
					 *
					 * Exibe o box
					 *
					 **/
					$( 'div#modal-box' ).fadeIn( 'fast', function( )
					{
						$( 'div#modal-box div#content-modal' ).css( 'width', $( 'div#modal-box div#content-modal img' ).outerWidth( ) );
					});



					/**
					 *
					 * Fecha a janela modal ao clicar sobre o overlay ou a imagem
					 *
					 **/
					$( 'div#modal-box:not(span.botao), div#overlay' ).live( 'click', function( )
					{

						/**
						 *
						 * Move o scroll até o ponto onde o usuário estava
						 *
						 **/
						window.scrollTo( 0, scroll_atual );



						/**
						 *
						 * Remove a janela modal e o overlay
						 *
						 **/
						$( 'div#modal-box' ).fadeOut( 'fast', function( )
						{

							$( 'div#overlay' ).fadeOut( 'fast', function( )
							{

								$( 'div#modal-box' ).remove( );
								$( 'div#overlay' ).remove( );

							});

						});

					});

				});


			return false;

		});

	}



	/**
	 *
	 * Formulário de contato
	 *
	 *
	 * Soluções: Links Patrocinados
	 * http://www.mbmarketing.com.br/links-patrocinados
	 *
	 * Contato
	 * http://www.mbmarketing.com.br/acoes-de-marketing-digital
	 *
	 **/
	if( exists( 'form#form-contato' ) )
	{

		/**
		 *
		 * Máscara
		 *
		 **/
		$( 'input#telefone' ).setMask( 'phone' );
	

	
		/**
		 *
		 * Adiciona um contador ao textarea
		 *
		 **/
		$( 'textarea#mensagem' ).charactersCounter( { limit	: 5000 } );



		/**
		 *
		 * Validação do formulário
		 *
		 **/
		$( 'form#form-contato' ).submit( function( )
		{

			/**
			 *
			 * Nome
			 *
			 **/
			if( empty( 'input#nome' ) )
			{
				alert( 'Informe seu nome' );
				$( 'input#nome' ).focus( );
				return false;
			}



			/**
			 *
			 * E-mail
			 *
			 **/
			if( 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 ( opcional )
			 *
			 **/
			if( !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( empty( 'textarea#mensagem' ) )
			{
				alert( 'Escreva uma mensagem' );
				$( 'textarea#mensagem' ).focus( );
				return false;
			}

		});

	}



});



/**
 *
 * 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 absolute_path = ( exists( 'base' ) ) ? $( 'base' ).attr( 'href' ) : '';
	var filter = /^(http)/;
	var url = ( filter.test( url ) ) ? url : absolute_path + 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 elemento existe no DOM
 *
 * @param element: Elemento que será verificado
 * @return Boolean
 *
**/
function exists( element )
{
	return ( $( element ).length >= 1 ) ? true : false;
}



/**
 *
 * Verifica se o campo está vazio
 *
 * @param element: Elemento que será validado
 * @return Boolean
 *
**/
function empty( element )
{
	return ( $( element ).val( ) === '' ) ? 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 'telefone':
			filter = /^\([0-9]{2}\) [0-9]{4}-[0-9]{4}$/;
			break;

		default:
			return false;
	}

	return ( filter.test( value ) ) ? true : false;


}



