$(window).addEvent('domready', init);

function init() {
	/*$$('#wrapper .popup').each(function(el) {
		el.position({
			'relativeTo': $('wrapper'),
			'position': 'center',
			'edge': 'left',
			'offset': {
				'x': 0,
				'y': -42
			}
		});
		el.fade('hide');
	});*/
	
	/*$$('#wrapper .popup .next').addEvent('click', function(ev) {
		switchPopup(ev, 'next');
	});
	
	$$('#wrapper .popup .prev').addEvent('click', function(ev) {
		switchPopup(ev, 'prev');
	});
	
	$$('#content-overview a').addEvent('click', openExtPopup);*/
	
	$$('#footer ul a', '#navi a', '#content-overview a').addEvent('click', openExtPopup);
	//$$('#content-overview a').addEvent('click', function(){alert('click');});
	//alert($$('#content-overview a'));
	/*$('overlay').setStyles({
		'width': $(document.body).getScrollSize().x,
		'height': $(document.body).getScrollSize().y
	});*/
	
	/*$(window).addEvent('scroll', function() {
		$('overlay').setStyle('top', $(document.body).getScroll().y);
	});*/
}

function openPopup(ev) {
	ev.stop();
	var descId = ev.target.get('href');
	$('overlay').fade(1);
	$$(descId).getLast().show().fade(1);
}

function closePopup(ev) {
	ev.stop();
	var descEl = ev.target.getParent();
	descEl.fade(0).hide();
	$('overlay').fade(0);
}

function switchPopup(ev, which) {
	ev.stop();
	var from = ev.target.getParent('.popup');
	var to = (which == 'next') ? from.getNext('.popup') : from.getPrevious('.popup') ;
	from.fade(0).hide();
	to.show().fade(1);
}

function openExtPopup(ev) {
	ev.stop();
	var popupUri = new URI(ev.target.get('href'));
	var which = popupUri.get('file').replace(/\.html/, '');
	if(!$(which)) {
		var request = new Request.HTML({
			'url': which+'.html',
			'onComplete': function(responseTree, responseElements, responseHTML, responseJavaScript) {
				var popupEl = new Element('div', {
					'id': which == 'contact' ? 'kontakt' : which ,
					'class': 'popup',
					'html': responseHTML
				});
				setCloseEvents(popupEl);
				setSwitchEvents(popupEl);
				$('overlay').fade(1);
				var injectedEl = popupEl.fade('hide').show().inject($('wrapper'), 'bottom');
				positionPopup(injectedEl).fade(1);
				if(which == 'kontakt' || which == 'contact') {
					setKontaktValidators();
					setKontaktFormEvents(); 
				}
			}
		});
		request.get();
	} else {
		$('overlay').fade(1);
		$(which).show().fade(1);
	}
}

function setKontaktValidators() {
	var kontaktform = new FormValidator($('kontakt').getElement('form'), {
		serial: false
	});
}

function setCloseEvents(el) {
	el.getElement('a[class=schliessen]').addEvent('click', closePopup);
}

function setSwitchEvents(el) {
	el.getElements('.popup-nav a').addEvent('click', function(ev) {
		this.getParent().getParent().fade(0).hide();
		openExtPopup(ev);
	});
}

function setKontaktFormEvents() {
	$('kontakt').getElement('form').addEvent('submit', function(ev){
		ev.stop();
		if($('kontakt-sent')) {
			$('kontakt-sent').destroy();
		}
		if(!this.getElement('.validation-failed')) {
			var request = new Request.JSON({
				'url': $('kontakt').getElement('form').get('action')+'?xhr=true',
				'onComplete': function(response) {
					if(response.status == 'success') {
						var popupEl = new Element('div', {
							'id': 'kontakt-sent',
							'class': 'popup',
							'html': response.html
						});
						popupEl.hide().fade(0);
						popupEl.inject($('wrapper'), 'bottom').show();
						positionPopup(popupEl).fade(1);
						$('kontakt').fade(0).destroy();
						setCloseEvents($('kontakt-sent'));
					}
				}
			});
			request.post(this);
		}
	});
}

function positionPopup(el) {
	var x = el.position({
		'relativeTo': $('wrapper'),
		'position': 'center',
		'edge': 'center',
		'offset': {
			'x': -40,
			'y': -88
		}
	});
	return el;
}