var InfoPopup = new Class({
	options: {
		enableTitle: true,
		enableCloseButton: true,
		enableIcon: true,
		width: 300,
		height: 200,
		autoHeight: false,
		makeDragable: true,
		skin: 'base',
		resources: '/resources',
		container: $(document.body),
		correctPosition: true,
		containerId: ''
	},
	
	initialize: function(options) { // TODO: it is good to describe all options, which are required
		this.setOptions(options);
		this._visible = false;
		this.initDOM();
	},
	
	
	initDOM: function() {
		new Asset.css(this.options.resources + '/js/infoPopup/' + this.options.skin + '.css');
		
		// hack
		this.options.icon = this.options.resources + '/images/information.png'; 
		
		this.domNode = new Element('div').setStyle('display', 'none').addClass('infoPopup');
		if(this.options.containerId.length > 0) {
			this.domNode.setProperty('id', this.options.containerId);
		}
		this.domNode.setStyles({
			'width': this.options.width,
			'height': this.options.height
		});
		
		if(this.options.enableTitle) {
			this.createTitle();
		}
		this.infoContainer = new Element('div').addClass('info_container').injectInside(this.domNode);
		this.domNode.injectInside(this.options.container);
	},
	
	createTitle: function() {
		var textWidth = this.options.width - 9; //left: 3, padding-left: 3, padding-right: 3
		var textPosition = 3;
		
		this.title = new Element('div').addClass('info_title');
		if(this.options.enableIcon) {			
			new Element('img', {'src': this.options.icon}).addClass('info_icon').injectInside(this.title);
			
			textPosition += 16;
			textWidth -= 16;
		}
		this.titleText = new Element('div').addClass('info_title_text').injectInside(this.title);
		
		if(this.options.enableCloseButton) {
			var closeButton = new Element('img', {'src': this.options.resources + '/images/fileclose.png'}).addClass('info_close').injectInside(this.title);
			closeButton.addEvent('click', this.hide.bind(this));
			this.closeButton = closeButton;
			textWidth -= 19;
		}
		
		this.titleText.setStyles({
			'left': textPosition,
			'width': textWidth
		});
		
		this.title.injectInside(this.domNode);
		
		if(this.options.makeDragable) {
			new Drag.Move(this.domNode, {handle: this.title});
		}
	},
	
	emptyInfo: function() {
		this.infoContainer.empty();
	},
	
	setTitle: function(title) {
		if(this.options.enableTitle) {
			this.titleText.setHTML(title);
		}
	},
	
	setInfo: function(data) {
		this.infoData = data;
		
		this.emptyInfo();
		
		var table = new Element('table', {'cellpadding': 0, 'cellspacing': 0});
		var tBody = new Element('tBody').injectInside(table);
		for(var i = 0; i < data.length; i++) {
			var tr = new Element('tr').injectInside(tBody);
			var title_td = new Element('td').addClass('info_data_title').setHTML(Locale.get(data[i].title) + ':');
			var value_td = new Element('td').addClass('info_data_value').setHTML(data[i].value);
			if (typeof data[i].type != 'undefined' && data[i].type == 'error') {
				title_td.addClass('error');
				value_td.addClass('error');
			}
			title_td.injectInside(tr);
			value_td.injectInside(tr);
		}

		table.injectInside(this.infoContainer);

		this.adjustHeight();
	},
	
	setMessage: function(message) {
		this.emptyInfo();
		this.infoContainer.setHTML(message);
		this.adjustHeight();
	},
	
	appendChild: function (div) { // analog of setMessage and of setInfo for more complex constructions
		this.emptyInfo();
		this.infoContainer.appendChild(div);
		this.adjustHeight();
	},
	
	setPosition: function(position) {
		if(this.options.correctPosition) {
			var windowBottom = parseInt(window.innerHeight)+parseInt(window.pageYOffset);
			var windowRight = parseInt(window.innerWidth)+parseInt(window.pageXOffset);
			
			var nodeHeight = parseInt(this.domNode.offsetHeight);
			var nodeWidth = parseInt(this.domNode.offsetWidth);
			
			if(nodeHeight + parseFloat(position.top) > windowBottom) {
				position.top -= 16 + nodeHeight;
			}
			if(nodeWidth + parseFloat(position.left) > windowRight) {
				position.left -= 16 + nodeWidth;
			}
		}
		
		this.domNode.setStyles(position);
	},
	
	setBusyState: function(busy) {
		if(busy) {
			this.emptyInfo();
			this.loadingIndicator = new Element('div').addClass('info_loading').injectInside(this.infoContainer);
			new Element('img', {'src': this.options.resources + '/images/loading.gif'}).injectInside(this.loadingIndicator);
			if(this.options.autoHeight) {
				this.adjustHeight();
			}
		}
		else {
			if($type(this.loadingIndicator) == 'element') {
				this.loadingIndicator.remove();
			}
		}
	},
	
	hide: function() {
		this._visible = false;
		this.domNode.setStyle('display', 'none');
	},
	
	show: function() {
		if(!this._visible) {
			this.domNode.setStyle('display', 'block');
		
			this.adjustHeight();
			
			this._visible = true;
		}
	},
	
	adjustHeight: function() {
		if(this.options.autoHeight) {
			var height = parseInt(this.infoContainer.getCoordinates().height);
			if(typeof this.infoData != 'undefined') {
				height += this.infoData.length*4;
			}
			if(this.options.enableTitle) {
				height += 25;
			}
			
			this.options.height = height;
			this.domNode.setStyle('height', height);
		}
	}
});

InfoPopup.implement(new Options);