var dialogLoaderAjax = new Class({
    Implements: [Events, Options],
	
	options: {
		html: null,
		msg: 'Alert!',
		button: 1,
		url: null,
		method: 'post',
		destroyOnClose: false,
		onLoadComplete: $empty,
		onSelect: $empty,
		onClose: $lambda,
		zIndex: null,
		styles: {
			'background': '#C00',
			'font-family': 'Arial, Georgia',
			'font-size': '11px',
			'color': '#FFF'
		},
		bg: { 
			'color':'#000',
			'alpha': .7
		},
		styleclose: {
			'display': 'block',
			'color': 'black',
			'width': '17px',
			'height': '15px',
			'background': 'white',
			'font-size': '9px',
			'padding-top': '3px',
			'text-decoration': 'none',
			'text-align': 'center',
			'font-family': 'Arial, sans, serif',
			'position': 'absolute',
			'top': '4px',
			'right': '4px',
			'border': 'none',
			'opacity': '.3',
			'-moz-border-radius': '5px',
			'border-radius': '5px',
		}
	},
	
    initialize: function(options){
		this.setOptions(options);
		if (this.options.zIndex == null) this.options.zIndex = document.body.getNextHighestZindex()
		//console.log(this.options.url);
        this.el = new Element('div', {
							  'id': 'bgfadeblack'+this.options.zIndex,
							  'styles': {
								  'width': Window.getScrollSize().x,
								  'height': Window.getScrollSize().y,
								  'background-color': this.options.bg.color,
								  'top': 0,
								  'left': 0,
								  'position': 'absolute',
								  'display': 'none',
								  'z-index': this.options.zIndex
							  }
					});
		this.el.setOpacity(0);
		this.eltop = new Element('div', {
							  'id': 'bg'+this.options.zIndex+1,
							  'styles': {
								  'width': Window.getSize().x,
								  'height': Window.getSize().y,
								  'background': 'none',
								  'top': Window.getScroll().y,
								  'left': 0,
								  'position': 'absolute',
								  'display': 'none',
								  'z-index': this.options.zIndex+1
							  }
					});
		this.eltop.setOpacity(0);
		this.st = $H({
								  'top': Window.getScroll().y,
								  'left': 0,
								  'padding': '10px',
								  'position': 'relative',
								  'float': 'left',
								  'display': 'none'
							  });
		this.st.extend(this.options.styles);
		this.el2 = new Element('div', {
							  'id': 'pr' + (this.options.zIndex+2),
							  'styles': this.st  
					});
		this.el.injectInside(document.body);
		this.eltop.injectInside(document.body);
		this.el2.injectInside(this.eltop);
    },
	centerize: function(){
		elwidth = (Window.getSize().x-this.el2.getCoordinates().width)/2;
		elheight = (Window.getSize().y-this.el2.getCoordinates().height)/2;
		this.el2.setStyle("left", elwidth);
		this.el2.setStyle("top", elheight);
	},
	
	resize: function(){
		this.el.setStyle('width', Window.getScrollSize().x);
		this.el.setStyle('height', Window.getScrollSize().y);
		this.eltop.setStyle('width', Window.getSize().x);
		this.eltop.setStyle('height', Window.getSize().y);
	},
	loadDialog: function(url){
		if (url) this.options.url = url;
		this.el.setStyle('z-index', this.options.zIndex);
		this.eltop.setStyle('z-index', this.options.zIndex+1);
		var p = this;
		this.resize();
		this.el.setStyle('display', 'block');
		this.myFx = new Fx.Morph(this.el.id, {duration: 100, transition: Fx.Transitions.Sine.easeOut});
		this.myFx.start({
			'opacity': [this.options.bg.alpha]
		}).chain(function(){
			p.el2.innerHTML = "";
			p.el2.setStyle("display", "block");
			p.eltop.setStyle("display", "block");
			if (p.options.url != null){
				this.typedialog = "html";
				p.el2.setStyle('padding', '30px 10px 10px 10px');
				p.mySchedRequest = new Request({url:p.options.url, update: p.el2.id, method: p.options.method, onSuccess: function(t,x){
					//p.el2.adopt(myhtml);
					rp = x;
					rp2 = parseXml(t);
					//mpg.hide();
					eval('pew = ' + xml2json(rp2,""));
					if (pew.ajaxresponse.html) p.el2.innerHTML = xmlData(pew.ajaxresponse.html);
					if (pew.ajaxresponse.run) eval(xmlData(pew.ajaxresponse.run));
					//p.el2.adopt(myhtml);
					p.clbut = new Element('a', {
						href: 'javascript:',
						styles:p.options.styleclose
					});
					p.clbut.addEvent('click', function(ev){
						p.closeDialog();
					});
					p.el.addEvent('click', function(ev){
						p.closeDialog();
					});
					p.clbut.appendText('x');
					p.clbut.injectInside(p.el2);
					p.centerize();
					p.myFx2 = new Fx.Morph(p.eltop.id, {duration: 100, transition: Fx.Transitions.Sine.easeOut});
					p.myFx2.start({
						'opacity': [1]
					});
					p.fireEvent('loadComplete', pew);
				}});
				p.mySchedRequest.send();
			}else if ($chk(p.options.html)){
				this.typedialog = "dialog";
				p.el2.adopt(p.options.html);
				p.centerize();
				p.myFx2 = new Fx.Morph(p.eltop.id, {duration: 100, transition: Fx.Transitions.Sine.easeOut});
				p.myFx2.start({
					'opacity': [1]
				});
				p.fireEvent('loadComplete');
			}else{
				this.typedialog = "alert";
				p.el3 = new Element('div', {'styles': {'width': '200px', 'text-align': 'center'}});
				p.el4 = p.el3.clone();
				p.el3.innerHTML = p.options.msg;
				p.el3.injectInside(p.el2);
				p.el4.injectInside(p.el2);
				p.capts = new Array();
				p.capts.push(new Array());
				p.capts.push(new Array("OK"));
				p.capts.push(new Array("OK", "Cancel"));
				p.capts.push(new Array("Yes", "No"));
				p.capts.push(new Array("Yes", "No", "Cancel"));
				p.el5 = new Array();
				for(i=0;i<p.capts[p.options.button].length;i++){
						p.el5.push(new Element('input', {'type': 'button', 'id': 'bbutt'+(i+1), 'value': p.capts[p.options.button][i], 'styles': { 'margin': '5px 3px'}, 'events': { 'click': function(){ p.fireEvent('select', this.id.slice(5)); /*p.el.destroy(); p.el2.destroy();*/p.closeDialog();}}}).injectInside(p.el4));
				}
				p.centerize();
				p.myFx2 = new Fx.Morph(p.eltop.id, {duration: 100, transition: Fx.Transitions.Sine.easeOut});
				p.myFx2.start({
					'opacity': [1]
				});
				p.fireEvent('loadComplete');
			}
		});
	},
	setMsgAnswer: function(aw){
		this.myanswer = aw;
	},
	getMsgAnswer: function(){
		return this.myanswer;
	},
	closeDialog: function(){
		this.chainclose = new Chain();
		var p=this;
		var q=this.chainclose;
		//this.chainclose.chain(onClose).chain(this.reallyclose).callChain().callChain();
		this.chainclose.chain(function(){ p.fireEvent('close'); }, p.reallyclose.bind(p));
		this.chainclose.callChain();
		this.chainclose.callChain();
		//this.fireEvent('onClose').reallyclose().callChain();
	},
	reallyclose: function(){
		var p = this;
		p.myFx2 = new Fx.Morph(this.eltop.id, {duration: 200, transition: Fx.Transitions.Expo.easeOut});
		p.myFx2.start({'opacity': [0]}).chain(function(){
			p.eltop = this.element.dispose();
			p.el.setStyle('display', 'none');
			p.eltop.setStyle('display', 'none');
			p.el.innerHTML = "";
			p.myFx = new Fx.Morph(p.el.id, {duration: 200, transition: Fx.Transitions.Expo.easeOut, onComplete: function(){p.el = this.element.dispose(); p.fireEvent('reallyClose'); }});
			p.myFx.start({'opacity': [0]});
		});
	}
});
