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) {
		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
		DwrManager.loadNotPersistentNotifications(notPersistentNotifications);
	};
	
	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, 'tl', 'tr'] });
		
		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) > 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);
		updateCounter(type, 1);		
		
		// 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);
		}
	};
	
	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 = 'http://' + notify.webNotification.sender.nick + '.' + conf.domain;
		
		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 > 30 ? '...' : '') + '"</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 = _format[type](n_types[type]);
		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';
				else if(num == 1) return 'Hai <b>1</b> nuovo messaggio';
				else return 'Hai <b>' + num + '</b> nuovi messaggi';
			},
	
			chat: function (num) {
				if(num == 0) return 'Nessuna chat';
				else if(num == 1) return 'Hai <b>1</b> richiesta di chat';
				else return 'Hai <b>' + num + '</b> richieste di chat';
			},
	
			kiss: function (num) {
				if(num == 0) return 'Nessun bacio';
				else if(num == 1) return 'Hai <b>1</b> nuovo bacio';
				else return 'Hai <b>' + num + '</b> nuovi baci';
			}
	};
	
	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, 'tl', 'tr']);
			_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);
};