function setStatus() {
	$.ajax({
		url: "user/setuserstatus/",
		cache: false,
		type: "POST"
	});
}

//setInterval("setStatus()", 5000)

$(document).ready(function() {
	/*
	 * Chiamata per togliere il box d'aiuto dopo la registrazione
	 */
	$("#howToExitLinkA").bind('click', function() {
		$.getJSON('/user/leavehelpmessage/', function(data){
			$("#howToLink").hide(500);
		});
	});

	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;
	});
	
	$(".deleteRequest").bind('click', function() {
		if (confirm("Sei sicuro di cancellare la Richiesta?")) {
	    	$.getJSON($(this).attr('deleteRequestUrl'), function(data){
	    		$dialog.dialog('option', 'title', $(this).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);
	});

try {
	$("#editor").ckeditor({
		toolbar: [['Bold','Italic','Underline','Smiley','TextColor','BGColor','Styles','Font','FontSize']]
	});
	
	$("#editorSmall").ckeditor({
		toolbar: [['Bold','Italic','Underline','Smiley','TextColor','BGColor','Styles','Font','FontSize']], 
        height: '100', 
        toolbarStartupExpanded: false
	});
} catch (e) {
	
}

	$("#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 setFriendRequest(friendId, status, checkReq) {
	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);

			if (data.totFriendRequests == 0 && checkReq == true) {
				setTimeout(function() {
					$("#friendReqLogContainerGeneral").html(data.messageNoReq);
				}, 8000);
			}

			if (data.hideBox == true) {
				setTimeout(function() {
					$("#friendReqLogContainer"+friendId).hide('blind', {}, 3000)
				}, 5000);
			}

			setTimeout(function() {
				$("#friendReqLog"+friendId).hide('blind', {}, 3000);
			}, 5000);
		});
	}
	return false;
}

function spamMessage(msgId, type) {
	if (confirm("Sei sicuro di segnalare questo messaggio come Spam?")) {
		if (type == 'cronache') {
			$.ajax({
				type: "POST", 
				url: "/cronache/spamblogmessage", 
				data: "msgId=" + msgId,
				dataType: "html",
				complete: function(data) {
					document.getElementById("spamLink" + msgId).innerHTML = data.responseText
				}
			});
		} else if (type == 'bacheca') {
			$.ajax({
				type: "POST", 
				url: "/bacheca/spamblogmessage", 
				data: "msgId=" + msgId,
				dataType: "html",
				complete: function(data) {
					document.getElementById("spamLink" + msgId).innerHTML = data.responseText
				}
			});
		} else if (type == 'message') {
			
		}
	}
	return false;
}

function deleteMessage(msgId, type) {
	if (confirm("Sei sicuro di Cancellare questo messaggio?")) {
		if (type == 'cronache') {
			$.ajax({
				type: "POST", 
				url: "/cronache/deleteblogmessage", 
				data: "msgId=" + msgId,
				dataType: "html",
				complete: function(data) {
					document.getElementById("deleteLink" + msgId).innerHTML = data.responseText
				}
			});
			setTimeout(function() {
				$("#messageContainer" + msgId).hide('blind', {}, 3000)
			}, 5000);
		} else if (type == 'bacheca') {
			$.ajax({
				type: "POST", 
				url: "/bacheca/deleteblogmessage", 
				data: "msgId=" + msgId,
				dataType: "html",
				complete: function(data) {
					document.getElementById("deleteLink" + msgId).innerHTML = data.responseText
				}
			});
			setTimeout(function() {
				$("#messageContainer" + msgId).hide('blind', {}, 3000)
			}, 5000);
		} else if (type == 'message') {
			$.getJSON('/message/delete/id/'+msgId, function(data){
				$("#log_"+msgId).html(data.message);
			});
			setTimeout(function() {
				$("#msg_" + msgId).hide('blind', {}, 3000)
			}, 5000);
		}
	}
	return false;
}

function deleteMember(cronacaId, userId, type) {
	$.getJSON('/cronache/deleterequest/cronacaId/' + cronacaId + '/userId/' + userId + '/type/' + type, function(data){
		$("#log_" + userId).html(data.message);
	});
	setTimeout(function() {
		$("#playerBox" + userId).hide('blind', {}, 3000)
	}, 5000);
	return false;
}

function clearText(theField) {
	if (theField.defaultValue == theField.value) {
		theField.value = '';
	}
}

function addText(theField) {
	if (theField.value == '') {
		theField.value = theField .defaultValue;
	}
}

