$(function() {
	showActiveMenu();
	calcOrderSum();
	
	$('#content').css('height', calcContentHeight());
	$(window).resize(function() {
		$('#content').css('height', calcContentHeight());
	});
	
	//Contentsize nochmals berechnen, wenn alle (ALLE!) Daten geladen wurden
	$(window).load(function(){
		$('#content').css('height', calcContentHeight());
	});
	
	//Wechsel zwischen den oberen Reitern
	$('ul.level0').children('li').children('a').mouseenter(function() {
		resetMenu();
		$(this).parent('li').children('ul').css('display', 'block');
		$(this).parent('li').css('background', '#006698');
		$(this).css('color', '#FFFFFF');
	});
	
	//Einblenden des aktiven Punktes bei Verlassen des Menubereiches
	$('ul.level0').mouseleave(function() {
		showActiveMenu();
	});
	
	//Contentbereich (div.grey): Beim letzten Absatz (p) margin:0 setzen.
	$('div.grey').find('p:last').css('margin','0');
	
	// Filter auf Terminseite onChange => Submit
	$('#examinationfilter :input').change( function() {
		$(this).parents('form:first').submit();
	});
	
	//Herverhebung im Untermenu lv1 bewegen
	$('ul.level1').children('li').children('a').mouseenter(function() {
		$('ul.level1').children('li').children('a').css('borderBottom', 'none');
		if($.browser.msie) {
			$('ul.level1').children('li').children('a').css('textDecoration', 'none');
		}
		
		$(this).css('borderBottom', '2px solid #006698');
		if($.browser.msie) {
			$(this).css('textDecoration', 'underline');
		}
	}).mouseleave(function() {
		$('ul.level1').children('li').children('a').css('borderBottom', 'none');
		if($.browser.msie) {
			$(this).css('textDecoration', 'none');
		}
	});
	
	//Hervorhebung in Tabellen steuern
	$('table.examination tbody tr').mouseenter(function() {
		$(this).addClass('hover');
	});
	
	$('table.examination tbody tr').mouseleave(function() {
		$(this).removeClass('hover');
	});
	
	$('table.examination tbody tr').click(function() {
		var href = $(this).find('a').attr('href');
		if(href.length > 0) {
			window.location=$(this).find('a').attr('href'); 
			return false;
		}
	});
	
	$('table.organisation tbody tr').mouseenter(function() {
		$(this).addClass('hover');
	});

	$('table.organisation tbody tr').mouseleave(function() {
		$(this).removeClass('hover');
	});

	$('table.organisation tbody tr').click(function() {
		window.location=$(this).find('a').attr('href'); 
		return false;
	});
	
	$('table.downloads').find('tr:has("td")').mouseenter(function() {
		$(this).addClass('hover');
	});

	$('table.downloads').find('tr:has("td")').mouseleave(function() {
		$(this).removeClass('hover');
	});
	
	$('table.downloads').find('tr:has("td")').click(function() {
		if($(this).has('a').length > 0) {
			window.location=$(this).find('a').attr('href'); return false;
		}
	});
	
	$('table.shippingcosts').find('tr:has("td")').mouseenter(function() {
		$(this).addClass('hover');
		$(this).addClass('cursordefault');
	});

	$('table.shippingcosts').find('tr:has("td")').mouseleave(function() {
		$(this).removeClass('hover');
		$(this).removeClass('cursordefault');
	});
	
	$('table.order').find('tr:has("td")').mouseenter(function() {
		$(this).addClass('hover');
		$(this).addClass('cursordefault');
	});
	
	$('table.order').find('tr:has("td")').mouseleave(function() {
		$(this).removeClass('hover');
		$(this).removeClass('cursordefault');
	});
	
	//Hervorhebung im Shop steuern
	$('div.shopitem').mouseenter(function() {
		$(this).addClass('hover');
	});

	$('div.shopitem').mouseleave(function() {
		$(this).removeClass('hover');
	});
	
	$('div.shopitem').click(function() {
		if($(this).has('a').length > 0) {
			window.location=$(this).find('a').attr('href'); return false;
		}
	});
	
	//Bestellformular
	$('select.quantity').each(function() {
		if($(this).val() == '0') {
			$(this).parent('td').parent('tr').find('span').addClass('grey');
		}
	});
	
	$('select.quantity').change(function() {
		if($(this).val() == '0') {
			$(this).parent('td').parent('tr').find('span').addClass('grey');
		}
		else {
			$(this).parent('td').parent('tr').find('span').removeClass('grey');
		}
		
		calcOrderSum();
	});
	
	$('table.contact').find('select[name="payment"]').change(function() {
		if($(this).val() == 'bankeinzug') {
			$('table.contact').find('input[name="bank_account"]').parent('td').parent('tr').removeClass('hide');
			$('table.contact').find('input[name="bank_account_holder"]').parent('td').parent('tr').removeClass('hide');
			$('table.contact').find('input[name="bank_code"]').parent('td').parent('tr').removeClass('hide');
		}
		else {
			$('table.contact').find('input[name="bank_account"]').parent('td').parent('tr').addClass('hide');
			$('table.contact').find('input[name="bank_account_holder"]').parent('td').parent('tr').addClass('hide');
			$('table.contact').find('input[name="bank_code"]').parent('td').parent('tr').addClass('hide');
		}
	});
});

