// Register Window-Events
window.onload = function() {
	fixBaseWidth();
};
window.onresize = function() {
	fixBaseWidth();
};
document.observe('dom:loaded', function(){ fixBaseWidth(); });

// Manipulate Layout on Window Load and Resize
var docIsDynCentered = false;
function fixBaseWidth() {
	var docWidth = document.viewport.getWidth();
	if (docWidth-1140<0) {
		if(docIsDynCentered==true) { $('basecontainer').removeClassName('isCentered'); }
		docIsDynCentered = false;
	} else {
		if(docIsDynCentered==false) { $('basecontainer').addClassName('isCentered'); }
		docIsDynCentered = true;
	}
}

// Observer for ContactForm at Bottom of Page
function addContactFormObserver() {
	Event.observe('contactform', 'submit', function(event) {
		if ($('contactform_email') && !doEmailValidation($('contactform_email').value)) {
			event.stop();
		}
		//event.stop();
	});
	Event.observe('contactform_name', 'focus', function(event) {
		if ($('contactform_name').value == 'NAME') { $('contactform_name').clear(); }
	});
	Event.observe('contactform_name', 'blur', function(event) {
		if ($('contactform_name').value == '') { $('contactform_name').value = 'NAME'; }
	});
	Event.observe('contactform_email', 'focus', function(event) {
		if ($('contactform_email').value == 'EMAIL') { $('contactform_email').clear(); }
	});
	Event.observe('contactform_email', 'blur', function(event) {
		if ($('contactform_email').value == '') { $('contactform_email').value = 'EMAIL'; }
	});
	Event.observe('contactform_subject', 'focus', function(event) {
		if ($('contactform_subject').value == 'BETREFF') { $('contactform_subject').clear(); }
	});
	Event.observe('contactform_subject', 'blur', function(event) {
		if ($('contactform_subject').value == '') { $('contactform_subject').value = 'BETREFF'; }
	});
	Event.observe('contactform_message', 'focus', function(event) {
		if ($('contactform_message').value == 'DEINE NACHRICHT') { $('contactform_message').clear(); }
	});
	Event.observe('contactform_message', 'blur', function(event) {
		if ($('contactform_message').value == '') { $('contactform_message').value = 'DEINE NACHRICHT'; }
	});
}
document.observe('dom:loaded', function(){ addContactFormObserver(); });

// Validate an email
function doEmailValidation(email) {
	if (!(/^.+@.+\..+/.exec(email))) {
		return false;
	}
	return true;
}

// Home: Teaser behaviour
var teaserbox_cur_selectedID = null;
var teaserbox_cur_effect = false;
var teaserbox_cur_autoCntInit = 6;
var teaserbox_cur_autoCnt = teaserbox_cur_autoCntInit;
var teaserbox_cur_auto = true;
function initHomeTeaser() {
	objHomeTeaser = $('home_teaser');
	if (!objHomeTeaser) { return; }

	Event.observe(objHomeTeaser, 'mouseover', function(event) {
		teaserbox_cur_auto = false;
		teaserbox_cur_autoCnt = teaserbox_cur_autoCntInit;
	});
	Event.observe(objHomeTeaser, 'mouseout', function(event) {
		teaserbox_cur_auto = true;
	});

	objPlane = objHomeTeaser.down();
	objButtons = objPlane.next();
	
	aButton = objButtons.down();
	Event.observe(aButton.down(), 'click', function(event) { homeTeaserSwapByClick(event); });
	Event.observe(aButton.next().down(), 'click', function(event) { homeTeaserSwapByClick(event); });
	Event.observe(aButton.next().next().down(), 'click', function(event) { homeTeaserSwapByClick(event); });
	
	teaserbox_cur_selectedID = aButton.down();
	
	(function() {
		homeTeaserSwapByTimer();
	}).delay(1.000);
}
document.observe("dom:loaded", function() { initHomeTeaser(); });
function homeTeaserSwap(newElement) {
	objHomeTeaser = $('home_teaser');
	if (!objHomeTeaser) { return; }
	if (teaserbox_cur_selectedID==newElement) { return; }
	
	teaserbox_cur_selectedID.removeClassName('active');
	newElement.addClassName('active');
	
	if (teaserbox_cur_effect) {
		teaserbox_cur_effect.cancel();
	}
	
	xOffset = (newElement.rel - 1) * 770;
	
	objPlane = objHomeTeaser.down();
	teaserbox_cur_effect = new Effect.Move(objPlane, { x: -xOffset, y: 0, mode: 'absolute' });
	
	teaserbox_cur_selectedID = newElement;
}
function homeTeaserSwapByClick(event) {
	event.stop();
	aElement = event.element();
	if (teaserbox_cur_selectedID==aElement) { return; }
	homeTeaserSwap(aElement);
}
function homeTeaserSwapByTimer() {
	objHomeTeaser = $('home_teaser');
	if (!objHomeTeaser) { return; }

	if(teaserbox_cur_auto)
	{
		if(teaserbox_cur_autoCnt<=0)
		{
			objPlane = objHomeTeaser.down();
			objButtons = objPlane.next();

			if(teaserbox_cur_selectedID.rel<3)
			{
				nextElement = teaserbox_cur_selectedID.up().next().down();
			}
			else
			{
				nextElement = teaserbox_cur_selectedID.up().previous().previous().down();
			}
			homeTeaserSwap(nextElement);
			teaserbox_cur_autoCnt = teaserbox_cur_autoCntInit;
		}
		else
		{
			teaserbox_cur_autoCnt--;
		}
	}
	
	(function() {
		homeTeaserSwapByTimer();
	}).delay(1.000);
}

