var $ = function (id) { return document.getElementById(id); }

var tasks1 = [];



if ( jsLib.cookies.hasCookie("tasklist") ) {
    var raw_tasks = jsLib.cookies.getCookie("tasklist").split(",");
    
    for ( var i in raw_tasks ) {
        tasks1.push( decodeURIComponent(raw_tasks[i]) );
        
    }
}

var list = new TaskList(tasks1);

var display_list = function () {
    
    $("task_list").value = list.getList();
    if(list.hasTasks()) {
    	
        
        jsLib.cookies.setCookie("tasklist", list.getCookieList(), 365 * 86400);
       } else {
       // $("task_list").value = "nothing yet";
       jsLib.cookies.deleteCookie("tasklist");
        
       }
       
        
     
}

var add_task_click = function () {
    var task = prompt("Enter a task:");
    if ( task != "" && task !== null && task != "undefined") {
        list.addTask(task);
        
        
    }
    display_list();
}

var delete_task_click = function () {
    
        var returned = prompt("Enter the task number to delete:");
        if(!(returned == null)) {
        var index = parseInt(returned) - 1;
        list.deleteTask(index);
        // if(index == 0) jsLib.cookies.deleteCookie("tasklist");
        
        display_list();
        }
    
}

var edit_task_click = function () {
   	
   	var returned = prompt("Enter the task number to edit:");
	if(!(returned == null)) {   	
        var index = parseInt(returned);
        
        index--;
        
        if ( list.validIndex(index) ) {
        
            var task = prompt("Enter update for task " + (index+1), list.getListItem(index) );
            if ( task != "" && task != null && task != "undefined") {
                list.editTask(index, task);
                
                display_list();
            
        }
        }
        }
        }

    
 var mark_done_click = function () {
    	var returned = prompt("Enter the task number to mark DONE:");
    	if(!(returned == null)) { 
         var index = parseInt(returned);
         index--;
         if ( list.validIndex(index) ) {
             task = list.getListItem(index);
             task = task.replace(" *In-Progress*", "");
             task += " *DONE*";
                 list.editTask(index, task);
                 
                 display_list();
             
         
    }
    }
}

 var mark_inprogress_click = function () {
    	
         
         var returned = prompt("Enter the task number to mark In-Progress:");
         if(!(returned == null)) {
         var index = parseInt(returned);
         index--;
         if ( list.validIndex(index) ) {
             task = list.getListItem(index);
             task = task.replace(" *DONE*", "");
             task += " *In-Progress*";
                 list.editTask(index, task);
                 
                 display_list();
             
         
    }
    }
}

 var mark_undone_click = function () {
    
         var returned = prompt("Enter the task number to mark back to regular:");
         if(!(returned == null)) {
         var index = parseInt(returned);
         index--;
         if ( list.validIndex(index) ) {
             task = list.getListItem(index);
             
             task = task.replace(" *DONE*", "");
             task = task.replace(" *In-Progress*", "");
                 list.editTask(index, task);
                 
                 display_list();
             
         
    }
    }
}

 var delete_all_click = function () {
    	
    	var theans = confirm("Do you really want to DELETE ALL ENTRIES?");
         
         if(theans) {
		
		
		
		
		var thenumb = list.getAllItemsBlank();
		
		for(var y=thenumb - 1 ; y>=0; y--)
		{
		list.deleteTask(y);
		
		}
		
		display_list();
		jsLib.cookies.deleteCookie("tasklist");
		
		
    }
}

window.onload = function() {
    $("add_task").onclick = add_task_click;
    $("edit_task").onclick = edit_task_click;
    $("delete_task").onclick = delete_task_click;
    $("mark_task").onclick = mark_done_click;
    $("unmark_task").onclick = mark_undone_click;
    $("unmark_inprogress").onclick = mark_inprogress_click;
    $("delete_all").onclick = delete_all_click;
    
    display_list();
   
}

window.onunload = function() {




}