$ = jQuery.noConflict(false);
$(document).ready(function($){
	
	// impostiamo gli effetti di colore per le locations
	$("#locations li").hover(
		function(){$(this).toggleClass('locationHover');},
		function(){$(this).toggleClass('locationHover');}
	).change(function(){
		$("#locations li").removeClass('locationSelected');
		$("#locations li :checked").parent().parent().addClass('locationSelected');
	});


	
	$("#navigation a").corner(5);
	$("#messagebox").corner(5);
	
	$(document).oneTime(3000,function(){msg("Suggerimento: puoi continuare a scrivere anche se ti scolleghi");});

	$("#iscrizione-form").validate({
	  rules: {
	    eman: {
	      required: true,
		  minlength: 3
	    },
	    liame: {
	      required: true,
	      email: true
	    }		
	  },
	  messages: {
		eman: "*",
		liame: "*"
	  }
	});	
	
  $('.tips').cluetip({
    cluetipClass: 'jtip',
    splitTitle: '|',
	arrows: true, 
	dropShadow: false,
	hoverIntent: true,
	sticky: false,
    mouseOutClose: true,
    showTitle: false
  });
	
	
});

$(document).everyTime(15000, 'autoSave', function(i) {
  salvataggioAutomatico();
});

var msgBox = null;

function salvataggioAutomatico(){

	var oEditor = FCKeditorAPI.GetInstance('edit-area') ;
	if(!oEditor.IsDirty()) return;
	
	msg("Salvataggio automatico in corso...");
	
	$.cookie("YouDrama_testo", oEditor.GetHTML(), {expires: 365 });
	
	oEditor.ResetIsDirty();
}

function msg(t){
	if(!msgBox) msgBox = $("#messagebox div.content_container div:first-child");
	msgBox.text(t);
	msgBox.css('opacity',1);
	
	msgBox.oneTime(2000, function(){
		msgBox.animate({opacity:0}, 1500);
	});
}

$(window).unload( function () { salvataggioAutomatico(); } );

function inviaProposta(){
	$(document).stopTime('autoSave');
	$("#form-scrivi").css('display','none');
	$("#opzioni").css('display','block');	
}

// invio normale, quando si è già loggati
function inviaProposta2(){
	$(document).stopTime('autoSave');
	$("#form-scrivi").css('display','none');
	
	$("#attesa").css('display','block');

	var oEditor = FCKeditorAPI.GetInstance('edit-area') ;
	oEditor.GetHTML();
	

	ajaxData = {text: oEditor.GetHTML(),
				titolo: $('#title').val(),
				location: $('#locations input:checked').val(),
				tags: $('#ttags').val()
				};
	
	var risultato = $.ajax({ 
	 url: "http://www.youdrama.it/ajax?a=invia", 
	 type: 'POST',
	 data: ajaxData,
	 async: false 
	}).responseText; 	
	
	if(risultato.substr(0,2) == "Ok"){
		$("#mssg").html("Invio effettuato con successo!");

		// Pubblichiamo su Facebook
		eval(risultato.substr(2));
		
		// document.location.href = "leggi";
	}else{
		$("#mssg").html(risultato);
	}	
}

function pubblicaProposta(){
	$("#opzioni").css('display','none');	
	$("#iscrizione").css('display','block');		
}

function completaTutto(){
	// Controlliamo prima che il nome e l'email siano inseriti
	if(!($("#iscrizione-form").valid())){
		alert("Devi inserire un nome e una email valida");
		return;
	}
	
	$("#iscrizione").css('display','none');
	$("#attesa").css('display','block');

	var oEditor = FCKeditorAPI.GetInstance('edit-area') ;
	oEditor.GetHTML();
	
	ajaxData = {nome: $('#eman').val(),
				email: $('#liame').val(),
				location: $('#locations input:checked').val(),
				text: oEditor.GetHTML(),
				titolo: $('#titolo').val(),
				tags: $('#tags').val()
				};
	
	var risultato = $.ajax({ 
	 url: "http://www.youdrama.it/ajax?a=iscrivieinvia", 
	 type: 'POST',
	 data: ajaxData,
	 async: false 
	}).responseText; 	
	
	if(risultato == "Ok"){
		$("#mssg").html("Registrazione e invio effettuati con successo!");
		document.location.href = "leggi";
	}else{
		$("#mssg").html(risultato);
	}
	
}

function caricaIncipit(){
	$("#incipit").css('display','none');
	var risultato = $.ajax({ 
	 url: "http://www.youdrama.it/ajax?a=incipit", 
	 async: false 
	}).responseText; 	
	
	$("#incipit").html(risultato);
	$("#incipit").show('slow');
}

function caricaPersonaggio(){
	var risultato = $.ajax({ 
	 url: "http://www.youdrama.it/ajax?a=personaggio", 
	 async: false 
	}).responseText; 
	
	$("#personaggi").html(risultato);
	$("#personaggi").show('fast');
}