/***********************************/
/*                                 */
/*       WebshopInfoPopup          */
/*                                 */
/***********************************/

var WebshopInfoPopup =  new Class({
	
	initialize: function (params) {
		//required params: button, getRemoteData function, metacategory_id
		this.setParams(params);
		if (typeof params.common_name != 'undefined') { // for product
			this.common_name = params.common_name;
		}
		this.initializeInfoPopup();
		
		this.setVisible(false);
	},
	
		
	setParams: function (params) {
		this.params = params;
	},
	
	initializeInfoPopup: function () {
		var width = typeof this.params.popupWidth != 'undefined' ? this.params.popupWidth : 400;
		this.popup = new InfoPopup({
			'container': $(document.body),
			'enableTitle': true,
			'resources': '/accounting_res',
			enableCloseButton: true,
			autoHeight: true,
			'width': width // select's width = 220px + label's width
		});
		if (typeof this.params.onCompleteHandler == 'function') { 
			var onComplete   = this.params.onCompleteHandler;
			var remoteCaller = this.getRemoteCaller(onComplete); // ?
			if (this.params.button) {
				this.params.button.addEvent('click', function (event) {
					return this.show(event, remoteCaller, onComplete);
				}.bind(this));
			}
		} 
	// hack to handle isVisible	
		this.popup.closeButton.removeEvents('click');
		this.popup.closeButton.addEvent('click', this.hide.bind(this));
	},
	
	getRemoteCaller: function () {
		var instance  = this;
		return function (onCompleteHandler) {// to not to bind with this 'getRemoteData' function
			var common_id = instance.params.common_id;
			var popup_id = instance.params.id;  
			// here 'getRemoteData' is 'loadAttributesData' of WebshopProductClass
			instance.params.getRemoteData(common_id, popup_id, onCompleteHandler); 
		}
	},
	
	
	show: function (event, handler, onCompleteHandler) { 
		WebshopInfoPopups.hideAll(); // as I do not make many instances - so should hide them...
		if (!this.isVisible()) {
			this.changePosition(event);
			this.popup.setBusyState(true);
			this.popup.show();
			
			var onCompleteHandler_ = function (data) {
				this.popup.setBusyState(false);
				onCompleteHandler(data, this.popup, event);
				if (this.params.autohide) {
					window.setTimeout("WebshopInfoPopups.get('" + this.params.id + "').hide();", this.params.autohide_interval);
				}
			}.bind(this);
			handler(onCompleteHandler_);
			this.setVisible(true);
		}
		return false;
	},
		
	hide: function (e) {
	/* hack - as i give id to elements inside of popup, and recreate them, so
		2 dom-elements with the same id,
		so we clear when hide
	*/	
		this.clearNode();
		this.popup.hide();
		this.setVisible(false);
	},
	
	setVisible: function (visible) {
		if (typeof visible != 'undefined') {
			this.is_visible = visible;
		} else {
			this.is_visible = true; // default
		}
	},
	
	isVisible: function () {
		return this.is_visible;
	},
	
	changePosition: function(event) { 
		e = new Event(event);
		this.popup.setPosition({left: e.page.x + 10, top: e.page.y + 10});	
	},
	
	adjustHeight: function () {
		this.popup.adjustHeight();
	},
	
	clearNode: function (node) {
		if (typeof node == 'undefined') { var node = this.getNode(); }
		if (node) node.empty();		
	},
	
	getNode: function () {
		return this.popup.attributes_form_node;
	}
});


/// Factory

WebshopInfoPopup.implement(new Options);
WebshopInfoPopup.implement(new Events);
	
var WebshopInfoPopups = new Class({
	instances: {},

	create: function(params) {
		var instance = new WebshopInfoPopup(params);
		params.id = params.id ? params.id : 'popup' + Math.round(100000 * Math.random());
		this.instances[params.id] = instance;
		return this.instances[params.id];
	},
	
	get: function(id) {
		if (!this.instances[id]) {
			throw new Error('There is no WebshopInfoPopup with id = "' + id + '"'); 
		}
		return this.instances[id];
	},
	 
	hideAll: function () {
		for (var i in this.instances) {
			if (typeof this.instances[i] != 'function') {
				this.instances[i].hide();
			}
		}
	}
});

var WebshopInfoPopups = new WebshopInfoPopups();
