var CDMGFloater = (function($) {
	CDMGFloater = function CDMGFloater(floater_id,form_id,params) {
		this.floater = checkId(floater_id);
		this.form_id = form_id;
		this.params = params;
		if (arguments.length>3) {
			this.fields = arguments[3];
		} else {
			this.fields = null;	
		};
	};
	
	CDMGFloater.prototype.init = function init() {
		if (this.dropornot()) {
			this.init_dropin();
			this.init_form();
		};
		return this;
	};
		
	CDMGFloater.prototype.init_form = function init_form() {
		var _this = this;
		
		CDMGFloaterForm = function CDMGFloaterForm() {};
		CDMGFloaterForm.prototype = new CDMGForm(this.form_id,this.params,this.fields ? this.fields : null);
		CDMGFloaterForm.constructor = CDMGFloaterForm;
	
		this.form = new CDMGFloaterForm();
		this.form.onsuccess.add(function() {
			_this.dropinhandler.close();	
		});
		this.form.init();
	};
	
	CDMGFloater.prototype.init_dropin = function init_dropin() {
		this.new_dropin_instance();
		this.add_dropin_timer(9000);
		this.add_dropin_listeners();		
	};
	
	CDMGFloater.prototype.new_dropin_instance = function new_dropin_instance() {
		this.dropinhandler = new CDMGDropInHandler($(this.floater),this.params);
		this.dropinhandler.init();		
	};

	CDMGFloater.prototype.add_dropin_timer = function add_dropin_timer(t) {
		this.dropinhandler.add_timer(t);	
	};
	
	CDMGFloater.prototype.add_dropin_listeners = function add_dropin_listeners() {
		this.dropinhandler.add_listeners.apply(this.dropinhandler,this.listeners);
	};
	
	CDMGFloater.prototype.get_cookie = function get_cookie(Name) {
	    var search = Name + "="
	    var returnvalue = ""
	    if (document.cookie.length > 0) {
	        offset = document.cookie.indexOf(search)
	        if (offset != -1) {
	            offset += search.length
	            end = document.cookie.indexOf(";", offset)
	            if (end == -1) end = document.cookie.length;
	            returnvalue = unescape(document.cookie.substring(offset, end))
	        }
	    }
	    return returnvalue;		
	};
	
	CDMGFloater.prototype.dropornot = function dropornot() {
	    var exp = new Date();
	
	    if (this.get_cookie("droppedin") == "") {
	        exp.setTime(exp.getTime() + (24 * 60 * 60 * 1000)); //expire in one day // 24 * 60 * 60 * 1000 * 45
	        document.cookie = "droppedin=yes; expires=" + exp.toGMTString();
	       	return true;
	    };
	    return false;
	};
	
	return CDMGFloater;
})(jQuery);
