// namespace
com.jobrapido.notification = {};

com.jobrapido.notification.NotificationArea = function(area, options) {
	var clazz = "com.jobrapido.notification.NotificationArea";
	var optionsDefaults = {
		"animationSpeed" : 500
	};
	var options = jQuery.extend({}, optionsDefaults, options);
	var _nextId = 0;
	var _this = this;
	var _area = area;
	var _notifications = [];
	
	// ---------------
	//   PUBLIC API
	// ---------------
	this.add = function(notifications){
		for (index=0;index<notifications.length;index++) {
			var notification = notifications[index];
			jQuery.extend({}, {"id":_getNextId()}, notification);
			_notifications.push(notification);
		}
	}
	
	this.display = function(){
		log.debug(clazz+"|Displaying notifications",_notifications);
		for (index=0;index<_notifications.length;index++) {
			notification = _notifications[index];
			_area.append(jQuery(_covertNotificationToHtml(notification)));
		}
		
		_displayAnimationStep(_area.find(">:first-child"));		
	}
	
	this.containsErrors = function(){
		for (index=0;index<_notifications.length;index++) {
			notification = _notifications[index];
			if (notification.type == "error") {
				return true;
			}
		}
		return false;	
	}	

	// ---------------
	//    PRIVATE
	// ---------------
	var _covertNotificationToHtml = function(notification) {
		var html = [];
		html.push("<div id=\""+notification.id+"\" class=\"jrwt-notification-msg jrwt-notification-"+notification.type+"\" style=\"display:none\">");
		html.push(notification.text);
		html.push("</div>");
		return html.join("");
	}
	
	var _getNextId = function() {
		var notificationAreaId = _area.attr("id");
		_nextId+=1;
		return notificationAreaId+"-msg-"+_nextId;
	}
	
	var _displayAnimationStep = function(element) {
		jQuery(element).fadeIn(options.animationSpeed, 
			function() {
				var nextMsg = jQuery(element).next("div.jrwt-notification-msg");
				if (nextMsg) { 
					_displayAnimationStep(nextMsg);
				}
			}
		);
	}
	
	// ---------------
	//   CONSTRUCTOR
	// ---------------
	var _new = function() {
		log.debug(clazz+"|NEW|");
	}
	_new.call();
}
