// Slideshow
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

// Newsletter-Eingaben prüfen
function check() {

	// Vorname ist Pflichtfeld
	if (document.newsletter.prename.value == "" || document.newsletter.prename.value == "VORNAME") {
	    alert("Bitte geben Sie Ihren Vornamen ein.");
	    document.newsletter.prename.focus();
	    return false;
  	}

    // Nachname ist Pflichtfeld
    if (document.newsletter.surname.value == "" || document.newsletter.surname.value == "NAME") {
        alert("Bitte geben Sie Ihren Nachnamen ein.");
        document.newsletter.surname.focus();
        return false;
    }

    // Postleitzahl ist Pflichtfeld
    var zipValue = document.newsletter.zip.value;
    if (zipValue.length != 5 || isNaN(zipValue)) {
        alert("Bitte geben Sie eine fünfstellige Postleitzahl ein.");
        document.newsletter.zip.focus();
        return false;
    }

	// E-Mail-Adresse prüfen
	emailRe = /^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/;
	if (!emailRe.test(document.newsletter.email.value)) {
			alert('Bitte geben Sie eine korrekte E-Mail-Adresse ein.');
	    document.newsletter.email.focus();
			return false;
	}

	// Captcha ist Pflichtfeld
	if (document.newsletter.captcha.value == "") {
	    alert("Bitte geben Sie den Sicherheitscode ein.");
	    document.newsletter.captcha.focus();
	    return false;
  	}

}

// set for global scope
var intervalHeaderImage;
var imageCount;

// 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
    $("#header img:visible").fadeOut("slow");
    $("#header img:eq(" + newImageIndex + ")").fadeIn("slow");
    
    // change state of numbered navigation
    $('#navHeaderImage li.active').removeClass('active');
    $('#navHeaderImage li:eq(' + newImageIndex + ')').addClass('active');
    
    // set link to "more"
    $('#navHeaderImageMore a').attr('href', $('#header img:eq(' + newImageIndex + ')').attr('rel'));
    
}

// 
function startAutomaticHeaderChange() {
    
    // automation
    intervalHeaderImage = window.setInterval(function() {
        // get current index
        currentImageIndex = parseInt($("#header img").index($("#header img: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);
    }, 3000);
    
}

$(function() {

	// Elemente verstecken
	$(".hidden").hide();

	// Anzeigen der rechten Spalte beim Klick auf Partner
	$("a.partner").click(function(event){
		event.preventDefault();
		var partner = $(this).attr("rel");
		$("#right_content").fadeOut(function(){
			$("#right_content").load("inc/inc.partner.php?partner=" + partner, function() {
				$("#right_content").fadeIn();		
			});
		});	
	});

	// Highlighten der Kategorien im Maschinenmarkt
	// $("div.categories").animate({backgroundColor: "yellow"}, "slow").animate({backgroundColor: "white"}, "normal");

	// Vorschaubilder des Maschinenmarktes wechseln
	$("a.preview").click(function(event){
		event.preventDefault();
		$(event.target).parents("div").children(".full").attr("src", $(this).attr("href"));
	});

    /* move through header images                                                                   */

    // hide all but the first image
    $("#header img").not(":first").hide();
    
    // get image count
    imageCount = $("#header img").length;

    // set first link
    currentImageIndex = parseInt($("#header img").index($("#header img:visible:first")));
    $('#navHeaderImageMore a').attr('href', $('#header img:eq(' + currentImageIndex + ')').attr('rel'));
    
    
    // cycling through images
    $("#navHeaderImagePrev").click(function(event){
    	event.preventDefault();
        // get current index
        currentImageIndex = parseInt($("#header img").index($("#header img:visible:first")));
        // get next image by index
        newImageIndex = (imageCount + currentImageIndex - 1) % imageCount;
        // hide all visible images and fade in the new image by index (using the selector :eq())
        changeImage(newImageIndex);
    });
    $("#navHeaderImageNext").click(function(event){
    	event.preventDefault();
        // get current index
        currentImageIndex = parseInt($("#header img").index($("#header img: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);
    });
   
    // navigation by numbers
    $('#navHeaderImage a').not('#navHeaderImageMore a').click(function(event){
       event.preventDefault();
       var newImageIndex = $('#navHeaderImage li').index($(event.target).parents('li'));
       changeImage(newImageIndex);
    });

    // start automation
    startAutomaticHeaderChange();

    // reset automation, if any of the nav-elements was clicked
    // └> 100528: stop animation, requested by Bernhard
    $('#navHeaderImageNext, #navHeaderImagePrev, #navHeaderImage a').not('#navHeaderImageMore a').click(function(){
        // startAutomaticHeaderChange();
        window.clearInterval(intervalHeaderImage);
    });

});
