// JavaScript Document

// Author: Sebastian Friedrich (i. A. RUSKA, MARTIN, ASSOCIATES)
// Mail: friedrich@ruskamartin.de
// Web: http://ruskamartin.de

$(function(){
	var IDLE_TIMEOUT = 750;
	var ANIM_SPEED = 250;
	var hoverTimeout = undefined;

	function switchSubMenu() {
		setTimeout(showMenu, 500);
	}
	
	function showMenuSleep() {
		hoverTimeout = setTimeout(showMenu, IDLE_TIMEOUT, target = this);
	}
		
	function showMenu() {
		$(".menu-dropdown ul ul")
			.animate({
				height:"show",
				opacity:1
			}, ANIM_SPEED);
		$(".menu-dropdown > ul")
			.addClass("active")
			.children("li")
			.children("h2")
			.css("cursor","default");
	}
	
	function hideMenu() {
		clearTimeout(hoverTimeout);		
		$(".menu-dropdown ul ul")
			.animate({
				height:"hide",
				opacity:0
			}, ANIM_SPEED * 2);
		$(".menu-dropdown > ul")
			.removeClass("active")
			.children("li")
			.children("h2")
			.css("cursor","pointer");
	}
	
	// init menu
	$(".menu-dropdown > ul")
		.addClass("js-menu-node")
		.children("li")
		.children("h2")
		.css("cursor","pointer")
		.next()
		.hide();
	
	$(".js-menu-node")
		.hover(showMenuSleep, hideMenu)
		.click(showMenu);
		
	// barrierefreie Popups für externe Links + pdf files
	$("a[href^='http://'], a[href$='.pdf']").click(function(e) {
		e.preventDefault();
		window.open(this.href);
	});
	
	// projekt sample controlller
	$(".imageblock a:has(img)").css("position","absolute");
	$(".imagecontrol a").click(function(e) {
		$(".imagecontrol a.active").removeClass("active");
		$(this).addClass("active");
		$(".imageblock a:visible").not($(this).attr("href")).fadeOut(500);
		$($(this).attr("href")).fadeIn(500);
		location.href = this.href;
		e.preventDefault();
	});
		
	// image gallery controller
	setInitialHashState();
	$(".galleryblock a").click(function(e) {
		$(".galleryblock a.active").removeClass("active");
		$(".galleryblock a[href='" + $(this).attr("href") + "']").addClass("active");
		$(".panningblock").children("img:eq(" + $(".galleryblock li").index($(this).parent()) + ")").fadeIn(750).siblings("img").fadeOut(250); 
		$(".textblock.hookin:has(" + $(this).attr("href") + ")").fadeIn(500).siblings(".textblock.hookin").fadeOut(500);
		location.href = this.href;
		e.preventDefault();
	});
	
	// react on hash-change (e.g. back button hit) in modern browsers
	function processHashChange() {
		if (window.location.hash == "") { 
			setInitialHashState();
		} else {
			$(".imagecontrol a[href='" + window.location.hash + "']").click();
			$(".galleryblock a[href='" + window.location.hash + "']").click();
		}
	}
	window.onhashchange = processHashChange;
	
	function setInitialHashState() {
		$(".galleryblock a.active").removeClass("active");
		$(".panningblock img").show();
		$(".textblock.hookin, .panningblock img:not(:first)").hide();
	}
	
	// image controllers: start page view with image marked by url hash index
	processHashChange();
	
	// references effect
	$(".clientwall li").hover(
		function() {
			$(this).children("img:eq(1)").fadeOut(250);
		}, function() {
			$(this).children("img:eq(1)").fadeIn(750);
		});
		
	// expand to full text
	$("a[href='#fulltext']").click(function(e) {
		$(".imageblock.double")
			.animate({
				width:"332px"
			}, 500, "swing", function() {
				$("#fulltext").fadeIn(500).removeClass("hidden");
			});
		$(this).remove();
		location.href = this.href;
		e.preventDefault();
	});
});
