/*
* Author:      Koch Kommunikation (http://www.koch-k.ch/)
*/


// OVERRIDING THE $-FUNCTION
jQuery.noConflict();

jQuery(function(){	   

	// fade out the content container
	// and display the gallery navigation incl. first gallery item
	jQuery("#showGallery").click(function () {					  

		jQuery("#toggle").fadeOut("slow");	
		jQuery(".secondlevelnav").addClass("hidden");	
		if(jQuery(".secondlevelnav").hasClass("hidden")) {
			jQuery("#logoAlternative").show();
			jQuery("#logoOriginal").hide();
		}
		jQuery("#showGallery").fadeOut("slow", function() {
			jQuery("#galleryItems").fadeIn("slow");
		});	
		
		jQuery('.header_title').hide();
		jQuery('.header_subtitle').hide();
		
		showImage(jQuery('li.items a.active').attr( 'rel' ), jQuery('li.items a.active').attr( 'title' ));
		
		return false;
	});
	
	
	// fade out the gallery navigation
	// and display the content container incl. associated headlines
	jQuery("#hideGallery").click(function () {					  
		
		jQuery('.header_title').hide();
		jQuery('.header_subtitle').hide();
				
		jQuery("#toggle").fadeIn("slow") ;	
		if(jQuery(".secondlevelnav").hasClass("hidden")) {
			jQuery(".secondlevelnav").removeClass("hidden");
			jQuery("#logoAlternative").hide();
			jQuery("#logoOriginal").show();
		}
		jQuery('.header_title').not('#firstline').fadeIn('slow');
		jQuery('.header_subtitle').not('#secondline').fadeIn('slow');
		jQuery("#galleryItems").hide();						
		jQuery("#showGallery").fadeIn("slow");

		return false;
	});
	
	// fade out the content container
	// and display the gallery navigation incl. first gallery item
	jQuery("a.gallery-link").click(function () {					  

		jQuery("#toggle").fadeOut("slow");	
		jQuery(".secondlevelnav").addClass("hidden");	
		if(jQuery(".secondlevelnav").hasClass("hidden")) {
			jQuery("#logoAlternative").show();
			jQuery("#logoOriginal").hide();
		}
		jQuery("#showGallery").fadeOut("slow", function() {
			jQuery("#galleryItems").fadeIn("slow");
		});	
		
		jQuery('.header_title').hide();
		jQuery('.header_subtitle').hide();
		
		showImage(jQuery('li.items a.active').attr( 'rel' ), jQuery('li.items a.active').attr( 'title' ));
		
		return false;
	});
	
	
	// count gallery items
	var elementCount = jQuery("li.items > a.element").size();
	
	
	// Click navigation
	jQuery('li.items a.element').click(function() {
		showImage(jQuery(this).attr( 'rel' ), jQuery(this).attr( 'title' ));
		jQuery('li.items').find('a.active').removeClass('active');
		jQuery(this).addClass('active');
		
		return false;
	});
	
	// Backwards navigation
	jQuery('li.items a.first').click(function() {
		prevElement = jQuery('li.items').find('a.active');
		if(prevElement.index() == 1) {
			prevElement.removeClass('active');
			jQuery('li.items').find('a.element:last').addClass('active');
		} else {
			prevElement.removeClass('active').prev().addClass('active');
		}
		showImage(jQuery('li.items a.active').attr( 'rel' ), jQuery('li.items a.active').attr( 'title' ));

		return false;
	});
	
	
	// Forward navigation
	jQuery('li.items a.last').click(function() {
		nextElement = jQuery('li.items').find('a.active');
		if(nextElement.index() == elementCount) {
			nextElement.removeClass('active');
			jQuery('li.items').find('a.element:first').addClass('active');
		} else {
			nextElement.removeClass('active').next().addClass('active');
		}
		showImage(jQuery('li.items a.active').attr( 'rel' ), jQuery('li.items a.active').attr( 'title' ));
		
		return false;
	});
	
	
	// Function for displaying single gallery items
	var activeContainer = 1;	
	var currentZindex = 498;
	
	var showImage = function(image, caption) {
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}

		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		jQuery("#bgroundimg" + activeContainer).css({
			"background-image" : "url("+ image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		jQuery("#firstline").css({"display" : "none"});
		jQuery("#secondline").css({"display" : "none"});
		
		var captionArray = caption.split(" - ");
		
		
		// Set the new header text
		jQuery("#firstline").html(captionArray[0]);
		jQuery("#secondline").html(captionArray[1]);
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		jQuery("#bgroundimg" + currentContainer).fadeOut('slow',function() {
			jQuery("#firstline").fadeIn('slow');
			jQuery("#secondline").fadeIn('slow');
		});
	};

	
});
