function removeRestaurantAlias(alias) {
    
    var all = $.cookie('restaurant_alias');
    if (!all) return;
    all = all.split('|');
    for (var i=0; i<all.length; i++) {
        if (all[i] == alias) {
            all.splice(i, 1);
            break;
        }
    }
    $.cookie('restaurant_alias', all.length ? all.join('|') : null);
    $('#restaurant_alias').get(0).style.display = 'none';
}

function setRestaurantAlias(value) {
    $('#restaurant_name').get(0).value = value;
    $('#restaurant_alias').get(0).style.display = 'none';
}

$(document).ready(function() {
    if ($.cookie('restaurant_alias') && $.cookie('restaurant_alias').split('|').length == 1) {
        $('#restaurant_name').get(0).value = $.cookie('restaurant_alias');
    }
    
    $('#restaurant_name').focus(function() {
        var aliases = $.cookie('restaurant_alias');
        if (!aliases) return;
        $('#restaurant_alias').html('');
        
        $(aliases.split('|')).each(function(index, value) {
           
            $('#restaurant_alias').append(
                
                "<img src='images/close.png' style='float: right; margin-left: 10px; color: white;' onclick=\"removeRestaurantAlias('"+escape(value)+"')\" />"+
                "<a style='padding: 10px; color: white' href=\"javascript: setRestaurantAlias('"+escape(value)+"');\">".concat(
                value
                ).concat("</a><br />")
            )
            
        });
        
        $('#restaurant_alias').css('display', 'block');
    });
    
    $("#status").dialog();
});
