$(document).ready(function(){
	
	showHint();
	
	$('#email').focus();
	
	$('#slider').innerfade({ 
		timeout: '8000',
		speed: 'slow'
	}); 
	
	// funktioner -----------------------------------------------------------------
	
	// Allmänna
	
	function saving() {
		//$('#logo').toggle
	
	}
	
	function updatecount() {
		
		var i=0;
		for (i=1;i<=7;i++) {
			
			// Kolla alla
			var both = '0';
			both = $('tbody td.c'+ i +' span').length;
			
			// Kolla negativa
			var neg = '0';
			neg = $('.neg td.c'+ i +' span').length;
			
			// visa summa
			$('table tfoot td:eq('+ (i-1) +')').html(both - (2*neg));
		
		}
	}
	
	// Visa hints om det inte finns några mål
	function showHint() {
		//$numRows = $('tbody tr').length;
		if ($('tbody tr').length < 1) {
			$('#add-goal').effect("pulsate", { times: 5 }, 1500);
			//$('#add-goal').parent().append('<span id="hint">Börja med att lägga till ett mål</span>');
			setTimeout('$(\'#hint\').fadeOut(\'fast\')',5000);
		
			$.get('/actions/first-use.php',function(data){
				$('tbody').append(data);
			}); 
			
			$('#add-goal').click(function() {
				$("#slider").animate({ 
					left: "-810px"
			  	}, 800 );				  
				
				$('#frame2').click(function() {
					$('#slide-wrapper').slideUp(1000, function () {
						$('#intro').remove();
					});
				});
			});
			
		}
	}
	
	
	// Knutna till händelser
	
	
	// Händelsehantering ----------------------------------------------------------
	
	// Räkna bilder
	updatecount();

	// Sortering
	$("#sort").sortable({
		axis: 'y',
		containment: 'table',
		cursor: 'move',
		tolerance: 'pointer',
		forceHelperSize: true,
		forcePlaceholderSize: true,
		handle:'.move',
		update : function () {
			
			var order = $('#sort').sortable('serialize');
			$("#footer").load("/actions/goal_sort.php?"+order);
		
		}
    });


	// Ta bort marker
	$('tr.goal td span').live('click', function(event) { 

		var info = $(this).parent().attr("id").substring(1);
		info = info.split("_");

		$(this).remove(); 
   		
		$.get("/actions/marking_remove.php", { goal_id: info[0], mark_date: info[1] },
  			function(data){
    			//alert("Data Loaded: " + data);
 			});
	
		updatecount();
	});

	// Lägga till marker
	$('tr.goal td').live('click', function(event) { 
		if (event.target == this) {

			$(this).append('<span title="Klicka för att ta bort"></span>'); 
			
			var info;
			info = $(this).attr('id').substring(1);
			info = info.split("_");
			
			$.get("/actions/marking_add.php", { goal_id: info[0], mark_date: info[1] }, function(data){
					//alert("Data Loaded: " + data);
			});
		
		// TODO: Visa fel om ändringen inte sparades
	
			updatecount();
		}
	});
	
	// Radera mål
	$('th .delete').live('click', function(event) { 

		var info = $(this).parents('.goal').attr("id").substring(4);
		$(this).parents('.goal').remove(); 

		$.get("/actions/goal_delete.php", { goal_id: info }, function(data){
    		//alert("Data Loaded: " + data);
 		});
		
		updatecount();
	});
	
	
	// Edit in place
	$('th h3').live('click', function() {
    	var edited = $(this);
		var gid = $(this).parents('tr').attr("id").substr(4);

		var editBox = $('<input type="text" value="' + $(this).html() + '" />').bind("blur keypress",function(e) {
			if(e.keyCode==13 || e.keyCode==null){

				edited.html(this.value);
				var newtitle = encodeURIComponent(this.value);
				$("#footer").load("/actions/goal_edit.php?goal_id="+gid+"&newtitle="+newtitle+"");
				editBox.remove();
			
			}
		})
    	edited.after(editBox).html('');
    	editBox.focus();
	});
	
	// Nytt mål
	$('#add-goal').click(function() {
		// Spara i db, hämta svar i form av färdigmonterad tabelrad
		$.get("/actions/goal_new.php", function(response){
			
			$('#sort').append(response);
			var gid = $('.new-input').parents('tr').attr("id").substr(4);
			$('.new-input').focus();

			$('.new-input').bind("blur keypress",function(ev) {
				
				if(ev.keyCode==13 || ev.keyCode==null) {	
					var newtitle = $('.new-input').attr('value');
					$("#footer").load("/actions/goal_edit.php?goal_id="+gid+"&newtitle="+encodeURIComponent(newtitle)+"");
					$('.new-input').after('<h3>'+newtitle+'</h3>').remove();
				}
			
			});		
		});
	});
	
	
	// Växla mellan pos/neg
	$('.type').live('click', function() {
		
		// Växla klass på raden
		$(this).parents('tr').toggleClass("neg");
		
		// Spara ny typ
		var info = $(this).parents('tr').attr("id");
		info = info.split("_");
		$("#footer").load("/actions/goal_type.php?goal_id="+info[1]);

		updatecount();
	});
});