var windowHandler = {

	overlay : {},

	openedId : null,

	sources : {},

	parents : {},

	containers : {},

	headers : {},

	closeButtons : {},

	sourceHolders : {},

	params : {},

	init : function(params) {

		if (typeof(params) == 'object') {
			var id = params.id;
			this.params[id] = params;
		} else {
			id = params;
			params = this.params[id];
		}

		// checking existing element
		if (!this.sources[id]) this.sources[id] = $(id);
		if (!this.sources[id]) {
			this.sources[id] = document.createElement('div');
			Element.extend(this.sources[id]);
			this.sources[id].id = id;
		}

		// setting source height
		if (params.width) this.sources[id].setStyle({ width : params.width + 'px'});
		if (params.height) this.sources[id].setStyle({ height : params.height + 'px'});

		if (!this.parents[id]) {
			this.parents[id] = this.sources[id].up(); // elements parent
			this.sources[id].addClassName('windowParent');
			if (params.parentClassName) this.sources[id].addClassName(params.parentClassName);
			this.sources[id].setStyle({ display : 'block'});
		}

		// overlay container
		if (!this.overlay[id] && params.overlay) {
			this.overlay[id] = document.createElement('div');
			Element.extend(this.overlay[id]);
			this.overlay[id].addClassName('overlay');
			this.overlay[id].id = 'overlay_' + id;
			this.overlay[id].setStyle({ diplay : 'none'});
			document.body.appendChild(this.overlay[id]);

			Event.observe(window, 'resize', windowHandler.resize.bind(windowHandler));
		}

		// creating dom elements if not exist
		if (!this.containers[id]) {
			// main (outer) container
			this.containers[id] = document.createElement('div');
			Element.extend(this.containers[id]);
			this.containers[id].addClassName('formContainer');
			this.containers[id].setStyle({ diplay : 'none'});

			// header bar
			this.headers[id] = document.createElement('div');
			Element.extend(this.headers[id]);
			this.headers[id].addClassName('close');

			// close button
			if (this.params[id].closeButton) {
				this.closeButtons[id] = document.createElement('span');
				Element.extend(this.closeButtons[id]);

				//this.closeButtons[id].setAttribute('src', this.params[id].src ? this.params[id].src : 'main/img/close.jpg');
				//this.closeButtons[id].setAttribute('width', this.params[id].width ? this.params[id].imgwidth : 22);
				//this.closeButtons[id].setAttribute('height', this.params[id].height ? this.params[id].imgheight : 22);
				//this.closeButtons[id].setAttribute('alt', this.params[id].alt ? this.params[id].alt : jsLabels.MainClose);
				this.closeButtons[id].setAttribute('title', this.params[id].alt ? this.params[id].alt : (typeof(jsLabels) != 'undefined' ? jsLabels.MainClose : ''));
				this.closeButtons[id].setStyle({
					cursor : 'pointer',
					'background' : '#FFFFFF'
				});
				this.closeButtons[id].update('X');

				// close button -> header
				this.headers[id].insert(this.closeButtons[id]);

				// img onclick -> close
				this.closeButtons[id].observe('click', windowHandler.hide.bind(windowHandler))
			}

			// container of source
			this.sourceHolders[id] = document.createElement('div');
			Element.extend(this.sourceHolders[id]);

/*			// main container -> element parent
			this.parents[id].insert({ top : this.containers[id] }); */

			// main container -> body
			document.body.appendChild(this.containers[id]);

			// header -> main container
			this.containers[id].insert(this.headers[id]);
			// source -> main container
			this.containers[id].insert(this.sourceHolders[id]);
			// moving source element into source container
			this.sourceHolders[id].insert(this.sources[id]);
		}
	},

	show : function(id) {

		if (this.openedId) this.hide();

		this.init(id);

		if (this.overlay[id]) this.overlay[id].setStyle({ display : 'block' });
		this.containers[id].setStyle({ display : 'block' });

		this.openedId = id;

		this.resize();
	},

	resize : function() {

		if (!this.openedId) return;

		// viewport sizes
		var dimensions = document.viewport.getDimensions();
		// whole page sizes
		var pDimensions = WindowUtilities.getPageSize();
		// included element's dimensions
		var formDimensions = this.sources[this.openedId].getDimensions();

		// setting the overlay div's height
		if (this.overlay[this.openedId]) this.overlay[this.openedId].setStyle({
			height : pDimensions.pageHeight+'px'
		});

		// centering the content
		this.containers[this.openedId].setStyle({
			width : formDimensions.width+'px',
			height : formDimensions.height+'px',
			left : (dimensions.width/2)-(formDimensions.width/2)+'px',
			top : (dimensions.height/2)-(formDimensions.height/2)+'px'
		});
	},

	hide : function(event) {
		if (event) event.stop();
		if (!this.openedId) return false;

		if (this.overlay[this.openedId]) this.overlay[this.openedId].style.display='none';
		this.containers[this.openedId].style.display='none';

		this.openedId = null;
		try {__click=false;} catch(e) {}
		return false;
	},

	update : function(url, params) {
		if (!this.openedId) return false;
		new Ajax.Updater(this.openedId, url, params);
	}
}


