$(document).ready(function() {
	var $dialog = $('<div></div>').dialog({
		autoOpen: false,
		width: '500px', 
		position: 'center'
	});
	
    $(".xxx").bind('click', function() {
    	crnId = $(this).attr('crnId');
    	$.get($("#inviteFriendsToCronaca"+crnId).attr('inviteFriendsToCronacaUrl'), function(data){
    		$dialog.dialog('option', 'title', $("#inviteFriendsToCronaca"+crnId).attr('title'));
    		$dialog.dialog('open').html(data);
    	});
    });

    $(function() {
    	$(".column").sortable({
    		connectWith: '.column', 
    		items: $(".portlet"), 
    		//axis: 'y', 
    		cursor: 'move',
    		revert: true, 
    		placeholder: 'dropMeHere', 
    		tolerance: 'pointer'
    	});
    	$(".column").disableSelection();
    });

    $("#cronacaRequestFirst").bind('click', function() {
    	$.getJSON($("#cronacaRequestFirst").attr('cronacaRequestUrlFirst'), function(data){
    		$dialog.dialog('option', 'title', $("#cronacaRequestFirst").attr('title'));
    		$dialog.dialog('open').html(data.message);
    	});
    });

    $("#cronacaRequest").bind('click', function() {
    	$.getJSON($("#cronacaRequest").attr('cronacaRequestUrl'), function(data){
    		$dialog.dialog('option', 'title', $("#cronacaRequest").attr('title'));
    		$dialog.dialog('open').html(data.message);
    	});
    });

	$("#cancelRequest").bind('click', function() {
		if (confirm("Sei sicuro di volerti cancellare da questa Cronaca? dopo potrai re inoltrare la richiesta di partecipazione.")) {
	    	$.getJSON($("#cancelRequest").attr('cancelRequestUrl'), function(data){
	    		$dialog.dialog('option', 'title', $("#cancelRequest").attr('title'));
	    		$dialog.dialog('open').html(data.message);
	    	});
		}
		return false;
	});

	$("#addFriend").bind('click', function() {
    	$.getJSON($("#addFriend").attr('friendUrl'), function(data){
    		$dialog.dialog('option', 'title', $("#addFriend").attr('title'));
    		$dialog.dialog('open').html(data.message);
    	});
    });

	$(function() {
		setTimeout(function() {
			$("#flashMessage").hide('blind', {}, 3000)
		}, 5000);
	});

	$("#editor").ckeditor({
		toolbar: [['Bold','Italic','Underline','Smiley','TextColor','BGColor','Styles','Font','FontSize']]
	});

	$("#username").bind('keyup', usernameCheck);
	$("#email").bind('keyup', emailCheck);

	$("#profileEdit").tabs({});
	
	$("#sendMessageLink").bind('click', function() {
		/*jQuery("#sendMessageLink").load(jQuery(this).attr("href")).dialog({
			modal: true,
			autoOpen: true,
			width: '485px',
			position: 'center', 
			overlay: {opacity: 0.5, background: "black"}, 
			title: jQuery(this).attr("title")
		});
		return false;*/
		$('#friendAddLog').html(load($.attr('href')));
		return false;
    });
	
	$('a.highslide').click(function() {
		return hs.expand(this, options);
	});

});

// Functions

function usernameCheck() {
    var username = $("#username").val();
    {
	    postObject = new Object;
	    postObject.username = username;
	    $.post('/user/checkUsernameAjax/', postObject, 
	      function(data){
	        if (data.valid) {
	            $("#username_help").html('');            
	        } else {
	            $("#username_help").html('Questo Username &egrave; gi&agrave; preso');
	        }
	      }, "json" );
	 }
}

function emailCheck() {
    var email = $("#email").val();
    {
        postObject = new Object;
        postObject.email = email;
        $.post('/user/checkEmailAjax/', postObject, 
          function(data){
            if (data.valid) {
                $("#email_help").html('');            
            } else {
                $("#email_help").html('Questa Email &egrave; gi&agrave; registrata');
            }
          }, "json" );
     }
}

function sendFriendRequest(logId, url) {
	$.getJSON(url, function(data){
		$("#log_"+logId).html(data.message);
	});

	setTimeout(function() {
		$("#log_" + logId).hide('blind', {}, 3000)
	}, 5000);
}

function deleteMessage(logId, url) {
	$.getJSON(url, function(data){
		$("#log_"+logId).html(data.message);
	});

	setTimeout(function() {
		$("#msg_" + logId).hide('blind', {}, 3000)
	}, 5000);
}

function setFriendRequest(friendId, status) {
	if (status == 'approved' || ((status == 'deleted' || status == 'ignorated') && confirm("Sei sicuro di eliminare questo amico?"))) {
		$.getJSON('/friends/setstatus/friendId/'+friendId+'/status/'+status, function(data){
			$("#friendReqLog"+friendId).html(data.message);
		});
		setTimeout(function() {
			$("#friendReqLog"+friendId).hide('blind', {}, 3000)
		}, 5000);
	}
	return false;
}
