$(document).ready(function() {

    if($("li.notification-menu-item").length != 0) {
        fetchNewNotificationCount();
        setInterval('fetchNewNotificationCount()', 20000);
    }

});

function updateUnreadNotifications(amount)
{
    
    var house = $('#homebutton');
    var link = house.parent();
    var counter = $('#msg_count');
    var stub;
    if (amount != '0') {
	if (amount > 1) {
	    stub = ' nye beskeder ';
	} else {
	    stub = ' ny besked '
	}
	house.attr('src', 'fileadmin/template/images/icons/home_white_full.png');
	link.parent().attr('title', amount + stub + 'på min intranet forside');
	if (amount > 9) { amount = '@'; }
	counter.html(amount);
	
    } else {
	house.attr('src', 'fileadmin/template/images/icons/home_white_empty.png');
	counter.html('');
	link.parent().attr('title', 'Min intraforside');
    }

}

function fetchNewNotificationCount()
{

    $.ajax({
        url      : 'fileadmin/template/ajax/updatesping.php',
        type     : 'POST',
        dataType : 'json',
        success  : function(json) {

            if(json.result == 'success') {
                updateUnreadNotifications(json.new_updates);
            }

        }
    });

}




