/****** Slider Teaser ******/
function carousel_itemVisibleInCallback(carousel, item, i, state, evt) {
	var idx = carousel.index(i, carousel_itemList.length);

	if (!carousel_itemList[idx - 1].title_text)
		carousel_itemList[idx - 1].title_text = carousel_itemList[idx - 1].title;

	if (state == "init")
	{
		if (idx == 2) // Middle column
			carousel_itemList[idx - 1].title = '<h2 style="color:#FFFFFF;">'+carousel_itemList[idx - 1].title_text+'</h2>';
		else
			carousel_itemList[idx - 1].title = '<h2>'+carousel_itemList[idx - 1].title_text+'</h2>';
	}
	else if (state == "next")
	{
		if (idx == 1)
		{
			var index_active_old = 1;
			var index_active_new = 2;
			var index_new = 0;
		}
		else if (idx == 2)
		{
			var index_active_old = 2;
			var index_active_new = 0;
			var index_new = 1;
		}
		else if (idx == 3)
		{
			var index_active_old = 0;
			var index_active_new = 1;
			var index_new = 2;
		}

		carousel_itemList[index_active_old].title = '<h2>'+carousel_itemList[index_active_old].title_text+'</h2>';
		carousel.add(i-2, carousel_getItemHTML(carousel_itemList[index_active_old]));

		carousel_itemList[index_active_new].title = '<h2 style="color:#FFFFFF; line-spacing: 1px;">'+carousel_itemList[index_active_new].title_text+'</h2>';
		carousel.add(i-1, carousel_getItemHTML(carousel_itemList[index_active_new]));

		carousel_itemList[index_new].title = '<h2>'+carousel_itemList[index_new].title_text+'</h2>';
	}
	else if (state == "prev")
	{
		if (idx == 1)
		{
			var index_active_old = 2;
			var index_active_new = 1;
			var index_new = 0;
		}
		else if (idx == 2)
		{
			var index_active_old = 0;
			var index_active_new = 2;
			var index_new = 1;
		}
		else if (idx == 3)
		{
			var index_active_old = 1;
			var index_active_new = 0;
			var index_new = 2;
		}

		carousel_itemList[index_active_old].title = '<h2>'+carousel_itemList[index_active_old].title_text+'</h2>';
		carousel.add(i+2, carousel_getItemHTML(carousel_itemList[index_active_old]));

		carousel_itemList[index_active_new].title = '<h2 style="color:#FFFFFF;">'+carousel_itemList[index_active_new].title_text+'</h2>';
		carousel.add(i+1, carousel_getItemHTML(carousel_itemList[index_active_new]));

		carousel_itemList[index_new].title = '<h2>'+carousel_itemList[index_new].title_text+'</h2>';
	}


	carousel.add(i, carousel_getItemHTML(carousel_itemList[idx - 1]));
}

function carousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
	carousel.remove(i);
}

function carousel_getItemHTML(item) {
	return '<div class="scrollerteaser"> <b>'+item.title+'</b> '+item.subtitle+' <img src="'+item.url+'" /> '+item.linkextra+' </div>';
}

var active1 = 0;
var active2 = 0;
var active3 = 0;
var stprev = 0;
function current_active(carousel, item, i, state, evt) {
	var max = carousel_itemList.length;
	if(state == "init") {
		if(active1 == 0) {
			active1 = i;
		} else {
			if(active2 == 0) {
				active2 = i;
			} else {
				if(active3 == 0) {
					active3 = i;
				}
			}
		}
	}
	if(state == "next") {
		active1 = active1 + 1;
		active2 = active2 + 1; // Mitte
		active3 = active3 + 1;
		if(active1 > max) { active1 = 1; }
		if(active2 > max) { active2 = 1; }
		if(active3 > max) { active3 = 1; }
	}
	if(state == "prev") {
		active1 = active1 - 1;
		active2 = active2 - 1; // Mitte
		active3 = active3 - 1;
		if(active1 < 1) { active1 = max; }
		if(active2 < 1) { active2 = max; }
		if(active3 < 1) { active3 = max; }
	}

	if(active1 > 0 && active2 > 0 && active3 > 0) {
		if(stprev != 0) { $("#teaserimg"+stprev).fadeTo(750, 0).css("z-index", "10"); }
		$("#teaserimg"+active2).fadeTo(500, 1, function() { $(this).css("filter", ""); }).css("z-index", "100");
		//Cufon.replace('.scrollerteaser b');
		stprev = active2;
	}
}

function carousel_initCallback(carousel) {
	jQuery('.jcarousel-list li').live('click', function() {
		carousel.scroll(jQuery(this).attr('jcarouselindex')-1);
		carousel.startAuto(0);
		return false;
	});

	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});
}

function initTeaser(carousel, teaser) {
	jQuery("#"+carousel).jcarousel({
		auto: 0, // Auto scroll set to off - was 8
		wrap: 'circular',
		scroll: 1,
		initCallback: carousel_initCallback,
		itemVisibleInCallback: {
			onBeforeAnimation: carousel_itemVisibleInCallback,
			onAfterAnimation: current_active
		},
		itemVisibleOutCallback: {onAfterAnimation: carousel_itemVisibleOutCallback}
	});
	// Teaser
	$("#"+teaser+" > div").each(function(i) {
		if(i == 1) { // Zweites Bild!
			$(this).attr("id", "teaserimg"+(i+1)).css("z-index", "100");
		} else {
			$(this).attr("id", "teaserimg"+(i+1)).fadeTo(1, 0).show().css("z-index", "10");
		}
	});
	$("#"+teaser+" > div > h1").each(function() {
		pos = "lines"+($("br", $(this)).size()+1);
		$(this).html("<span>"+($(this).html().replace(/<br>/gi, "</span><br /><span>"))+"</span>").addClass(pos);
	});
	$("#"+teaser+" > div > p").each(function() {
		$(this).html("<span>"+($(this).html().replace(/<br>/gi, "</span><br /><span>"))+"</span>");
	});
}

function initTeaser_noslide(teaser) {
	$("#"+teaser+" h1").each(function() {
		pos = "lines"+($("br", $(this)).size()+1);
//		$(this).html("<span>"+($(this).html().replace(/<br>/gi, "</span><br /><span>"))+"</span>").addClass(pos);
		$(this).addClass(pos);
	});
}


/* ScrewIE */
$(document).ready(function() {
	$(function() {
		var zIndexNumber = 10000;
		$("ul, li").each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});
});

