(function(b) { b.fn.extend({ counter: function(a) { a = b.extend({}, { type: "char", count: "down", goal: 140 }, a); var d = false; return this.each(function() { function e(c) { if (typeof a.type === "string") switch (a.type) { case "char": if (a.count === "down") { g = " Caracteres"; return a.goal - c } else if (a.count === "up") { g = " Caracteres (" + a.goal + " max)"; return c } break; case "word": if (a.count === "down") { g = " Palabras"; return a.goal - c } else if (a.count === "up") { g = " Palabras (" + a.goal + " max)"; return c } break; default: } } var g, f = b(this); b('<div class="textarea_counter" id="' + this.id + '_counter"><span>' + e(b(f).val().length) + "</span>" + g + "</div>").insertAfter(f); var i = b("#" + this.id + "_counter span"); f.bind("keyup click blur focus change paste", function(c) { switch (a.type) { case "char": c = b(f).val().length; break; case "word": c = f.val() === "" ? 0 : b.trim(f.val()).replace(/\s+/g, " ").split(" ").length; break; default: } switch (a.count) { case "up": if (e(c) >= a.goal && a.type === "char") { b(this).val(b(this).val().substring(0, a.goal)); d = true; break } if (e(c) === a.goal && a.type === "word") { d = true; break } else if (e(c) > a.goal && a.type === "word") { b(this).val(""); i.text("0"); d = true; break } break; case "down": if (e(c) <= 0 && a.type === "char") { b(this).val(b(this).val().substring(0, a.goal)); d = true; break } if (e(c) === 0 && a.type === "word") d = true; else if (e(c) < 0 && a.type === "word") { b(this).val(""); d = true; break } break; default: } f.keydown(function(h) { if (d) { this.focus(); if (h.keyCode !== 46 && h.keyCode !== 8) if (b(this).val().length > a.goal && a.type === "char") { b(this).val(b(this).val().substring(0, a.goal)); return false } else return h.keyCode !== 32 && h.keyCode !== 8 && a.type === "word" ? true : false; else { d = false; return true } } }); i.text(e(c)) }) }) } }) })(jQuery);
$(document).ready(function(){
	$("textarea[max_length]").counter({
		type: 'char',
		count: 'up',
		goal: 255
	});
	$('#v_formulario_producto').click(function(){
		$('.formulario_producto').slideToggle();
	});
	$('.send').live('click',function() {
		form = $(this).parent();
		id = $(this).attr('id');
		contenedor = form.parent();
		activeStart(contenedor);
		sendForm(form,contenedor,id);
		return false;
	});
});
function sendForm(form,contenedor,id){
	contenedor.children('#msg').remove();
	$.ajax({
		type: "POST",
		url: form.attr('action'),
		data: form.serialize(),
		success: function(msg){
			contenedor.html(msg);
			if($('.star').length > 0){
				$('.star').rating();
			}
			if(form.find('textarea').attr('max_length')){
				$("textarea[max_length]").counter({
					type: 'char',
					count: 'up',
					goal: 255
				});
			}
		}
	});
}

function activeStart(contenedor){
	divLoad = contenedor.children("#loading");
	divLoad.ajaxStart(function(){
		$(this).unbind('click',sendForm);
		divLoad.show();
	});
	divLoad.ajaxComplete(function(){
		divLoad.hide();
	});
}
