$(function() {
	
	window.checkChatStatus = function() {
		$.ajax({
			url: "http://economizenoseguro.com.br/ajax/chat_status_helper.php", 
			dataType: 'html',
			success: function(html) {
				if (html) {
					if (/\<a href/.test(html)) {
						$("#chat-status").html("online").parent().click(openChat);
					} else {
						$("#chat-status")
							.html("offline").parent()
							.unbind("click", openChat)
							.click(function (e) { e.preventDefault(); return false; });
					}
				}
			}
		});
		setTimeout("checkChatStatus();", 300000); //5 min
	};
	
	$("#contact-channels .chat-icon a")
		.data("defHref", $("#contact-channels .chat-icon a").attr("href"))
		.attr("href", "javascript:void(0)");

	if (window.location.hash != '') {
		hash = window.location.hash.split("#")[1];
		$("#img").attr('class', '').addClass(hash);
	}
	
	$.getScript('scripts/jquery.plugins.js', function() {
		$('head').append('<link rel="stylesheet" type="text/css" media="screen" href="styles/jquery.plugins.css">');
		
		
		if ($("#contato").length > 0 && $("#errors-box").length < 1) {
			$('#contact-form').prepend("<div class='errors-box'><span>Corrija os seguintes erros:</span><ul class='errors-list'></ul></div>");
		}
		
		$("#contact-form").validate({
			rules: {
				client_name: 'required',
				client_mail: { required: true, email: true },
				client_message: 'required'
			},
			messages: {
				client_name: { required: 'Digite o seu nome' },
				client_mail: {
					required: 'Digite o seu e-mail',
					email: 'E-mail inválido'
				},
				client_message: { required: 'Digite sua mensagem' }
			},
			errorElement: 'strong',
			errorLabelContainer: ".errors-list",
			errorContainer: '.errors-box',
  			wrapper: "li"
		});
		
		
		$.mask.masks.decimal = {mask: '9', type: 'repeat'};
		$("#client_phone, #client_mobile").attr('alt', 'phone').setMask(); //Alt attribute applies the mask
		$("#client_address_zip").attr('alt', 'cep').setMask();
		$("#client_address_num").attr('alt', 'decimal').setMask();

	});
	
	checkChatStatus();
});
