$(document).ready(function() {
    // add a new todo
    $('#new').click(function(){
        $('body').prepend('<div id="black_layer">&nbsp;</div>'); 
        $('#black_layer').addClass('new_todo').animate({ opacity: '.7' }, 500);  
        $('#box_new').fadeIn(); 
        $('#box_new input[name="todo"]').focus(); 
    }); 
    $('#box_new button[name="cancel"], #black_layer').live('click', function(){
        if($('#black_layer').hasClass('new_todo')){
            $('#box_new').fadeOut(); 
            $('#box_new input[name="todo"]').val(''); 
            $('#black_layer').animate({ opacity: '0' }, 500, function(){
                $('#black_layer').remove(); 
            }); 
            return false; 
        }
    }); 
    // show/hide the note-area
    $('#box_new span').toggle(function(){
        $('#box_new textarea').slideDown(); 
        $('#box_new textarea').focus(); 
    },
    function(){
        $('#box_new textarea').slideUp(); 
    }); 

    $('input[name="email"]').focus(function(){
        if($(this).val() == "email"){
            $(this).val(''); 
        }
    }); 
    $('input[name="email"]').blur(function(){
        if($(this).val() == ""){
            $(this).val('email'); 
        }
    }); 
    $('input[type="password"]').focus(function(){
        if($(this).val() == "password"){
            $(this).val(''); 
        }
    }); 
    $('input[type="password"]').blur(function(){
        if($(this).val() == ""){
            $(this).val('password'); 
        }
    }); 

    // clear input of 'new project'
    $('#form_projectname').focus(function(){
        if($(this).val() == "projectname"){
            $(this).val(''); 
        }
    }); 
    $('#form_projectname').blur(function(){
        if($(this).val() == ""){
            $(this).val('projectname'); 
        }
    }); 

    // show/hide project-layer
    $('#project_nav > span').click(function(){
        if($('#project_details').css('display') == 'none'){
            $('#project_details').slideDown(); 
            $('#project_details .graph span').each(function(){
                var percent = $(this).css('width'); 
                $(this).css('width','0').animate({ width: percent }, 1000); 
            }); 
            $('#todo_list').animate({width: '50%', opacity: '0.5'}, 500); 
        }
        else{
            $('#project_details').slideUp(); 
            $('#todo_list').animate({width: '60%', opacity: '1'}, 500); 
        }
    }); 
    
    // show/hide 'new project'-area
    $('#new_project').toggle(function(){
        $('#project_add').slideDown(); 
    }, 
    function(){
        $('#project_add').slideUp(); 
    }); 

    // show/hide delete-options of a project
    $('#project_details ol li span').toggle(function(){
        var id = $(this).parent().parent().attr('rel'); 
        $('#project_details ol li[rel='+id+'] form').slideDown(); 
    }, 
    function(){
        var id = $(this).parent().parent().attr('rel'); 
        $('#project_details ol li[rel='+id+'] form').slideUp(); 
    }); 

    // show 'move to'-area if 'move'-option is selected in the delete-options
    $('#project_details ol li form fieldset input:radio').click(function(e){
        var id = $(this).parent().parent().parent().attr('rel'); 
        if($(e.target).val() == 'move'){
            $('#project_details ol li[rel='+id+'] form fieldset select').fadeIn(); 
        }
        else{
            $('#project_details ol li[rel='+id+'] form fieldset select').fadeOut(); 
        }
    }); 

    // sort-function for todos
    $('#todo_list').sortable({ axis: 'y' }); 
    $('#todo_list').bind("sortupdate", function(event, ui) {
        var new_pos_list = ''; 
        var id_tmp = ''; 
        for(var i = 0; i < $('#todo_list li').length;){
            id_tmp = ''; 
            id_tmp = $('#todo_list li:eq('+i+')').attr('id'); 
            new_pos_list += id_tmp+'_'+pos_alt[i]; 
            if(i < $('#todo_list li').length - 1){
                new_pos_list += '§yxz$'; 
            }
            i++; 
        }
        var ajaxfile = '/set.php'; 
        $.ajax({type: 'POST', url: ajaxfile, data: 'rm=sorting&pos_string='+new_pos_list, dataType: 'json', success: 
            function(response){ 
                if(response['status'] != 'okay'){
                    $('body #content').prepend('<p id="error">'+response['msg']+'</p>'); 
                }
            }
        }); 
    }); 
    
    // fadeIn for todos
    var timer = 0;
    $('#todo_list li').each(function(i){
        var _this = this;
        window.setTimeout(function(){$(_this).animate({left: '0', opacity: '1'}, 500);}, timer);
        timer += 200; 
    }); 
    
    // show todo edit form
    $('#todo_list li .icon_edit_todo').click(function(){
        if(!$(this).parent().hasClass('active')){
            var id = $(this).parent().attr('id'); 
            $('body').prepend('<div id="black_layer">&nbsp;</div>'); 
            $('#black_layer').addClass('edit_todo').animate({opacity: '.7'}, 500); 
            $(this).parent().addClass('active'); 
            var width = $('body').width(); 
            var box_width = Math.floor(width/100 * 60); 
            var offset = Math.floor(width/2 - box_width/2); 
            $(this).parent().css('width',box_width); 
            $(this).parent().animate({top: '20%', left: offset}, 500, function(){
                $('#'+id+' .todo_details').slideDown().animate({opacity: '1'}, 500); 
            }); 
            $(this).parent().prepend('<span id="close" title="close this menu">&#10006;</span>'); 
            $('#todo_list').sortable('disable'); 
        }
    }); 
    $('#todo_list li #close, #black_layer').live('click', function(){
        if($('#black_layer').hasClass('edit_todo')){
            var id = $('#todo_list li.active').attr('id'); 
            $('#close').fadeOut('fast').remove(); 
            $('#black_layer').animate({opacity: '0'}, 500, function(){
                $('#black_layer').remove(); 
            }); 
            $('#'+id+' .todo_details').hide().css('opacity','0'); 
            $('#'+id).css('left','0').css('top','auto').css('width','100%'); 
            $('#'+id).removeClass('active'); 
            $('#todo_list').sortable('enable'); 
        }
    }); 
    
    // switch highlighting of work/private-status on editing todo
    $('.todo_options label.kat_edit_work, .todo_options label.kat_edit_private').click(function(){
        var id = $(this).parent().parent().parent().parent().attr('id'); 
        if(!$(this).hasClass('active')){
            if($(this).hasClass('kat_edit_work')){
                $(this).addClass('active'); 
                $('#'+id+' .todo_options label.kat_edit_private').removeClass('active'); 
            }
            else{
                $(this).addClass('active'); 
                $('#'+id+' .todo_options label.kat_edit_work').removeClass('active'); 
            }
        }
    }); 

    // switch highlighting of work/private-status on new todo
    $('#box_new label.kat_work, #box_new label.kat_private').click(function(){
        if(!$(this).hasClass('active')){
            if($(this).hasClass('kat_work')){
                $(this).addClass('active'); 
                $('#box_new label.kat_private').removeClass('active'); 
            }
            else{
                $(this).addClass('active'); 
                $('#box_new label.kat_work').removeClass('active'); 
            }
        }
    }); 
    
    // switch highlighting of week-status on editing todo
    $('.todo_options label.week_option_edit').click(function(){
        if($(this).hasClass('active')){
            $(this).removeClass('active'); 
        }
        else{
            $(this).addClass('active'); 
        }
    }); 

    // switch highlighting of week-status on new todo
    $('#box_new label.week_option').click(function(){
        if($(this).hasClass('active_week')){
            $(this).removeClass('active_week'); 
        }
        else{
            $(this).addClass('active_week'); 
        }
    }); 
}); 
