$(document).ready(function() {

	/* Suche Toggle für das Label */

	$('#form_topsearch input.text').focus(function() {
		if(this.value == 'Suchbegriff eingeben' || this.value == 'Enter Search Term')
			this.value = '';
	});
	$('#form_topsearch input.text').blur(function() {
		if(this.value == '')
			if($('#form_topsearch').hasClass('de'))
			{
				this.value = 'Suchbegriff eingeben';
			}
			else
			{
				this.value = 'Enter Search Term';
			}
	});

	/* Hauptnavigation Einblendung der zweiten Ebene */

	$('#navigation li:has(ul)').hover(function() {
		$(this).addClass('expanded');
	}, function() {
		$(this).removeClass('expanded');
	});

	/* Schnellauswahl Dropdown */

	$('#quickselect').css('display', 'block');
	$('#quickselect > a').click(function () {
		var qs = $('#quickselect');
		if(qs.hasClass('expanded'))
			qs.removeClass('expanded');
		else
			qs.addClass('expanded');
	});
	$('body').click(function(evt) {
		var qs = $('#quickselect');
		if(qs.hasClass('expanded'))
		{
			var os = qs.offset();
			if( !( (evt.pageX > os.left && evt.pageX < (os.left+qs.width())) && (evt.pageY > os.top && evt.pageY < (os.top+qs.height()) ) ) )
			{
				qs.removeClass('expanded');
			}
		}
	});

});

/* Feature-Bereich (Startseite) */

$(document).ready(function() {
	var feature_containers = $('#feature_showcase li');
	var eventArr = new Array();
	var currImg = 0;
	var timout = null;
	var effect = false;

	var click_handler = function(elem) {
		if(effect)
			return;
		if(timeout)
			clearTimeout(timeout);
		var self_id = parseInt($(elem).text(), 10)-1;
		if(self_id == currImg)
			return;
		$('#feature_navigation li').removeClass('active');
		$(elem.parentNode).addClass('active');

		$(feature_containers[currImg]).removeClass('active');
		$(feature_containers[currImg]).fadeOut(800);
		effect = true;
		$(feature_containers[elem.parentNode.index]).fadeIn(800, function() {
			effect = false;
			timeout = setTimeout(timer, 7000);
		});
		currImg = self_id;
	};

	if(feature_containers.length > 1)
	{
		$(feature_containers).mouseover(function(){
			clearTimeout(timeout);
		});
		$(feature_containers).mouseout(function(){
			clearTimeout(timeout);
			timeout = setTimeout(timer, 7000);
		});
		/* Liste erzeugen */
		var feature_navigation = document.createElement('ul');
		feature_navigation.id = 'feature_navigation';
		$('#feature').append(feature_navigation);

		feature_containers.hide();

		for(var i = 0; i < feature_containers.length; i++)
		{
			/* Listenelemente erzeugen */
			var feature_navigation_li = document.createElement('li');
			feature_navigation_li.index = i;
			if(i == 0) $(feature_navigation_li).addClass('active');

			/* Links erzeugen */
			var feature_navigation_a = document.createElement('a');
			feature_navigation_a.style.cursor ='pointer';
			

			/* Spans in den Links erzeugen */
			var feature_navigation_span = document.createElement('span');
			feature_navigation_span.appendChild(document.createTextNode(i + 1));
			feature_navigation_a.appendChild(feature_navigation_span);


			/* Eventhandler setzen */
			var event = function() {
				click_handler(this);
			}
			$(feature_navigation_a).click(event);
			eventArr.push(feature_navigation_a);

			feature_navigation_li.appendChild(feature_navigation_a);
			feature_navigation.appendChild(feature_navigation_li);

			/* Bildwerte */
			feature_containers[i].index = i;
			if(i == 0) $(feature_containers[i]).show();
			
		}
		var timer = function() {
			timeout = null;
			next = currImg +1;
			if(next == feature_containers.length)
				next = 0;	
			click_handler(eventArr[next]);
		}
		timeout = setTimeout(timer, 7000);
	}
});

/* Touch support */

$(document).ready(function() {
	var is_touch_device = ('undefined' != typeof(TouchEvent));
	if (is_touch_device) {
		$('ul#navigation > li:has(ul) > a').click(function(){
			if($(this.parentNode).hasClass('expanded'))
				return true;
			$('ul#navigation > li:has(ul)').removeClass('expanded');
			$(this.parentNode).addClass('expanded');
			return false;
		});
	}
});

/* Popup-Funktion (wird für die "Hilfe zum Tagesplaner" genutzt) */

function openPopup(url, width, height)
{
	width = parseInt(width);
	height = parseInt(height);
	if (width < 50) width = 550;
	if (height < 50) height = 330;
	
	var x = Math.floor(document.body.clientWidth / 2) - Math.floor(width /2);
	var y = Math.floor(document.body.clientHeight / 2) - Math.floor(height /2);
	
	var desc = 'top=' + String(y) + ',left=' + String(x) + ',height=' + String(height) + ',width=' + String(width) + ',location=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,status=no,dependent=yes';
	var wind = window.open(url, '_popup', desc);
	wind.focus();
}

/* keine Ahnung, ob und wofür diese Funktion genutzt wird */

function confirmclick(msg)
{
	if(confirm(msg))
	{
		return true;
	}
	else
	{
		return false;
	}
}

