var CDMGDropInHandler = (function($) {
	CDMGDropInHandler = function CDMGDropInHandler(elem,params) {
		this.elem = elem;
		this.params = $.extend({
			transition:300
		},params);
		this.ok2drop = true;
	};
	
	CDMGDropInHandler.prototype.init = function init() {
		$.easing.def = "linear"

		var _elem = this.elem
			,_params = this.params
			,_this = this;
			
		this.callback = function() {
	    	if (_this.ok2drop) {
		    	_elem.dropIn(_params); 
		    	$('#closeFloater').click(function() {
		    		_this.close();
		    		return false;	
		    	});
	    	};
	    };		
	};
	
	CDMGDropInHandler.prototype.add_listeners = function add_listeners() {
		if (arguments.length) {
			for (var i=0;i<arguments.length;i++) {
				var listener = arguments[0];
				switch(listener) {
					case 'scroll':
						this.add_scroll_listener();
						break;	
				};
			};
		};		
	};
	
	CDMGDropInHandler.prototype.close = function close() {
		this.elem.animate({top:'-1000px'});
		$('#overlay-screen').fadeOut(function() { $(this).remove() });
		this.ok2drop = false;
		$('body').css('overflow','auto');
	};
	
	CDMGDropInHandler.prototype.add_scroll_listener = function add_scroll_listener() {
		$(window).scroll(this.callback);	
	};
	
	CDMGDropInHandler.prototype.add_timer = function add_timer(t) {
		this.timer = new CDMGTimer(t);
		this.timer.add_callback(this.timer_callback, this);
		this.timer.start();
	};
	
	CDMGDropInHandler.prototype.timer_callback = function timer_callback() {
		this.callback(); 
		this.timer.clear();
	};
		
	return CDMGDropInHandler;
})(jQuery);


var CDMGTimer = (function($) {
	CDMGTimer = function CDMGTimer(t) {
		this.timer = null;
		this.timeout = t;
	};
	
	CDMGTimer.prototype.add_callback = function add_callback(cb,ref,args) {
		this.callback = cb;
		this.ref = ref;
		this.args = args;		
	};
	
	CDMGTimer.prototype.start = function start() {
		var _this = this;
		this.timer = setTimeout(function() { _this.execute() }, this.timeout);	
	};
	
	CDMGTimer.prototype.clear = function clear() {
		clearTimeout(this.timer);
	};
	
	CDMGTimer.prototype.execute = function execute() {
		this.callback.apply(this.ref,this.args);
	};
	
	return CDMGTimer;
})(jQuery);