function calcContentHeight() {
	$('#content').css('height', 'auto');
	
	if($.browser.msie) {
		//1: Border.
		if( $('#content').height()+1 < (window.document.body.clientHeight - $('#grey_top').height() - $('#header').height() - $('#menu').height()) ) {
			return(window.document.body.clientHeight - $('#grey_top').height() - $('#header').height() - $('#menu').height()-26);
		}
	}

	//51: Padding und Border.
	if( $('#content').height()+51 < (window.document.body.clientHeight - $('#grey_top').height() - $('#header').height() - $('#menu').height()) ) {
		return(window.document.body.clientHeight - $('#grey_top').height() - $('#header').height() - $('#menu').height()-51);
	}
	
	return($('#content').height());
}

function showActiveMenu() {
	resetMenu();
	
	//Aktives Untermenu anzeigen
	$('li.active').children('ul.level1').css('display', 'block');
	
	//Aktive Einträge hervorheben
	$('li.active').css('background', '#006698');
	$('li.active').children('a').css('color', '#FFFFFF');
	
	$('li.active').find('a.active').css('borderBottom', '2px solid #006698');
	if($.browser.msie) {
		$('li.active').find('a.active').css('textDecoration', 'underline');
	}
}

function resetMenu() {
	//Alle Untermenus ausblenden
	$('ul.level1').css('display', 'none');
	
	//Alle Hervorhebungen zurücknehmen
	$('ul.level0').children('li').css('background', '#FFFFFF');
	$('ul.level0').children('li').children('a').css('color', '#006698');
	
	$('ul.level1').children('li').children('a').css('borderBottom', 'none');
	if($.browser.msie) {
		$('ul.level1').children('li').children('a').css('textDecoration', 'none');
	}
}

function calcOrderSum() {
	var orderSum = 0;
	var quantity = 0;
	var price	 = 0;
	
	$('select.quantity').each(function() {
		if($(this).val() != '0') {
			quantity         = new Number($(this).val());
			var priceText    = $(this).parent('td').parent('tr').find('span.price').text();
			var priceTextArr = priceText.split(' ');
			price            = new Number(priceTextArr[0].replace(/,/g, '.'));
			
			orderSum += quantity*price;
		}
	});

	$('strong.ordersum').text(getFormattedPrice(orderSum));
}

function getFormattedPrice(sum) {
	sum        = Math.round(sum * 100) / 100+'';
	var sumArr = sum.split('.');
	
	if(sumArr.length == 2) {
		if(sumArr[1].length < 2) {
			sumArr[1] += '0';
		}
		return(sumArr[0]+','+sumArr[1]+' EUR');
	}
	else {
		return(sumArr[0]+',00 EUR');
	}
}
