// JavaScript Document
var $n = jQuery.noConflict();


$n.messageDiv = function(message, id, style){
        var $div = $n('<div>').addClass(style);
        //var $message = $('<p>').appendTo($div);

       // $message.html(message);
	   
        $div.append(message); 
		$div.append($n.dismissButton(id));
		$div.attr("id","notification_" + id);
		
		return $div;
}

$n.dismissButton = function(id){
	
	var a = document.createElement('a');
	a.href = '#';
	a.title = 'Close';
	var $aImg = $n(a);
	var $aText = $aImg.clone();
	$aText.addClass("closeLink");
	
	var img = document.createElement('img');
	img.src="/notifications/close.gif";
	img.alt="Dismiss";
	img.border=0;
	var $img = $n(img);
	
	
	$aImg.bind("click",{id:id},dismissNotification);
	$aImg.append($img);
	
	$aText.bind("click",{id:id},dismissNotification);
	$aText.append("close");
	
	var p = document.createElement('p');
	$p = $n(p);
	$p.append($aImg);
	$p.append(" ");
	$p.append($aText);
	
	return $p;
}

function dismissNotification(event){
	$n("#notification_" + event.data.id).fadeOut("slow");
	$n.getJSON(
			  "/notifications/notifications.php",
			  {action: 'dismiss', id: event.data.id}
			  );
}

function checkNotifications(username){
	if(username != null && username.length != 0){
		$n.getJSON(
			  "/notifications/notifications.php",
			  {username: username, action: 'check'},
			  showNotifications
			  );
	}
}

function showNotifications(data){
	$n('#globalNotifications').hide();
	$n('#globalNotifications').empty();
	if(data.length > 0){
		$n.each(data,function(i,item){
							 $n('#globalNotifications').append($n.messageDiv(item.content, item.id, 'individualNotification'));
							 });
	
		$n('#globalNotifications').show("bounce");
	}
}

$n(document).ready(function() {
   $n('#globalNotifications').hide();
});

