$(function(){
    checkNotifies();
    
    function checkNotifies() {
        $.getJSON('/system/notifications.php',{ type: 'notifications', cmd: 'count' }, function(data) {
            $.each(data, function(key, val) {
                if (key != 'sendback' && key != 'title') {
                    switch (key) {
                        case 'notifications':
                            if (val > 0)
                                $("#notifications .notify").text(val).show();
                        break;
                        case 'mates':
                            if (val > 0)
                                $("#mates .notify").text(val).show();
                        break;
                        case 'post':
                            if (val > 0)
                                $("#post .notify").text(val).show();
                        break;
                    }
                }
            });
        });
    }
    
    
    
    $(document).click(function(event){
        checkNotifies();
        
        var target = $(event.target).attr('class');
        var target_id = $(event.target).attr('id');
        
        //alert(target_id);
        
        if ($(".notify-box").is(':visible') && target != 'icon' && target != 'notify' && target != 'notify-box')
            $(".notify-box").hide();
        
   });
    
   $("#user-interactive .icon").click(function(event){
        var target = $(event.target).attr('class');
        var target_id = $(event.target).attr('id');
        var show = true;
        if ($(this).children(".notify-box").is(':visible') && target != 'notify-box') show = false;

        $('.notify-box').hide();
        
        //alert(target);
        
        if (target == 'notify') {
            target_id = $(this).attr('id');
        }
        
        if (show) {
            $(this).children(".notify-box").show(1,function(){
                var sendback = $(this).attr('id');
                $.getJSON('/system/notifications.php',{ type: target_id, cmd: 'list', sback: sendback }, function(data) {
                    var items = '';
                    var error = '';
                    
                    $.each(data, function(key, val) {
                        var haystack = ['sendback','title','error','l_title','main_link'];
                        if ($.inArray(key,haystack) < 0)
                            items += '<a href="#" onclick="checked(\''+val.ref+'\',\''+val.id+'\');return false;">'+ val.text +'<br /><font class="time">'+val.time+'</font></a>';
                    });
                    
                    var header = '<h2>'+data.title+'</h2>';
                    
                    if (data.error)
                        error = '<p>'+data.error+'</p>';
                    
                    var bottom = '<a href="'+data.main_link+'" class="bottom">Se alle '+ data.l_title +'</a>';
                    
                    $('#'+data.sendback).html(header + ' ' + items + ' ' + error + ' ' + bottom);
                });
            });
        }
   }); 
});

function checked(url, nid) {
    $.getJSON('/system/notifications.php',{ type: 'notifications', cmd: 'check', id: nid });
    location.href = url;
}
