// Open Error-Message-Box
function showError(headline, content, errorClass)
{

    if (typeof errorClass == 'undefined') errorClass = 'notice';
    var errorContent = '<h1>' + headline + '</h1><p>' + content + '</p>';
    $("#errorMessage").addClass(errorClass).html(errorContent).slideDown();
	// scroll to top, so user can read the error message
	$(window).scrollTop(0);

}


// set for global scope
var intervalSlideshowImage;
var imageCount;
var currentImageIndex;

// hide all visible images and fade in the new image by index (using the selector :eq())
function changeImage(newImageIndex) {

    // hide visible images and show selected one
    $("#slideshow img.picture:visible").fadeOut("slow");
    $("#slideshow img.picture:eq(" + newImageIndex + ")").fadeIn("slow");

    // change state of numbered navigation
    $('#navSlideshow li.active').removeClass('active');
    $('#navSlideshow li:eq(' + newImageIndex + ')').addClass('active');

    if ($('#slideshowHistorieText').length != 0 ) {

    	var text = $('#slideshow img.picture:eq(' + newImageIndex + ')').attr('alt');
    	$("#slideshowHistorieText p").html(text);

    }

    currentImageIndex = newImageIndex;
}


//
function startAutomaticSlideshowChange() {

    // automation
    intervalSlideshowImage = window.setInterval(function() {
        // get current index
        currentImageIndex = parseInt($("#slideshow img.picture").index($("#slideshow img.picture:visible:first")));
        // get next image by index
        newImageIndex = (currentImageIndex + 1) % imageCount;
        // hide all visible images and fade in the new image by index (using the selector :eq())
        changeImage(newImageIndex);
    }, 4000);

}


// jQuery Ready-Handler
$(function() {


    // Hide all elements which should be initially hidden
    $(".hidden").hide();

	// open and close content of boxes
	$("a.slide").click(function(event) {
			$("div.slides").each(function(){
	  			$(this).slideUp("slow");

	  		});
	  		$("a.slide").each(function(){
	  			$(this).removeClass('active');
	  		});

	  		event.preventDefault();
		$($(this).attr("href")).slideToggle("slow");
		$(this).addClass('active');
	});

	// open and close content of boxes
	$("a.fade").click(function(event) {

			event.preventDefault();
	  		var elems = $("div.fades"), count = elems.length;
			var target = $(this).attr("href");
			var parentElement = $(this);

			elems.each( function(i) {
				//alert('hallo')
				$(this).fadeOut("slow", function() {
					if (!--count) {



						$("a.fade").each(function(){
				  			$(this).removeClass('active');
				  		});

				  		$(target).fadeIn("slow");

				  		parentElement.addClass('active');

					}
				});
			});





	});


     /* SLIDESHOW                                                        */

    // hide all but the first image
    $("#slideshow img.picture").not(":first").hide();

    // get image count
    imageCount = $("#slideshow img.picture").length;

    // set first link
    currentImageIndex = parseInt($("#slideshow img.picture").index($("#slideshow img.picture:visible:first")));
	$("#slideshowHistorieText p").html($('#slideshow img.picture:eq(' + currentImageIndex + ')').attr('alt'));
    // navigation by numbers
    $('#navSlideshow a').click(function(event){
       event.preventDefault();
       var newImageIndex = $('#navSlideshow li').index($(event.target).parents('li'));
       changeImage(newImageIndex);
    });

    // start automation
    startAutomaticSlideshowChange();

    // reset automation, if any of the nav-elements was clicked
    // └> 100528: stop animation
    $('#navSlideshowNext, #navSlideshowPrev, #navSlideshow a').click(function(){
        // startAutomaticHeaderChange();
        window.clearInterval(intervalSlideshowImage);
    });

    $("#slideshow img.picture").click(function(event){
    	var link = $('#slideshow img.picture:eq(' + currentImageIndex + ')').attr('rel');
    	$.prettyPhoto.open(link,'','');
    });



    //Loesungen
    $('#selectLoesungen1').change(function(){
		var loesungsnurl = $('#selectLoesungen1').val();
		if (loesungsnurl!='null') window.location = loesungsnurl;
	});
	$('#selectLoesungen2').change(function(){
		var loesungsnurl2 = $('#selectLoesungen2').val();
		if (loesungsnurl2!='null') window.location = loesungsnurl2;
	});


});
