<!-- 
function NoSpam(user,domain) {
locationstring = "mailto:" + user + "@" + domain;
window.location = locationstring; }
-->

window.addEvent('domready', function() {
	
	/* Rozwin / Zwin */
	$$('.menu-js').each(function(trig) {
		trig.addEvent('click', function(e) {
			var box = trig.getParent('.box-wide');
			
			e.preventDefault();
			if (trig.hasClass('menu-js-open')) {
				trig.set('text', 'Zwiń');
			} else {
				trig.set('text', 'Rozwiń');
			}
			trig.toggleClass('menu-js-open');
			box.toggleClass('box-closed');
		})
	});	
	
});


var Cal = new Class({
	Implements: [Events, Options],

	options: {
		months: ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII']
	},

	initialize: function(cal, options) {
		this.setOptions(options);
		this.cal = cal;
		this.metaholder = cal.getElement('.cal-meta');
		this.datesholder = cal.getElement('.cal-items');
		this.events = cal.getElement('.cal-events');
		this.date = new Date();	
				
		cal.getElement('.cal-next').addEvent('click', this.changeWeek.bindWithEvent(this, 1));
		cal.getElement('.cal-prev').addEvent('click', this.changeWeek.bindWithEvent(this, -1));
		
		this.datesholder.addEvent('click', function(e) {
			e.stop();
			if (e.target.tagName.toLowerCase() === 'a' && !this.locked) {
				this.showDay($(e.target));
			}
		}.bind(this));
		
		this.loader = new Element('div', {
			'class': 'cal-loader',
			'text': 'Wczytuję...',
			'styles': {'display': 'none'}
		}).inject(this.events, 'after');
	},
	
	changeWeek: function(e, dir) {
		
		if (e) {
			e.stop();
		}
		
		var datecopy, m, d, y,
			fragment = document.createDocumentFragment();
		
		this.date.setDate(this.date.getDate() + (dir * 7));
		datecopy = new Date(this.date.getTime());
		
		for (var i=0; i<7; i++) {
			d = this.handleZero(datecopy.getDate());
			m = this.handleZero(datecopy.getMonth() + 1);
			M = this.options.months[datecopy.getMonth()];
			y = datecopy.getFullYear();
			
			fragment.appendChild(
				new Element('li').grab(
					new Element('a', {
						'href': '#' + y + m + d,
						'html': d + '<br />' + M
					})
				)
			);
			
			datecopy.setDate(datecopy.getDate() + 1);
		}
		
		this.datesholder.empty().appendChild(fragment);
	},
	
	showDay: function(trig) {
		
		var nr = this.getNr(trig.href),
			id = 'cal-events_',
			tg,
			actActiveDate = this.datesholder.getElement('.active'),
			actActiveEvents = this.events.getElement('.active');
		
		if (actActiveDate) {
			actActiveDate.removeClass('active');
		}
		
		if (actActiveEvents) {
			actActiveEvents.removeClass('active');
		}
		
		trig.addClass('active');
		tg = $(id + nr);
		
		if (tg) {
			tg.addClass('active');
			this.metaholder.set('html', tg.getElement('.meta').get('html'));
		} else {
			this.locked = true;
			this.loader.setStyle('display', 'block');
			
			new Request.JSON({
				url: '/ajax.php',
				onSuccess: function(res) {
					var li = new Element('li', {
						'id': id + nr,
						'class': 'item active',
						'html': res.events
					}).inject(this.events);
					
					this.metaholder.set('html', li.getElement('.meta').get('html'));
					this.loader.setStyle('display', 'none');
					this.locked = false;	
				}.bind(this)
			}).get({
				state: 'calendar',
				date: nr
			});
		}
	},
	
	handleZero: function(v) {
		return v < 10 ? '0' + v : v;
	},
	
	getNr: function(str) {
		return str.match(/#([0-9]+)$/)[1];
	}
});


window.addEvent('domready', function() {
	var cal = $('calendar');
	if (!cal) { return; }
	new Cal(cal);
});


var Newsletter = new Class({
	Implements: [Events, Options],
	
	options: {},
	
	initialize: function(form, options) {
		this.setOptions(options);
		this.form = form;
		this.submit = form.getElement('input[name="submit"]');
		this.button = form.getElement('input[name="remove"]');
		this.input = form.getElement('input[name="email"]');
		
		this.submit.addEvent('click', this.handle.bindWithEvent(this, 'add'));
		this.button.addEvent('click', this.handle.bindWithEvent(this, 'del'));
	},
	
	handle: function(e, action) {
		e.stop();
		this.send({
			action: action, 
			state: 'newsletter', 
			email: this.input.value
		});
	},
	
	send: function(obj) {
		var wl = window.location;
		
		if (this.isValidEmail(obj.email)) {
		 	new Request.JSON({
				method: 'get', 
				url: '/ajax.php',
				onRequest: function() {
					this.fireEvent('requestStart', [this]);
				}.bind(this),
				onSuccess: function(res) {
					this.fireEvent('requestEnd', [this, res]);
				}.bind(this)
			}).send(obj);
		} else {
			this.fireEvent('invalidEmail');
		}
	},
	
	isValidEmail: function(str) {
		return /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/.test(str);
	}
});

window.addEvent('domready', function() {
	var form = $('newsletter');
	if (!form) { return; }
	
	new Newsletter(form, {
		onInvalidEmail: function() {
			alert('nieprawidłowy email');
		},
		onRequestStart: function(self) {
			self.submit.value = 'Wysyłam...';
		},
		onRequestEnd: function(self, res) {
			var msgnode = self.form.getElement('.message')
					|| new Element('p', {'class': 'message'})
						.inject(self.form, 'top');
			
			self.submit.value = 'Zapisz';
			msgnode.removeClass('message-good').removeClass('message-bad');
			if (res) {
				if (res.state === 1) {
					msgnode.addClass('message-good');
				} else {
					msgnode.addClass('message-bad');
				}
				msgnode.set('text', res.msg);
			} else {
				msgnode.addClass('message-bad').set('text', 'Wystąpił błąd podczas wysyłania');
			}
		}
	});
});


(function($) {

this.PioLightbox = new Class({
	
	Implements: [Events, Options, Chain],
	
	ie6: Browser.Engine.trident4,
	
	options: {
		startDimensions: {x: 100, y: 100},
		containerPadding: 10,
		containerMargin: 10,
		captionHeight: 40,
		overlayDuration: 200,
		resizeDuration: 500,
		showDuration: 250,
		captionDuration: 250,
		counter: "Zdjęcie {NUM} z {TOTAL}"
	},

	initialize: function(anchors, options) {
		this.setOptions(options);
		this.items = [];

		anchors.each(function(anchor, i) {
			this.items.push({
				src: anchor.href,
				caption: anchor.title
			});
			anchor.addEvent('click', function(e) {
				e.stop();
				this.open(i);
			}.bind(this));
		}, this);
		
		this.animations = [
			// rozszerzenie
			function() {
				var imgSize = {
						x: this.actual.img.width,
						y: this.actual.img.height
					},
					opt = this.options,
					newContSize = this.getSize(imgSize),
					newImgSize = this.changeSize(
							newContSize, -opt.containerPadding * 2, -opt.containerPadding * 2),
					newContPosition = this.getCenterPosition(
							this.changeSize(newContSize, null, opt.captionHeight));				
				
				this.actual.img.setStyles({
					width: newImgSize.x,
					height: newImgSize.y
				});
				
				this.navnext.setStyle('height', newImgSize.y);
				this.navprev.setStyle('height', newImgSize.y);
				
				this.fx.resize.start({
					width: newContSize.x,
					height: newContSize.y,
					left: newContPosition.x,
					top: newContPosition.y
				});
			},
			
			// pokazanie obrazka
			function() {	
				this.inner.removeClass('loading');
				this.inner.setStyle('opacity', 0);
				this.inner.grab(this.actual.img);
				this.fx.show.start(1);	
			},
			
			// pokazanie caption
			function() {
				var countertext;
				
				this.navprev.setStyle('visibility', 'visible');
				this.navnext.setStyle('visibility', 'visible');
				
				if (this.options.counter) {
					countertext = this.options.counter;
					countertext = countertext.replace(/\{NUM\}/, this.actualindex + 1);
					countertext = countertext.replace(/\{TOTAL\}/, this.items.length);
					this.counter.set('text', countertext);
				}
				
				this.desc.set('text', (this.actual.caption ? this.actual.caption : ''));
				
				this.caption.setStyle('display', 'block');
				this.fx.caption.start(0);
			},
			
			// preload kolenjego
			function() {
				return;
				var next = this.items[this.actualindex + 1];
				
				if (next) {
					this.loadImage(next, true);
				}
			}
		];
		
		this.resizeFn = this.reconstructOverlay.bindWithEvent(this);
		this.keyFn = this.keyboardListener.bindWithEvent(this);
	},
	
/*
	Public methods
*/	
	
	open: function(index) {
		index = index || 0;
		
		this.actual = this.items[index];
		this.actualindex = index;
		
		this.windowSize = window.getSize();
		this.windowScroll = window.getScroll();
		
		if (!$chk(this.container)) {
			this.createContainer();
			this.fireEvent('containerCreated');
			this.createFxs();
			window.addEvent('resize', this.resizeFn);
			document.addEvent('keypress', this.keyFn);
			this.fx.overlay.start(0.8);
		} else {
			this.clearChain();
			for (var x in this.fx) {
				this.fx[x].cancel();
			}
			this.inner.empty().addClass('loading');
			this.navnext.setStyle('visibility', 'hidden');
			this.navprev.setStyle('visibility', 'hidden');
			this.caption.setStyles({
				'display': 'none',
				'top': -this.options.captionHeight
			});
		}
		
		if (this.actual.img) {
			this.startAnimations();
		} else {
			this.loadImage(this.items[index]);
		}
	},

	next: function(e) {
		if (e) {
			e.stop();
		}
		
		var next = this.actualindex + 1;
		
		if (next > this.items.length - 1) {
			 next = 0;
		}
		if (next < 0) {
			next = (this.items.length - 1);
		}
		this.open(next);
	},
	
	previous: function(e) {
		if (e) {
			e.stop();
		}
		
		var prev = this.actualindex - 1;
		
		if (prev > this.items.length - 1) {
			prev = 0;
		}
		if (prev < 0) {
			prev = (this.items.length - 1);
		}
		this.open(prev);	
	},
	
	close: function() {
		this.container.destroy();
		new Fx.Tween(this.overlay, {
			duration: this.options.overlayDuration,
			property: "opacity",
			onComplete: function() {
				this.overlay.destroy();
				this.overlay = null;
				this.container = null;
				this.actual = null;
				this.actualIndex = null;
				this.fireEvent('close');
			}.bind(this)
		}).start(0);
		window.removeEvent('resize', this.resizeFn);
		document.removeEvent('keypress', this.keyFn);
	},

	add: function(mix) {
		if ($type(mix) == 'array' && mix.length > 0 && mix[0].src) {
			this.items.combine(mix);
		} else if ($type(mix) == 'object' && mix.src)  {
			this.items.push(mix);
		}
		return this;
	},
	
/*
	Private methods
*/
	
	startAnimations: function() {
		this.chain(this.animations);
		this.callChain();
	},
	
	loadImage: function(item, isOnlyPreload) {
		if (item.img) {
			return;
		}
		
		item.img = new Asset.image(item.src, {
			'onload': function(img) {
				if (item == this.actual && !isOnlyPreload) {
					this.startAnimations();
				}
			}.bind(this)
		});
	},
	
	getSize: function(obj) {
		var res = {},
			conMar = this.options.containerMargin * 2,
			conPad = this.options.containerPadding * 2,
        	ws = {
				x: this.windowSize.x - conMar,
				y: this.windowSize.y - conMar - this.options.captionHeight
			},
			con = {
				x: obj.x + conPad,
				y: obj.y + conPad
			};
		
		if (con.x <= ws.x && con.y <= ws.y) {
			return con;
        }

        if (con.x - ws.x > con.y - ws.y) {
            res.x = ws.x;
            res.y = Math.ceil((res.x / con.x) * con.y);
        } else {
        	res.y = ws.y;
            res.x = Math.ceil((res.y / con.y) * con.x);
        }

        return res;
	},	
	
	reconstructOverlay: function() {
		var scrollSize = window.getScrollSize();
		this.overlay.setStyles({
			width: scrollSize.x,
			height: scrollSize.y
		});
		this.fireEvent('overlayReconstructed');
	},
	
	keyboardListener: function(e) {
		if (e.key == "esc") {
			this.close();
		}
	},
	
	getCenterPosition: function(dim) {
		return {
			x: Math.ceil(this.windowSize.x / 2 - (dim.x / 2)) + this.windowScroll.x,
			y: Math.ceil(this.windowSize.y / 2 - (dim.y / 2)) + this.windowScroll.y
		}
	},
	
	changeSize: function(obj, x, y) {
		x = x || 0;
		y = y || 0;
		return {
			x: obj.x + x,
			y: obj.y + y
		}
	},
	
	createContainer: function() {
		var scrollSize = window.getScrollSize(),
			dim = this.options.startDimensions,
			position = this.getCenterPosition(dim);
						
		this.overlay = new Element('div', {
			'events': {
				'click': function(e) {
					this.close();
				}.bind(this)
			},
			'class': 'piolightbox-overlay',
			'styles': {
				'opacity': 0, 
				'width': scrollSize.x, 
				'height': scrollSize.y
			}
		}).inject(document.body);
		
		this.container = new Element('div', {
			'class': 'piolightbox',
			'styles': {
				'width': dim.x,
				'height': dim.y,
				'left': position.x,
				'top': position.y
			}
		}).adopt(
			this.inner = new Element('div', {
				'class': 'inner loading'
			}),
			this.caption = new Element('div', {
				'class': 'caption',
				'styles': {'display': 'none', 'top': -this.options.captionHeight}
			}).adopt(
				new Element('div', {
					'class': 'close',
					'events': {
						'click': this.close.bindWithEvent(this)
					}
				}),
				this.counter = new Element('div', {
					'class': 'counter'
				}),
				this.desc = new Element('div', {
					'class': 'desc'
				})
			),
			this.navprev = new Element('a', {
				'class': 'nav nav-prev', 
				'href': '#', 
				'events': {
					'click': function(e) {
						this.previous(e);
					}.bind(this)
				},
				'styles': {'visibility': 'hidden'}
			}),
			this.navnext = new Element('a', {
				'class': 'nav nav-next', 
				'href': '#',
				'events': {
					'click': function(e) {
						this.next(e);
					}.bind(this)
				},
				'styles': {'visibility': 'hidden'}
			})
		).inject(document.body);		
	},
	
	createFxs: function() {
		this.fx = {
			overlay: new Fx.Tween(this.overlay, {
				duration: this.options.overlayDuration,
				property: "opacity"
			}),
			
			resize: new Fx.Morph(this.container, {
				duration: this.options.resizeDuration,
				onComplete: this.callChain.bind(this)
			}),
			
			show: new Fx.Tween(this.inner, {
				duration: this.options.showDuration,
				property: "opacity",
				onComplete: this.callChain.bind(this)
			}),
			
			caption: new Fx.Tween(this.caption, {
				property: "top",
				duration: this.options.captionDuration,
				onComplete: this.callChain.bind(this)
			})
		};		
	}
});

})(document.id || $);

window.addEvent('domready', function() {
	
	var event_gallery = $('event-gallery'),
		event_images,
		data,
		gal,
		iframeshime;
	
	if (event_gallery) {
		
		event_images = event_gallery.getElements('.lightbox');
		data = commentData(event_gallery.getElement('.images'));
		
		if (event_images.length > 0) {
			
			gal = new PioLightbox(event_images, {
				onContainerCreated: function() {
					iframeshime = new IframeShim(gal.overlay, {display: true});	
				},
				onClose: function() {
					iframeshime.destroy();
				},
				onOverlayReconstructed: function() {
					iframeshime.position();
				}
			});
			
			if (data && data.others.length > 0) {
				for (var i = 0, l = data.others.length; i < l; i++) {
					gal.add({src: data.others[i]});
				}
			}
			
		}
		
	}
	
});


/* ANKIETA */
window.addEvent('domready', function() {
	
	if (!document.getElementById('ankieta-form')) {
		return;
	}
	
	var form = $('ankieta-form'),
		radios = form.getElements('input[type="radio"]'),
		button_vote = $('ankieta-vote-trigger'),
		button_results = $('ankieta-results-trigger')
		
		getRadioValue = function() {
			var len = radios.length;
			
			while (len--) {
				if (radios[len].checked === true) {
					return radios[len].value;
				}
			}
			return false;
		},
		
		showMessage = function(msg) {
			alert(msg);
		},
		
		prepareButton = function(el) {
			el.store('previousValue', el.value);
			el.value = "Ładuję...";
			el.disabled = true;
		},
		
		getNodeFromInnerHTML = function(html) {
			var nodetmp = new Element('div');
			
			nodetmp.set('html', html);
			return nodetmp.getChildren()[0];
		},
		
		send = function(obj, trigger) {
			var data = $merge({state: 'ankieta'}, obj || {});
			
			new Request({
				url: '/ajax.php',
				onSuccess: function(res) {

					var resNode, button_back;
					
					trigger.value = trigger.retrieve('previousValue');
					trigger.disabled = false;
					
					if (res) {
						resNode = getNodeFromInnerHTML(res);
						form.addClass('hide');
						resNode.inject(form, 'after');
						button_back = resNode.getElementById('ankieta-back-trigger');
						
						if (button_back) {
							button_back.addEvent('click', function(e) {
								e.stop();
								form.removeClass('hide');
								resNode.destroy();
							});
						} else {
							form.destroy();
						}
					}
					
				}
			}).get(data);
		};
	
	
	button_vote.addEvent('click', function(e) {
		e.stop();
		
		if (this.disabled === true) { return; }
		
		var radiovalue = getRadioValue();
		
		if (radiovalue) {
			prepareButton(this);
			send({answer: radiovalue}, this);
		} else {
			showMessage('Nie wybrano żadnej odpowiedzi');
		}
	});
	
	button_results.addEvent('click', function(e) {
		e.stop();

		if (this.disabled === true) { return; }

		prepareButton(this);
		send(null, this);
	});
	
});
