var NotificationsList = function (notificationBarId) {
	var n_overlays = new Array();
	var n_types = { message: 0, chat: 0, kiss: 0 };
	var _timer = null;
	var _target = null;
	var _timeout = new Array();
	
	this.loadNotify = function (notify) {
		if(YAHOO.util.Dom.get(notificationBarId)) append(notify);
	};
	
	var initialize = function () {
		for(var key in n_types)
			if(n_types[key] == 0)
				createOverlay(key);
		
		// Carica le notifiche di messaggi e baci
		try {
			if(notPersistentNotifications)
				DwrManager.loadNotPersistentNotifications(notPersistentNotifications);
		} catch (e) {}
	};
	
	var createOverlay = function (type) {
		n_overlays[type] = new YAHOO.widget.Panel('_' + type, { width: '200px',
																visible: false,
																zindex: 1000,
																close: false,
																draggable: false,
																context: ['n_' + type, 'tr', 'tl']
															});
		
		n_overlays[type].setHeader('');
		n_overlays[type].setBody('');
		n_overlays[type].setFooter('');
		
		n_overlays[type].renderEvent.unsubscribe();

		n_overlays[type].render(document.body);
		
		YAHOO.util.Dom.addClass('_' + type, 'notifiche');
		
		YAHOO.util.Event.on('n_' + type, 'mouseover', _anchorOver, { overlay: n_overlays[type], type: type }, true);
		YAHOO.util.Event.on('n_' + type, 'mouseout', _hide);
		YAHOO.util.Event.on('_' + type, 'mouseover', _itemOver);
		YAHOO.util.Event.on('_' + type, 'mouseout', _hide);
	};
	
	var append = function (notify) {
		var type = notify.webNotification.type;
		var detail = createDetail(notify);
		var line = null;
		
		if(_getNotificationsCount(type) < 10) {
			if(_getNotificationsCount(type) > 0) {
				line = document.createElement("div");
				YAHOO.util.Dom.setAttribute(line, 'class', 'n_line');
			}
			
			if(line) n_overlays[type].appendToBody(line);
					
			n_overlays[type].appendToBody(detail);
				
			// Associa il timer per rimuovere l'utente alla scadenza
			if(notify.webNotification.timeToLive > 0) {
				// Inizializzazione
				if(!_timeout[type])
					_timeout[type] = new Array();
				
				_timeout[type][notify.webNotification.id] = window.setTimeout(function () {
					remove(notify.webNotification.id, type);
				}, notify.webNotification.timeToLive * 1000);
			}
		}
		
		if(_getNotificationsCount(type) < 11) updateCounter(type, 1);
	};
	
	this.removeNotify = function (nid, ntype) {
		remove(nid, ntype);
	};
	
	this.reset = function (type) {
		if(!type)
			for(var type in n_types)
				this.resetByType(type);
		else
			this.resetByType(type);
		
		// Carica le nuove notifiche
		initialize();
	};
	
	this.resetByType = function (type) {
		// Azzera i contatori
		updateCounter(type, -_getNotificationsCount(type));
		n_types[type] = 0;		
	
		// Cancella l'overlay
		n_overlays[type].setBody("");
		
		// Azzera i timer
		for(var id in _timeout[type])
			window.clearTimeout(_timeout[type][id]);
	};
	
	var remove = function (n_id, n_type) {
		if(_timeout[n_type][n_id]) clearTimeout(_timeout[n_type][n_id]);
		
		var item = YAHOO.util.Dom.get(_getNotifyDivId(n_id, n_type));
		if(item) {
			var parent = item.parentNode;
			
			// Cancella la linea di divisione
			var previousSibling = YAHOO.util.Dom.getPreviousSibling(item);
			var nextSibling = YAHOO.util.Dom.getNextSibling(item);
			
			if(previousSibling) parent.removeChild(previousSibling);
			else if(nextSibling) parent.removeChild(nextSibling);
			
			parent.removeChild(item);
			updateCounter(n_type, -1);
		}
	};
	
	var createDetail = function (notify) {
		var item = document.createElement("div");
		YAHOO.util.Dom.setAttribute(item, "id", _getNotifyDivId(notify.webNotification.id, notify.webNotification.type));
		YAHOO.util.Dom.addClass(item, "notify_detail");
		
		item.innerHTML = formatDetails(notify);
						
		return item;
	};
	
	var formatDetails = function (notify) {
		var user_home_url = Utilities.getUserUrl(notify.webNotification.sender.nick);
		
		return '<div class="avatar">' +
				'<a href="' + user_home_url + '" class="' + (notify.webNotification.sender.gender == "M" ? "male" : "female") + '">' +
					'<span></span>' +
					'<img src="' + Utilities.getUserAvatar(notify.webNotification.sender.avatar, notify.webNotification.sender.gender) + '" alt="">' +
				'</a>' +
				'</div>' + 
				'<div class="detail">' +
					'<p class="nickname"><a href="' + user_home_url + '">' + notify.webNotification.sender.nick + '</a></p>' +
					formatNotifyText(notify) +					
					'<div class="actions">' +
						notify.notifierFormat() +
					'</div>' +
				'</div>';
	};
	
	var formatNotifyText = function (notify) {
		if(notify.webNotification.type == 'message')
			return '<p class="message_preview">"' + notify.webNotification.text.substr(0, 20) + (notify.webNotification.text.length > 20 ? '...' : '') + '"</p>';			
		else if(notify.webNotification.type == 'kiss')
			return '<p class="kiss_date">' + Utilities.prettyDate(notify.webNotification.creationDate) + '</p>';
		else
			return '<p class="age_location">' + notify.webNotification.sender.ageLocation + '</p>';
	};
	
	var updateCounter = function (type, inc) {
		n_types[type] += inc;
		YAHOO.util.Dom.get('n_' + type).innerHTML = (type != 'chat' ? '<a href="http://' + conf.baseUrl + '/' + _link[type] + '" title="Vai">' : '') + _format[type](n_types[type]) + (type != 'chat' ? "</a>" : "");
		if(_getNotificationsCount(type) == 0) n_overlays[type].hide();
	};
	
	var _getNotifyDivId = function (notifyId, notifyType) {
		return "n_" + notifyType + "_" + notifyId;
	};
	
	var _getNotificationsCount = function (type) {
		/*try {
			return parseInt(YAHOO.util.Dom.get("n_n" + type).innerHTML);
		} catch (e) {
			return 0;
		}*/
		return n_types[type];
	};
	
	var _format = {
		message: function (num) {
			if(num == 0) // return 'Nessun messaggio';
				YAHOO.util.Dom.setStyle('n_message', 'display', 'none');
			else {
				YAHOO.util.Dom.setStyle('n_message', 'display', 'inline-block');
				if(num == 1) return 'Hai <b>1</b> nuovo messaggio';
				else if(num <= 10) return 'Hai <b>' + num + '</b> nuovi messaggi';
				else return "Pił di <b>10</b> messaggi";
			}
		},

		chat: function (num) {
			if(num == 0) // return 'Nessuna chat';
				YAHOO.util.Dom.setStyle('n_chat', 'display', 'none');
			else {
				YAHOO.util.Dom.setStyle('n_chat', 'display', 'inline-block');
				if(num == 1) return 'Hai <b>1</b> richiesta di chat';
				else if(num <= 10) return 'Hai <b>' + num + '</b> richieste di chat';
				else return "Pił di <b>10</b> richieste di chat";
			}
		},

		kiss: function (num) {
			if(num == 0) // return 'Nessun bacio';
				YAHOO.util.Dom.setStyle('n_kiss', 'display', 'none');
			else {
				YAHOO.util.Dom.setStyle('n_kiss', 'display', 'inline-block');
				if(num == 1) return 'Hai <b>1</b> nuovo bacio';
				else if(num <= 10) return 'Hai <b>' + num + '</b> nuovi baci';
				else return "Pił di <b>10</b> nuovi baci";
			}
		}
	};
	
	var _link = {
		message: 'message/conversation',
		kiss: 'message/kisses'
	};
	
	var _anchorOver = function () {
		if(_timer) clearTimeout(_timer);
		if(_target) _target.hide();
		
		_target = this.overlay;
		if(_getNotificationsCount(this.type) > 0) {
			_target.cfg.setProperty('context', ['n_' + this.type, 'tr', 'tl']);
			_target.show();
		}
	};
	
	var _itemOver = function () {
		if(_timer) clearTimeout(_timer);
	};

	var _hide = function () {
		_timer = window.setTimeout(function () {
			if(_target) _target.hide();
		}, 750);
	};
	
	YAHOO.util.Event.onContentReady(notificationBarId, initialize);
};