var setLoginCheckboxPosition = function() {
	var e = $('LoginPamietajTopBar');
	if($type(e) == 'element') {
		var ch = e.getCoordinates().height.toInt();
		var dh = e.getParent('form').getCoordinates().height.toInt();
		var t = ((dh - ch) / 2 - 1).toInt();
		if(Browser.Engine.trident) {
			if(Browser.Engine.version == 6) {
				e.setStyle('margin-top', t + 'px');
			}
		} else {
			e.setStyle('margin-top', t + 'px');
		}
	}
};

var setLoginEmailWidth = function() {
	var e = $('mainHeaderLoginEmail');
	if($type(e) == 'element') {
		var ow = e.getCoordinates().width.toInt();
		e.setStyle('width', 'auto');
		var w = e.getCoordinates().width.toInt();
		w = (w - ow).toInt();
		e = e.getParent('div#mainHeaderLogin');
		ow = e.getCoordinates().width.toInt();
		ow = (ow + w + 5).toInt();
		e.setStyle('width', ow + 'px');
	}
	if(Browser.Engine.trident && Browser.Engine.version == 4) {
		var e = $('mainMenuTop');
		if($type(e) == 'element') {
			e = e.getChildren('div.floatL');
			if(e.length > 0) {
				e = e[0];
				if($type(e) == 'element') {
					if(e.getStyle('width') == '163px') {
						e.setStyle('width', '157px');
					} else if(e.getStyle('width') == '63px') {
						e.setStyle('width', '58px');
					}
				}
			}
		}
	}
};

var fileUpload = function(filesUpload) {
	if(Browser.Engine.trident) {
		return true;
	}
	if(!$type(filesUpload) ) {
		var filesUpload = $$('.fileInput');
	}
	if(filesUpload.length == 0) {
		return true;
	}
	filesUpload.setStyles({
		'opacity': 0,
		'visibility': 'visible'
	}).addEvent('change', function() {
		var div = this.getParent('div.selectSpacer');
		if(div) {
			var p = div.getChildren('p.fileName')[0];
			if($type(p) != 'element') {
				new Element('div', {'class': 'fileImg', 'html': '&nbsp;', 'styles': {'z-index': '100'}}).inject(div, 'top');
				p = new Element('p', {'class': 'fileName', 'html': '&nbsp;', 'styles': {'z-index': '100', 'width': (div.getCoordinates().width - 65).toInt() + 'px'}}).inject(div, 'top');
				this.setStyle('z-index', '1000');
			}
			p.set('html', (this.value == '' ? '&nbsp;' : this.value));
		}
	}).fireEvent('change');
};

var wTrakcie = function(el) {
	if(!el) {
		el = $$('.wTrakcie');
	}
	if(!$type(el) || el.length == 0) {
		return true;
	}
	var pr = ['Year', 'Month', 'Day', 'Hour', 'Minute'];
	el.each(function (el) {
		var clas = el.get('class');
		if(clas) {
			clas = clas.split(' ');
			if(clas.length > 0) {
				clas.each(function (c) {
					if(c.substr(0, 8) == 'element_') {
						var c = c.substr(8);
						var attached = new Array();
						pr.each(function(p) {
							if($(c + p)) {
								attached.push($(c + p));
								$(c + p).addEvent('change', function() {
									if(this.getSelected().length > 0 && this.getSelected()[0].get('value') != '') {
										el.set('checked', false);
									}
								});
							}
						}, this);
						if(attached.length > 0) {
							el.addEvent('click', function() {
								if(this.get('checked')) {
									attached.each(function(s) {
										if(s.getSelected().length) {
											s.getSelected().setProperty('selected', false);
										}
										s.getElement('option').set('selected', true);
										s.set('disabled', true);
									});
								} else {
									attached.each(function(s) {
										s.set('disabled', false);
									});
								}
							}).removeClass('wTrakcie').fireEvent('click');
						}
					}
				}, this);
			}
			
		}
	}, this);
};

var toggleWojewodztwo = function() {
	var sel = this.getSelected();
	if(sel.length > 0 && this.get('title') && $(this.get('title'))) {
		var el = $(this.get('title'));
		var els = el.getElements('select')[0];
		if(sel[0].get('value') != 163 && sel[0].get('value') != '') {
			el.hide();
			var wsel = els.getSelected();
			this.store('selected', wsel);
 			wsel.setProperty('selected', false);
		} else {
 			var wsel = this.retrieve('selected', []);
			if(wsel.length > 0) {
				wsel.setProperty('selected', true);
			}
			el.show();
		}
	}
};

var attachToggelWojewodztwo = function() {
	$$('.panstwo').each(function(el) {
		var title = el.get('title');
		if(title != '') {
			if($(title) && $(title).getElements('select').length > 0) {
				el.addEvent('change', toggleWojewodztwo).removeClass('panstwo').fireEvent('change');
			}
		}
	});
};

var paginWidth = function() {
	if(Browser.Engine.trident || Browser.Engine.version == 4) {
		return false;
	}
	var tmp = $$('.paginator');
	if(tmp.length > 0) {
		tmp.each(function(el) {
			el.getElements('.current').setStyles({
				'width': 'auto',
				'min-width': '22px'
			});
			el.getElements('a').setStyles({
				'width': 'auto',
				'min-width': '22px'
			});
		});
	}
}

window.addEvent('domready', function() {
	setLoginCheckboxPosition();
	setLoginEmailWidth();
	if($$('.bottomButton').length > 0) {
		$$('.bottomButton').addEvents({
			'mouseover': function() {
				this.addClass('hovered');
			},
			'mouseout': function() {
				this.removeClass('hovered');
			}
		});
	}
	fileUpload();
	wTrakcie();
	attachToggelWojewodztwo();
	if($('LoginLoginTopBar')) {
		new OverText($('LoginLoginTopBar'), {'poll': true, 'textOverride': 'e-mail'});
	}
	if($('LoginPasswordTopBar')) {
		new OverText($('LoginPasswordTopBar'), {'poll': true, 'textOverride': 'hasło'});
	}
	if($$('.overTxtLabel')) {
		$$('.overTxtLabel').setStyle('width', '110px');
	}
	if($('LoginLogOutTopBar')) {
		$('LoginLogOutTopBar').addEvent('click', function(e) {
			window.location.href = this.getParent('a').href;
		});
	}
	if($$('.error-message').length > 0) {
		new Fx.Scroll(window, {'offset': {'x': 0, 'y': -200}}).toElement($$('.error-message')[0]);
	}
	paginWidth();
});
