$(document).ready(function() {
	setupNavigation();
	addTeaserPictureGridMargins();
	setupTeaserPopups();

	// pages "Branche" and "Kompetenzen"
	setupZoomPhotos();
	
	// pages "Mitarbeiter" and "Geschäftsleitung":
	$("li.employee a").click(function() {
		var target = $(this).next("div.slideout");
		$("li.employee div.slideout").not(target).slideUp();
		target.slideToggle();
	});

	// add target="_blank" for external links
	$("a.external, a[href^='http://'], a[href^='https://']").not("a.internal").attr("target", "_blank"); 
});

/*** Teaser ***/
function addTeaserPictureGridMargins() {
	var lists = [];
	var curList = [];
	var lastItem = null;
	$("#Teaser .teaser-picture").each(function() {
		if($(lastItem).next()[0] != this) {
			if(curList.length > 0) {
				lists.push(curList);
			}
			curList = [];
		}
		curList.push(this);
		lastItem = this;
	});
	if(curList.length > 0) {
		lists.push(curList);
	}
	
	$.each(lists, function() {
		$(this).addGridMargins(10);
	});
}

function setupZoomPhotos() {
	$("ul.photos li").decorateAsGrid();
	
	$("ul.photos li > a").click(function() {
		var overlay = $("#ZoomOverlay");
		var children = overlay.children();
		overlay.children("img.photo").attr("src", "../images/blank.gif");
		overlay.children("img.photo").attr("src", $(this).attr("href"));
		overlay.find("p").html($(this).next("p").html());
		children.hide();
		
		var offset = $(this).parent().offset();
		overlay.css({
			top: offset.top,
			left: offset.left + 1,
			width: $(this).parent().width(),
			height: $(this).parent().height()
		});
		overlay.show();
		overlay.animate({
			left: offset.left - 598 + $(this).width(),
			width: 598,
			height: 323
		}, null, null, function() {
			children.fadeIn(400, function() {
				/* work around IE bug */
				$(".text-bar").css("opacity", 0.5);
			});
		});
		var w = $(this).width();
		var h = $(this).height();
		overlay.expose({
			color: "#606060",
			opacity: 0.5,
			onBeforeClose: function() {
				children.fadeOut(200);
				overlay.animate({
					left: offset.left,
					width: w,
					height: h
				}).fadeOut(400);
			}
		});
		return false;
	});
}

function setupTeaserPopups() {
	var iframe = $("<iframe/>").attr({frameborder: 0, scrolling: "no"});
	var popup = $("<div/>").addClass("teaser-popup").append(iframe);

	$("body").append(popup);
	$("a.popup-link").each(function() {
		$(this).overlay({
			target: popup,
			top: "center",
			mask: {
				color: '#606060',
				loadSpeed: 500,
				opacity: 0.5
			},
			onBeforeLoad: function() {
				var trigger = this.getTrigger();
				var overlay = this.getOverlay();
	
				var src = trigger.attr("href");
				// extract size from url (e.g. "demo.aspx?oid=12#w900h600")
				var w = /#.*w(\d+)/.exec(src);
				var h = /#.*h(\d+)/.exec(src);
				
				overlay.css({
					width: (w && w[1] ? parseInt(w[1], 10) : null),
					height: (h && h[1] ? parseInt(h[1], 10) : null)
				});
				
				// set the url only if it's not been done yet
				if(iframe.attr("src") != src) {
					iframe.attr("src", src);
				}
			},
			onBeforeClose: function() { iframe.attr("src", ""); }
		});
	});
}

/*** Navigation ***/
function setupNavigation() {
	$("ul.sub-nav").each(splitSubmenu);

	// add vertical separator bars to navigation
	addSeparators.call($("#Navigation"));
	$("ul.sub-nav").each(addSeparators);

	var subNavBox = $("#SubNavigationBox");
	$("#Navigation > li").each(function() {
		var link = $(this).children("a");
		var subNav = $(this).children("ul.sub-nav");
		subNavBox.append(subNav);
		
		link.hover(
			function() {
				subNavBox.doTimeout("hide");
			
				if(subNav.length === 0) {
					subNavBox.hide();
					return;
				}
			
				// hide all sub menus, show current one
				subNavBox.children().hide();
				subNav.show();

				subNavBox.show();
			},
			function() { subNavBox.doTimeout("hide", 500, function() { subNavBox.hide(); }); }
		);
		
		subNavBox.hover(
			function() { subNavBox.doTimeout("hide"); subNavBox.show(); },
			function() { subNavBox.doTimeout("hide", 500, function() { subNavBox.hide(); }); }
		);
	});
}

function splitSubmenu() {
	var separator = $(this).children("li:empty");
	if(separator.length === 0) {
		return;
	}

	// create empty copy of submenu
	var newMenu = $(this).clone().empty();
	// insert after original submenu
	$(this).after(newMenu);

	// move items after separator into new submenu
	newMenu.append(separator.nextAll());
	// remove separator
	separator.remove();
}

var verticalBar;

function addSeparators() {
	if(!verticalBar) {
		verticalBar = $("#HtmlTemplates img.vertical-bar");
	}
	
	var copy = verticalBar.clone();
	// work-around for IE7 bug
	if(!$(this).hasClass("sub-nav") && $.browser.msie && $.browser.version.substr(0,1) == "7") {
		copy.css({marginLeft: 1, marginRight: 3});
	}

	var listItems = $(this).children("li");
	if(!$(this).hasClass("sub-nav")) {
		listItems.not(":last").append(copy);
	} else {
		listItems.not(":last").append("&nbsp;").append(copy).append("&nbsp;");
	}
}

/*** Printing ***/
function printPage() {
	$.fx.off = true;
	$(".head *").removeAttr('style');
	window.print();
	$.fx.off = false;
}

/*** Diagram Animation ***/
function setupAnimations(diagram, legend) {
	diagram.mouseleave(function() {
		diagram.itemSelector("deselect");
		//FIXME: why is this needed?
		legend.itemSelector("deselect");
	});

	legend.eventBlocker(100, null, function(target) {
		target.stop(true, true).slideDown();
	});
	legend.itemSelector(legend.find("li > a"), {
		select: function(target) { legend.eventBlocker("invoke", $(target).next("div.slideout")); },
		deselect: function(target) { $(target).next("div.slideout").stop(true, true).delay(600).slideUp(); },
		mirror: diagram
	});

	legend.find("li").each(function(idx) {
		$(this).hover(
			function() { legend.itemSelector("select", idx); },
			function() {
				legend.itemSelector("deselect");
				//FIXME: why is this needed?
				diagram.itemSelector("deselect");
			}
		);
	});

	var legendLinks = legend.find("li > a");
	
	diagram.find("div img").each(function(idx) {
		$(this).click(function() {
			var href = $(legendLinks[idx]).attr("href");
			if(!(/^javascript/).test(href)) {
				window.location = href;
			}
		});
		
	});
}
