$(document).ready(function() {
	/* Navigation main dropdown */
	$('#nav > ul li').hover(function() {
		$(this).addClass('hover');	
		$('#nav > ul:first', this).show();
	}, function() {
		$(this).removeClass('hover');
		$('#nav > ul:first', this).hide();
	});
	
	/* Image Switch */
	if ($.browser.msie) {
		$(function() {
		    setInterval( "simpleSwitch()", 5000 );
		});
	}
	else {
		$(function() {
		    setInterval( "slideSwitch()", 5000 );
		});
	}
	
	// Search event
	$(function() {
	    var foundin = $('*:contains("Keine Einträge gefunden")');
	    if (foundin.text() != '') {
	    	$('#search').hide();
	    	$('#search table').hide();
	    }
	});

	$('h3.search').toggle(function() {
		$('#search').slideDown();
	}, function() {
		$('#search').slideUp();
	});
	
	/* Form Label replace */
	setField('index_search_field');
	//setField('mailformname');
	//setField('mailformemail');
	//setField('mailformmessage');
});

/* Image Slider Switch */
function slideSwitch() {
    var $active = $('#banner img.active');

    if ( $active.length == 0 ) $active = $('#banner img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#banner img:first');
	
	// uncomment the 3 lines below to pull the images in random order
	
	var $sibs  = $active.siblings();
	var rndNum = Math.floor(Math.random() * $sibs.length );
	var $next  = $( $sibs[ rndNum ] );
	
    $active.addClass('last-active');

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

/* Image Simple Switch */
function simpleSwitch() {
	var $active = $('#banner .images img.active');
	
    if ( $active.length == 0 ) $active = $('#banner .images img:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#banner .images img:first');

    // uncomment the 3 lines below to pull the images in random order
    
    var $sibs  = $active.siblings();
    var rndNum = Math.floor(Math.random() * $sibs.length );
    var $next  = $( $sibs[ rndNum ] );

	$active.hide();
	$active.addClass('last-active');
	
	$next.addClass('active')
	$next.show();
    $active.removeClass('active last-active');
}

/* Set Field */
function setField(fieldName) {
	var field = jQuery('#' + fieldName);
	var labelText = jQuery('label[for="' + fieldName + '"]').text();
	
	field.val(labelText);
	field.addClass('label');
	
	field.focus(function() {
		if (field.val() == labelText)
			field.val('');
		field.removeClass('label');
	});
	
	field.blur(function() {
		if (field.val() == '')
		{
			field.val(labelText);
			field.addClass('label');
		}
	});
}
