// ūtf8
var newWin = new Class({
	options : {
		w : 426,
		title : '',
		content : '',
		button : 'Nosūtīt'
	},
	_ie : /MSIE/.test(navigator.userAgent),
	initialize : function(tips,options,funkcija) {
		this.tips = (tips) ? tips : 'forma';
		this.funkcija = (funkcija) ? funkcija : $empty;
		this.setOptions(options);
		this._construct();
		this._inject();
		this._center();
		this._specefects();
		this._events();
	},
	_construct : function() {
		if (this.tips == 'forma') {
			this._html = new Element('table',{
				align : 'center',
				border : 0,
				cellspacing : 0,
				cellpadding : 0,
				'class' : 'newWin newWin-forma',
				styles : {
					opacity : 0
				},
				width : this.options.w,
				html : '<tr><td width="34"><img src="' + host + 'img/x.gif" width="34" /></td><td class="newWin-title">' + this.options.title + '<a class="close" href="#">Aizvērt</a></td><td class="newWin-border tr" width="11"><img src="' + host + 'img/x.gif" width="11" /></td></tr><tr><td class="gray"></td><td class="newWin-content gray"><p>' + this.options.content + '</p><textarea name="zinojums"></textarea></td><td class="newWin-border rr" width="11"></td></tr><tr><td></td><td class="newWin-button"><a class="action" href="#">' + this.options.button + '</a></td><td class="newWin-border rr" width="11"></td></tr><tr><td class="newWin-border bl" height="11"></td><td class="newWin-border bb"></td><td class="newWin-border br" width="11"></td></tr>'
			});
		}
		else if (this.tips == 'bildes') {
			this._html = new Element('table',{
				align : 'center',
				border : 0,
				cellspacing : 0,
				cellpadding : 0,
				'class' : 'newWin newWin-bildes',
				styles : {
					opacity : 0
				},
				width : this.options.w,
				html : '<tr><td width="34"></td><td class="newWin-title">' + this.options.title + '<a class="close" href="#">Aizvērt</a></td><td class="newWin-border tr" width="11"></td></tr><tr><td class="newWin-content gray pl34" colspan="2" width="415">' + this.options.content + '</td><td class="newWin-border rr" width="11"></td></tr><tr><td></td><td class="newWin-button"><a href="#">' + this.options.button + '</a></td><td class="newWin-border rr" width="11"></td></tr><tr><td class="newWin-border bl" height="11"></td><td class="newWin-border bb"></td><td class="newWin-border br" width="11"></td></tr>'
			});
		}
		this._cover = new Element('div',{
			'class' : 'newWin-cover',
			styles : {
				opacity : 0
			}
		});
		this._holder = new Element('table',{
			'class' : 'newWin-holder',
			'html' : '<tr><td width="100%" height="100%" align="center" valign="middle"></td></tr>',
			border : 0,
			cellspacing : 0,
			cellpadding : 0
		});
	},
	_center : function() {
		var size = {};
		size.doc = document.getSize();
		size.scr = document.getScroll();
		size.con = $('container').getSize();
		this._cover.setStyles({
			width : size.doc.x,
			height : ( (size.doc.y < size.con.y) ? size.con.y : size.doc.y )
		});
		this._holder.setStyle('top',size.scr.y);
	},
	_inject : function() {
		this._cover.inject($(document.body));
		this._holder.inject($(document.body));
		this._html.inject(this._holder.getElement('td'));
	},
	_specefects : function() {
		if (this._ie){
			this._html.setStyle('opacity',1);
			this._cover.setStyle('opacity',0.3);
			this._fx = '';
			this._fy = '';
		}
		else {
			this._fx = new Fx.Tween(this._html,{
				duration : 400
			});
			this._fy = new Fx.Tween(this._cover,{
				duration : 200
			});
			this._fy.start('opacity',0.3);
			this._fx.start('opacity',1);
		}
	},
	_events : function() {
		this._x = this._html.getElement('a[class=close]');
		this._x.store('fx',{'fx':this._fx,'fy':this._fy,'ie':this._ie,'co':this._cover,'ho':this._holder});
		this._x.addEvent('click',function(e) { e.stop();
			var store = this.retrieve('fx');
			if (store.ie) {
				store.co.destroy();
				store.ho.destroy();
			}
			else {
				store.fy.start('opacity',0);
				store.fx.start('opacity',0).chain(function clear() {
					store.co.destroy();
					store.ho.destroy();
				});
			}
		});
		this._esc();
		var el = this._html.getElement('a[class=action]');
		el.store('action',{'fire' : this.funkcija,content : this._html});
		el.addEvent('click',function(e) { e.stop()
			this.removeEvent('click');
			window.removeEvent('keydown');
			store = this.retrieve('action')
			store.fire(store.content.getElement('textarea').value,store.content);
		});
		
	},
	_esc : function() {
		store = this._html.getElement('a[class=close]').retrieve('fx');
		window.addEvent('keydown', function(e) {
			if (e.key == 'esc') {
				if (store.ie) {
					store.ho.destroy();
					store.co.destroy();
				}
				else {
					store.fy.start('opacity',0);
					store.fx.start('opacity',0).chain(function() {
						store.ho.destroy();
						store.co.destroy();
					});
				}
			} 
		});
	}
});
newWin.implement(new Options);
