var xBox = new Class({   
	Implements: [Options, Events],

	options:	{
				boxWidth: 540,
				boxHeight: 400,
				boxTop: 125
	},
 
	initialize: function(options){
			this.setOptions(options);
			if(this.options.boxWidth) this.boxWidth = this.options.boxWidth;
			if(this.options.boxHeight) this.boxHeight = this.options.boxHeight;
			if(this.options.boxTop) this.boxTop = this.options.boxTop;
			
			this.box = null;
	},

    getBox: function() {  
    	// sa postara aby sa znovuvytvoril prázdny box 
		try {
			this.boxHTML = "<tr><td class=\"tl seventy-percent-opacity\">&nbsp;</td><td class=\"topbottom seventy-percent-opacity\">&nbsp;</td><td class=\"tr seventy-percent-opacity\">&nbsp;</td></tr><tr><td class=\"side seventy-percent-opacity\">&nbsp;</td><td class=\"content\"><form id=\"obsah\" action=\"\"><div class=\"reading\" ></div></form></td><td class=\"side seventy-percent-opacity\">&nbsp;</td></tr><tr><td class=\"bl seventy-percent-opacity\">&nbsp;</td><td class=\"topbottom seventy-percent-opacity\">&nbsp;</td><td class=\"br seventy-percent-opacity\">&nbsp;</td></tr>";
		} catch(e) { 
			alert("nepodarilo sa iniaciovat html envelope");
		}	
		if(this.box) {
			try{
				this.box.dispose();
			} 
			catch(e){
				alert('neda sa dispose spravit');
			}
		} 
		try {
			this.box = new Element('table', {
				'id': 'modal_dlg',
				'html': this.boxHTML,
				'styles': {
					'position': (Browser.ie6) ? 'absolute' : 'fixed',						
					'background-position': '50% 50%',
					'top': this.boxTop,
					'left': ((window.getWidth() - this.boxWidth)/2).toInt(),
					'width':  this.boxWidth,
					'z-index': 800
				}
			});
		} catch(e) {
			alert("nepodarilo sa spravit element box");
		}

		return this.box;
    },

    getUrl: function(urlAjax) {
		
		try {
			this.request = new Request({ url: urlAjax, method: 'post' });
			this.request.addEvent('success',this.loadExt.bindWithEvent(this));				
			this.request.send();
		}catch (e)  {
			alert("problem s requestom");
		}
		
	},
	
	formSend: function(ev,urlAjax){
	
		try {
			this.request = new Request({ url: urlAjax, method: 'post' });
			this.request.addEvent('request',this.setIsLoading.bindWithEvent(this));
			this.request.addEvent('success',this.loadExt.bindWithEvent(this));				
			this.request.send();
			
		}catch (e)  {
			alert("problem s requestom");
		}
	},
    
	setIsLoading: function(){
		this.btn_ok = $('modal_ok');
		this.btn_ok.removeEvents('click');
		this.btn_ok.setStyle('background-color' , '#aaaaaa');
		this.btn_ok.set('html','Loading ...');
	},
	
	
	
	loadExt: function(response, xml) {
		
	  	try {
				var content = $('obsah');
				content.set('html', response);
		} catch(e) {alert("nepoodarilo sa vlozit response do elementu \"obsah\"");}

		//predpokladame 2 tlacidla
		$('modal_close').addEvent('click', this.hideme.bindWithEvent(this)); 
		//$('modal_ok').addEvent('click', this.formSend.bindWithEvent(this, [this.serverScript])); 
		
		
		
	},
	

	show: function(urlAjax) {
			
			
			this.serverScript = urlAjax;
			this.getBox();
			
			try {
				this.getUrl(urlAjax);
			} catch(e) {
				alert("nepodarilo sa spustit getUrl");
			}	
			
			try {
				this.box.setStyle('opacity','0.0');
			} catch(e) {
				alert("opacity sa nepodarilo na boxik dat");
			}
			
			
			
			this.box.inject($("bodyelement"));
	
			var myForm = $('obsah'); 
			myForm.action = this.serverScript;
			myForm.addEvent('submit', function(e) {
				//Prevents the default submit event from loading a new page.
				e.stop();
				//Empty the log and show the spinning indicator.
				var log = $('loader').addClass('box_loading_active');
				//Set the options of the form's Request handler. 
				//("this" refers to the $('myForm') element).
				this.set('send', {
					onComplete: function(response) { 
						log.removeClass('box_loading_active');
						myForm.set('html', response);
						
						$('modal_close').addEvent('click', function(){
							var myEffects = new Fx.Morph($('modal_dlg'), {duration: 500, transition: Fx.Transitions.linear});
							myEffects.start({
				   				 'opacity': [1, 0]
							});
						}); 
					}
				});
				//Send the form.
				this.send();
			});
					

			var myEffects = new Fx.Morph(this.box, {duration: 500, transition: Fx.Transitions.linear});
			myEffects.start({
   				 'opacity': [0, 1]
			});
			
			//$$('.draggable').each(function(el,i){el.makeDraggable();});
			
			return false;
	},
	
	hideme: function() {
			
			var myEffects = new Fx.Morph(this.box, {duration: 500, transition: Fx.Transitions.linear});
			myEffects.start({
   				 'opacity': [1, 0]
			});
			
			return false;
	}
});