var shower = false;	
var activeImage = false;
var imageGalleryLeftPos = false;
var imageGalleryWidth = false;
var imageGalleryObj = false;
var maxGalleryXPos = false;
var slideSpeed = 0;
var imageGalleryCaptions = new Array();
var textDB = "";
				
$(document).ready
			(function () 
			{
				
				//droppableSlot();
				
				if($("#relarore").attr("checked")) {$("#relarore").show();}
				
				$('#menuItems .item a').css( {backgroundPosition: "-20px 94px"} )
				.mouseover(function(){
					$(this).stop().animate({backgroundPosition:"(-20px 35px)"}, {duration:1000})
				})
				.mouseout(function(){
					$(this).stop().animate({backgroundPosition:"(-20px 94px)"}, {duration:500, complete:function(){
						$(this).css({backgroundPosition: "-20px 94px"})
					}})
				})
			
				var success = $("#successRegister").attr("value");
				
				if(success=="true")
				{
					var utente = $("#successRegister").attr("rel");
					//la funzione non riesce a partire al primo clic e devo richiamarla due volte per il corretto funzionamento
					openOverlay("message", "");
					openOverlay("message", "REGISTRAZIONE AVVENUTA CON SUCCESSO!<br>Benvenuto "+utente+" in DULP09.<br/><br/><small>Clicca fuori da questo layer per chiuderlo.</small>");
				}
				else if(success=="false")
				{
					//la funzione non riesce a partire al primo clic e devo richiamarla due volte per il corretto funzionamento
					openOverlay("message", "");
					openOverlay("message", "ERRORE DURANTE LA REGISTRAZIONE!<br>RITENTARE PIU TARDI.<br/><br/><small>Clicca fuori da questo layer per chiuderlo.</small>");
				}
			}
	)
	
	
	function dbclickSlot() {
		
		$(".slotTimePieno").dblclick(
				function() {riassegnaSlotPost($(this).attr("id"));}
		);
		
	}
	
	function droppableSlot() {
		
		$(".slotTimeVuoto").droppable({
		      drop: function(event, ui) {
				var idSlot = $(this).attr("id"); //lo slot droppato
				var idUser = $(ui.draggable).attr("rel");
				$.post("function.inc.php",{azione:"aggiornaSlotPost", idSlot:idSlot, idUser:idUser});}
		    });
		
		$(".slotTimePieno").droppable({
		      drop: function(event, ui) {
				var idSlot = $(this).attr("rel"); //lo slot droppato
				var idUser = $(ui.draggable).attr("rel");
				$.post("function.inc.php",{azione:"aggiornaSlotPost", idSlot:idSlot, idUser:idUser});}
		    });
	}
	
	function closerSlot(idUser){
		if(confirm("Cancellando il post viene cancellata la prenotazione. Continuare?")){
			$.post("function.inc.php",{azione:"cancellaPrenotazione", idUser:idUser});
			$("#divDraggable_"+idUser).remove();}
	}
	
	function sortableSlot() {
		
		$('.contentSlotTime').sortable(
				 {
						accept: 		'divSlotEmpty',
						helper:			'original',
						revert: 		true,
						activeclass : 	'divSlotEmpty',
						forceHelperSize: true,
						forcePlaceholderSize: true,
						//hoverclass : 	'sortablehover',
						handle: 		'#spostatore',
						tolerance: 		'pointer',
						connectWith: 	[".divSlotEmpty"],
						axis: 'y',
						opacity: 0.7
						//update: function() {aggiornaPosizione();}
						
					} 
				);

	}
	
	function moduli(modulo)
			{
				var check = document.getElementById(modulo).checked;
				
				if(check==true) document.getElementById('tb_'+modulo).style.display='block';
				else document.getElementById('tb_'+modulo).style.display='none';
				
				if(modulo=="relatore") {
				var check = document.getElementById(modulo).value; 
				if(check==on) document.getElementById('tb_'+modulo).style.display='block';
				else document.getElementById('tb_'+modulo).style.display='none';}
			}
	

		
	function controlla()
			{
				//controllo i campi obbligatori
				var nome = document.getElementById('nome').value;
				var cognome = document.getElementById('cognome').value;
				var mail = document.getElementById('mail').value;
				var titolo = document.getElementById('titolo').value;
				var passwd = document.getElementById('passwd1').value;
				for (i=0; i<2; i++)
				if (document.registerMe.relatore[i].checked) var relatore = document.registerMe.relatore[i].value;

				var descr = document.getElementById('descrizione').value;
				if(nome=="" || cognome=="" || mail=="" || passwd=="") alert("Riempire campi obbligatori contrassegnati con una *!");
				else if(relatore=="on" && (titolo=="" || descr=="")) alert("Se si e' un relatore inserire titolo e descrizione intervento");
				else document.getElementById('registerMe').submit();
			}
			
	function deleteUser(idUser)
	{
		if(confirm("Delete this user?")){
			$.post("../../global.inc.php",{action:"deleteThisUser", idUser:idUser});
			$("#"+idUser).fadeOut();
		}
	}
	
	function selectUserTipe(radio)
	{
		var tipe = $(radio).val();
		$.post("function.inc.php",{azione:"reloadUserSubscriptList", tipe:tipe}, function(data){$("#contentUser").html(data);});
		
	}
	
	function showPassword(checkbox){
		
		if($(checkbox).attr("checked")){
			var pass1 = $("#passwd1").val();
			$("#passwd1").hide();
			$("#text1").show().attr("value",pass1);
		} else {
			var pass1 = $("#text1").val();
			$("#text1").hide();
			$("#passwd1").show().attr("value",pass1);
		}
	}
	
	function changeUserAcept(checkbox, idUser) {
		
		if($(checkbox).attr("checked")){
			$.post("../../global.inc.php",{action: "changeUserState", valore: 1, idUser:idUser});
		} else {
			$.post("../../global.inc.php",{action: "changeUserState", valore: 0, idUser:idUser});	
		}
		
	}
	
	function mostraSlot(IDSlot, tasto) {
		
		$(".slotModule").hide();
		$("#slotModule_"+IDSlot).toggle("slide");
		$("#slotModule_"+IDSlot+" #messageError").html("");	
	}
	
	function prenotaSlot(IDSlot) {
		
		var datiString = $("#formSlot_"+IDSlot).serialize();
		$.post("function.inc.php",{azione: "prenotaSlot", dati: datiString}, function(data){ $("#slotModule_"+IDSlot+" #messageError").html(data);});
	}
	
	function riassegnaSlotPost(IDUser) {
		//azzero lo slot esistente
		$.post("function.inc.php",{azione: "azzeraPostitSlot", IDUser: IDUser}, function(data){
			$("#"+IDUser).attr("class","slotTimeVuoto").html(""); 
			$("#"+IDUser).attr("id",$("#"+IDUser).attr("rel"));
			droppableSlot();
			$("#errorMessage").html("");
			$(data).appendTo("html");
			var heights = $("html").scrollTop(); heights = (heights+200);
			$("#divDraggable_"+IDUser).show().css("top",heights+"px").css("left","50%").draggable({ addClasses: false });
		});
	}
	
	function addPostItPrenot() {
		
		var datiString = $("#formAddPostitPrenot").serialize();
		$.post("function.inc.php",{azione: "prenotaPostitSlot", dati: datiString}, function(data){
			
			if(data=="coord"){ dbclickSlot();$("#errorMessage").html(returntextDB("info_coordinatore"));}
			else if(data.indexOf("divDraggable")<0){
				var dataSplit = data.split(":::");
				var idUser = dataSplit[0];
				if(!$("#divDraggable_"+idUser).html()){
				$("#errorMessage").html(dataSplit[1]);	}
			}else {
				var dataSplit = data.split(":::");
				var idUser = dataSplit[0];
				if(!$("#divDraggable_"+idUser).html()){
				$(dataSplit[1]).appendTo("html");var heights = $("html").scrollTop(); heights = (heights+200);
				$("#divDraggable_"+idUser).show().css("top",heights+"px").css("left","50%").draggable({ addClasses: false });
				}else {$("#errorMessage").html("Postit gia creato!");} 
			}
		});	
	}
	
	function returntextDB(text) {
		//le chiamate di questo tipo devono essere SINCRONE altrimenti la variabile viene restituita sempre undefinited!!!
		var textDB="";
		$.ajax({type: "POST", url: "function.inc.php", data: "azione=textDB&text="+text, async:false, success: function(data){textDB = data;}});
		return textDB;
	}
	
	function initSlideShow()
	{	
		
		imageGalleryObj = document.getElementById('menuItems');
		imageGalleryLeftPos = (imageGalleryObj.offsetLeft) ? imageGalleryObj.offsetLeft : 0;
		imageGalleryWidth = document.getElementById('menu').offsetWidth - 20;
		maxGalleryXPos = imageGalleryObj.offsetLeft; 
		minGalleryXPos = imageGalleryWidth - document.getElementById('itemEnd').offsetLeft;
		
		var divs = imageGalleryObj.getElementsByTagName('DIV');
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='imageCaption')imageGalleryCaptions[imageGalleryCaptions.length] = divs[no].innerHTML;
		}
		gallerySlide();
	}
	window.onload = initSlideShow();
