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