// JavaScript Document
$(function(){
	$('#user-function .notif-icon li a').click(function(){
		e = $(this);
		e.addClass('active');
		$('.' + e.attr('rel')).find('loading').show();
		/*$.ajax({
			url: e.attr('hrefx'),
			type: 'post',
			dataType: 'json',
			success: function(data){
				$('.' + e.attr('rel')).find('loading').hide();
				//console.log(data);
				html = "";
				$.each(data, function(k, v){
					html += "<li class='btn' onclick=\"window.location.href='" + v.link + "'\">";
					html += "<a class='avatar' style=\"background-image: url(\'" + v.sender.avatar + "\')\"></a>";
					html += "<div class='info'>";
					html += "<div class='username'>" + v.sender.username + "</div>";
					html += "<div class='m-title'>" + v.subject + "<span> - " + v.time_str + "</span></div>";
					html += "</div><div class='cl'></div>";
					html += "</li>";
				});
				$('.' + e.attr('rel')).find('ul').html(html);
				notifScrolling();
			}
		});*/
		$(".notification").not("." + $(this).attr('rel')).fadeOut("fast");
		$('#user-function .notif-icon li a[rel!="' + $(this).attr('rel') + '"]').removeClass('active');
		$('.' + $(this).attr('rel')).fadeToggle(300);
		return false;
	});
	
	$(document).click(function(e) {
		var $clicked = $(e.target);
		if (!$clicked.parents().hasClass('notification')) {
			$('#user-function .notif-icon li a').removeClass('active');
			$('.notification').fadeOut("fast");
		}
	});
	checkCountNotif();
});

function checkCountNotif() {
	$.each($('#user-function .notif-icon li a'), function(k, v){
		if($(v).find('span').length > 0) {
			sp = $(v).find('span');
			c = parseInt(sp.html());
			if(c > 0) {
				sp.show();
			} else
				sp.hide();
		}
	});
}

function notifScrolling() {
	var bars = '.jspHorizontalBar, .jspVerticalBar';
	$('.scrolling-list').bind('jsp-initialised', function (event, isScrollable) {
		//hide the scroll bar on first load
		$(this).find(bars).hide();
	}).jScrollPane().hover( function () {
		$(this).find(bars).stop().fadeTo('fast', 0.9);
	}, function () {
		$(this).find(bars).stop().fadeTo('fast', 0);
	});
}