function debug(msg) {
    $("#debug").prepend("<p>"+msg+"</p>");
}

$(document).ready(function() {

    $("input#recipient_names").each(function(i){
        $(this).keyup(function(){
            // alert($(this).val())
            var term = $(this).val()

            // the input value could be a comma seperated
            // string so split on the comma and use the
            // last array item as term
            term = term.split(',')
            term = term.pop()
            var search_input = $(this)
            $.get('/search/ajaxColleagues/'+term, {},function(data){
                $("#ajax-users").remove()
                $(search_input).after(data)
                $("#ajax-users").find("a").each(function(i){
                    $(this).click(function(e){
                        var new_search = search_input.val()
                        if (new_search.indexOf(',') > -1){
                            new_search = new_search.split(',')
                            new_search.pop()
                            new_search.push($(this).text())
                            new_search = new_search.join()
                        }
                        else{
                            new_search = $(this).text()
                        }
                        search_input.val(new_search)
                        e.preventDefault()
                    })
                })
            })
        })
    })
    
    
    
    $("div.entry:not(.no-hover)").each(function(i) {
        // add in icon beside entries
        if ($(this).attr("class").indexOf("icon-") != -1) {
            $(this).prepend("<span class=\"entry-icon\"></span>");
        }

        // if an entry has a "deletable" class, add the delete icon
        if ($(this).hasClass("deletable")) {
            try{
                var delete_id = $(this).attr('class').match(/message_\d+/)
                delete_id = delete_id.toString().replace(/message_/, '');
            }
            catch(e){
                var delete_id = $(this).attr('id').match(/entry-\d+/)
                delete_id = delete_id.toString().replace(/entry-/, '');
            }

            var controller = window.location.toString().replace('http://','');
            controller = controller.split('/')[1];
            var delete_link = $('<a class="entry-delete">Delete</a>');
            delete_link.attr('href','/'+controller+'/ajax_delete/'+delete_id+'/');
            $(this).prepend(delete_link);
        }

        // hover states
        $(this).mouseover(function(e) {
            $(this).addClass("entry-hover");
        });
        $(this).mouseout(function(e) {
            $(this).removeClass("entry-hover");
        });
    });

    // get last content block of a column, and add dotted border to bottom
    // IF it doesn't contain an entry-list UL (which will have a dotted bottom border already)
    $("div.column").each(function() {
        $("div.module:last div.content:not(:has(ul.entry-list)):last", this).addClass("bg-dot");
    });

    // add hover states for entries in lists
    $("ul.entry-list li").each(function() {
        $(this).mouseover(function() { $(this).addClass("entry-hover"); });
        $(this).mouseout(function() { $(this).removeClass("entry-hover"); });
    });

    $("ul#nav-breadcrumb li a:not(:last)").each(function() {
        $(this).parent().after("<li>&raquo;</li>")
    });

    $("#footer ul li a:not(:last)").each(function() {
        $(this).parent().after("<li>|</li>")
    });

    removeEndingDottedBorders();

    // build events for delete icon
    $("div.entry a.entry-delete").livequery("click", function(e) {
        var id = $(this).parent("div.entry").attr("id").replace(/entry-/, "");
        $("#entry-" + id).slideUp("slow ", function() {
            removeEndingDottedBorders();
        });
        var target = window.location.toString().replace("http://","");
        var controller = target.split("/")[1];
        target = target.split("/")[0];
        target = "http://"+target+$(this).attr("href");
        $.get(target, function(data){
            if (controller == 'inbox'){
                var inbox = $("li.inbox").find("a");
                var header = $("h2 ~ div.title").find("h3")
                switch(data.toString()){
                    case '1':
                        var message = "Inbox (1 new)";
                        var message2 = "1 unread message";
                        break;
                    case '0':
                        var message = "Inbox";
                        var message2 = "0 unread messages";
                        break;
                    default:
                        var message = "Inbox ("+data+" new)";
                        var message2 = data + " unread messages";
                        break;
                }
                inbox.text(message)
                header.text(message2)
            }
        });
        return false;
    });


    /*$("form#compose, form#reply, form#blackboard-post").hide();
    var msg = '';
    $("form#compose").find('input').each(function(i){
        if($(this).attr("type") == "text" && $(this).val() != ''){
            msg = 'show';
        }
    });
    if (msg == 'show'){
        $("form#compose").show();
    }

    $("a[href='#compose'], a[href='#reply'], a[href='#blackboard-post']").click(function() {
        if (!isIE6) {
            $($(this).attr("href")).slideToggle();
        } else {
            $($(this).attr("href")).toggle();
        }
        return false;
    });*/

    $("form input, form textarea, form select")
    .focus(function() { $(this).addClass("input-focus"); })
    .blur(function() { $(this).removeClass("input-focus"); });

    // add multiple recipients to a message
    $("form span.add").livequery("click", function(e) {
        $(this).hide();
        var select = $("div.recipient select:first");
        $("div.recipient").append($(select).clone());
        $("div.recipient select:last").after($(this).clone().show());
        return false;
    });

    $("div.column-half div#login div.entry, div.column-half div#register div.entry").equalizeCols();

    function removeEndingDottedBorders() {
        $("div.module div.content").each(function(i) {
            $("div.entry:visible:last", this).addClass("no-bg");
        });
    };
    
});
