﻿$(document).ready(function () {
	$(".accordion").accordion({ autoHeight: false, collapsible: true, alwaysOpen: false, active: false, animated: 'fold' });
	checkAndCorrectTeaserLength();
	makeCarousel('#newsSelectorContainer', '#newsSelectorArticlesList ul a', 'selectedNewsItem', 'selected');
	startSwapTimer('#newsSelectorArticlesList ul a');

	if (window.FlashDetect && FlashDetect.versionAtLeast(9)) {
		// change default flash player
		$.fn.media.defaults.flvPlayer = '/Videos/NestlePlayer.swf';

		// remove the current children
		$('.FlashPlayer').text('');

		//	mp3Player: '/Videos/NestleAudioPlayer.swf'
		$('.FlashPlayer').media();
	}
	else {
		$('.NoFlashImage').each(function () {
			var data = $(this).metadata();
			if (data.src) {
				$(this).attr('src', data.src);
				$(this).show();
			}
		});
	}

	// setup tracking of print link
	$('a.iconPDFBig').click(function () {
		_tag.dcsMultiTrack('WT.tx_e', 'pr', 'WT.dl', '410');
	});

	// setup Teaser trackings
	$('a.TrackingLink').click(clickTrackingLink);
});

var allLinks;
var currentTimer;
var currentLinkIndex = 0;

function startSwapTimer(links) {
	allLinks = $(links);
	if (allLinks.length > 0) {
		currentTimer = window.setInterval(swapTimer, 5000);
	}
}

function swapTimer() {
	currentLinkIndex++;
	if (currentLinkIndex >= allLinks.length) {
		currentLinkIndex = 0;
	}
	$(allLinks[currentLinkIndex]).trigger("click");
	currentTimer = window.setInterval(swapTimer, 5000);
}

function makeCarousel(container, links, activeClass, linkActiveClass) {
    //setup
    $(container).scrollTop(0);
    //attach click events
    $(links).click(function (e) {
    	window.clearInterval(currentTimer);
    	var id = $(this).attr("storyKey");
    	if (!id) return false;

    	// load old visible item
    	var old = $(container).find('.' + activeClass);
    	// Switch item only if it's not already displayed.
    	if (old.attr("id") != ("newsItem" + id)) {
    		//set active class, or default to fade
    		if (linkActiveClass)
    			$(this).parent().addClass(linkActiveClass).siblings().removeClass(linkActiveClass);
    		else
    			$(this).parent().fadeTo('fast', .3).siblings().fadeTo('fast', 1);

    		//set the new front item
    		var newItem = $('#newsItem' + id);
    		newItem.siblings().not('.' + activeClass).hide();

    		// fade the old item out
    		old.fadeOut(function () {
    			old.removeClass(activeClass);
    		});

    		//fade the new item in
    		newItem.css({ zIndex: 10 }).addClass(activeClass).fadeIn(function () {
    			newItem.css({ zIndex: 0 });
    		});
    	}

    	return false;
    });
}

var defaultSecurityText = "Wir möchten verhindern, dass unser Angebot für das massenhafte Versenden von E-Mails missbraucht wird. Mit der Sicherheitsabfrage beugen wir möglichen Spammern vor - eine Vorsichtsmaßnahme, die leider notwendig ist.";

function SecurityPopup(text, clickEventArgs) {
	$("#security .securityText").html(text);
	var container = $("#security");
	container.css("left", clickEventArgs.pageX - container.width() / 2)
				.css("top", clickEventArgs.pageY - container.height() / 2)
				.show("slow");
	$("#security:visible .close").click(function () {
		container.hide("slow");
		return false;
	});
}

function checkAndCorrectTeaserLength() {
	var allTeaser = $(".LandingPageTeaser > div");
	for (var x = 0; x < allTeaser.length; x++) {
		for (var y = 0; y < allTeaser.length; y++) {
			if (x != y) {
				if (allTeaser[x].offsetTop == allTeaser[y].offsetTop
					&& allTeaser[x].offsetHeight != allTeaser[y].offsetHeight) {
					var newHeight = $(allTeaser[x]).find(".teasercontent").height();
					if (newHeight < $(allTeaser[y]).find(".teasercontent").height()) {
						newHeight = $(allTeaser[y]).find(".teasercontent").height();
					}
					$(allTeaser[x]).find(".teasercontent").height(newHeight);
					$(allTeaser[y]).find(".teasercontent").height(newHeight);
				}
			}
		}
	}
}

function openPopupUnternehmensgruppe() {
	popupUnternehmensgruppe = window.open("/SiteCollectionDocuments/unternehmensgruppe.html", "unternehmensgruppePopup", "width=365,height=390,resizable=no,scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no");
	popupUnternehmensgruppe.focus();
}

function clickTrackingLink() {
	var data = $(this).metadata();
	if (data.target) {
		_tag.dcsMultiTrack('WT.ac', 'Teaser - ' + $(document).attr('title') + ' - ' + data.target, 'WT.dl', '500', 'DCS.dcsuri', '');
	}
}

function trackVideoPlayer(targetData) {
	_tag.dcsMultiTrack('WT.clip_n', targetData, 'WT.clip_ev', 'v', 'WT.dl', '430', 'DCS.dcsuri', '');
}
