/****************************************/
/*   Klasa okienka                      */
/*   Piotr Kaluzynski                   */
/****************************************/


var IWindow = new Class ({
	Implements: Options,
	
	auto_left: false,
	auto_height: true,
	open: false,

	options: {
		width: 100,
		height: 100,
		x: 0,
		y: 0,
		autoHeight: false,
		anim: true,
		html: ''
	},
	
	initialize: function (options) {
		this.setOptions(options);
		
		if (this.options.x <= 0) {
			this.auto_left = true
		}
		if (this.options.y <= 0) {
			this.auto_top = true
		}	
		this.mask = new Mask();
		this.win = new Element ('div',{
			'class': 'iwindow',
			'html' : '<div class="iw_lt">' +					
				 		'<div class="iw_t">' +
							'<span class="iw_rt_inner"></span>' +
							'<div class="iw_close"></div>' +
				 			'<div class="iw_title">'+this.options.title+'</div>' +
						'</div>' +					
					'</div>' +
						'<table class="iw_middle">' +
							'<tr>' +
								
								'<td class="iw_l"></td>' +
									'<td class="iw_m">'+this.options.html+'</td>' +
								'<td class="iw_r"></td>' +
							'</tr>' +
						'</table>' +
									
								
					
					'<div class="iw_lb">' +
					 		'<div class="iw_b">' +
								'<div class="iw_rb"></div>' +
							'</div>' +
					'</div>',				
			'styles': {
				'display': 'none',
				'visibility': 'hidden',
				'opacity': 0,				
				'left': this.options.x/2,
				'top': this.options.y/2
			}
		})
		this.win.inject (document.body, 'top');
		
		
		
		this.win.getElements('div[class=iw_close]')[0].addEvent('click', function () {
			this.hide ()
		}.bind(this))
		
		try {
			$('iwin_cancel').addEvent('click', function () {
				this.hide();
			}.bind(this))
		} catch (e) {}
		
		this.fx_win = new Fx.Tween (this.win, {
			duration: 150
		})
		
		new Drag.Move (this.win, {
			'handle' : this.win.getElements('div[class=iw_t]')[0]
		});
	},
	
	set_positons: function () {

		if (this.auto_left) {
			var x = window.getScrollLeft() +((window.getSize().x - this.options.width) / 2);
		} else {
			var x = this.options.x + window.getScrollLeft();
		}
		if (this.auto_top) {
			
			var y = window.getScrollTop() + ((window.getSize().y - this.options.height) / 2 -40);
		} else {
			var y = this.options.y + window.getScrollTop();
		}

		this.win.setStyle('top', y);
		this.win.setStyle('left', x);
	},
	
	show: function () {
		if (!this.open) {
			this.mask.show();
			this.set_positons();	
					
			this.win.setStyle('display', 'block');
			
			var show_function = function () {
				this.fx_win.start('opacity', '1')
			}
			show_function.delay(100, this);
			this.open = true;
		}
	},
	
	hide: function () {
		if (this.open) {
			this.fx_win.start('opacity', '0')
			this.mask.hide();
			
			this.fx_win.onComplete = function () {
				this.win.setStyle('display', 'none')
				this.fx_win.onComplete = $empty;
			}.bind(this)			
			
			this.open = false;
			
		}
	}
})


var Mask = new Class ({
	
	initialize: function () {
		this.mask = new Element('div', {
			'id': 'iwindow_mask',
			'styles': {
				'display': 'none',
				'visibility': 'hidden',
				'opacity': 0,
				'left': 0,
				'top': 0,
				'height': $(document.body).getStyle('height').toInt()
			}
		})
		this.mask.inject (document.body, 'top');		
		this.fx_mask = new Fx.Tween (this.mask, {
			duration: 700
		})

	},
	
	show: function () {
		this.mask.setStyle('display', 'block')
		this.fx_mask.start('opacity', '0.5')
	},
	
	hide: function () {
		this.fx_mask.start('opacity', '0')
		this.fx_mask.onComplete = function () {
			this.mask.setStyle('display', 'none')
			this.fx_mask.onComplete = $empty;
		}.bind(this)
	}
});

window.addEvent('domready', function () {
	
	if ( Cookie.read('i_window_pzu') == null ) {
		if ($chk($('window_content'))) {
			Cookie.write('i_window_pzu', 1, { duration: 0, domain: 'leszko.pl', path: '/' });
			win = new IWindow({
				width: 600,
				height: 400,
				title: $('i_baner_name') ? $('i_baner_name').get('title') : '',
				autoHeight: true,
				anim: true,
				html: $('window_content').get('html')
			})
			win.show()
		}	
	}
})
