$(document).ready(function() {
    
    //Search field in the table title
    $("input[name=search]").focus(function() {
        if($(this).val() == "Search")
            $(this).val("");
    });
    $("input[name=search]").blur(function() {
        if($(this).val() == "")
            $(this).val("Search");
    })
    
    //Edit and delete icons in the table
    $(".contacts tr, .subscriptions tr, .notifications tr, .tickets tr").mouseover(function() {
        if(!$(this).hasClass("table-row")) {
            $(this).children().last().children('div').children().css('display','block');
            $(this).addClass('selected-hover');
        }
    });
    $(".contacts tr, .subscriptions tr, .notifications tr, .tickets tr").mouseout(function () {
        if(!$(this).hasClass("table-row")) {
            $(this).children().last().children('div').children().css('display','none');
            $(this).removeClass('selected-hover');
        }
    });
    $(".tickets tr").mouseover(function() {
        if(!$(this).hasClass("table-row")) {
            $(this).children().last().children('div').children().css('display','block');
            $(this).addClass('selected-hover');
            $(this).children().last().children('a').addClass('icons-shown');
        }
    });
    $(".tickets tr").mouseout(function() {
        if(!$(this).hasClass("table-row")) {
            $(this).children().last().children('div').children().css('display','none');
            $(this).removeClass('selected-hover');
            $(this).children().last().children('a').removeClass('icons-shown');
        }
    });
    
    //Checkboxes in the contacts table
    $(".contacts input[type=checkbox]").click(function() {
        var tableRow = $(this).parent().parent();
        if($(this).prop('checked'))
            tableRow.addClass('selected');
        else
            tableRow.removeClass('selected');
        checkSelection();
    });

    $(".select-all").click(function() {
        $(".contacts input[type=checkbox]").each(function() { $(this).prop('checked',true); });
        setSelected();
    });

    $(".form-trigger").click(function () {
        $(this).parents("form").submit();
        return false;
    });
    
});

function checkSelection()
{
    unsetSelected();
    $(".contacts input[type=checkbox]").each(function() {
        if($(this).prop('checked')) setSelected();
    });
}

function setSelected()
{
    $(".table-footer .selection").html("<a href='#' class='unselect'>Unselect</a><a href='#' class='remove'>Remove</a>");
    $(".unselect").click(function() {
        $(".contacts input[type=checkbox]").each(function() 
        { 
            $(this).prop('checked',false); 
            $(this).parent().parent().removeClass('selected');
        });
        unsetSelected();
    });
}

function unsetSelected()
{
    $(".table-footer .selection").html("<a href='#' class='select-all'>Select All</a>")
    $(".select-all").click(function() {
        $(".contacts input[type=checkbox]").each(function() 
        { 
            $(this).prop('checked',true); 
            $(this).parent().parent().addClass('selected');        
        });
        setSelected();
    });
}
