$(document).ready(function(){
	/*menus*/
	$('#menu li.level1').hover(
		function() { $('ul', this).css('display', 'block'); },
		function() { $('ul', this).css('display', 'none'); }
	);

	/*textareas*/
	$().maxlength();

	/*respostes de la superpregunta*/
	$('.sp_resp button').click(function(){
		$(this).parent().find('div').toggle();
	});

	/*validació de formularis sense missatge*/
	if ($("form.validate").length > 0) {
		$("form.validate").each(function(i){
			$(this).validate({
				errorPlacement: function(error,element) {
					return true;
				}
			});
		});
	}
	/*validació de formularis amb missatge*/
	if ($("form.validate2").length > 0) {
		$("form.validate2").each(function(i){
			$(this).validate();
		});
	}

	/*validació superpregunta*/
	if ($("form#formadd").length > 0) {
		$("form#formadd").validate({
			errorPlacement: function(error, element) {
				error.insertAfter(element.closest('div'));
			}
		});
	};

	/*average*/
	$(".average").each(function(i){
		$this = $(this);
		value = $this.find('.value').text();
		$this.append('<div class="stars-off" style="width:160px"><div class="stars-on"></div></div>');
		$this.find(".stars-on").width(Math.round( $this.find(".stars-off").width() / 10 * parseFloat(value) ))
	});
	$(".average2").each(function(i){
		$this = $(this);
		value = $this.text();
		$this.html('<div class="stars-off" style="width:160px"><div class="stars-on"></div></div>');
		$this.find(".stars-on").width(Math.round( $this.find(".stars-off").width() / 10 * parseFloat(value) ))
	});

	/*votacions*/
	$(".rating").each(function(i){
		var $this = $(this);
		$this.children().not("select").hide();
		var action = $this.attr('action');
		$this.stars({
			oneVoteOnly: true,
			cancelShow: false,
			split: 2,
			inputType: "select",

			callback: function(ui, type, rate)
			{
				$.post(action, {value: rate}, function(json)
				{
				}, "json");
			}

		});

	});

});
