// Sifr


$(document).ready(function() {
	var defaultSearchKeyword = $('#tx-indexedsearch-searchbox-sword').val();

	$('#tx-indexedsearch-searchbox-sword').focus(function() {
		if ($(this).val() == defaultSearchKeyword) $(this).val('');
	});

	$('#tx-indexedsearch-searchbox-sword').blur(function() {
		if ($(this).val() == '') $(this).val(defaultSearchKeyword);
	});

	$('.teaserSlider').each(function() {
		var itemsCount = $(this).find('div.items div').length;
		$(this).find('.totalElementsNo').text('von ' + itemsCount);
		if (itemsCount == 1) {
			$(this).find('.slide-btn').remove()
		}
	});

	if ($('#playerdetail .slideshow .items div').length > 0) {
		$('.slideshow').show();
		$('#playerdetail').css({'height': '574px'});
	}

	$('.panes .news-container').each(function() {
  		if ($(this).find('.news').length > 3) {
  			hideMoreNews($(this).index());
  		}
	});

	$('.video-btn .preview').each(function() {
		if ($(this).find('div a').length == 0) {
			$(this).remove();
		}
	});

	// jQuery.euqalheights
	(function($) {
		$.fn.equalHeights = function(minHeight, maxHeight) {
			tallest = (minHeight) ? minHeight : 0;
			this.each(function() {
				if($(this).height() > tallest) {
					tallest = $(this).height();
				}
			});
			if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
			return this.each(function() {
				$(this).height(tallest).css("overflow","hidden");
			});
		}
	})(jQuery);
	$(".equalHeights .box-content").equalHeights();
	$(".min-sitemap ul").equalHeights();

	// jQueryTools Tabs
	$(".box ul.tabs").tabs("div.panes > div");
	$(".tabbox ul.tabs").tabs("div.panes > div");

	// jQueryTools Gallery
	$('.teaserSlider .scrollable').scrollable({
		onBeforeSeek: function(event, index) {
			if (event.target.getRoot().find('div.items div:eq(' + index + ')').length > 0) {
				event.target.getRoot().parent().find('.currentElementNo').text(++index);
			}
		}
	});
	$('.slideshow-content .scrollable').scrollable({itemsPerPage: 3});
	$('#playerdetail .scrollable').scrollable({itemsPerPage: 5});
	$('.slideshow-layer .scrollable, .nextgame, .slideshow-highlights, .start-gallery').scrollable();

	$('.scrollable .items > a').remove();

	// stage
	$(".start-gallery .items a").click(function() {
		var aTag = $(this);
		currentStageItemIndex = aTag.index()+(4*(aTag.parent().index()));
		setStageItem(aTag);

		// when page loads simulate a "click" on the first image
		return false;
	}).filter(":first").click();

	if ($('.start-gallery').length > 0) {
		stageItemsCount = $('.start-gallery .items a').length;

		$('.start-gallery').hover(function() {
			animateStage('stop');
		},
		function () {
			animateStage('start');
		});

		animateStage('start');
	}

	// Main Sub Navigation
	$('#subnav .sub').each(function() {
		if ($(this).find('ul').length == 0) {
			$(this).remove();
		}
		else {
			var parts = $(this).attr('id').split('-');
			var prefix = '';
			if ($('#nav ul').hasClass('business')) prefix = 'business_';
			else if ($('#nav ul').hasClass('english')) prefix = 'en_';
			$(this).find('.head-img img').attr('src', $(this).find('.head-img img').attr('src').replace(/no1/, prefix + parts[1]));
		}
	});

	var subnavHeight = '184px';
	var active = hidingTriggered = false;
	var activeSubnavElement = null;
	$('.sub').css({'margin-top': '-' + subnavHeight});
	$('#nav ul > li a').hover(function() {
		hidingTriggered = false;
		var currentElement = $('#sub-' + $(this).attr('class'));

		if (currentElement.length == 0) {
			triggerSubNavigationHiding();
		}
		else {
			if (!active) {
				active = true;
				$('.sub').css({'margin-top': '-' + subnavHeight});
				if ($.support.opacity) {
					$('#subnav, #subnav-shadow').stop(false, true).fadeIn('fast');
				}
				else {
					$('#subnav, #subnav-shadow').show();
				}
			}

			if (activeSubnavElement !== null) {
				$('.sub').stop(true, true);
				if (activeSubnavElement.attr('id') !== currentElement.attr('id')) {
					activeSubnavElement.animate({'margin-top': subnavHeight}, 400);
					currentElement.stop(false, true).css({'margin-top': '-' + subnavHeight}).animate({'margin-top': '0'}, 400);
				}
			}
			else {
				currentElement.css({'margin-top': '0'});
			}

			activeSubnavElement = currentElement;
			$('#nav li').removeClass('hover');
			$(this).parent().addClass('hover');
		}
	},
	function() {
		triggerSubNavigationHiding();
	});

	$('.sub').hover(function() {
		active = true;
		hidingTriggered = false;
	},
	function() {
		triggerSubNavigationHiding();
	});

	function triggerSubNavigationHiding() {
		hidingTriggered = true;
		window.setTimeout(function() {
			if (hidingTriggered) {
				if ($.support.opacity) {
					$('#subnav, #subnav-shadow').stop(false, true).fadeOut('fast');
				}
				else {
					$('#subnav, #subnav-shadow').hide();
				}

				if (activeSubnavElement !== null) {
					activeSubnavElement.css({'margin-top': '-' + subnavHeight});
					activeSubnavElement = null;
					$('#nav li').removeClass('hover');
				}

				active = false;
			}
		}, 200);
	}

	// IE6 Focus Outline
	$('.tabs li a').click(function() {
		$(this).blur();
	});

	gamesSliderAllGames = $('.nextgame .items').html();

	if ($('.nextgame .items .previousMatches').length == 0) {
		$('#nextGamesHeadline label').remove();
	}
	else {
		$('.nextgame .items div.previousMatches').remove();
	}

	if ('undefined' != typeof shareURL) {
		$.ajax({
			type: 'GET',
			url: '/CDA/fileadmin/php/ajax.shareCounts.php',
			data: 'url=' + shareURL,
			dataType: 'json',
			success: function(data) {
				$('.facebookShareCount').html(data.facebookShareCount);
				$('.tweetCount').html(data.tweetCount);
				$('.googlePlusCount').html(data.googlePlusCount);
			}
		});
	}
});

