jQuery.ajaxSetup({
    'beforeSend' : function(xhr) {
        xhr.setRequestHeader('Accept', 'text/javascript')
        }
})
/*
function redirect()
{
	alert('Mensagem enviada com sucesso!');
	
	window.location = $('input#url').val();
}
*/

$(function(){
	//alert($(window).scrollTop());
	$(window).scroll(function(){
		$('div#boxAlert').css({
			'top' : $(window).scrollTop() + 'px',
		});
	});
});

function boxAlert(alerta)
{
	//alert('teste');
	
	$('body').append('<div id="boxAlert"><div></div><h2><table width="350" height="50" cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle">' + alerta + '</td></tr></table></h2></div>');
	
	$('div#boxAlert').css({
		'width'			: '100%',
		'height'		: '100%',
		'top'			: $(window).scrollTop() + 'px',
		'left'			: '0px',
		'position'		: 'absolute',
		'z-index'		: '99990',
		
		'display'		: 'none'
	});
	
	$('div#boxAlert > div').css({
		'width'				: '100%',
		'height'			: '100%',
		'top'				: '0px',
		'left'				: '0px',
		'background'		: 'black',
		'position'			: 'absolute',
		
		'-ms-filter'		: '"progid:DXImageTransform.Microsoft.Alpha(Opacity=70)"',
		'filter'			: 'alpha(opacity=70)',
		'-moz-opacity'		: '0.7',
		'-khtml-opacity'	: '0.7',
		'opacity'			: '0.7',
		
		'z-index'			: '99991'
	});
	
	$('div#boxAlert > h2').css({
		'width'						: '350px',
		'height'					: '50px',
		
		'margin'					: '-75px -225px',
		
		'top'						: '50%',
		'left'						: '50%',
		'background'				: 'white',
		
		'padding'					: '50px',
		
		'position'					: 'absolute',
		
		'border-radius'				: '10px',
		'-webkit-border-radius'		: '10px',
		'-moz-border-radius'		: '10px',
		
		'color'						: 'black',
		
		'color'						: '#E59C31',
		'font-family'				: '"GillSansMTProBook",Arial,Sans-serif',
		'font-size'					: '16px',
		
		'text-align'				: 'center',
		
		'position'					: 'relative',
		
		'z-index'					: '99992'
	});
	
	//$('body, html').css({
	//	'overflow' : 'hidden'
	//});
	
	$('div#boxAlert').show('fast');
	
	setTimeout(removeBoxAlert, 4000);
	
}

function removeBoxAlert()
{
	//$('body, html').attr('style' , '');
	
	$('div#boxAlert').fadeOut('slow', function(){
		$(this).remove();
	});
}

function redirect()
{
	window.location = $('input#url').val();
}

jQuery.fn.submitWithAjax = function(block){
    this.submit(function(){
        var self = $(this);
        var error = '';

        $('input, textarea', self).each(function(){
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('title')){
                error = 'Todos os campos são obrigatórios.';
                return false;
            }

        });
		
        if (error != ''){
            //alert(error);
			boxAlert(error);
			$('input, textarea', self).blur();
        }
		
        else{
            /*if (block){
               $.blockUI({ message: '<h2>Aguarde...</h2>', css: { border: 'none', padding: '15px', backgroundColor: '#000', opacity: .5, color: '#fff'}});
			   //alert('Aguarde...')
			   boxAlert('Aguarde...');
            }*/
            
			$.post(self.attr('action'), self.serialize(), null, 'script');
			
			if(block == true)
			{
				removeBoxAlert();
				boxAlert('Sua mensagem foi enviada com sucesso.<br />Em breve entraremos em contato');
				//setTimeout(redirect, 4000);
				
			}
			
        }
        return false;
    });
};

$(function(){
    $('.input-magic').label_magic();
    
    $('#new_search').submit(function(){
       var term = $(this).children('input').first().val();
       if (term == '') {
          // alert('Informe um valor para a busca.');
		   boxAlert('Informe um valor para a busca.');
           return false;
       }
       if (term.length < 3) {
		   boxAlert('Informe uma palavra com pelo menos 3 caracteres.');
           //alert('Informe uma palavra com pelo menos 3 caracteres.');
           return false;
       }
    });
    $('form.balaforms').bind('open-modal', function()
    {
            boxAlert('Sua mensagem foi enviada com sucesso.<br />Em breve entraremos em contato.');
            return false;
    });
});

function checkChat(){
	$.ajax({ url: "actions/contadorchat.php" })
}
