
var SmartHoverBox = new Class({
  options: {
				boxTimer: 1000,
				xOffset: 0,
				yOffset: 0,
				smartBoxSuffix: '_smarthbox',
				smartBoxClose: 'smarthbox_close',
				container: document
  },
  initialize: function(options) {
    // Merges the default options with the ones given as parameters
    this.setOptions(options);

				this.smartBoxes=new Object();
    this.closeBoxesTimer = 0;
				
	   //var smartBoxes = $(document.body).getElements('[id$=' + smartBoxSuffix + ']');
	
	   var closeElem = $(document.body).getElements('.' + this.options.smartBoxClose); 
	   closeElem.addEvent('click', function(){
					this.closeBoxes();
				}.bind(this)).setStyle('cursor', 'pointer');	
				
				this.currentTrigger=null;
				
  },
  create: function(trigger, content, show) {
			
			trigger.addEvent('mouseenter', function(trigger){ 
				if($defined(this.closeBoxesTimer)) $clear(this.closeBoxesTimer);
				this.show(trigger);
				trigger.addClass("active");
				this.currentTrigger=trigger;
			}.bind(this,trigger));
			
			trigger.addEvent('mouseleave', function(){
				this.closeBoxesTimer = this.closeBoxes.delay(this.options.boxTimer,this);
			}.bind(this));

			trigger.setStyle('cursor', 'pointer');
			
			if (!$defined(this.smartBoxes[trigger.id])) {
			
			 //crea box
	   var box=new Element('div',{'id':trigger.id+'_smarthbox','class':'smartbox','styles':{'display':'none','margin':0}});
    box.setHTML(content);
		  box.injectInside(document.body);	

				box.addEvent('mouseenter', function(){
					if($defined(this.closeBoxesTimer)) $clear(this.closeBoxesTimer);
				}.bind(this));
				
				box.addEvent('mouseleave', function(){
					this.closeBoxesTimer = this.closeBoxes.delay(this.options.boxTimer,this);								  
				}.bind(this));
				
			 this.smartBoxes[trigger.id]=box;
			}
			
			if (show) this.show(trigger);
  },
		show: function(trigger) {
			
				this.closeBoxes();
				
				var box=this.smartBoxes[trigger.id];
				box.setStyles({ display: 'block', position: 'absolute' }).setStyle('z-index', '1000000');
 
				//coordinates and size vars and math 
				var windowSize = $(window).getSize().size;
				var windowScroll = $(window).getSize().scroll;
				var halfWindowY = windowSize.y / 2;
				var halfWindowX = windowSize.x / 2;
				var boxSize = box.getSize().size;
				
				var inputCOOR = trigger.getCoordinates();
				var inputPOS = trigger.getPosition([this.options.container]);
				var inputSize = trigger.getSize().size;
				
				var inputTopPOS = inputPOS.y;
				var inputBottomPOS = inputTopPOS + inputSize.y;
				var inputBottomPOSAdjust = inputBottomPOS - windowScroll.y
				
				var inputLeftPOS = inputPOS.x;
				var inputRightPOS = inputLeftPOS + inputSize.y;
				var inputLeftPOSAdjust = inputLeftPOS - windowScroll.x
 
				if(inputBottomPOSAdjust < halfWindowY) {
					box.setStyle('top', inputBottomPOS + this.options.yOffset);
					if (inputLeftPOSAdjust < halfWindowX) {
						box.setStyle('left', inputLeftPOS + this.options.xOffset);
					} else {
						box.setStyle('left', inputRightPOS - boxSize.x - this.options.xOffset);
					};
				} else {
					box.setStyle('top', inputTopPOS - boxSize.y - this.options.yOffset);
					if (inputLeftPOSAdjust < halfWindowX) {
						box.setStyle('left', inputLeftPOS + this.options.xOffset);
					} else {
						box.setStyle('left', inputRightPOS - boxSize.x - this.options.xOffset);
					}; 
				}
  },
  closeBoxes: function() {
			for (var key in this.smartBoxes) {
    this.smartBoxes[key].setStyle('display', 'none');
   }
			if (this.currentTrigger) this.currentTrigger.removeClass("active");
  }		
});
 
// Adds options management support
SmartHoverBox.implement(new Options, new Events);
