var editmode = false;

$().ready( function() { 
     //disable enter press in body
//     $('body').keypress( function(e) { return (e.keyCode != 13); } );

    //$('#todolisttitle').hover( function(){$('.deletelistlink').css({'visibility','visible'});return false;},function(){$('.deletelistlink').hide();return false;});
    
   
    

	 $('body').click( function(event) {
	
		//list of tags that should not work with click anywhere save
	 	if (event.target.tagName != "LABEL" && event.target.tagName != "INPUT"  && event.target.tagName != "A" && event.target.tagName != "TEXTAREA")
		{
			//console.log('check for save');
  			//save any unsaved todos
			$('.edit:visible').trigger('dblclick');
			$('#todolisttitle > input:visible').trigger('dblclick');

	    } //end of event is not label or input
	 } );

//   $('.check[@checked]').parent().addClass('done');
	 
	 $('newlist').click( function() { addlist(); } );
	 
	 //init items
     inititems();
     initlists();
     initleftcol();     
        
} );
 
 //makes a new todoitem jquery object
 function makenewtodo(o)
 {
 
        var id;
        var o = o || null;
        var indent = 'noindent';

        //if there is no json object just use temporary guid
        if (!o)
        {
            var guid = new Date().getTime();
            id = 't' + guid;
        }
        //if there is json, parse it, and update values
        else
        {
            var jsonobj = eval('(' + o + ')');
            id = jsonobj.newid;
            indent = jsonobj.indent;           
            //alert("indent:" + jsonobj.indent + "  );
        }
		
		
		
	
		
		
 
 	 	var $insert = $('<a onclick="inserthere(this); return false;" href="#" class="insert" ><span>insert</span></a>');
		
		var $ticheck = $('<input type="checkbox"></input>').addClass('check');
		var $tilabel = $('<label></label>').addClass('view').html('new todo');
		var $tiedit = $('<input type="text"></input>').addClass('edit').val('new todo');
		var $timor = $('<a href="#"></a>').addClass('mor').html('more');
		var $tidelete = $('<a href="#"></a>').addClass('del').html('delete');		
		//var $timore = $('<div style="float:left;"></div').addClass('more').html('<div style="width:230px; float:left; display:none;"><label>notes</label><textarea style="height:66px;"></textarea><a href="#">save</a></div><div style="width:300px; float:left; display:none;"><label>priority</label><a class="oshlink" href="#">o sh!t</a> <a class="highlink" href="#">high</a> <a class="normallink" href="#">normal</a> <a class="lowlink" href="#">low</a><br clear="all" /><br /><label>labels</label><input type="text" style="float:left;" /><a style="display:block;float:left; margin-left:8px;margin-top:4px;" href="#">add</a></div>');
		var $timore = $('<div style="float:left;"></div').addClass('more').html('<div style="width:230px; float:left; display:none;"><label>notes</label><textarea style="height:66px;"></textarea><a href="#">save</a></div><div style="width:300px; float:left; display:none;"><label>priority</label><a class="oshlink" href="#">o sh!t</a> <a class="highlink" href="#">high</a> <a class="normallink" href="#">normal</a> <a class="lowlink" href="#">low</a><br clear="all" /><br /></div>');
		
		var guid = new Date().getTime();
		var $todoitem = $('<div id="'+id+'"></div>').addClass('todoitem').append($ticheck).append($tilabel).append($tiedit).append($timor).append($tidelete).append($timore);
		
		if (indent != 'noindent')
		{
		    $todoitem.addClass( indent );
		}
			
		return $todoitem;
 }

 function makenewlist ( o )
 {
        var id;
        var listname = '';
        var o = o || null;

        //if there is no json object just use temporary guid
        if (!o)
        {
            id = o;  //not implemented anywhere yet
        }
        //if there is json, parse it, and update values
        else
        {
            var jsonobj = eval('(' + o + ')');
            
            id = jsonobj.newid;
            listname = jsonobj.listname;
        }
 
		var $listitem = $('<a href="#" id="'+id+'"></a>').addClass('listlink').html(listname);
		return $listitem;
 }

 function addlist( caller )
 {
   
    var listname = $(caller).prev().val();
	listname = encodeURIComponent( listname );
	

    $.ajax({     
    
            type: "GET", 
            url: "entries/m/addlist.snip", 
            data: "n=" + listname + "&m=" + member_id,
            success: function(msg){ 
                //alert( "Json Returned: " + msg ); 
                var $newlist = makenewlist(msg);
    
                //$('.memberlists:first').append($newlist).hide().fadeIn("normal");
                $newlist.hide().appendTo('.memberlists:first').fadeIn("normal");
                $(caller).prev().val('');
                
                initleftcol();
                 
                //show new list just made
                $newlist.trigger('click');
                
            } //end of success
    }); //end of $ajax
 } //end of addlist    
    
 
 function reloadlists()
 {
   var guid = new Date().getTime();        
   $('.memberlists:first').load('plugins/memberlists/get.uc?guid=' + guid + '&mid=' + member_id, function(){    																			   
             
            initleftcol();
        });    
 }
 
 
 function initleftcol()
 {
   
    //switch todolist link
    $('.listlink').unbind().click( function() {
        
        var listid = this.id;
        var guid = new Date().getTime();        
        
        $('#todocontainer').load('plugins/todolist/get.uc?id=' + listid + '&guid=' + guid, function(){    																			   
                inititems();
                initlists();
                //$('.check[@checked]').parent().addClass('done');
        });    
    });
    
    //email todolist link
    $('.emaillink').unbind().click(  function() {
    
            var todolistid = $('.todolist')[0].id;
            var email = $(this).prev().val();
			
			var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
			
			var isEmail = re.test(email);
			
			if(isEmail){
       
            $.ajax({ 
                type: "GET", 
                url: "entries/m/emailtodolist.snip", 
                data: "tl=" + todolistid + "&e=" + email
            });  

			//$(this).prev().attr("value","your@email.com");
			$('.stemaillink').trigger('click');
			
			} //is email
			return false;
    });
    


    //sms todolist link
    $('.smslink').unbind().click(  function() {
    
            var todolistid = $('.todolist')[0].id;
            var phone = $(this).prev().prev().val().replace(/[^\d]/g, "");
            var carrier = $(this).prev().val();
			
//	        var reg = /indent(\d)/i;
//			phone = phone.replace(/[^\d]/g, "")
			
            
    //        alert("todolistid: " + todolistid + " phone: " + phone + " carrier: " + carrier );
       
            $.ajax({ 
                type: "GET", 
                url: "entries/m/smstodolist.snip", 
                data: "tl=" + todolistid + "&p=" + phone + "&c=" + carrier
            });   
			
			//$(this).prev().prev().attr("value","555-555-555");
			$('.stcell').trigger('click');
			return false;
    });

    
 }
 
 function initlists()
 {

/*
    //edit list title label / link
    $('#todolisttitle > label').click( function() { 
        
        $(this).hide().next().show();
        
    
    });
	*/






/*
	//edit list label title / link
    $('#todolisttitle').click( function(event) { 
		//if not my delete or options link        
		if (event.target != $(this).children('a:last')[0] && event.target != $(this).children('a:first')[0])
		{
			if ( $(this).children('label:visible')[0] )
			{
				$(this).children('label').hide().next().show();
			}
		}
    });



$('#todolisttitle').hover( function(){ $('#todolisttitle > a.deletelistlink').css({'visibility':'visible'}); }, function(){  $('#todolisttitle > a').css({'visibility':'hidden'}); } );
// $('#todolisttitle').hover( function(){$('.deletelistlink').css({'visibility':'visible'});},function(){$('.deletelistlink').css({'visibility':'hidden'});});	
		
    //edit list input
    $('#todolisttitle > input').unbind().dblclick( function() { 
            closeandsavetitle( $(this) );
            return false;
    }).keydown( function(e) {
        //if up or down or enter
	    if ( e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 13)
		{
		    closeandsavetitle( $(this) );
		    return false;
		}
	});
 
 
     //delete list link
    $('.deletelistlink').unbind().click( function(event) { 
												  
		event.stopPropagation();
        var todolistid = $('.todolist')[0].id;
        
        //alert( todolistid );
        
        var loadurl = "entries/m/deletelist.snip?tl="+todolistid;
        $("#divmessage").load(loadurl).hide();

        //remove the deleted one
        $('.memberlists > a[@id='+todolistid+']').fadeOut().remove();
        
        
        var listid = $('.memberlists > a[id!='+todolistid+']:last')[0].id;

        
        var guid = new Date().getTime();        
        
        $('#todocontainer').load('plugins/todolist/get.uc?id=' + listid + '&guid=' + guid, function(){    																			   
                inititems();
                initlists();
        
        });  
		
        
        //probaby load next list 
    } );
  */  
     //strikethru done
     //$('.check[@checked]').parent().addClass('done');   
     
     //make base link visible if already indented past 1
     if ( $('.todolist > .todoitem:last').is(".indent1,.indent2,.indent3,.indent4,.indent5,.indent6,.indent7,.indent8,.indent9") )
	 {	
	    $('.closesub').css('visibility','visible');				
        if (! $('.todolist > .todoitem:last').is(".indent1") ) 
	            $('.addbase').css('visibility','visible');
	 
	 }


 
	 instext = '<a onclick="inserthere(this); return false;" href="#" class="insert" ><span>insert</span></a>';
 
    //setup add links
     $('.todolist > .add').click ( function() { 
	 
	    $('.view:hidden').siblings('.edit').trigger('dblclick');
	 
        if ( $(this).siblings('.todoitem:last').is(".indent1,.indent2,.indent3,.indent4,.indent5,.indent6,.indent7,.indent8,.indent9") )
        {
			var $todoitem = makenewtodo();

            if ( $(this).siblings('.todoitem:last').is(".indent1")   )
				$todoitem.addClass('indent1');
		  	else if (  $(this).siblings('.todoitem:last').is(".indent2")  )
				$todoitem.addClass('indent2');           
            else if ( $(this).siblings('.todoitem:last').is(".indent3")  )
               	$todoitem.addClass('indent3');           
	        else if ( $(this).siblings('.todoitem:last').is(".indent4")   )
				$todoitem.addClass('indent4');
		  	else if (  $(this).siblings('.todoitem:last').is(".indent5")  )
				$todoitem.addClass('indent5');           
            else if ( $(this).siblings('.todoitem:last').is(".indent6")  )
               	$todoitem.addClass('indent6');           
	        else if ( $(this).siblings('.todoitem:last').is(".indent7")   )
				$todoitem.addClass('indent7');
		  	else if (  $(this).siblings('.todoitem:last').is(".indent8")  )
				$todoitem.addClass('indent8');           
            else if ( $(this).siblings('.todoitem:last').is(".indent9")  )
               	$todoitem.addClass('indent9');           
				
			//add to dom
            $(this).siblings('.endofitems:first').before( $todoitem ); 
        }
        else 
        {
			var $todoitem = makenewtodo();
			var $inserthere = $(this).siblings('.endofitems:first');
            $(this).siblings('.endofitems:first').before( $todoitem ); 
            //$todoitem.hide().insertBefore($inserthere).fadeIn("slow");
        }      
       
		//add insert inbetween row
		$(this).siblings('.todoitem:last').before( $('<a onclick="inserthere(this); return false;" href="#" class="insert" ><span>insert</span></a>') );
        //reinit
        inititems();    
		$(this).siblings('.todoitem:last').children('.view').trigger('dblclick');		
        return false;    
     } );
     
     $('.todolist > .addbase').click ( function() { 
	 
	    $('.view:hidden').siblings('.edit').trigger('dblclick');
		$('.addbase').css('visibility','hidden');
		$('.closesub').css('visibility','hidden');		
			 
       	var $todoitem = makenewtodo();
        $(this).siblings('.todoitem:last').after( $todoitem ); 

		//add insert row
		$(this).siblings('.todoitem:last').before( $('<a onclick="inserthere(this); return false;" href="#" class="insert" ><span>insert</span></a>') );
        //reinit
        inititems();    
		$(this).siblings('.todoitem:last').children('.view').trigger('click');		
        return false;   
     } );
     
    //setup add sub links
     $('.todolist > .addsub').click ( function() { 
	 
        $('.view:hidden').siblings('.edit').trigger('dblclick');
		$('.closesub').css('visibility','visible');
        
        //check last item on list
        if ( $(this).siblings('.todoitem:last').is(".indent1,.indent2,.indent3,.indent4,.indent5,.indent6,.indent7,.indent8,.indent9") )
        {
			var $todoitem = makenewtodo();
		
            if ( $(this).siblings('.todoitem:last').is(".indent1")   )
				$todoitem.addClass('indent2');        
            else if (  $(this).siblings('.todoitem:last').is(".indent2")  )
				$todoitem.addClass('indent3');           
			else if ( $(this).siblings('.todoitem:last').is(".indent3")   )
				$todoitem.addClass('indent4');        
            else if (  $(this).siblings('.todoitem:last').is(".indent4")  )
								alert("temporary limit set");
				//$todoitem.addClass('indent5');           				
			else if ( $(this).siblings('.todoitem:last').is(".indent5")   )

				$todoitem.addClass('indent6');        
            else if (  $(this).siblings('.todoitem:last').is(".indent6")  )
				$todoitem.addClass('indent7');        
            else if (  $(this).siblings('.todoitem:last').is(".indent7")  )
				$todoitem.addClass('indent8');           				
			else if ( $(this).siblings('.todoitem:last').is(".indent8")   )
				$todoitem.addClass('indent9');        
            else if (  $(this).siblings('.todoitem:last').is(".indent9")  )
			 	alert("Only 9 levels allowed.");  
				
				
			$('.addbase').css('visibility','visible');				
			//add to dom
            $(this).siblings('.todoitem:last').after( $todoitem ); 				          
        }
        else  // if there is no indent it is a parent row
        {
			var $todoitem = makenewtodo();
			$todoitem.addClass('indent1');    
            $(this).siblings('.todoitem:last').after( $todoitem ); 

			$('.addbase').css('visibility','hidden'); //no reason to see addbase on level 1
        }
        
		//add insert row
		$(this).siblings('.todoitem:last').before( $('<a onclick="inserthere(this); return false;" href="#" class="insert" ><span>insert</span></a>') );
		
        //reinit
        inititems();   
    	
		$(this).siblings('.todoitem:last').children('.view').trigger('click');
        return false;     
     } );     
     
     
     
    //setup add close sub links
     $('.todolist > .closesub').click ( function() { 
        
        $('.view:hidden').siblings('.edit').trigger('dblclick');
		
		//check last item on list
        if ( $(this).siblings('.todoitem:last').is(".indent1,.indent2,.indent3,.indent4,.indent5,.indent6,.indent7,.indent8,.indent9") )
        {
			var $todoitem = makenewtodo();
		
            if ( $(this).siblings('.todoitem:last').is(".indent1")   )
           	{
			 // do nuthing, $todoitem should be ok
			 		$('.addbase').css('visibility','hidden');
					$('.closesub').css('visibility','hidden');
			}
            else if (  $(this).siblings('.todoitem:last').is(".indent2")  )
               	$todoitem.addClass('indent1');           
            else if ( $(this).siblings('.todoitem:last').is(".indent3")  )
			 	$todoitem.addClass('indent2');
            else if (  $(this).siblings('.todoitem:last').is(".indent4")  )
               	$todoitem.addClass('indent3');           
            else if ( $(this).siblings('.todoitem:last').is(".indent5")  )
			 	$todoitem.addClass('indent4');
            else if (  $(this).siblings('.todoitem:last').is(".indent6")  )
               	$todoitem.addClass('indent5');           
            else if (  $(this).siblings('.todoitem:last').is(".indent7")  )
               	$todoitem.addClass('indent6');           
            else if ( $(this).siblings('.todoitem:last').is(".indent8")  )
			 	$todoitem.addClass('indent7');
            else if (  $(this).siblings('.todoitem:last').is(".indent9")  )
               	$todoitem.addClass('indent8');           
				
		
			//add to dom
            $(this).siblings('.todoitem:last').after( $todoitem );
        }
        else 
        {
			var $todoitem = makenewtodo();
            $(this).siblings('.todoitem:last').after( $todoitem ); 
        }
        
		//add insert row
		$(this).siblings('.todoitem:last').before( $('<a onclick="inserthere(this); return false;" href="#" class="insert" ><span>insert</span></a>') );
		
        //reinit
        inititems();   
		
		$(this).siblings('.todoitem:last').children('.view').trigger('click');
        return false;     
     } );     
     
     
 }
 
 function inserthere( target )
 {
		var $todoitem = makenewtodo(); 		


    	if ( $(target).prev('.todoitem').is(".indent1")   )
			$todoitem.addClass('indent1');
		else if (  $(target).prev('.todoitem').is(".indent2")  )
			$todoitem.addClass('indent2');           
        else if ( $(target).prev('.todoitem').is(".indent3")  )
               	$todoitem.addClass('indent3');           
		else if (  $(target).prev('.todoitem').is(".indent4")  )
			$todoitem.addClass('indent4');           
        else if ( $(target).prev('.todoitem').is(".indent5")  )
               	$todoitem.addClass('indent5');           
		else if (  $(target).prev('.todoitem').is(".indent6")  )
			$todoitem.addClass('indent6');           
		else if (  $(target).prev('.todoitem').is(".indent7")  )
			$todoitem.addClass('indent7');           
        else if ( $(target).prev('.todoitem').is(".indent8")  )
               	$todoitem.addClass('indent8');           
		else if (  $(target).prev('.todoitem').is(".indent9")  )
			$todoitem.addClass('indent9'); 			
				
		$('<a onclick="inserthere(this); return false;" href="#" class="insert" ><span>insert</span></a>').insertAfter(target);
		$todoitem.insertAfter(target).children('label').click( function() { 

        	//since I cannot use inititems while chaining...
	        $('.view:hidden').siblings('.edit').trigger('dblclick');
    	    $(this).hide().siblings('.edit').show().get(0).focus();  
        	$(this).siblings('.edit').get(0).select();  		
				
     	})
		.trigger('click');  //trigger the function I just made ;)
		
		inititems();
 }
 
 function inititems()
 {
    //save link
    $('.save').unbind().click( function(event) { 
            
        event.stopPropagation();
    	closeandsavemore( $(this) );        
    
    });
 
 	//o sh link
 	$('.oshlink').unbind().click( function() { 
	
		$(this).parent().parent().parent().removeClass("low").removeClass("normal").removeClass("high").removeClass("osh").addClass("osh");
		savepriority( $(this), 'osh' );
		return false;
	
	} );
 	//high link
 	$('.highlink').unbind().click( function() { 
	
		$(this).parent().parent().parent().removeClass("low").removeClass("normal").removeClass("high").removeClass("osh").addClass("high");
		savepriority( $(this), 'high' );		
		return false;
	
	} );
 	//normal link
 	$('.normallink').unbind().click( function() { 
	
		$(this).parent().parent().parent().removeClass("low").removeClass("normal").removeClass("high").removeClass("osh").addClass("normal");
    	savepriority( $(this), 'normal' );		
		return false;
	
	} );
 	//low link
 	$('.lowlink').unbind().click( function() { 
	
		$(this).parent().parent().parent().removeClass("low").removeClass("normal").removeClass("high").removeClass("osh").addClass("low");
		savepriority( $(this), 'low' );		
		return false;
	
	} );
 
 	
 	//set more links
	$('.todoitem > .mor').unbind().toggle( function() { 
	
		$('.more:visible').siblings('.mor').trigger('click');
		
		$(this).siblings('.more').slideDown("fast",function(){$(this).children('div').show()});
		//$('.more').slideDown("fast");
	
	} , function() { 	
		//$('.more').slideUp("fast"); 
		$(this).siblings('.more').children('div').hide();
		$(this).siblings('.more').slideUp("fast");
	});
  
     //setup labels
	 
	 
	 //iphone only: changed label to double click
    $('.todoitem > label').unbind().dblclick( function() { 
        $('.view:hidden').siblings('.edit').trigger('dblclick');
        //check to make sure no update needs to be sent out
        $(this).parent().siblings('label:hidden');
        $(this).hide().siblings('.edit').show().get(0).focus();  
        $(this).siblings('.edit').get(0).select();  		
	
     });

    //setup edits
    $('.todoitem > .edit').unbind().blur( function(event) { 
            event.stopPropagation();
			closeandsave( $(this) );
            return false;
    //}).change( function() { 
    //        closeandsave( $(this) );
    //        return false;
    }).dblclick( function(event) { 
            event.stopPropagation();	
            closeandsave( $(this) );
            return false;
    //}).change( function() { 
    //        closeandsave( $(this) );
    //        return false;
    }).keydown( function(e) {
        //if up or down or enter
        e.stopPropagation();
	    if ( e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 13)
		{
		    closeandsave( $(this) );
		    return false;
		}
	});
   /*
   */
   
     //setup checkboxes
     $('.todoitem').unbind().click( function() { 
        
        var doneid = this.id;
        
        //strikethru 
		if ($(this).children('.check')[0].checked)
			$(this).children('.check')[0].checked = false;
		else
			$(this).children('.check')[0].checked = true;		
		
        if ( $(this).children('.check')[0].checked ) 
        {
            $(this).addClass('done');
//            alert('done: ' + doneid);
            
            $.ajax({ 
                type: "GET", 
                url: "entries/m/setisdone.snip", 
                data: "ti=" + doneid + "&d=1"
            });
        }
        else  
        {
            $(this).removeClass('done'); 
//            alert('done: ' + doneid);            
            $.ajax({ 
                type: "GET", 
                url: "entries/m/setisdone.snip", 
                data: "ti=" + doneid + "&d=0"
             });
        }

        reloadlists();

        //for subtasks only
        //check (going backwards) if everything  is checked
        if ( $(this).parent().is('.indent1,.indent2,.indent3,.indent4,.indent5,.indent6,.indent7,.indent8,.indent9') )
        {
            var prevcheck = true;
            var nextcheck = true;
            var cursor = $(this).parent();
			
//			console.log(cursor);
          
            //check forward
            while ( cursor.is('.indent1,.indent2,.indent3,.indent4,.indent5,.indent6,.indent7,.indent8,.indent9') )
            {
			 
                if ( !cursor.children('.check')[0].checked ) nextcheck = false;
                cursor = cursor.next().next();
//				console.log(cursor);
            }
          
            cursor = $(this).parent();
            
            //check backwards
            while ( cursor.is('.indent1,.indent2,.indent3,.indent4,.indent5,.indent6,.indent7,.indent8,.indent9') )
            {
                if ( !cursor.children('.check')[0].checked ) prevcheck = false;
                cursor = cursor.prev().prev();
//				console.log(cursor);				
            }
       
//			console.log("nextcheck" + nextcheck + "prevcheck" + prevcheck);

            //update parent check and strikethru             
            cursor.children('.check')[0].checked = ( prevcheck && nextcheck );
       
            if ( prevcheck && nextcheck ) 
                cursor.addClass('done'); 
            else 
                cursor.removeClass('done'); 
       
        }//end of subitem parent check

     });
     
     //delete links
     $('.del').unbind().click ( function() { 
        
        var todo_id = $(this).parent()[0].id;
        //alert(todo_id);
        var loadurl = "entries/m/deletetodo.snip?ti="+todo_id;
        $("#divmessage").load(loadurl).hide();
        //$().get(loadurl);
                
        $(this).parent().addClass('deleted').fadeOut("slow", function(){ $(this).prev('.insert').remove(); reloadlists(); });
        
        
        
        return false;
     } );
 }
 
 function closeandsave(target)
 {
    //save to db
    
    //add no duplicate code in the function
    
    //if this was a temp save and turn to real   
    if ( target.parent()[0].id.indexOf('t') > -1 )
    {
        var newtext =  encodeURIComponent( target.val() ); 
        var oldtext = target.val();  
        var indent = 'noindent';
		var guid = target.parent()[0].id;
        
        
        //save any indent on the parent        
        var reg = /indent(\d)/i;
        if ( reg.exec(target.parent().attr('class')) )
        {
            var ar = reg.exec(target.parent().attr('class'));
            //console.log (ar);
            indent = 'indent' + ar[1];
        }
        
				
        $.ajax({ 
            type: "GET", 
            url: "entries/m/addtodo.snip", 
            data: "t=" + newtext + "&l=" + target.parent().parent()[0].id + "&i=" + indent + "&g=" + guid,
            success: function(msg)
            { 
                //alert( "Data Saved: " + msg ); 
                var $newtodo = makenewtodo(msg);
                $newtodo.children('.view').html( oldtext );
                $newtodo.children('.edit').val( oldtext );
                target.parent().before( $newtodo );
                target.parent().remove();
                inititems();
            } 
        });
    }
    else
    {
        //this is an update gig
        var newtext =  encodeURIComponent( target.val() ); 
        var oldtext = target.val();  


        $.ajax({ 
            type: "GET", 
            url: "entries/m/updatetodo.snip", 
            data: "t=" + newtext + "&ti=" + target.parent()[0].id 
        });

        target.siblings('.view').html(target.val());
        target.hide().siblings('.view').show();  
    }

			
	$('.add')[0].focus();
    reloadlists();
    return false;
 }

