/*
	*** Detlef Surrey - Illustration, Comic, Animation ***
	jQuery Enhancement JavaScript for Detlef Surrey
	(c) Mareike Hybsier - http://www.die-programmiererin.de
*/

// $(document).ready(function() {
$(function() {

	// Hide all closed sections
	$(".closed").next().hide();
	
	$('h2').each(function(index) {
		$(this).append($(this).find('a').text());
		$(this).find('a').hide();
  });
	
	// Add slide functions to all sections (h1 elements)
	/* $("h1").click(function () {
		if($(this).is('.closed')) {
			$("h1.open").delay(200, function() { $(this).next().slideUp("slow"); });
			// $("h2.open").delay(200, function() { $(this).next().slideUp("slow"); });
			$(this).delay(200, function() { $(this).next().slideDown("slow"); });
			$("h1").deactivateElement();
			// $("h2").deactivateElement();
			$(this).activateElement();
		}
		else if($(this).is('.open')) {
			// $("h2.open").delay(200, function() { $(this).next().slideUp("slow"); });
			$(this).delay(200, function() { $(this).next().slideUp("slow"); });
			// $("h2").deactivateElement();
			$(this).deactivateElement();
		}
	}); */
	
	// Add slide functions to all categories (h2 elements)
	$("h2").click(function () {
		if($(this).is('.closed')) {
			$(this).css('color', '#b31113');
			$("h2.open").css('color', '#928F88');
			$("h2.open").css('font-weight', 'normal');
			$("h2.open").delay(200, function() { $(this).next().slideUp("slow"); });
			$(this).delay(200, function() { $(this).next().slideDown("slow"); });
			$("h2").deactivateElement();
			$(this).activateElement();
			// window.location=$(this).find("a").attr("href"); return false;
		}
		$(this).delay(300, function() { window.location=$(this).find("a").attr("href"); return false; });
	});
	
	// Add hover functions to all sections and categories (h1 + h2 elements)
	// $("h1").hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
	$("h2").hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
	
	
	// Delay the call to (slide) functions
	// => http://james.padolsey.com/javascript/jquery-delay-plugin/
	$.fn.delay = function(time, callback) {
		jQuery.fx.step.delay = function() {};
		return this.animate( { delay: 1 }, time, callback);
	}
	
	// Set an element class to 'open' or 'closed'
	$.fn.activateElement = function() { switchClasses($(this), 'open', 'closed'); }
	$.fn.deactivateElement = function() { switchClasses($(this), 'closed', 'open'); }
});


// Utility function for switching/toggling classes
function switchClasses (element, classToAdd, classToRemove) {
	element.addClass(classToAdd);
	element.removeClass(classToRemove);
	// De/Activate the given element
	// (classToAdd == 'open') ? element.addClass('active') : element.removeClass('active');
}