var stageAnimation;
var stageItemsCount = 0;
var currentStageItemIndex = -1;
var gamesSliderAllGames = '';
function showGames(mode) {
	$('#nextGamesHeadline, #previousGamesHeadline').hide();
	$('#' + mode + 'GamesHeadline').show();
	$('.nextgame, .nextgame-btn').remove();
	$('#previousGamesHeadline').after('<div class="scrollable nextgame"><div class="items">' + gamesSliderAllGames + '</div></div>');

	if (mode == 'previous') $('.nextgame .items div.nextMatches').remove();
	else $('.nextgame .items div.previousMatches').remove();

	if ($('.nextgame .items div.' + mode + 'Matches').length > 1) {
		$('.nextgame').after('<div class="nextgame-btn"><a class="prev browse left"><span>zurück</span></a><a class="next browse right"><span>weiter</span></a></div>');
	}

	$('.nextgame').scrollable();
}

function setStageItem(clickedATag) {
	// see if same thumb is being clicked
	if (clickedATag.hasClass("active")) { return; }
	// calclulate large image's URL based on the thumbnail URL (flickr specific)
	var imageURL = clickedATag.attr("rel");
	var url = clickedATag.attr('href');
	// get handle to element that wraps the image and make it semi-transparent
	var wrap = $("#image_wrap").fadeTo(0, 1);
	// the large image from www.flickr.com
	var image = new Image();
	// call this function after it's loaded
	image.onload = function() {
		// change the image
		var stageATag = wrap.find('a');
		var currentImage = wrap.find("img");
		var newImage = currentImage.clone();
		newImage.attr("src", imageURL);
		newImage.css({ display: 'none' });

		stageATag.append(newImage);

		newImage.fadeIn();
		currentImage.fadeOut('normal', function() {
			$(this).remove();
		});

		stageATag.attr('href', url);
		// activate item
		$(".items a").removeClass("active");
		clickedATag.addClass("active");
	};
	// begin loading the image from www.flickr.com
	image.src = imageURL;
}

function animateStage(action) {
	if (action == 'start') {
		stageAnimation = window.setTimeout(function() {
			currentStageItemIndex++;
			if (currentStageItemIndex >= stageItemsCount) currentStageItemIndex = 0;

			$('.start-gallery .items a:eq(' + currentStageItemIndex + ')').click();
			if ('function' == typeof $('.start-gallery .scrollable').scrollable().seekTo) {
				$('.start-gallery .scrollable').scrollable().seekTo(currentStageItemIndex/4);
			}

			animateStage('start');
		}, 8000);
	}
	else {
		window.clearTimeout(stageAnimation);
	}
}

function showMoreNews(newsContainerIndex) {
	$('.panes .news-container:eq(' + newsContainerIndex + ') div').slideDown();
	$('.panes .news-container:eq(' + newsContainerIndex + ') .moreNews').remove();

	$('.panes .news-container:eq(' + newsContainerIndex + ') .box-link:last').append('<a class="right lessNews" href="javascript:hideMoreNews(' + newsContainerIndex + ')">weniger News anzeigen</a>');
}

function hideMoreNews(newsContainerIndex) {
	$('.panes .news-container:eq(' + newsContainerIndex + ') .news:gt(2), .news-container:eq(' + newsContainerIndex + ') .box-link').slideUp();
  	$('.panes .news-container:eq(' + newsContainerIndex + ') .news:eq(2)').after('<div class="box-link ic-down clearfix moreNews"><a class="right" href="javascript:showMoreNews(' + newsContainerIndex + ')">weitere News anzeigen</a></div>');
	$('.panes .news-container:eq(' + newsContainerIndex + ') .lessNews').remove();
}

function popup(url, b, h) {
	var maxwidth = screen.width-50;
	var maxheight = screen.height-120;
	var scrolling = "no";
	var resizable = "no";
	h += 0;
	if (b > maxwidth) b = maxwidth;

	if (h > maxheight) {
		h = maxheight;
		b += 20;
	}

	if (navigator.appVersion.search(/AOL/) != -1) {
		scrolling = 'yes';
		b = maxwidth;
		h = maxheight;
	}

	var param = "resizable=" + scrolling + ",status=no,menubar=no,scrollbars=" + scrolling + ",toolbar=no,width=" + b + ",height=" + h;
	f = window.open(url,'',param);
	f.moveTo((screen.width-b)/2,(screen.height-h)/2-40);
}