function closeandsavetitle( target )
 {
    //update list name in database

        //this is an update gig
        var newtext =  encodeURIComponent( target.val() ); 
        var oldtext = target.val(); 
        var todolistid = $('.todolist')[0].id; 


        $.ajax({ 
            type: "GET", 
            url: "entries/m/updatetodolist.snip", 
            data: "ln=" + newtext + "&tl=" + todolistid
        });

        target.siblings('label').html(target.val());
        target.hide().siblings('label').show();  


        //update list on side
        reloadlists();
        
    return false;
 }

function closeandsavemore(target)
 {

        //this is an update gig
        
        var newtext =  encodeURIComponent( target.prev().val() ); 
        var oldtext = target.prev().val();
        var todo_id = target.parent().parent().parent()[0].id;
        
        console.log ('newtext: ' + newtext);
        console.log ('oldtext: ' + oldtext);
        console.log (target.parent().parent().parent()[0].id);
        
        //close more
        target.parent().parent().siblings('.mor').trigger('click');   
      
         $.ajax({ 
            type: "GET", 
            url: "entries/m/updatetodonote.snip", 
            data: "n=" + newtext + "&ti=" + todo_id
        });

        
        return false; 
        
 /*
			
	$('.add')[0].focus();
    reloadlists();
     */
 }
 
 
 function savepriority(target, pri)
 {

        //this is an update gig
        

        var todo_id = target.parent().parent().parent()[0].id;
        
     
         $.ajax({ 
            type: "GET", 
            url: "entries/m/updatetodopri.snip", 
            data: "p=" + pri + "&ti=" + todo_id
        });

        
        return false; 
 
 }