$(document).ready(function() {

   function rebindTodo() {
	   
	  
	   
	   
      $("a.todoEdit").unbind("click").bind( "click", function() {
         var todo_id = $(this).attr('id');
         var todo_title = $(this).attr('title');
         var todo_href = $(this).attr('href');
         $('#inputAddTodo').val(todo_title);
         $('#inputAddTodoId').val(todo_id);
         $('#inputAddLink').val(todo_href);
         $('#doLinkLink').parent().remove();
		 $('.todoLink').hide();
         $('.newLink').show();
		 

         return false;
      });
      
	  $('#inputAddShout').change(function(){
		$(this).addClass('changed');								  
	  });
	  
	  $('#buttonAddShout').livequery(function(){
		  $(this).click(function() {
			 newshout = $('#inputAddShout').val();
			 if ($('#inputAddShout').hasClass('changed')) {
				 $.ajax({
					type:	 		"POST",
					url:			"/index/ajax-saveshout",
					data: 		{
					   shout: newshout
					},
					success: 	function(msg) {
					   if (!msg.error) {
						  $('.newsfeed .featured').next().removeClass('second');
						  $('.newsfeed .featured').after(msg);
						  $('.newsfeed .featured').next().addClass('second');
						  $('#inputAddShout').val('').removeClass('changed');
					   } else {
						  window.alert(msg.error);
					   }
					}
				 });
			 } else {
				$('#inputAddShout').css({
					borderColor: '#f00'
				});
				setTimeout(function(){
					$('#inputAddShout').css({
						borderColor: '#ABADB3 #E2E3EA #E2E3EA'
					});				
				}, 2000);
			}
			return false;
		  });
	  });
	  
	  $('.addNewShout').livequery(function(){
		currentShout = $('#inputAddShout').val();
		$('.addNewShout').submit(function(){   
			
			if ($('#inputAddShout').val() == currentShout) {
				$('#inputAddShout').css({
										border: '1px solid #f00'
										})
				return false;
			}
		});
	});

      $(".todoCheckbox").unbind("click").bind( "click", function() {
		 $(this).addClass('checkedBox');
         id = $(this).parents('li').attr("id");
         $.ajax({
            type:	 		"POST",
            url:			"/index/ajax-checktodo",
            data: 		{
               wb_id:id
            },
            success: 	function(msg) {
               if (!msg.error) {
                  $("#divAddTodo").html(msg);
                  rebindTodo();
               } else {
                  window.alert(msg.error);
               }
            }
         });
         return false;
      });

      $("a.showDone").unbind("click").bind( "click", function() {
         $.ajax({
            type:	 		"POST",
            url:			"/index/ajax-loadtodo",
            data: 		{
               done:1
            },
            success: 	function(msg) {
               if (!msg.error) {
                  $("#divAddTodo").html(msg);
                  rebindTodo();
               } else {
                  window.alert(msg.error);
               }
            }
         });
         return false;
      });

      $("a.showNotDone").unbind("click").bind( "click", function() {
         $.ajax({
            type:	 		"POST",
            url:			"/index/ajax-loadtodo",
            data: 		{
               done:0
            },
            success: 	function(msg) {
               if (!msg.error) {
                  $("#divAddTodo").html(msg);
                  rebindTodo();
               } else {
                  window.alert(msg.error);
               }
            }
         });
         return false;
      });

      $("a.todoDelete").unbind("click").bind( "click", function() {
         id = $(this).parent().parent().attr("id");
         $.ajax({
            type:	 		"POST",
            url:			"/index/ajax-deletetodo",
            data: 		{
               wb_id:id
            },
            success: 	function(msg) {
               if (!msg.error) {
                  $("#divAddTodo").html(msg);
                  rebindTodo();
               } else {
                  window.alert(msg.error);
               }
            }
         });
         return false;
      });


      $("ul.toDo").sortable({
         opacity: 0.5,
         cursor: 'move',
         handle: '.handle',
         update : function () {
            serialize(this);
         }
      });
      //$("ul.toDo").disableSelection();

   }

   function serialize(t) {
      var query = $(t).attr('id');
      $(t).children().each(function(t) {
         query += ":" + $(this).attr('id');
      });
      $.ajax({
         type:	 		"POST",
         url:			"/index/ajax-ordertodo",
         data: 			{
            query:query
         },
         /*dataType:		"json",*/
         success: 		function(msg) {
            $('#divAddTodo').html(msg);
            rebindTodo();
         }
      });
      return false;
   }

   $("#buttonAddTodo").unbind("click").bind( "click", function() {
      var todo = $('#inputAddTodo').val();
      var todoid = $('#inputAddTodoId').val();
      var link = $('#inputAddLink').val();
      $.ajax({
         type:	 		"POST",
         url:			"/index/ajax-savetodo",
         data: 		{
            todo: todo,
            todo_id: todoid,
            link: link
         },
         success: 	function(msg) {
            $("#divAddTodo").html(msg);
            $('#inputAddTodo').val('');
            $('#inputAddTodoId').val('');
            $('#inputAddLink').val('');
            rebindTodo();
         }
      });
      return false;
   });

   
   rebindTodo();

});
