

var stopAnimation = 0;

b_showImage = function(showTeaser) {
	var activeImage = $('.keyVisualSliderBannerWrap').children('#' + showTeaser);
	var wrap = $('.keyVisualSliderBannerWrap');
	var imgWidth = $('.keyVisualSliderBannerWrap').children().width();
	var imgNr = Math.abs(showTeaser.substring(showTeaser.search('-')).substr('-1'));
	var jumpToImg = parseInt((imgNr-1) * imgWidth);
	$(wrap).animate({ 'marginLeft': '-' + jumpToImg }, 1000);
	
	
	$('#' + showTeaser).addClass('active').siblings().removeClass('active');
	
	$('.keyVisualSliderPageBrowserInnerWrap .inlineMenu li').children('a').removeClass('active');
	$('.keyVisualSliderPageBrowserInnerWrap .inlineMenu li').children('a[rel^=' + showTeaser + ']').addClass('active');
	
	if (jumpToImg == 0) {
		$('.navLeft').children().addClass('inactive');
		$('.navRight').children().removeClass('inactive');
	} else if (jumpToImg == imgWidth * ($('.keyVisualSliderBannerWrap').children().length - 1)) {
		$('.navRight').children().addClass('inactive');
		$('.navLeft').children().removeClass('inactive');
	} else {
		$('.navLeft, .navRight').children().removeClass('inactive');
	}
}


//animate the teaser on the start page
b_animateTeaser = function() {
	if (!stopAnimation) {
		var images = $('.keyVisualSliderItem');
		var activeImage = $('.keyVisualSliderItem.active');
		var nextImage = $(activeImage).next();
		if (!nextImage.length) {
			nextImage = $(images).first();
		}
		var showImage = $(nextImage).attr('id');
		
		b_showImage(showImage);	
	}
	
	window.setTimeout(function() {
		b_animateTeaser();
	}, 8000);
}



// functionality for the keyVisualSlider 
b_init_keyVisualSlider = function() {
		var wrap = $('.keyVisualSliderBannerWrap');
		var countPages = wrap.children().length;
		var elWidth = $(wrap).children().width();
		
		var totalwidth = elWidth * countPages;
		
		$(wrap).css('width', totalwidth);
		
		$('.keyVisualSliderItem').first().addClass('active');
		$('.keyVisualSliderPageBrowserInnerWrap .inlineMenu li').children('a[rel^=keyVisualItem-1]').addClass('active');
		
		$('.navLeft').children().addClass('inactive');
}

b_showNavigation = function(menuEl) {
	menuEl.addClass('isOpen');
	var h = $('.mainMenuSub', menuEl).height();
	$('#content').css('marginTop', h + 10 + 'px');
}

b_hideNavigation = function() {
	$('.isOpen').removeClass('isOpen');
	$('#content').css('marginTop', '5px');
};

function b_checkthebox(el) {
alert(el)
	if (typeof el == "string") {
		var checkId = el;
		el = $('#check_' + checkId);
	} else {
		var checkId = $(el).attr('id').substr(6);	// something like 'uid8_0'
	}
	var val = $('input[name=value_' + checkId + ']').val();

	if ($(el).attr('checked')) {
		$(el).val(val);
		$('#' + checkId).val(val);
	} else {
		$(el).val('');
		$('#' + checkId).val('');
	}
};


b_initTabBrowser = function () {
	// only init tab browser, if we have tabs
	if ($('.tabFrame').length == 0) {
		return;
	}

		// hide the last "next" button
	$('.tabFrame:last').find('.tabNavRight').hide();

		// hide all tabs except the first one
	b_showTabBrowserContent(1);
/* 	$('.tabFrame').parent().not(':first').hide(); */
	
		// show content when clicking on a menu item
	$('.csc-section a').click(function(evt) {
		evt.preventDefault();
		var itemPos = $(this).parent().prevAll().length + 1;
		b_showTabBrowserContent(itemPos);
	});

		// show content when clicking on the previous item
	$('.tabNav a').click(function(evt) {
		evt.preventDefault();
		var itemPos = $(this).attr('rel');
		b_showTabBrowserContent(itemPos);
	});

	
};

/** show a specific tab */
b_showTabBrowserContent = function(itemPos) {
	$('.tabFrame').parent().hide();
	$('#tab-' + itemPos).parent().show();
	$('.csc-section').removeClass('active').eq(itemPos-1).addClass('active');
}



$(document).ready(function() {
	
	b_init_keyVisualSlider ();
	
	//key visual slider
	if ($('.keyVisualSlider').length > 0) {
		$('.navLeft a').attr('rel', 'prev');
		$('.navRight a').attr('rel', 'next');
		
		window.setTimeout(function() {
			b_animateTeaser();
		}, 4000);
		
		$('.keyVisualSlider').mouseenter(function(){
			stopAnimation = 1;
		}); 
		$('.keyVisualSlider').mouseleave(function(){
			stopAnimation = 0;
		});
		
		$('.keyVisualSliderPageBrowserInnerWrap .inlineMenu li').children().click(function(evt) {
			evt.preventDefault();
			var activeImage = $('.keyVisualSliderItem.active');
			var jumpToImg = $(this).attr('rel');
			var showImage = $(activeImage).prev();
			
			if (jumpToImg == "prev") {
				showImage = $(activeImage).prev().attr('id');
			} else if (jumpToImg == "next") {
				showImage = $(activeImage).next().attr('id');
			} else {
				showImage = jumpToImg;
			}
			
			b_showImage(showImage);
		})
	};
	
	
	
	$('.mainLink').click(function(evt) {
			// don't use this for inklusive-schule.de
		if ($(this).hasClass('mainLinkInklusion')) {
			return;
		}
		evt.preventDefault();
		evt.stopPropagation();
		
		var menuEl = $(this).parent();
		if (!menuEl.hasClass('isOpen')) {
			b_hideNavigation();
			b_showNavigation(menuEl);
		} else {
			b_hideNavigation();
		}

		$('body').one('click', function() {
			b_hideNavigation(menuEl);
		});

	});


	//
	$('#amount').blur(function() {
		var amount = $(this).val();
		amount = amount.replace(/,/, '.');
		$(this).val(amount);
	});
	
	b_initTabBrowser();

		// make Link Submits working (be careful, it is not Easy-Accessable "Barrierefreiheit")
	$('.formSubmit').click(function(evt) {
		evt.preventDefault();
		$(this).parents('form').first().submit();
	});
	
	
	
		// for tracking with Google Analytics and many Domains 
	
	$('a').click(function() {
		var goalUrl = $(this).attr('href')
		if (goalUrl.match(/tilly-lahnstein-schule.de/) || goalUrl.match(/berufsbildungswerk-stuttgart.de/) || goalUrl.match(/betty-hirsch-schule.de/) || goalUrl.match(/nikolauspflege.de/) || goalUrl.match(/nikosehzentrum.de/) ) {
			_gaq.push(['_link', goalUrl]); 
		}
	});
	
	$('form').submit(function() {
		var goalUrl = $(this).attr('action')
		if (goalUrl.match(/tilly-lahnstein-schule.de/) || goalUrl.match(/berufsbildungswerk-stuttgart.de/) || goalUrl.match(/betty-hirsch-schule.de/) || goalUrl.match(/nikolauspflege.de/) || goalUrl.match(/nikosehzentrum.de/) ) {
			_gaq.push(['_linkByPost', this]);
		}
	});
	
	
	
	
	
	
	
	
	

});