// Aboutus: Teaser behaviour
var aboutus_tb_cur_selectedID = null;
var aboutus_tb_cur_effect = false;
var aboutus_tb_cur_autoCntInit = 6;
var aboutus_tb_cur_autoCnt = aboutus_tb_cur_autoCntInit;
var aboutus_tb_cur_auto = true;
var aboutus_tb_elems_cnt = 0;
function initAboutusTeaser() {
	objAboutusTeaser = $('aboutus_teaser');
	if (!objAboutusTeaser) { return; }

	Event.observe(objAboutusTeaser, 'mouseover', function(event) {
		aboutus_tb_cur_auto = false;
		aboutus_tb_cur_autoCnt = aboutus_tb_cur_autoCntInit;
	});
	Event.observe(objAboutusTeaser, 'mouseout', function(event) {
		aboutus_tb_cur_auto = true;
	});
	
	objPlane = objAboutusTeaser.down();
	objButtons = objPlane.next();

	aboutus_tb_elems_cnt = objButtons.select('div[class=item]').length;
	
	aButton = objButtons.down();
	Event.observe(aButton.down(), 'click', function(event) { aboutusTeaserSwapByClick(event); });
	for (i=1; i<aboutus_tb_elems_cnt; i++) {
		Event.observe(aButton.next(i-1).down(), 'click', function(event) { aboutusTeaserSwapByClick(event); });
	}
	
	aboutus_tb_cur_selectedID = aButton.down();
	
	(function() {
		aboutusTeaserSwapByTimer();
	}).delay(1.000);
}
document.observe("dom:loaded", function() { initAboutusTeaser(); });
function aboutusTeaserSwap(newElement) {
	objAboutusTeaser = $('aboutus_teaser');
	if (!objAboutusTeaser) { return; }
	if (aboutus_tb_cur_selectedID==newElement) { return; }
	
	aboutus_tb_cur_selectedID.removeClassName('active');
	newElement.addClassName('active');
	
	if (aboutus_tb_cur_effect) {
		aboutus_tb_cur_effect.cancel();
	}
	
	xOffset = (newElement.rel - 1) * 770;
	
	objPlane = objAboutusTeaser.down();
	aboutus_tb_cur_effect = new Effect.Move(objPlane, { x: -xOffset, y: 0, mode: 'absolute' });
	
	aboutus_tb_cur_selectedID = newElement;
}
function aboutusTeaserSwapByClick(event) {
	event.stop();
	aElement = event.element();
	if (aboutus_tb_cur_selectedID==aElement) { return; }
	aboutusTeaserSwap(aElement);
}
function aboutusTeaserSwapByTimer() {
	objAboutusTeaser = $('aboutus_teaser');
	if (!objAboutusTeaser) { return; }

	if(aboutus_tb_cur_auto)
	{
		if(aboutus_tb_cur_autoCnt<=0)
		{
			objPlane = objAboutusTeaser.down();
			objButtons = objPlane.next();

			if(aboutus_tb_cur_selectedID.rel<aboutus_tb_elems_cnt)
			{
				nextElement = aboutus_tb_cur_selectedID.up().next().down();
			}
			else
			{
				nextElement = aboutus_tb_cur_selectedID.up().previous(aboutus_tb_elems_cnt-2).down();
			}
			aboutusTeaserSwap(nextElement);
			aboutus_tb_cur_autoCnt = aboutus_tb_cur_autoCntInit;
		}
		else
		{
			aboutus_tb_cur_autoCnt--;
		}
	}
	
	(function() {
		aboutusTeaserSwapByTimer();
	}).delay(1.000);
}

