// Homepage - Carousel

function mycarousel_initCallback(carousel) {
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });

    jQuery('#header_stage_controls a').click(function() {
    	jQuery('#header_stage_controls a').removeClass('active');
   		jQuery(this).addClass('active');
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        carousel.stopAuto();
        window.setTimeout('carousel.startAuto()');
        return false;
    });
    jQuery('#header_stage_controls a:first').click();
};


function mycarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
    var next = (idx % 4)-1;
    jQuery('#header_stage_controls a').removeClass('active').filter(':eq(' + (next < 0 ? 3 : next) + ')').addClass('active');
};



// Normal Page - Carousel

function jcarousel_controls_initCallback(carousel) {
	var container = carousel.container.get(0);
	var controls = $(container).next();
	
	$('a.number', controls).click(function() {
		$('a', controls).removeClass('active');
		$(this).addClass('active');
		carousel.scroll(jQuery.jcarousel.intval($(this).text()));
		return false;
	});
	$('a.next', controls).click(function() {
		var nextPosition = jQuery.jcarousel.intval($('a.active', controls).text()) + 1;
		var maxItems = $(this).parents('.jcarousel').find('li.jcarousel-item').length;
		$('a.number-' + (nextPosition > maxItems ? 1 : nextPosition), controls ).click();
		return false;
	});
	$('a.previous', controls).click(function() {
		var nextPosition = jQuery.jcarousel.intval($('a.active', controls).text()) - 1;
		var maxItems = $(this).parents('.jcarousel').find('li.jcarousel-item').length;
		$('a.number-' + (nextPosition < 1 ? maxItems : nextPosition), controls ).click();
		return false;
	});
};



$(document).ready(function(){
	var fieldHints = {
		search: {
			field: 'input.search_main',
			value: 'Suchwort eingeben'
		},
		guestbookName: {
			field: 'input[name="tx_veguestbook_pi1[firstname]"]',
			value: 'Name'
		},
		guestbookAge: {
			field: 'input[name="tx_veguestbook_pi1[surname]"]',
			value: 'Alter'
		},
		guestbookMessage: {
			field: 'textarea[name="tx_veguestbook_pi1[entry]"]',
			value: 'Nachricht schreiben'
		}
	}
	
	$.each(fieldHints, function(key, fieldInfo) {
		if($(fieldInfo.field).val() == '') {
			$(fieldInfo.field).val(fieldInfo.value);
		}
		
		$(fieldInfo.field).focus(function() {
			if($(this).val() == fieldInfo.value) $(this).val('');
		}).blur(function() {
			if($(this).val() == '') $(this).val(fieldInfo.value);
		});
	});
	
	
	// guestbook
	$('#ve-guestbook-intro a').click(function() {
		$('#ve-guestbook-intro').hide();
		$('#ve-guestbook-form').slideDown();
		return false;
	});
	if($('#ve-guestbook-form .tx-guestbook-form-error strong:empty').size() > 0) {
		$('#ve-guestbook-form').hide();
	} else {
		$('#ve-guestbook-intro').hide();
	}
	
	// submit
	$('button[name="tx_veguestbook_pi1[submit]"]').click(function() {
		$('input[name="tx_veguestbook_pi1[firstname]"], input[name="tx_veguestbook_pi1[surname]"], textarea[name="tx_veguestbook_pi1[entry]"]').focus();
	});
	// reset
	$('button[name="tx_veguestbook_pi1[reset]"]').click(function() {
		$('input[name="tx_veguestbook_pi1[firstname]"], input[name="tx_veguestbook_pi1[surname]"], textarea[name="tx_veguestbook_pi1[entry]"]').val('').blur();
		return false;
	});
	
	
	
	
	// homepage
	jQuery('#carousel').jcarousel({
        scroll: 1,
        animation: 1000,
        wrap: 'circular',
        auto: 6,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
        itemVisibleInCallback: {
            onBeforeAnimation:  mycarousel_itemVisibleInCallbackBeforeAnimation
        }
    });
	
	
	
	// content slider
	$('#col2_content .jcarousel').each(function() {
		var that = $(this);
		var maxHeight = 0;
		var maxWidth = 0;
		var count = 0;
		
		if($('.csc-default', $(this)).length > 1) {
			$(this).prepend('<ul></ul><div class="controls"></div>');
			$('.controls', $(this)).append('<a class="previous" href="#"><span>Zurück</span></a>');
			$('.csc-default', $(this)).each(function() {
				count++;
				maxHeight = $(this).height() > maxHeight ? $(this).height() : maxHeight;
				maxWidth = $(this).width() > maxWidth ? $(this).width() : maxWidth;
				$('ul', that).append($(this));
				$('.controls', that).append('<a class="number number-' + count + (count == 1 ? ' active' : '') + '" href="#"><span>' + count + '</span></a>');
			}).wrap('<li />');
			$('.controls', $(this)).append('<a class="next" href="#"><span>Nächstes</span></a>');
			$('ul li', that).children('.csc-default').andSelf().height(maxHeight).width(maxWidth);
		}
	});
	
	$('#col2_content .jcarousel ul').each(function() {
		$(this).jcarousel({
			size: $('li', $(this)).length,
			sroll: 1,
			easing: 'swing',
			initCallback: jcarousel_controls_initCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null 
		});
	});
});	






