Cufon.replace('h1.amltitle');
Cufon.replace('h2.amltitle');
Cufon.replace('h3.amltitle');

$(function(){
	$("#nav > li").fixHover();
	$("#nav ul").fixLastItem("li");
	
	$("#header form .text").handleInputFocus();
	
	$("#contact-form .clear-bt").click(function(){
		$("#contact-form .text").val("");
		return false;
	});
	
	$("#comments-list").fixLastItem(".comment");
	
	$("#new-comment form .clear-bt").click(function(){
		$("#new-comment form .text").val("");
		$("#new-comment form textarea").blur();
		return false;
	});
	
	$("#new-comment textarea").setMaxLength({
		maxchar: 1000,
		defaultphrase: ""
	});
});

// Hover plugin
//
// Adds .hover class on elements hover
/////////////////////////////////////////////////////////////////////////////////
(function($){
	$.fn.extend({
		fixHover: function() {
			return this.each(function() {
				$(this)
					.mouseenter(function(){
						$(this).addClass("hover");
					})
					.mouseleave(function(){
						$(this).removeClass("hover");
					});
			});
		}
	});
})(jQuery);

// Last items plugin
//
// Fixes lists last items
/////////////////////////////////////////////////////////////////////////////////
(function($){
	$.fn.extend({
		fixLastItem: function(child) {
			return this.each(function() {
				$(this).find(child+":last-child").addClass("last");
			});
		}
	});
})(jQuery);

// Input focus plugin
//
// Deletes default text on input fields and adds class on focus
/////////////////////////////////////////////////////////////////////////////////
(function($){
	$.fn.extend({
		handleInputFocus: function() {
			return this.each(function() {
				var text = $(this).attr("title");

				$(this).data("default_text", text);
				$(this)
					.focus(function(){
						if($(this).val() == $(this).data("default_text")) {
							$(this).val("");
						}
						$(this).removeClass("default");
						$(this).addClass("focus");
					})
					.blur(function(){
						if($(this).val() == "" || $(this).val() == undefined) {
							$(this).val($(this).data("default_text"));
							$(this).addClass("default");
						}
						$(this).removeClass("focus");
					});
			});
		}
	});
})(jQuery);

// Char counter plugin
//
// Limits the char quantity of a textarea or input
/////////////////////////////////////////////////////////////////////////////////
(function($){
	$.fn.extend({
		setMaxLength: function(options) {
			//Settings list and the default values
			var defaults = {
				maxchar: 100,
				delay: 10,
				container: ".char-counter",
				counter: ".chars",
				phrase: ".phrase",
				singularphrase: "caracter restante",
				pluralphrase: "caracteres restantes",
				fullphrase: "Você alcançou o limite de caracteres",
				defaultphrase: "Digite aqui a sua mensagem:"
			};

			var opt = $.extend(defaults, options);

			return this.each(function() {
				if($(this).is("input") || $(this).is("textarea")) {
					var textarea = this;
					var checkerTimer;

					var checker = function() {
						var text = textarea.value;

						if(!text.length){
							$(opt.container).find(opt.counter).text(opt.maxchar);
							$(opt.container).find(opt.phrase).text(opt.pluralphrase);
						} else {
							if (text == opt.defaultphrase) {
								$(opt.container).find(opt.counter).text(opt.maxchar);
								$(opt.container).find(opt.phrase).text(opt.pluralphrase);
							} else if (opt.maxchar - text.length == 1) {
								$(opt.container).find(opt.counter).text("1");
								$(opt.container).find(opt.phrase).text(opt.singularphrase);
							} else if (text.length == opt.maxchar) {
								$(opt.container).find(opt.counter).text("");
								$(opt.container).find(opt.phrase).text(opt.fullphrase);
							} else if (text.length > opt.maxchar) {
								textarea.value = text.substr(0, opt.maxchar);
								$(opt.container).find(opt.counter).text("");
								$(opt.container).find(opt.phrase).text(opt.fullphrase);
							} else {
								$(opt.container).find(opt.counter).text(opt.maxchar - text.length);
								$(opt.container).find(opt.phrase).text(opt.pluralphrase);
							}
						}
					}

					var checkerReset = function() {
						clearTimeout(checkerTimer);
						checkerTimer = setTimeout(checker, opt.delay);
					}

					$(this)
						.focus(checkerReset)
						.blur(checkerReset)
						.keypress(checkerReset)
						.keydown(checkerReset)
						.keyup(checkerReset);

					checker();
				}
			});
		}
	});
})(jQuery);