$(document).ready(function(){

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

	/*respostes de la superpregunta*/
	$('.sp_resp div').hide();
	$('.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'));
			}
		});
	};


	/*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");
			}

		});

	});

	/*formulari afegir fremsa*/
	if ($("form#afegirpremsa").length > 0) {
		$("form#afegirpremsa").hide();
//		$("form#btafegirpremsa").bind('click', function())

		$('a#btafegirpremsa').click(function(){
			$("a#btafegirpremsa").toggle();
			$("form#afegirpremsa").toggle();
			return false;
		});

	};

	if ($("select.chzn-select").length > 0) {
//		$("select.chzn-select").chosen();
		$("select.chzn-select").ajaxChosen({
			method: 'GET',
			url: '/llibres/ajaxllistat',
			dataType: 'json'
		}, function (data) {
			var terms = {};

			$.each(data, function (i, val) {
				terms[i] = val;
			});

			return terms;
		});


	};


});

