

function loadContent(url) {
	$("#ajax_content").fadeOut("fast", function() {
		$("#ajax_content").empty();
		var content;
		$.get(url , function(data){
			content = data.split("TRENNER");
			self.document.title = content[0];
			$("#ajax_content").append( content[1] );
			$("#ajax_content").fadeIn("fast");

			$("span.shadowbox img").click(function(){
				openContent = $("span.shadowbox").attr("href");
				Shadowbox.open({
					content: openContent,
					player:"iframe",
					width: $("span.shadowbox").attr("width"),
					height: $("span.shadowbox").attr("height"),
				});
			});
		});
	});
}

function loadContentWithoutFade(url) {
	var content;
	$.get(url , function(data){
	    content = data.split("TRENNER");
	    self.document.title = content[0];
		$("#ajax_content").empty();
		$("#ajax_content").append( content[1] );
	});
}

var timerHandle;
var showTimeout = 3000;

function fadeOutItem(itemNumber){
	var item = $(".itemBox:eq(" + (itemNumber) + ")");
	item.fadeOut("fast", function(){
		if (item.parent().next().text() == '') {
			jQuery($(".itemBox:eq(" + (0) + ")")).fadeIn("fast");
			jQuery($(".thumb:eq(" + (0) + ")")).addClass("thumbActiv");
		} else {
			jQuery($(".itemBox:eq(" + (itemNumber + 1) + ")")).fadeIn("fast");
			jQuery($(".thumb:eq(" + (itemNumber + 1) + ")")).addClass("thumbActiv");
		}
	})
}

var isStarted = false;

function hideAndFadeOutItem(itemNumber){
	// Check that 1 div is visible, because if
	// no div is visible the fadeOut is started
	var count = 0;
	$(".itemBox").each(function(i){
		if ($(this).is(':visible') ) {
			count = count +1;
		}
	})

    window.clearTimeout(timerHandle);
	timerHandle = 0;
	if(count == 1) {
		// console.log("%s %s %d", "Testen wir mal", isStarted, count);
		$(".itemBox").each(function(i){
			jQuery($(".thumb:eq(" + (i) + ")")).removeClass("thumbActiv");
			$(this).hide();
		})
		if(itemNumber == 0) {itemNumber = $(".itemBox").size();}
		fadeOutItem(itemNumber-1);
	}
}

function hideAndFadeOutItemEnd(itemNumber){
	if(timerHandle == 0) {
		timerHandle = window.setTimeout("startSlideShow()",showTimeout);
	}
}

function createSlideShowLinks() {
	$(".itemBox").each(function(i){
		jQuery($(".thumb:eq(" + (i) + ") a")).attr("onmouseover","javascript:hideAndFadeOutItem("+(i)+");");
		jQuery($(".thumb:eq(" + (i) + ") a")).attr("onmouseout","javascript:hideAndFadeOutItemEnd();");
		jQuery($(".thumb:eq(" + (i) + ") a")).attr("href",
			jQuery($(".itemBox:eq(" + (i) + ") a")).attr("href")
		);
	})
}

function startSlideShow() {
	$(".itemBox").each(function(i){
		jQuery($(".thumb:eq(" + (i) + ")")).removeClass("thumbActiv");
		if ($(this).is(':visible') ) {
			fadeOutItem(i);
		}
	})
	timerHandle = window.setTimeout("startSlideShow()",showTimeout);
}


