// SERVES the product (shareClass) pages

// the product tab switch function
function productTabClicked(tab_link) {
	var linkId = tab_link.id;
	var tabId = linkId.substring(0, linkId.indexOf('_link'));

	$('#area2 .nav_item').removeClass("selected");
	$(tab_link).parent().addClass('selected');

	$('#area2 .product_tab').css('display', 'none');
	$('#area2 #' + tabId).css('display', 'block');

	return false;
}

// Initialize the product-top-navigation click listeners
function initilizeProductTabListeners () {
	$('#level5 .nav_item a').each(
		function (i) {
			$(this).click(
				function () {
					return productTabClicked(this);
				});
		});

	// check for selected tab
	$('#area2 .selected a').click();

}
/*
 * RENMOVED, now based on class 'selected' of tab navigation links
 * function setSelectedTab() {
	if ( selectedTab
			&& ('facts' == selectedTab
					|| 'performance' == selectedTab
					|| 'history' == selectedTab
					|| 'portfolio' == selectedTab
					|| 'downloads' == selectedTab)) {
		$('#area2 .nav_item').removeClass("selected");
		$('#' + selectedTab + '_tab_link').click();

	} else {
		// check for selected tab
		$('#area2 .selected a').click();

	}

	//$('#area2 #' + selectedTab + '_tab').css('display', 'block');

}*/

function rewriteDownloadLinks() {

	$("a[href$=_LU0433846515_de.pdf]").each(function() {
		this.href = "https://ad3.adfarm1.adition.com/track?tid=1266&sid=3924&counturl=" + this.href;
	});
	$("a[href$=_LU0433846515_en.pdf]").each(function() {
		this.href = "https://ad3.adfarm1.adition.com/track?tid=1266&sid=3925&counturl=" + this.href;
	});
	$("a[href$=_LU0433846606_de.pdf]").each(function() {
		this.href = "https://ad3.adfarm1.adition.com/track?tid=1265&sid=3922&counturl=" + this.href;
	});
	$("a[href$=_LU0433846606_en.pdf]").each(function() {
		this.href = "https://ad3.adfarm1.adition.com/track?tid=1265&sid=3923&counturl=" + this.href;
	});

}

$(document).ready(function () {
	initilizeProductTabListeners();
	//setSelectedTab();
	rewriteDownloadLinks();
});


// append a clear element for float control - in ie the footer was jumping around because the outlines of a containers is not really defined if children are floating and it has no clear-element at the end
jQuery(function () {
	jQuery('div.product_tab, div.product_tab > div').append('<div class="clear_both"></div>');
});


// TEAM (COVER FLOW)
jQuery(function () {
	if (!jQuery('#content_team').length) return;
  var animDuration = 500;
  var container = jQuery('#content_team');
	var memberNum = container.find('.team_member').length;
	if (!memberNum) return;
	var positions = new Array();
	var els = new Array();
	// set active tab to the first one because TEAM-COVER-FLOW has to be visible for initialisation
		var firstTab = $('#content_nav_head li.first a');
		var aktSelTab = $('#content_nav_head li.selected a')
		firstTab.trigger('click');
	var processImages = function () {
		// assign names and positions to images
		  var myRight, myBottom, myWidth, myHeight;
			container.find('img.team_member').each(function () {
			  var me = jQuery(this);
			  var nameEl = container.find('span.teamMemberName:first');
			  var posEl = container.find('span.teamMemberPosition:first');
				me.data('name', nameEl.text()).data('position', posEl.text());
				nameEl.remove();
				posEl.remove();
				if (me.is(':visible')) {
					myRight = parseInt(me.css('right').replace("px", ""));
					myBottom = parseInt(me.css('bottom').replace("px", ""));
					myWidth = me.width();
					myHeight = me.height();
				}
				positions.push({
					right: myRight,
					bottom: myBottom,
					width: myWidth,
					height: myHeight,
					'z-index': me.css('z-index')
				});
				// fix for IE to prevent IE from displaying black background on animated PNGs with aplha transparency
			    if (jQuery.browser.msie) {
			      me.data('imgSrc', me.attr('src'));
						me.css({
							width: myWidth + "px",
			        height: myHeight + "px",
			        'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + me.data('imgSrc') + "',sizingMethod='scale')"
						}).attr('src', '/templates/template_bellevue/common/css/content/empty.gif');
		 			}
				if (me.is(':hidden')) me.css(positions[positions.length - 1]);
				els.push(me);
			});
		// handle image clicks
		  var handleFirstImageClick = function () {
			  var me = els.shift();
				me.unbind('click').removeClass('clickableTeamMember').animate({
					right: parseInt(me.css('right').replace("px", "")) + 80,
					bottom: parseInt(me.css('bottom').replace("px", "")) - 20,
					width: me.width() + 24,
					height: me.height() + 40,
					opacity: 0
				}, animDuration, function () {
					me.css(positions[positions.length - 1]).animate({opacity:1}, {queue:false, duration:animDuration, complete:function () {
				    if (jQuery.browser.msie && jQuery.browser.version < 8) me.css({ 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + me.data('imgSrc') + "',sizingMethod='scale')" });
					}});
					els[0].bind('click', handleFirstImageClick).addClass('clickableTeamMember');
					jQuery('#teamMemberName').text(els[0].data('name'));
					jQuery('#teamMemberPosition').text(els[0].data('position'));
				});
				for (var i=0; els[i]; i++) els[i].data('newZindex', positions[i]['z-index']).animate(positions[i], animDuration, function () {
					jQuery(this).css('z-index', jQuery(this).data('newZindex'));
				});
				els.push(me);
			};
			els[0].bind('click', handleFirstImageClick).addClass('clickableTeamMember');
	};
	// load all team images and then start processing them
	  var personImgCount = 0;
	  var personImgLoaded = function () {
			personImgCount++;
			if (personImgCount == memberNum) processImages();
		};
		container.find('img.team_member').each(function () {
		  if (this.complete) personImgLoaded();
		  else jQuery(this).bind('load', personImgLoaded);
		});
	// reset active tab to the initial selected one
		aktSelTab.trigger('click');
});
