Event.observe(window, 'load', function() {
	// events on események page
	if (Element.hasClassName(document.body, 'esemenyek') && !Element.hasClassName(document.body, 'admin')) {
		// accordion for visitors only (all details are open for admin to edit)
		accordioninit();
		
		// floater positioning
		positionFloater();
		Event.observe(window, 'scroll', positionFloater);
		Event.observe(window, 'resize', positionFloater);
	}
});

/*	ESEMENYEK page functions	*/
function positionFloater() {
	// set dimensions & elements
	var el = $('floater'),
	parent = el.up(),
	elOff = el.cumulativeScrollOffset(),
	elDim = el.getDimensions(),
	parentOff = parent.cumulativeOffset(),
	parentDim = parent.getDimensions(),
	contDim = $('container').getDimensions(),
	vpDim = document.viewport.getDimensions();
	
	// calculate position & make sure it stays within container
	var pos = elOff.top - parentOff.top + (vpDim.height - elDim.height)/2;
	pos = pos > 0 ? pos : 0;
	pos = (pos > $('container').getHeight() - parentOff.top - elDim.height - 10) 
		? $('container').getHeight() - parentOff.top - elDim.height - 10
		: pos;
	// animate floater
	if (el.effect) el.effect.cancel();
	el.effect = new Effect.Morph(el, {
		style: 'top: ' + pos +'px;',
		transition: Effect.Transitions.sinoidal,
		duration: 0.5
	});	
}

function accordioninit() {
	$$('.trigger').each(function(el) {
		// hide all, but first detail box
		if (el != $('right').down('.trigger')) {
			el.next('.box').hide();
			el.down('span').update('+');
		}
		
		// open & close animation, button state change
		el.onclick = function() {
			var box = el.next('.box');
			// only if currently not animating
			if (!box.effect) {
				if (box.visible()) {
					box.effect = new Effect.BlindUp(box, {
						duration: 0.5,
						afterFinish: function(obj) {
							box.effect = false;
							el.down('span').update('+');
						}
					});
				} else {
					box.effect = new Effect.BlindDown(box, {
						duration: 0.5,
						afterFinish: function(obj) {
							box.effect = false;
							el.down('span').update('&ndash;');
						}
					});
				}
			}
		}
	});
	
}

/*	LOGIN function	*/
function checkLogin() {
	new Ajax.Request('.', {
		parameters: {
			username: $F('inp_username'),
			password: $F('inp_password'),
			ajaxCommand: 'checkLogin'
		},
		onComplete: function(transport, json) {
			showFeedback(json);
		}
	});
}

function showLogin() {
	$('inp_feedback_btn').blur();
	new Effect.Morph('feedback_wrapper', {
		style: 'margin-top: -300px',
		transition: Effect.Transitions.sinoidal,
		duration: 0.5,
		afterFinish: function () {
			new Effect.Morph('form_wrapper', {
				style: 'margin-top: 21px',
				transition: Effect.Transitions.sinoidal,
				duration: 0.3,
				afterFinish: function() {
					$('inp_username').focus();
				}
			});
		}
	});
}

function showFeedback(json) {
	$('inp_username').blur();
	$('inp_password').blur();
	new Effect.Morph('form_wrapper', {
		style: 'margin-top: -300px',
		transition: Effect.Transitions.sinoidal,
		duration: 0.5,
		afterFinish: function () {
			$('feedback_text').update(json.feedback);
			new Effect.Morph('feedback_wrapper', {
				style: 'margin-top: 21px',
				transition: Effect.Transitions.sinoidal,
				duration: 0.3,
				afterFinish: function() {
					$('inp_feedback_btn').focus();
					if (json.result) window.location.reload();				
				}
			});
		}
	});
	
}