
    $(document).ready( 
        function () {
            var displayHidden = false;
            function updateOrder()
            {
                var items = $(".todolistitem, .todolistdone");
                todoItems = "";
             
                for(var x=0; x < items.length; x++)
                {
                    todoItems += items[x].id + ',';
                }
             
                var updateInfo = {};
                updateInfo.list_id = $("#todo_list_id").val();
                updateInfo.order = todoItems;
                $.post('/trial/updateorder',updateInfo, 
                    function(data){
                        $('#notifications').html(data);
                        $('#notifications').fadeIn(1000).fadeTo(1500, 1).fadeOut(1000);
                    },'html');
            }
            function showNotification(notificationText)
            {
                $('#notifications').html(notificationText);
                $('#notifications').fadeIn(1000).fadeTo(1500, 1).fadeOut(1000);
            }
            function bindEvents()
            {
                $("#todo_list").sortable(
                {
                    update:updateOrder,
                    scroll: true,
                    revert: true, 
                    opacity: 0.6,
                    handle: $(".todoTitle") 
                });
                $('.todoDelete').toggle(
                    function(){
                        var obj = {};
                        obj.id = $(this).parent().attr('id');
                        obj.list_id = $('#todo_list_id').val();
                        currentElement = $(this);
                        $.post('/trial/getiteminfo', obj, function(data){
                            currentElement.siblings('.itemExtras').children('.itemInfo').html(data);
                            currentElement.siblings('.itemExtras').slideDown();
                        },'html');
                        $(this).text('-');
                    },
                    function(){
                        $(this).siblings('.itemExtras').slideUp();
                        $(this).text('+');
                    }
                );
                $('.confirmDelete').click(
                    function()
                    {
                        $(this).parent().stopTime('confirm');
                        var objtodel = {};
                        objtodel.id = $(this).parents('.todolistitem, .todolistdone').attr('id');
                        $.post('/trial/deletetodo', objtodel, function(data){
                                $('#' + objtodel.id).fadeOut('slow'); 
                                $('#notifications').html(data);
                                $('#notifications').fadeIn(1000).fadeTo(1500, 1).fadeOut(1000);
                            },'html');
                        return false;
                    }
                );
                $('.todoComplete').click(
                    function()
                    {
                        var objtodel = {};
                        objtodel.id = $(this).parent().attr('id');
                        objtodel.list_id = $('#todo_list_id').val();
                        if($('#' + objtodel.id).hasClass('todolistitem'))
                        {
                            $.post('/trial/completetodo', objtodel, function(data){
                                $('#' + objtodel.id + ' .todoComplete').unbind('mouseenter').unbind('mouseleave');
                                $('#' + objtodel.id + ' img').attr('src','/img/complete.png');
                                if(displayHidden == false)
                                {
                                    $('#' + objtodel.id).fadeTo(1500, 1).slideUp('slow');
                                }
                                $('#' + objtodel.id).removeClass('todolistitem').addClass('todolistdone');
                                showNotification(data);
                            },'html');
                        } else {
                            $.post('/trial/incompletetodo', objtodel, function(data){
                                $('#' + objtodel.id).removeClass('todolistdone'); 
                                $('#' + objtodel.id).addClass('todolistitem');  
                                $('#' + objtodel.id + ' img').attr('src','/img/incomplete.png');
                                $('#' + objtodel.id + ' .todoComplete').hover(
                                    function(){
                                        $(this).children('img').attr('src', '/img/complete.png');
                                    },
                                    function() {
                                        $(this).children('img').attr('src', '/img/incomplete.png');
                                    }
                                );
                                showNotification(data);
                            },'html');
                        }
                        return false;
                    }
                );
                $('.todolistitem .todoComplete').hover(
                    function(){
                        $(this).children('img').attr('src', '/img/complete.png');
                    },
                    function() {
                        $(this).children('img').attr('src', '/img/incomplete.png');
                    }
                );
                $('.todoTitle').dblclick(function(e){
                    $(this).hide();
                    $(this).siblings('.todoEdit').show();
                    var textVal = $(this).text();
                    $(this).siblings('.todoEdit').children('.text').caret(textVal.length);
                    var textCount = 140 - textVal.length;
                    $(this).siblings('.todoEdit').children('.editCounter').text(textCount + ' left');
                });
                
                $('.todoEdit').submit(
                    function() {
                        $.post('/trial/edittodo', $(this).serialize(),
                            function (data) {
                                $('#item'+data.id+' .todoTitle').html(data.title);
                                $('#item'+data.id+' .todoTitle').show();
                                $('#item'+data.id+' .todoEdit').hide();
                                $('#item'+data.id+' .todoEdit .text').blur();
                                showNotification('Updating');
                            }, 'json');
                        return false;
                    }
                );
                $('.text, #todo_title').keyup().keydown(
                    function(e){
                        var key = e.charCode || e.keyCode || 0;
                        if (key == 27)
                        {
                            $(this).val($(this).parent('.todoEdit').siblings('.todoTitle').text());
                            $(this).parent('.todoEdit').hide();
                            $(this).parent('.todoEdit').siblings('.todoTitle').show();
                            $(this).blur();
                            showNotification('Cancelled');
                        } else {
                            var textVal = $(this).val();
                            var textCount = textVal.length;
                            var maxChars = 140;
                            if (textCount >= maxChars)
                            {
                                $(this).val(textVal.substring(0,maxChars-1));
                            }
                            $(this).siblings('.textCounter, .editCounter').html(maxChars - textCount + '<br/> left');
                        }
                    }
                );
                $('.text').blur(
                    function(e){
                        var existingVal = $(this).parent('.todoEdit').siblings('.todoTitle').text();
                        $(this).val(existingVal);
                        $(this).parent('.todoEdit').hide();
                        $(this).parent('.todoEdit').siblings('.todoTitle').show();
                    }
                );
                if(displayHidden == true)
                {
                    $('.todolistdone').show();
                } else {
                    $('.todolistdone').hide();
                }
            }
            $('#add_todo').submit(
                function () {
                    if (($('#todo_title').val() == 'click here, add a todo, press enter.') || $('#todo_title').val() == "")
                    {
                        $('#todo_title').blur();
                        return false;
                    }
                    $.post('/trial/addtodo', $('#add_todo').serialize(),
                        function(data){
                            $("#todo_list").sortable('destroy');
                            $('#todo_list').html(data);
                            $('#todo_title').val('');
                            $('#todo_title').focus();
                            $('.textCounter').text('');
                            bindEvents();
                            showNotification('Updating');
                            updateOrder();
                        }, 'html');
                    return false;
                }
            );
            $('#todo_title').blur(
                function() {
                    if( $(this).val() == "")
                    {
                        $('.textCounter').text('');
                    }
                }
            );
            $('#show_completed').click(
                function () {
                    if(displayHidden == true)
                    {
                        $('.todolistdone').slideUp();
                        displayHidden = false;
                    } else {
                        $('.todolistdone').slideDown();
                        displayHidden = true;
                    }
                    return false;
            });
            $('#todo_title').Watermark('click here, add a todo, press enter.');
            bindEvents();
        }
    );