// Social Overlays: Send
function topicSendInitOverlay() {
	var objBody = document.getElementsByTagName("body").item(0);

	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlayTopicSend');
	objOverlay.style.display = 'none';
	objBody.appendChild(objOverlay);
	$('overlayTopicSend').hide().observe('click', function(){ topicSendHideShortcut(); });
	
	var objTopicSendBox = document.createElement("div");
	objTopicSendBox.setAttribute('id','topicSendBox');
	objTopicSendBox.style.display = 'none';
	objBody.appendChild(objTopicSendBox);

	var objTopicSendLabel = document.createElement("div");
	objTopicSendLabel.setAttribute('class','cheader');
	objTopicSendLabel.innerHTML = "Weiterempfehlen"
	objTopicSendBox.appendChild(objTopicSendLabel);

	var objTopicSendTextfield = document.createElement("div");
	objTopicSendTextfield.setAttribute('id','topicSendBoxContent');
	objTopicSendTextfield.innerHTML = '<form id="socials_send_form" action="" method="post" accept-charset="utf-8"><p><input type="hidden" name="r" value="blog" /><input id="topicSendBoxTopicID" type="hidden" name="t" value="" /><input type="hidden" name="op" value="send" /><label for="send_my_name">Ihr Name</label><input type="text" name="send_my_name" value="" id="send_my_name" /><br /><label for="send_my_email">Ihre E-Mail-Adresse</label><input type="text" name="send_my_email" value="" id="send_my_email" /><br /><label for="send_other_name">Name des Empfängers</label><input type="text" name="send_other_name" value="" id="send_other_name" /><br /><label for="send_other_email">E-Mail Adresse des Empfängers</label><input type="text" name="send_other_email" value="" id="send_other_email" /><br /><input class="submit_overlay" type="submit" value="Empfehlen!"><br /><br /></p></form>';
	objTopicSendBox.appendChild(objTopicSendTextfield);

	var objTopicSendCloseBtn = document.createElement("div");
	objTopicSendCloseBtn.setAttribute('id','topicSendBoxClose');
	objTopicSendCloseBtn.innerHTML = "Schlie&szlig;en"
	objTopicSendBox.appendChild(objTopicSendCloseBtn);
	
	$('topicSendBoxClose').observe('click', function(){ topicSendHideShortcut(); });
	$('topicSendBox').hide();

    document.observe('click', (function(event){
        var target = event.findElement('a[rel^=showSocialsTopicSendBox]');
        if (target) {
            event.stop();
			if (!target.hasClassName('disabled')) {
				topicSendShowShortcut(target.href);
			}
        }
    }));
}
function topicSendShowShortcut(url) {
	// Data update
	if (url.length) {
		url = url.substring(url.indexOf('&t=') + 3);
		url = url.substr(0, url.indexOf('&'));
		strnum = parseInt(url);
		if (strnum>0) {
			$('topicSendBoxTopicID').value = strnum;
		} else {
			return;
		}
	}
	// Show Overlay
	objOverlay = $('overlayTopicSend');
	objOverlay.setStyle({ width: '100%', height: '100%' });
	new Effect.Appear(objOverlay, { duration: 0.5, from: 0.0, to: 0.5 });
	// Show Box
	objBox = $('topicSendBox');
	new Effect.Appear(objBox, { duration: 0.5, from: 0.0, to: 1.0, delay: 0.25 });
}
function topicSendHideShortcut() {
	$('topicSendBox').hide();
	$('overlayTopicSend').hide();
}
document.observe('dom:loaded', function(){ topicSendInitOverlay(); });

// Social Overlays: Twitter
function twitterInitOverlay() {
	var objBody = document.getElementsByTagName("body").item(0);

	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlayTwitter');
	objOverlay.style.display = 'none';
	objBody.appendChild(objOverlay);
	$('overlayTwitter').hide().observe('click', function(){ twitterHideShortcut(); });
	
	var objTwitterBox = document.createElement("div");
	objTwitterBox.setAttribute('id','twitterBox');
	objTwitterBox.style.display = 'none';
	objBody.appendChild(objTwitterBox);

	var objTwitterLabel = document.createElement("div");
	objTwitterLabel.setAttribute('class','cheader');
	objTwitterLabel.innerHTML = "Shortcut"
	objTwitterBox.appendChild(objTwitterLabel);

	var objTwitterTextfield = document.createElement("div");
	objTwitterTextfield.setAttribute('id','twitterBoxInput');
	objTwitterTextfield.innerHTML = "..."
	objTwitterBox.appendChild(objTwitterTextfield);

	var objTwitterCloseBtn = document.createElement("div");
	objTwitterCloseBtn.setAttribute('id','twitterBoxClose');
	objTwitterCloseBtn.innerHTML = "Schlie&szlig;en"
	objTwitterBox.appendChild(objTwitterCloseBtn);
	
	$('twitterBoxClose').observe('click', function(){ twitterHideShortcut(); });
	$('twitterBox').hide();

    document.observe('click', (function(event){
        var target = event.findElement('a[rel^=showSocialsTwitterBox]');
        if (target) {
            event.stop();
			if (!target.hasClassName('disabled')) {
				twitterShowShortcut(target.href);
			}
        }
    }));
}
function twitterShowShortcut(url) {
	// Data update
	$('twitterBoxInput').innerHTML = url;
	// Show Overlay
	objOverlay = $('overlayTwitter');
	objOverlay.setStyle({ width: '100%', height: '100%' });
	new Effect.Appear(objOverlay, { duration: 0.5, from: 0.0, to: 0.5 });
	// Show Box
	objBox = $('twitterBox');
	new Effect.Appear(objBox, { duration: 0.5, from: 0.0, to: 1.0, delay: 0.25 });
}
function twitterHideShortcut() {
	$('twitterBox').hide();
	$('overlayTwitter').hide();
}
document.observe('dom:loaded', function(){ twitterInitOverlay(); });

