var clouds = Class.create({	

	/* ****************************************************************************************************************** */
	/* Init function : hiding popup content, building cloud, adding hover actions */
	
	initialize: function(id, selector, style, options, offset, arrow) {
		document.observe('dom:loaded', function() {
			clouds.prototype.contentHide(selector);
			var cloud = clouds.prototype.buildCloud(id, style);
			clouds.prototype.doHover(selector, cloud, options, offset);
		}.bindAsEventListener(this));
	},

	/* ****************************************************************************************************************** */
	/* Function to create empty div popup at the end of body */
	
	buildCloud: function(id, style, _offset, arrow) {

		$$("body")[0].insert(new Element("div", { id: id })).down();
		
		$(id).insert(new Element("div", { id: id + '_cloud_content' }));
		$(id).insert(new Element("div", { id: id + '_cloud_arrow' }));
		
		$(id + '_cloud_arrow').setStyle({ position : 'absolute' });
		$(id + '_cloud_arrow').setStyle(arrow);
				
		$(id).setStyle({ position : 'absolute', display : 'none', width : '200px', height : '100px', marginTop : '15px', marginLeft : '15px' });
		$(id).setStyle(style);
		
		return $(id);
	},

	/* ****************************************************************************************************************** */
	/* Function to add hover actions (popup show) to the selected elements */
	
	doHover: function(selector, cloud, options, offset, event) {
		$$("." + selector + " .visible").each(function(obj) {
			
			$(obj).observe('mouseover', function(event){
				$(cloud).show();
				$(cloud.id + '_cloud_content').update($(obj).next(".popup").innerHTML);
			});
			
			$(obj).observe('mousemove', function(event){			
				$(cloud).setStyle({ top : (clouds.prototype.positioningHelperY(cloud, options, Event.pointerY(event), offset) + 'px'), left : (clouds.prototype.positioningHelperX(cloud, options, Event.pointerX(event), offset) + 'px') });	
			});
			
			$(obj).observe('mouseout', function(event){
				$(cloud).hide();
			});	
		});
	},

	/* ****************************************************************************************************************** */
	/* Function to get position Y for popup (including popup cloud margins) */
	
	positioningHelperY: function(obj, options, y, offset) {
		if(typeof options == 'object') {
			if(options.valign) {
				offset_b = (offset && offset.bottom ? offset.bottom : 0);
				offset_t = (offset && offset.top ? offset.top : 0);
				y = (parseInt(y) - parseInt(offset_b) + parseInt(offset_t));
				switch(options.valign) {
					case 'bottom' :
						return (y - $(obj).getHeight());
					break;
					case 'middle' :
						return (y - ($(obj).getHeight() / 2));
					break;
					case 'top' :
					default :
						return y;
					break; 
				}
			}
		}
		return y;	
	},
	
	/* ****************************************************************************************************************** */
	/* Function to get position X for popup (including popup cloud margins) */
	
	positioningHelperX: function(obj, options, x, offset) {
		if(typeof options == 'object') {
			if(options.align) {
				offset_r = (offset && offset.right ? offset.right : 0);
				offset_l = (offset && offset.left ? offset.left : 0);
				x = (parseInt(x) - parseInt(offset_r) + parseInt(offset_l));
				switch(options.align) {
					case 'right' :
						return (x - $(obj).getWidth());
					break;
					case 'center' :
						return (x - ($(obj).getWidth() / 2));
					break;
					case 'left' :
					default :
						return x;
					break; 
				}
			}
		}
		return x;	
	},
	
	/* ****************************************************************************************************************** */
	/* Function to hide popup content */
	
	contentHide: function(selector) {
		$$("." + selector + " .popup").each(Element.hide);
	}
	
});