/*
 * tchibo.js
 * Globale Skripte
 */
function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function showDownloadmanagerHelp(element, pos){	
	var offset = $(element).show().offsetParent().offset();
	// Ausgleich am Bildschirmrand
	var left = pos.pageX + $(element).width() + 32 + 16;
	var top = pos.pageY  + 16;
	var fixX = 0;
	var fixY = 0;
	if(left > $(window).width()){ fixX = $(element).width() + 32 + 16; }
	if( (top+$(element).height()) > $(window).height()){ fixY = $(element).height(); }
	$(element).css({'position': 'absolute', 'top': pos.pageY - offset.top + 16 - fixY , 'left':   pos.pageX - offset.left + 16 - fixX });
}


$(document).ready(function() {

	// JavaScript support indicator
	$('body').removeClass('no-js').addClass('js');

	// Branch-Selector
	$('.branch-select>a').click(function(event){
		event.preventDefault();
		$(this).next().show();
	});

	$('body').click(function(e){
		if(!$(e.target).hasClass('branch-select-handle')){
			$('.branch-select ul').hide();
		}
	});

	// Equalize column heights
	equalHeight($('.bottom-teaser .content'));

	// Download-Manager init
	$( "a.downloadmanager-file[href][title]" ).downloadHelper({
		directlink: "direct-download",
		addlink: "add-to-downloadmanager"
	});

	// Custom select menus
	$('.wrapper').not('.admin').find('select').selectmenu({
		positionOptions: {
			offset: "0 -1"
		}
	});

	// Hilfe-Box anzeigen
	$('#downloadmanager-help, .what-is-downloadmanager a.handle').click(function(event){
		event.preventDefault();
		showDownloadmanagerHelp('#downloadmanager-help-container', event);
		$('body').click(function(event){
			if($(event.target).attr('id') != 'downloadmanager-help' && !$(event.target).hasClass('handle')){
				$('#downloadmanager-help-container').hide();
			}
		});
	});

	// Direkte Downloads auf Dokumentenseite tracken
	$('.direct-download a').click(function(event){
		wt.sendinfo({linkId: $(this).attr('href')});
	});

});
