var tinyMCEInitialized = false;
var configSlide = null;
var newPostSlide = null;
var newCommentSlide = null;

// Crea una nuova voce nel blog o modifica una esistente
function postBlog() {
	var editor1 = tinyMCE.get('editor1');
	editor1.setProgressState(1);
	if (document.newPostForm.idBlogEntry.value == '') { // nuovo post
		DWRBlogManager.post(document.newPostForm.title.value, editor1.getContent(),
			function() {
				document.newPostForm.idBlogEntry.value = '';
				document.newPostForm.title.value = '';
				editor1.setContent('');
				document.newPostForm.body.value = '';
				window.location.reload();
			});
	} else {
		DWRBlogManager.edit(document.newPostForm.idBlogEntry.value, document.newPostForm.title.value, editor1.getContent(),
			function() {
				document.newPostForm.idBlogEntry.value = '';
				document.newPostForm.title.value = '';
				editor1.setContent('');
				document.newPostForm.body.value = '';
				window.location.reload();
			});	
	}
	return false;
}

// Elimina una voce del blog
function removeBlog(idBlogEntry) {
	if (confirm("Sei sicuro di voler eliminare questo post ?")) {
		DWRBlogManager.remove(idBlogEntry,
			function() {
				window.location.reload();
			});
		postBlogHide();
		$("post-"+idBlogEntry).style.display = 'none';
	}
	return false;
}

// Edit di un post esistente
function editBlog(idBlogEntry) {
	var editor1 = tinyMCE.get('editor1');
	
	if (editor1.isHidden())
		editor1.show();

	document.newPostForm.idBlogEntry.value = idBlogEntry; // id
	document.newPostForm.title.value = $('blogSubject-'+idBlogEntry).innerHTML; // titolo	
	editor1.setContent($('blogContent-'+idBlogEntry).innerHTML, {format : 'raw'}); // body
	$('newPostTitle').setText('Edit Post');
	
	newPostSlideIn();
	return true;
}

// Mostra il form per scrivere nuovi post
function postBlogShow() {
	var editor1 = tinyMCE.get('editor1');
	
	if (editor1.isHidden())
		editor1.show();
		
	if (document.newPostForm.idBlogEntry.value != '') {
		document.newPostForm.idBlogEntry.value = ''; // id
		document.newPostForm.title.value = '';
		editor1.setContent('');
		document.newPostForm.body.value = '';
		$('newPostTitle').setText('Nuovo Post');
	}
	
	newPostSlideIn();
	return false;
}

// Nasconde il form per scrivere nuovi post
function postBlogHide() {
	newPostSlideOut();
	
	var editor1 = tinyMCE.get('editor1');
	editor1.hide();
	return false;
}

// Crea una nuova voce nel blog o modifica una esistente
function postComment(blogId) {
	var editor2 = tinyMCE.get('editor2');
	editor2.setProgressState(1);
	if (document.newCommentForm.idBlogComment.value == '') { // nuovo commento
		DWRBlogManager.postComment(blogId, editor2.getContent(),
			function() {
				document.newCommentForm.idBlogComment.value = '';
				editor2.setContent('');
				document.newCommentForm.body.value = '';
				window.location.reload();
			});
	} else {
		DWRBlogManager.editComment(document.newCommentForm.idBlogComment.value, editor2.getContent(),
			function() {
				document.newCommentForm.idBlogComment.value = '';
				editor2.setContent('');
				document.newCommentForm.body.value = '';
				window.location.reload();
			});	
	}
	return false;
}

// Elimina un commento del blog
function removeComment(idBlogComment) {
	if (confirm("Sei sicuro di voler eliminare questo commento ?")) {
		DWRBlogManager.removeComment(idBlogComment);
		$("comment-"+idBlogComment).style.display = 'none';
	}
	return false;
}

// Edit di un commento esistente
function editComment(idBlogComment) {
	var editor2 = tinyMCE.get('editor2');
	if (editor2.isHidden())
		editor2.show();

	document.newCommentForm.idBlogComment.value = idBlogComment; // id
	editor2.setContent($('commentContent-'+idBlogComment).innerHTML, {format : 'raw'}); // body
	$('newCommentTitle').setText('Modifica Commento');
	
	newCommentSlideIn();
	return true;
}

// Mostra il form per scrivere nuovi commenti
function postCommentShow() {
	var editor2 = tinyMCE.get('editor2');
	if (editor2.isHidden())
		editor2.show();

	if (document.newCommentForm.idBlogComment.value != '') {
		document.newCommentForm.idBlogComment.value = ''; // id
		editor2.setContent('');
		document.newCommentForm.body.value = '';
		$('newCommentTitle').setText('Nuovo Commento');
	}
	
	newCommentSlideIn();
	return false;
}

// Nasconde il form per scrivere nuovi commenti
function postCommentHide() {
	newCommentSlideOut();
	
	var editor2 = tinyMCE.get('editor2');
	editor2.hide();
	return false;
}