$(document).ready(function() {
	
	HTTP_GET_VARS=new Array();
	strGET=document.location.search.substr(1,document.location.search.length);
	if(strGET!='') {
		gArr=strGET.split('&');
		for(i=0;i<gArr.length;++i) {
			v='';vArr=gArr[i].split('=');
			if(vArr.length>1){v=vArr[1];}
			HTTP_GET_VARS[unescape(vArr[0])]=unescape(v);
		}
	}

	jQuery($(".thumb:eq(0)")).addClass("thumbActiv");
	createSlideShowLinks();
	timerHandle = window.setTimeout("startSlideShow()",showTimeout);

	var firstShow = 0;
	$(".image a.ajax").each(function() {

		var url_temp = $(this).attr("href");
		var title_temp_image = $(this).attr("title");

		url_temp_image = "javascript:loadContent('"+url_temp+"?type=7');";
		$(this).attr("title",title_temp_image).attr("href",url_temp_image);
		if (firstShow == 0) {
			firstShow = 1;
			loadContent(url_temp+"?type=7");
		}
	});



	$("h3 a.ajax").each(function() {

		var url_temp_h3 = $(this).attr("href");
		var title_temp_h3 = $(this).attr("title");

		var url_temp = "javascript:loadContent('"+url_temp_h3+"?type=7');";
		$(this).attr("title",title_temp_h3).attr("href",url_temp);

	});

	$(".layout4 .image a").each(function() {
		var url_temp = $(this).attr("href");
		url_temp = "javascript:loadContentWithoutFade('/"+url_temp+"?type=7');";
		$(this).attr("href",url_temp);
	});

	if ($('#slider .scrollContainer > div').text() != '') {
		var $panels = $('#slider .scrollContainer > div');
		var $container = $('#slider .scrollContainer');

		// if false, we'll float all the panels left and fix the width
		// of the container
		var horizontal = true;

		// float the panels left if we're going horizontal
		if (horizontal) {
			$panels.css({
				'float': 'left',
				'position': 'relative' // IE fix to ensure overflow is hidden
			});

			// calculate a new width for the container (so it holds all panels)
			$container.css('width', $panels[0].offsetWidth * $panels.length);
		}

		// collect the scroll object, at the same time apply the hidden overflow
		// to remove the default scrollbars that will appear
		var $scroll = $('#slider .scroll').css('overflow', 'hidden');

		// apply our left + right buttons
//		$scroll.before('<img class="scrollButtons left" src="images/scroll_left.png" />').after('<img class="scrollButtons right" src="images/scroll_right.png" />');

		// handle nav selection
		function selectNav(){
			$(this).parents('ul:first').find('a').removeClass('selected').end().end().addClass('selected');
		}

		$('#slider .navigation').find('a').click(selectNav);

		// go find the navigation link that has this target and select the nav
		function trigger(data){
			var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
			selectNav.call(el);
		}

		if (window.location.hash) {
			trigger({
				id: window.location.hash.substr(1)
			});
		}
		else {
			$('ul.navigation a:first').click();
		}

		// offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset.  Try removing this to get a good idea of the effect
		var offset = parseInt((horizontal ? $container.css('paddingTop') : $container.css('paddingLeft')) ||
		0) *
		-1;


		var scrollOptions = {
			target: $scroll, // the element that has the overflow
			// can be a selector which will be relative to the target
			items: $panels,

			navigation: '.navigation a',

			// selectors are NOT relative to document, i.e. make sure they're unique
			prev: 'img.left',
			next: 'img.right',

			// allow the scroll effect to run both directions
			axis: 'xy',

			onAfter: trigger, // our final callback
			offset: offset,

			// duration of the sliding effect
			duration: 500,

			// easing - can be used with the easing plugin:
			// http://gsgd.co.uk/sandbox/jquery/easing/
			easing: 'swing'
		};

		// apply serialScroll to the slider - we chose this plugin because it
		// supports// the indexed next and previous scroll along with hooking
		// in to our navigation.
		$('#slider').serialScroll(scrollOptions);

		// now apply localScroll to hook any other arbitrary links to trigger
		// the effect
		$.localScroll(scrollOptions);

		// finally, if the URL has a hash, move the slider in to position,
		// setting the duration to 1 because I don't want it to scroll in the
		// very first page load.  We don't always need this, but it ensures
		// the positioning is absolutely spot on when the pages loads.
		scrollOptions.duration = 1;
		$.localScroll.hash(scrollOptions);

	}

	if ($('#slider .scrollContainer2 > div').text() != '') {
		var $panels = $('#slider .scrollContainer2 > div');
		var $container = $('#slider .scrollContainer2');

		// if false, we'll float all the panels left and fix the width
		// of the container
		var horizontal = false;

		// float the panels left if we're going horizontal
		if (horizontal) {
			$panels.css({
				'float': 'left',
				'position': 'relative' // IE fix to ensure overflow is hidden
			});

			// calculate a new width for the container (so it holds all panels)
			$container.css('width', $panels[0].offsetWidth * $panels.length);
		}

		// collect the scroll object, at the same time apply the hidden overflow
		// to remove the default scrollbars that will appear
		var $scroll = $('#slider .scroll2').css('overflow', 'hidden');

		// apply our left + right buttons
//		$scroll.before('<img class="scrollButtons left" src="images/scroll_left.png" />').after('<img class="scrollButtons right" src="images/scroll_right.png" />');

		// handle nav selection
		function selectNav(){
			$(this).parents('ul:first').find('a').removeClass('selected').end().end().addClass('selected');
		}

		$('#slider .navigation').find('a').click(selectNav);

		// go find the navigation link that has this target and select the nav
		function trigger(data){
			var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
			selectNav.call(el);
		}

		if (window.location.hash) {
			trigger({
				id: window.location.hash.substr(1)
			});
		}
		else {
			$('ul.navigation a:first').click();
		}

		// offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset.  Try removing this to get a good idea of the effect
		var offset = parseInt((horizontal ? $container.css('paddingTop') : $container.css('paddingLeft')) ||
		0) *
		-1;


		var scrollOptions = {
			target: $scroll, // the element that has the overflow
			// can be a selector which will be relative to the target
			items: $panels,

			navigation: '.navigation a',

			// selectors are NOT relative to document, i.e. make sure they're unique
			prev: 'img.left',
			next: 'img.right',

			// allow the scroll effect to run both directions
			axis: 'xy',

			onAfter: trigger, // our final callback
			offset: offset,

			// duration of the sliding effect
			duration: 500,

			// easing - can be used with the easing plugin:
			// http://gsgd.co.uk/sandbox/jquery/easing/
			easing: 'swing'
		};

		// apply serialScroll to the slider - we chose this plugin because it
		// supports// the indexed next and previous scroll along with hooking
		// in to our navigation.
		$('#slider').serialScroll(scrollOptions);

		// now apply localScroll to hook any other arbitrary links to trigger
		// the effect
		$.localScroll(scrollOptions);

		// finally, if the URL has a hash, move the slider in to position,
		// setting the duration to 1 because I don't want it to scroll in the
		// very first page load.  We don't always need this, but it ensures
		// the positioning is absolutely spot on when the pages loads.
		scrollOptions.duration = 1;
		$.localScroll.hash(scrollOptions);

	}

	$(function() {
		$('.scroller').jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 16});
	});

	var firstShow = 0;
	$("span a.ajax").each(function(id) {

		var url_temp = $(this).attr("href");
		var title_temp_more = $(this).attr("title");
	    
		url_temp_more = "javascript:loadContent('"+url_temp+"?type=7');";
		$(this).attr("title",title_temp_more).attr("href",url_temp_more);

		if (firstShow == 0) {
			firstShow = 1;
			
		    if(HTTP_GET_VARS["showItem"]) {
		    	url_temp = 'index.php?id=' + HTTP_GET_VARS["showItem"] + '&';
		    } else {
		    	url_temp = url_temp + '?';
		    }
		    if(HTTP_GET_VARS["panelId"]) {
		    	jQuery($(".pagination .navigation a:eq(" + HTTP_GET_VARS["panelId"] + ")")).click();		    	
		    }
		    loadContent(url_temp+"type=7");
		}

	});
});