// Social Overlays: Facebook
function facebookInitOverlay() {
	var objBody = document.getElementsByTagName("body").item(0);

	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlayFacebook');
	objOverlay.style.display = 'none';
	objBody.appendChild(objOverlay);
	$('overlayFacebook').hide().observe('click', function(){ facebookHideShare(); });
	
	var objFacebookBox = document.createElement("div");
	objFacebookBox.setAttribute('id','facebookBox');
	objFacebookBox.style.display = 'none';
	objBody.appendChild(objFacebookBox);

	var objFacebookFrame = document.createElement("iframe");
	objFacebookFrame.setAttribute('id','facebookBoxFrame');
	objFacebookFrame.setAttribute('scrolling','off');
	objFacebookFrame.setAttribute('src','');
	objFacebookBox.appendChild(objFacebookFrame);

	var objFacebookCloseBtn = document.createElement("div");
	objFacebookCloseBtn.setAttribute('id','facebookBoxClose');
	objFacebookCloseBtn.innerHTML = "Schlie&szlig;en"
	objFacebookBox.appendChild(objFacebookCloseBtn);
	
	$('facebookBoxClose').observe('click', function(){ facebookHideShare(); });
	$('facebookBox').hide();

    document.observe('click', (function(event){
        var target = event.findElement('a[rel^=showSocialsFacebookBox]');
        if (target) {
            event.stop();
			if (!target.hasClassName('disabled')) {
				facebookShowShare(target.href);
			}
        }
    }));
}
function facebookShowShare(url) {
	// Data update
	$('facebookBoxFrame').src = url;
	// Show Overlay
	objOverlay = $('overlayFacebook');
	objOverlay.setStyle({ width: '100%', height: '100%' });
	new Effect.Appear(objOverlay, { duration: 0.5, from: 0.0, to: 0.5 });
	// Show Box
	objBox = $('facebookBox');
	new Effect.Appear(objBox, { duration: 0.5, from: 0.0, to: 1.0, delay: 0.25 });
}
function facebookHideShare() {
	$('facebookBox').hide();
	$('overlayFacebook').hide();
}
document.observe('dom:loaded', function(){ facebookInitOverlay(); });

// Blog: Timeline Expand
function blogTimelineInit() {
	objTimeline = $('blog_timeline');
	if (objTimeline) {
		objMonthList = objTimeline.getElementsBySelector('ul[class^=months]');
		var num = 0;
		while(objMonthList[num]) {
			if (!objMonthList[num].hasClassName('expanded')) {
				objMonthList[num].hide().addClassName('expanded');
			}
			num++;
		}
	    document.observe('click', (function(event){
	        var target = event.findElement('a[rel^=blog_timeline_year]');
	        if (target) {
	            event.stop();
				blogTimelineExpand(target);
	        }
	    }));
	}
}
function blogTimelineExpand(elem) {
	objElem = elem.up().next();
	if (!objElem.hasClassName('isExpanded')) {
		new Effect.BlindDown(objElem, { duration: 0.5 });
		objElem.addClassName('isExpanded')
	} else {
		new Effect.BlindUp(objElem, { duration: 0.5 });
		objElem.removeClassName('isExpanded')
	}
}
document.observe('dom:loaded', function(){ blogTimelineInit(); });

// Blog: Searchfield
function addBlogSearchfieldObserver() {
	Event.observe('blog_searchfield', 'focus', function(event) {
		if ($('blog_searchfield').value == 'SUCHEN...') { $('blog_searchfield').clear(); }
	});
	Event.observe('blog_searchfield', 'blur', function(event) {
		if ($('blog_searchfield').value == '') { $('blog_searchfield').value = 'SUCHEN...'; }
	});
}
document.observe('dom:loaded', function(){ addBlogSearchfieldObserver(); });

/* FIX: XHTML Strict don't like href-target-attribute... */
function makeExternalLinks() {
	var anchors = $$('a[rel=externalLink]');  
	for (var i=0; i<anchors.length; i++) {  
      		anchors[i].target = "_blank";
	}
}
document.observe('dom:loaded', function () { makeExternalLinks(); });
