$(document).ready(function(){
	function passarVitrine(obj)
	{
		$( '#vitrine .menu li' ).removeClass( 'active' );
		$(obj).addClass( 'active' );
		$( '#vitrine .slide' ).hide();

		var activeTab = $( obj ).find( 'a' ).attr( 'href' );
		$( '#vitrine .slide' ).removeClass( 'active' );
		$( activeTab ).addClass( 'active' );
		$( activeTab ).fadeIn();
	}

	var url = window.location.href.indexOf( '#videos' );
	if(url > 0)
	{
		if ($('.videos').length)
		{
			localizaVideos( '#videos' );
		}
	}

	$( '#chat .submit, #chat a' ).click(function()
	{
		verificaChat();
	});

	$( '#newsletter .submit' ).click(function()
	{
		cadastrarNewsletter();
	});

	if( $( '.scrollable.horizontal' ).length )
	{
		$( '.scrollable.horizontal' ).scrollable();
	}

	if( $( '.front' ).length )
	{
		$( '#vitrine .slide' ).hide();
		$( '#vitrine .menu li:first' ).addClass( 'active' ).show();
		$( '#vitrine .slide:first' ).show();

		$( '.menu li', '#vitrine' ).click( function() {

			passarVitrine(this);

			return false;
		});
	}

	if( $( '.popup' ).size() )
	{
		if( $( '.popup.faleconosco' ).size() )
		{
			abrePopup( '.popup.faleconosco' );
		}

		$( '.newsletter .enviar' ).click(function()
		{
			cadastrarNewsletter();
		});

		$( '.sombra' ).click(function()
		{
			fechaPopup();
		});

		$( '.popup .footer a' ).click(function()
		{
			fechaPopup();
		});

		$(window).resize(function()
		{
			posicionaPopup( '.popup' );
		});
	}

	$( 'input:text,textarea' ).focus( function()
	{
		var title = $(this).attr( 'title' );

		if( $( this ).attr( 'title' ) == $( this ).attr( 'value' ) )
		{
			$( this ).attr( 'value', '' );
		}
		$( this ).bind( 'blur', function()
		{
			if( $( this ).attr( 'value' ) == '' )
			{
				$( this ).attr( 'value', title );
			}
		})
	});

	if ($('a.colorbox').length)
	{
		$('a.colorbox').colorbox({
			maxHeight: '90%',
			maxWidth: '100%',
			current: 'imagem {current} de {total}',
			previous: 'anterior',
			next: 'próximo',
			close: 'fechar',
			slideShowStart: 'iniciar slideshow',
			slideshowStop: 'parar slideshow'
		});
	}

	function posicionaPopup( elemento )
	{
		$( elemento ).css( 'top', ( ($(window).height() / 2 ) - ( $( elemento ).height() / 2) ) );
		$( elemento ).css( 'left', ( ( $(window).width() / 2 ) - ( $( elemento ).width() / 2 ) ) );
	}

	function abrePopup( elemento )
	{
		$( '.sombra.hide' ).fadeTo( 'normal', 0.70 );
		$( '.sombra.hide' ).removeClass( 'hide' ).addClass( 'show' );
		$( elemento ).fadeIn();
		posicionaPopup( elemento );
	}

	function fechaPopup()
	{
		$( '.sombra.show' ).fadeTo( 'normal', 0 );
		$( '.sombra' ).removeClass( 'show' ).addClass( 'hide' );
		$( '.popup' ).fadeOut( 'slow' );
	}

	function verificaChat()
	{
		$.ajax({
			dataType: 'jsonp',
			url: ROOT + 'chat/status.php',
			crossDomain: true,
			jsonpCallback: 'retornaChat',
			cache: false,
			contentType: 'application/x-www-form-urlencoded'
		});
	}

	function retornaChat( data )
	{
		if(data.status)
		{
			window.open(ROOT + 'chat/chat.php','chatonline','location=no,statusbar=no,scrollbars=yes,menubar=no,width=500,height=500');
		}
		else
		{
			abrePopup( '.popup.chat' );
		}
	}

	function cadastrarNewsletter()
	{
		var nome	= $( '#newsletter .nome' ).val();
		var email	= $( '#newsletter .email' ).val();

		if(nome == 'Nome (*)' || email == 'Email (*)')
		{
			var args = '<p class="separado">Preencha os campos corretamente.</p>';

			$( '.popup.newsletter .mensagem' ).html( args );
			abrePopup( '.popup.newsletter' );
		}
		else
		{
			/*
			$.ajax({
				dataType: 'jsonp',
				url: 'http://maismail.maisemm.com.br/ws.php?form=17&CustomFields[2]=' + nome + '&email=' + email,
				crossDomain: true,
				jsonpCallback: 'imprimeResultado',
				cache: false,
				contentType: 'application/x-www-form-urlencoded'
			});
			*/
			var url = 'maismail.maisemm.com.br/ws.php?form=17&CustomFields[2]=' + nome + '&email=' + email;
			$.getJSON(url+"&callback=?", function(data) {
				imprimeResultado(data);
			});
		}
	}
	
	function htmlDecode(value)
	{ 
		return $('<div/>').html(value).text(); 
	}

	function imprimeResultado( data )
	{
		var mensagem = data.msg[0];
		if (mensagem.toString().length == 1)
		{
			mensagem = data.msg;
		}
		
		mensagem = htmlDecode(mensagem);
	
		var args = '<p class="separado">' + mensagem + '</p>';
		$( '.popup.newsletter .mensagem' ).html( args );
		abrePopup( '.popup.newsletter' );
		var nome	= $( '#newsletter .nome' ).val('Nome (*)');
		var email	= $( '#newsletter .email' ).val('Email (*)');
	}

	function localizaVideos( elemento )
	{
		elemento = elemento.replace( '#', '.' )
		var targetOffset = $( elemento ).offset().top;
		$( 'html, body' ).animate({scrollTop: targetOffset}, 1200);
	}

	if ($('body.front').length)
	{
		var i = setInterval( function()
		{
			var link = $( '#vitrine .menu li.active' );

			if( link.next( 'li' ).length )
			{
				passarVitrine(link.next( 'li' ));
			}
			else
			{
				passarVitrine($( '#vitrine .menu li:first' ));
			}
		}, 5000 );
		$('.menu li a', '#vitrine').click(function() {
			clearInterval(i);
		});

	}

	var base = $('.items', '#parceiros_scroll');
	var itens = $('.item.movel', '#parceiros_scroll');
	var quant = $('#parceiros_scroll').attr('visibleitems');
	if (itens.length > quant)
	{
		var callback = function(el) {
			$(el).appendTo(base);
			$(el).css('marginTop', 0);
			scrollParceiros();
		};

		var scrollParceiros = function(pixelsRemaining, item) {
			if (isNaN(pixelsRemaining))
			{
				pixelsRemaining = 0;
			}
			if (pixelsRemaining > 80)
			{
				callback(item);
				return;
			}
			pixelsRemaining = Math.abs(pixelsRemaining);
			var anim = 80 - pixelsRemaining;
			var percent = anim / 80;
			$('.items :first', '#parceiros_scroll').animate(
				{'marginTop' : '-=' + anim},
				2000 * percent,
				'linear',
				function() {
					callback(this);
				}
			);

		};
		scrollParceiros();

		base.hover(function() {
			$(this).find('.item').stop();
		}, function() {
			var item = $(this).find('.item:first');
			scrollParceiros(parseInt(item.css('marginTop')), item);
		});
	}
});

