
floatWidget = {
	_active: [],	
	
	getOpenActive: function() {
		
		var activeWidget = this._active[this._active.length-1]
		if (!activeWidget) return
		if (!activeWidget.$container || !activeWidget.$container.isShow()) return
		
		return activeWidget
	},
	
	closeActive: function(accept) {
		
		var activeWidget = this.getOpenActive()
		if (!activeWidget) return				
		
		if(!accept){ // close
			//console.log(activeWidget.getId())
			if (activeWidget.cancel) {
				activeWidget.cancel()
			} else {
				activeWidget.close();
			}
		}
		if (accept) {
			if (activeWidget.accept) {
				activeWidget.accept()
			} else {
				activeWidget.close();
			}
		}
	},
	
	
	setEscapeEnterHandler: function() {
		var self = this		
				
		$(document).keyup(function(e){ 	
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if (keycode !=27 && keycode != 13) return
			
			self.closeActive(keycode == 13)
			
		})
	},
	
	activePush: function(widget) {
		//console.log("push ", widget)
		this._active.push(widget)
	},
	
	activePop: function(widget) {
		//console.log("pop ", widget)
		
		// не использую простой pop(), т.к возможен сценарий,
		// когда один виджет открывается, а другой закрывается
		// причем событие открытия работает раньше закрытия
		// получается - новый виджет попадает в стек до удаления старого
		// pop() удалит в этом случае новый виджет
		
		
		for(var i=0;i<this._active.length;i++) {
			var w = this._active[i]
			if (w===widget) {
				//console.log("found")
				this._active.splice(i,1)
				//delete  this._active[i]
				break
			}			
		}
	}
}

floatWidgetMixin = {
	
	
	setHiders: function(on) {	
		//this.setEscapeClose(on)
		this.setHideIfClickOutside(on)
	},
	
	setHideIfClickOutside: function(on) {
		
		var self = this
		if (!this.hideIfClickOutsideHandler) {
			this.hideIfClickOutsideHandler = function(event) {
			
				if (!self.$container || !self.$container.isShow()) return
				
				var o = event.target
				while(o.tagName != 'BODY' && o != self.element[0]) {
					o = o.parentNode			
				}
				if (o != self.element[0] && !self.insideSelector(event)) {
					self.close();
				}
			}
		}
			
		$(document.body)[on ? 'bind':'unbind']("click", this.hideIfClickOutsideHandler)
		
	},
	
	

	insideSelector: function(event) {
		var offset = this.$container.offset();
		offset.right = offset.left + this.$container.outerWidth();
		offset.bottom = offset.top + this.$container.outerHeight();

	    return event.pageY < offset.bottom &&
           event.pageY > offset.top &&
           event.pageX < offset.right &&
           event.pageX > offset.left;
	}
}
	
	
  

