document.observe('dom:loaded', function () {
										 
	
	//	init lightwindow
	myLightWindow = new lightwindow({
		overlay : {
			opacity : 0.7,
			image : $$('html')[0].id + '/images/black.png',
			presetImage : $$('html')[0].id + '/images/black-70p.png'
		}
	});
	myLightWindow.options.skin.loading =
		'<div id="lightwindow_loading" >'+
			'<img src="' + $$('html')[0].id + '/images/ajax-loading.gif" alt="betöltés" />'+
			'<span><a href="javascript: myLightWindow.deactivate();">mégsem</a></span>'+
			'<iframe name="lightwindow_loading_shim" id="lightwindow_loading_shim" src="javascript:false;" frameBorder="0" scrolling="no"></iframe>'+
		'</div>';
		
	
		
	if ($('calendar')) eventCalendar.init();
	
	headerImgSlides.init();
	
	//	guestbook nickname rememberer
	if ($(document.body).hasClassName('guestbook')) {
		cookies = new CookieJar({
			expires: 36000000   // seconds
		});
		
		var form = $$('form')[0];
		var input = form.down('input[name="nick"]');
		
		var savednick = cookies.get('nick');
		if (savednick != undefined) input.value = savednick;
		
		form.observe('submit', function (e) {
			var newnick = input.value;
			if (newnick != savednick) cookies.put('nick', newnick);
		});
	}
	
});

var myLightWindow = null;

var eventCalendar = {
	value: null,
	
	events: false,
	
	init: function () {
		this.fetch();
		Calendar.setup({
         // dateField     : 'date',
          parentElement : 'calendar'
        });
	},
	
	fetch: function () {
		new Ajax.Request('.', {
			method: 'POST',
			parameters: {
				ac: 'fetch_events'
			},
			onComplete: function (resp) {
				var result = resp.responseText.evalJSON();
				this.events = $H(result.events);
				this.update();
			}.bind(this)
		});
	},
	
	update: function () {
		if (this.events) {
			this.events.each( function (ev) {
				//console.log(ev);
				var cell = $('cellid_' + ev.key);
				if (cell) {
					cell.title = ev.value.descr;
					cell.eventID = ev.value.id;
					cell.addClassName('event');
				}
			});
		}
	},
	
	show: function (date) {
		this.events.each( function (ev) {
			//console.log(ev.key + ' ---- ' + date);
			if (ev.key == date) {
				$('event-details').update(date + ':  <a href="/esemenyek/?esemeny=' + ev.value.id + '">' + ev.value.descr + ' &raquo;</a>');
			}
		});
	}
};

var headerImgSlides = {
	current: 	0,
	delay: 		15,
	duration:	0.8,
	
	transition: Effect.Transitions.linear,
	
	init: function () {
		this.div = $('header-img');
		if (headerImgs.length > 0) {
			headerImgs.each( function (el) {
				img = new Image;
				img.src = el;
			});
			this.set(headerImgs[this.current]);
		}
	},
	
	set: function (img) {
		this.div.setStyle({
			backgroundImage: 'url(' + img + ')'
		});
		if (headerImgs.length > 1) setTimeout(function () { this.next(); }.bind(this), this.delay*1000);
	},
	
	next: function () {
		this.current = this.current == headerImgs.length - 1 ? 0 : this.current + 1;
		new Effect.Fade(this.div, {
			duration: this.duration/2,
			transition: this.transition,
			afterFinish: function () {
				
				this.set(headerImgs[this.current]);
				new Effect.Appear(this.div, {
					duration: this.duration/2,
					delay: this.duration/2,
					transition: this.transition
				});
				
			}.bind(this)
		});
		
	}
}
