/*****************************************************************/
//
// okienko dialogowe
//
/*****************************************************************/
var Dialog = new Class({
	Implements: [Options, Chain, Events],
	options: {
		// początkowa szerokość okienka dialogowego
		width : '400px',
		
		// początkowa wysokość okienka dialogowego
		height: '100px',
		
		// typ okienka dialogowego. Możliwe opcje: alert, confim, custom,
		type: 'alert', 
		
		onYesClick : function(event){ 
			// zdarzenie wywoływane w momencie naciśnięcia przycisku "Tak"
			event.stop();
			this.hide();
		},
		
		onOkClick: function(event){
			// zdarzenie wywoływane w momencie naciśnięcia przyc8isku "OK"
			event.stop();
			this.hide();
		},
		
		onNoClick: function(event){
			// zdarzenie wywoływane w momencie naciśnięcia przycisku "Nie"
			event.stop(); 
			this.hide();
		}
		
	},
	initialize: function(options){
		this.setOptions(options);
		// zasłona 
		this.bckg = new Element( 'div', {
			'id' : 'dlg_bg',
			'class' : 'dlg_bck_div',
			'styles': {	'display' : 'none', 'height' : window.getScrollHeight() }
		})
		
	
		// tworzenie diva, w ktorym będą wyświetlane wiadomosci
		this.content = new Element( 'div', {
			'class' : 'dlg_content',
			'id'	: 'dlg_content',
			'styles' :{
				'width' : this.options.width.toInt()+'px',
				'height': this.options.height.toInt()+'px',
				'margin-left': '-'+this.options.width.toInt()/2 + 'px',
				'margin-top': '-'+this.options.height.toInt()/2 + 'px',
				'display' : 'none'
				
			}	
		})
		
		this.bckg.injectTop(document.body)
		this.content.injectAfter(this.bckg);
								
		// TODO: poprawka aby nie nadpisywało już istniejący eventów
		window.addEvent('keydown', this.keydown.bind(this));
		 
		//this.content.makeDraggable(); 
	},
	show: function(){
		this.bckg.setStyle('display','')
		this.bckg.setStyle('height', window.getScrollHeight())
		this.bckg.setOpacity(0.8);
//		this.bckg.tween('opacity', "0.8");
		
		this.content.setStyle('display','')
		
		from = window.getScrollTop() - this.options.height.toInt();
		to = window.getScrollTop() + 400; 
				
		this.content.setStyle('top', to+'px');
//		this.content.tween('top', to+'px')
		
		if("custom" != this.options.type){
			
			btns_div = new Element('div', {'class':'btns_div_class'})
			
			if("alert" == this.options.type){
				(new Element('a', {
					events: {
						'click' : (function(event){this.fireEvent("okClick", event);}).bind(this)
					},
					'class': "btns cp"
				}).set('text', 'ZAMKNIJ')).injectInside(btns_div);
				
			}
			
			if("confirm" == this.options.type){
				(new Element('a', {
					events: {
						'click' : (function(event){this.fireEvent("yesClick", event);}).bind(this)
					}
					,
					'class': "btns cp"			
				}).set('text', 'Tak')).injectInside(btns_div);
				
				(new Element('a', {
					events: {
						"click" : (function(event){ this.fireEvent("noClick", event); }).bind(this) 
					},
					'class': "btns cp"
				}).set('text', 'Nie')).injectInside(btns_div);
			}
			
			btns_div.injectInside(this.content);
		}
		
		return this
	},
	hide: function(){
//		this.bckg.setStyle('display', 'none')
//		this.content.setStyle('display', 'none')
		$$('dlg_bg').each(function(el,i){el.destroy();});
		$('dlg_bg').destroy();
		$$('dlg_content').each(function(el,i){el.destroy();});
		$('dlg_content').destroy();
		return this
	},
	
	getHandle: function(){
		return this.content;
	},
	
	setWidth: function(width){
		this.width = width
		this.options.content.setStyle('width', width)
		this.content.setStyle('margin-left', '-'+this.options.width.toInt()/2 + 'px')
		return this
	},
	
	setHeight: function(height){
		this.options.height = height
		this.content.setStyle('height',height)
		this.content.setStyle('margin-top', '-'+this.options.height.toInt()/2 + 'px')
		return this
	},
	
	load: function(aurl){
		update_content = this.content
		new Request.HTML({
			method: 'get',
			url: aurl,
			update: update_content
			
		}).send();
	},
	setHTML: function(aHTML){
		this.content.set('html', aHTML);
		return this
	},
	setType: function(aType){
		this.options.type = aType
		return this
	},
	
	keydown: function(event){
		if(event.key = 'esc') this.hide();
		window.removeEvent('keydown');
	}
});
