/**********************************************/
/**											 **/
/**	This file contains functions for the     **/
/** new design                               **/
/**											 **/
/** TypoConsult 02-2010                      **/
/**********************************************/


/* When ready */
jQuery(function($) {
	adjustBreadcrumbHeight();
	/* Splits the ul-list in the bottom into 4 ul-lists*/
	placeMenuTriangle();
	splitTopMenuList();
	splitBottomMenuList();
	
	original_page_height = $("#page").height();
	adjustBottom(original_page_height);
	
	$(window).resize(function(){
		adjustBottom(original_page_height);
	});
	
	topMenuSlideToggle();
	
	function adjustBreadcrumbHeight() {
		if($("#breadcrumb").is(":empty")) {
			$("#breadcrumb").height(0);
			$("#header").height("auto");
		}
	}
	
	function topMenuSlideToggle () {
		
		$("#servicemenu > span > ul > li.topmenuitem-1st-level-ifsub").each(function(i){
			$(this).children("a").click(function(){
				// Close allready opened menues
				$(this).parent().siblings()
					.children("ul").slideUp("slow").end()
					.children("a").removeClass("menuOpen");

				//console.log($(this).parent().children("ul").is(':hidden'));
				if ($(this).parent().children("ul").is(':hidden')) {
					$(this).addClass("menuOpen");
				}else {
					$(this).removeClass("menuOpen");
				}
				// open the selected one
				offset =  $(this).offset();
				obj = $(this).next("ul");
				obj.slideToggle("slow")
					.css({"left":(offset.left - parseInt(obj.css("padding-left")))+"px", "top": (offset.top + $(this).height())+"px"})
				return false;
			})

			$(this).children("ul").mouseleave(function() {
				$(this).slideUp("slow");
				$(this).parent().children("a").removeClass("menuOpen");
			});
		});
	}
	
	
	function placeMenuTriangle() {
		var offset = 0;
		$("#newHorzMenu .menu > li").each(function() {
			local_offset = parseInt($(this).children("a").width());
			position_x = parseInt(local_offset / 2) - 8 + offset;
			
			$(this).find("ul > li:first > table > tbody > tr > td > div > div.dropdownmenu_arrow").css("background-position", position_x + "px 0");
			offset += local_offset;
		});
	}
	
	function splitTopMenuList() {
		
		$("#newHorzMenu .menu li").each(function(i){
			
			attrClass = $(this).find("ul > li > table div.dropdownmenu_bottom > div:first > ul").attr("class");
			//console.log(attrClass);
			//console.log($(this).find("ul > li > table div.dropdownmenu_bottom > div:first > ul").html());
			var i = 0;
			do {
				var list = $(this).find("ul > li > table div.dropdownmenu_bottom > div:first > ul:eq(" + i + ")");
				var items = list.find('.break ~li');
				list.find('.break').remove(); // remove the marker
				if(items.size()) {
					list.after($('<ul/>'));
					var newlist = list.nextAll('ul:first');
					newlist.append(items);
				}
				i++;
			} while (items.size() > 0)
			
			
			$(this).find("ul > li > table div.dropdownmenu_bottom > div:first > ul:last")
				.attr("class", attrClass)
				.addClass ("dropdownmenuLastColomn");
		});
		
	}
	
	function splitBottomMenuList() {
		$("#footer #footer-footermenu ul li").each(function(index) {
			if(index % 10 == 0) {
				$("#footer #footer-footermenu > ul:last").after('<ul class="column"></ul>');
			}
			
			if($(this).parent().parent().is("#footer-footermenu")) {
				$(this).addClass('a1st-level');
			}
			
			if($(this).find('ul').size() || index == 0){	
				$(this).children('ul').remove();
			}
			
			$('.column:last').append(this);
		});
		
		$("#footer #footer-footermenu > .column:last").css("margin-right","0");
		$("#footer #footer-footermenu > ul:first").remove();
	}
	
	function adjustBottom(orig_page_height) {
		
		window_height = $(window).height();
		header_height = $("#header").height();
		page_height = $("#page").height();
		footer_height = 
			$("#footer").height() +
				parseInt($("#footer").css("margin-top")) +
				parseInt($("#footer").css("margin-bottom")) +
				parseInt($("#footer").css("padding-top")) +
				parseInt($("#footer").css("padding-bottom"))

		footer_height = $("#footer").outerHeight();
		footer_offset = $("#footer").offset();
		footer_top = footer_offset.top;

		delta = window_height - (footer_top + footer_height);
		if(delta > 0){
			page_height = page_height + delta;
			$("#page").height(page_height);
		}else {
			if(window_height - (footer_height + header_height) < orig_page_height) {
				$("#page").height(orig_page_height);
			}else {
				page_height = page_height + delta;
				$("#page").height(page_height);
			}
		}
	}
});

