$(function() {
	
	
	
	/* COMMENTI */
	// Mostra tutti
	$('.commenti-mostraTutti').live('click', function() {
		$(this).parents('.bottom').find('li').show();
		$(this).parents('li').remove();
		return false;
	});
	
	// Show/Hide nuovo commento
	$('.commento-nuovoFake').live('click', function() {
		$(this).hide().next().show().find('textarea:last').focus();
	});
	$('.commento-nuovo').live('blur', function() {
		if ($(this).find('textarea').val().length == 0)
		{
			$(this).hide().prev().show();
		}
	});
	$('.commento-commenta').live('click', function() {
		$(this).parents('.bottom').find('.commento-nuovo-li').show();
		$(this).parents('.bottom').find('.commento-nuovoFake').hide().next().show().find('textarea').focus();
		return false;
	});
	
	
	
	/* LIKE */
	// Like-Unlike
	$('.iLikeIt').live('click', function(a) {
		$.get(a.currentTarget.href, function(data) {
			$('body').append('<div id="temp"></div>');
			var temp = $('#temp').html(data);
			$(a.currentTarget).closest('.bottom').find('.like-box-container').replaceWith(temp.find('.like-box-container'));
			$(a.currentTarget).replaceWith(temp.find('.iLikeIt'));
			$('#temp').remove();
		});
		return false;
	});
	
	// Dialog
	$('.whoLikesIt').live('click', function(a) {
		$.get(a.currentTarget.href, function(data) {
			$('body').append(data);
			$('#whoLikes').dialog({ close: function() { $('#whoLikes').remove() } });
		});
		return false;
	});
	
	
	
	/* FAN */
	//Become Fan - Unbecome Fan
	$('.becomeFan, .unbecomeFan-link').live('click', function(a) {		
		$.get(a.currentTarget.href, function(data) {
			$(a.currentTarget).parents('.fanButton').replaceWith(data);
		});
		return false;
	});
	
	// Dialog
	$('.whoIsFan').live('click', function(a) {
		$('#whoIsFan-content').load(a.currentTarget.href, function() {
			$('#whoIsFan').dialog();
		});
		return false;
	});
	
	
	
	// Textarea
	$('textarea').elastic();
	
	
	
	// MENU EDITORE
	$('.oggetto').live('mouseover mouseout', function(event) {
  	if (event.type == 'mouseover') {
			$(this).children('.menuEditore').show();
		} else {
			$(this).children('.menuEditore').hide();
		}
	});
	
	// Elimina
	/*$('.elimina').live('click', function() {
		var elemento = $(this).closest('.oggetto');
		var url = $(this).attr('href');
		$('#elimina-dialog').dialog({
			modal: 				true,
			width: 				400,
			open: 				function() {
				$('.ui-dialog *:focus').blur();
				$(this).find('.dialog-conferma').click(function() {
					if (elemento.length != 0)
					{
						$.get(url, function() {
							$(elemento).fadeOut();
							$(dialog).dialog('close');
						});
					} else {
						console.log('ciao');
						window.location = url;
					}
				});
				var dialog = this;
				$(this).find('.dialog-close').click(function() {
					$(dialog).dialog('close');
				});
			}
		});
		return false;
	});*/
	
	
	
	// AJAX FORM
	$('.ajaxForm').submit(function() {
		var form = this;
		$(this).ajaxSubmit({ 
			success: function(responseText, statusText, xhr, $form) {
				$(form).parents('ul').find('li:last').before(responseText);
				$(form).find('textarea').focus().val('');
			}
		});
		return false;
	}); 
	$('.form-ajax').submit(function() {
																	console.log('ciao');
		var form = this;
		$(this).ajaxSubmit({ 
			success: function(responseText, statusText, xhr, $form) {
				$(form).replace(responseText);
			}
		});
		return false;
	}); 
	
	
	
	// Dialog Defaults
	$.extend(
		$.ui.dialog.prototype.options, {
			autoOpen: 			true,
			bgiframe: 			true,
			closeOnEscape: 	true,
			draggable:			false,
			hide: 					'fade',
			minHeight:			50,
			modal: 					false,
			resizable: 			false,
			width: 					500,
			open: 					function() {
				$('.ui-dialog *:focus').blur();
				var dialog = this;
				$(this).find('.dialog-close').click(function() {
					$(dialog).dialog('close');
				});
			}
	});

	
							
});