// Mostra il form per configurare il blog
function configShow() {
	configSlideIn();
	return false;
}

// Nasconde il form per configurare il blog
function configHide() {
	configSlideOut();
	return false;
}

// Memorizza la configurazione del blog
function saveConfig() {
	DWRBlogManager.saveConfig(
		document.configForm.showFriends.checked ? "1" : "0",
		document.configForm.showComments.checked ? "1" : "0",
		document.configForm.backgroundColor.value,
		document.configForm.backgroundUrl.value,
		document.configForm.backgroundFixed.value,
		document.configForm.backgroundRepeat.value,
		document.configForm.css.value,
			function() {
				window.location.reload();
			});	
	return false;
}

// --------------------------------------------------------------------------------------------------------------------

function getConfigSlide() {
	if ((configSlide == null) && ($('config') != null)) {
		configSlide = new Fx.Slide('config');
		$('config').style.display = 'block';
	}
	return configSlide;	
}

function configSlideIn() {
	var c = getConfigSlide();
	if (c != null)
		c.slideIn();
}

function configSlideOut() {
	var c = getConfigSlide();
	if (c != null)
		c.slideOut();
}

function configHide() {
	var c = getConfigSlide();
	if (c != null)
		c.hide();
}

// --------------------------------------------------------------------------------------------------------------------

function getNewPostSlide() {
	if ((newPostSlide == null) && ($('newPost') != null)) {
		newPostSlide = new Fx.Slide('newPost');
		$('newPost').style.display = 'block';
	}
	return newPostSlide;
}

function newPostSlideIn() {
	var c = getNewPostSlide();
	if (c != null)
		c.slideIn();
}

function newPostSlideOut() {
	var c = getNewPostSlide();
	if (c != null)
		c.slideOut();
}

function newPostHide() {
	var c = getNewPostSlide();
	if (c != null)
		c.hide();
}

// --------------------------------------------------------------------------------------------------------------------

function getNewCommentSlide() {
	if ((newCommentSlide == null) && ($('newComment') != null)) {
		newCommentSlide = new Fx.Slide('newComment');
		$('newComment').style.display = 'block';
	}
	return newCommentSlide;
}

function newCommentSlideIn() {
	var c = getNewCommentSlide();
	if (c != null)
		c.slideIn();
}

function newCommentSlideOut() {
	var c = getNewCommentSlide();
	if (c != null)
		c.slideOut();
}

function newCommentHide() {
	var c = getNewCommentSlide();
	if (c != null)
		c.hide();
}

// --------------------------------------------------------------------------------------------------------------------

// Se necessario, inizializza TinyMCE
function tinyMCEInit() {	
	if (!tinyMCEInitialized && (($('newPost') != null) || ($('newComment') != null))) {
		tinyMCE.init({
			mode : "exact",		
			elements : "editor1,editor2",		
			theme : "advanced",
			plugins : "safari,style,table,save,emotions,contextmenu,media",
			
			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent",
			theme_advanced_buttons2 : "formatselect,fontselect,fontsizeselect,|,forecolor,backcolor",
			theme_advanced_buttons3 : "undo,redo,|,link,unlink,anchor,image,media,emotions,|,cleanup,help,code,|,styleprops",
			theme_advanced_buttons4 : "tablecontrols,|,hr,removeformat,visualaid",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			width: 310
		});
		tinyMCEInitialized = true;
	}
}

// Init
function blogOnLoad() {
  
  configHide();
  newPostHide();
  newCommentHide();
  tinyMCEInit();

  if ($('backgroundColor') != null) {
    var startColor = new Color($('backgroundColor').value);
    rainbow = new MooRainbow('myRainbow', {
		imgPath: '/img/mooRainbow/',
		'startColor': startColor,
		'onChange': function(color) {
			$('backgroundColor').value = color.hex;
		}
    });
  }
}

// --------------------------------------------------------------------------------------------------------------------
// Show player 

function sms(sendto) {
	var s = open('/virtual_cell_telefono.ktml?sendto='+sendto,'VirtualSMS','width=272,height=390,toolbar=no,directories=no,menubar=no,scrollbars=no');
	s.focus();
}

function submitVote() {
	var url = "/@vota_inserisci";
	var param = "simpatia="+document.form1.simpatia.value+"&look="+document.form1.look.value+"&sendto="+document.form1.sendto.value;
	var myXHR = new XHR({
		autoCancel: true,
		headers: {'X-Request':'JSON'},
		method:"get",
		onSuccess: function(res){submitVoteOnSuccess(Json.evaluate(res));}
	});
	myXHR.send(url, param);
}

function submitVoteOnSuccess(res) {
	$('vote-msg').setHTML(res.mesg);
	if (res.hideSubmitButton) {
		$('vote-submit').style.display = 'none';
	}
}

//Associa la funzione blogOnLoad all'evento onload del tag body della pagina
window.onload = function(){blogOnLoad();};