jQuery(document).ready(function() {
	if ( jQuery("table.blog.accordion:first").length ) {
		addAccordionFunctionality();
	}
	if ( jQuery(".photos dd:first").length ) {
		jQuery(".photos dd a").fancybox({});
	}
	if ( jQuery(".kausgallery a").length ) {
		addSliderFunctionality();
		jQuery(".kausgallery a").fancybox({});
	}
	/*if ( document.location.hash.length && document.location.hash == "#details" ) {
		var targetOffset = jQuery("ul.entry.menu:first").offset().top;
		jQuery("html, body").animate({scrollTop: targetOffset}, 1000);
	}*/
});


function addAccordionFunctionality() {
	// make all headings closed
	jQuery(".contentheading.accordion").addClass("closed");
	// close all bodies
	jQuery(".accordion .componentitem").each(function(i, item) {
		if (jQuery(this).find(".body:first").length)
			jQuery(this).find(".body:first").hide();
		else
			jQuery(this).find(".contentbody").hide();
		jQuery(this).find(".contentreadmore").hide();
	});

	// bind the click event on heading
	jQuery(".contentheading.accordion").click(function() {
		var $header = jQuery(this);
		var $body = $header.parent().parent().parent().nextAll(".contentpaneopen.accordion:first");
		if ( $header.hasClass("closed") ) {
			// close the other, open elements
			jQuery(".contentheading.accordion:not(.closed)").each(function(i, item) {
				var $item = jQuery(item);
				accordionClose($item, $item.parent().parent().parent().nextAll(".contentpaneopen.accordion:first"));
			});
			// open the clicked element
			accordionOpen($header, $body);
		}
		else {
			// simply close the already open, clicked element
			accordionClose($header, $body);
		}
	});
}

function accordionClose($header, $body) {
	if ( $body.find(".body:first").length ) {
		$header.addClass("closed");
		$body.find(".body:first").hide(400);
		$body.find(".contentreadmore:first").hide();
	}
	else {
		$header.addClass("closed");
		$body.find(".contentbody:first").hide(400);
		$body.find(".contentreadmore:first").hide();
	}
}

function accordionOpen($header, $body) {
	if ( $body.find(".body:first").length ) {
		$header.removeClass("closed");
		$body.find(".body:first").show(400);
		$body.find(".contentreadmore:first").show();
	}
	else {
		$header.removeClass("closed");
		$body.find(".contentbody:first").show(400);
		$body.find(".contentreadmore:first").show();
	}
}


var sliderItemWidth = 168;
var sliderNrVisible = 4;
var sliderIsAnimating = false;
function addSliderFunctionality() {
	if ( jQuery(".kausgallery a").length > sliderNrVisible ) {
		jQuery(".kausgallery span.left").click(function() {
			sliderPrev(jQuery(this));
		});
		jQuery(".kausgallery span.right").click(function() {
			sliderNext(jQuery(this));
		});
	}
	else {
		jQuery(".kausgallery span.left").addClass("dsbl");
		jQuery(".kausgallery span.right").addClass("dsbl");
	}
}


function sliderPrev(obj) {
	if (sliderIsAnimating) return false;

	var $ul = jQuery(obj).nextAll("div:first").children("ul:first");
	var curLeft = Math.round($ul.position().left / sliderItemWidth) * sliderItemWidth; // IE correction
	if (-curLeft >= sliderItemWidth) {
		sliderIsAnimating = true;
		var newLeft = curLeft + sliderItemWidth;
		jQuery(".kausgallery span.right").removeClass("dsbl");
		if (newLeft >= 0) {
			jQuery(".kausgallery span.left").addClass("dsbl");
		}
		$ul.animate({left: newLeft}, 400, function() {
			sliderIsAnimating = false;
		});
	}
	return false;
}


function sliderNext(obj) {
	if (sliderIsAnimating) return false;

	var $ul = jQuery(obj).nextAll("div:first").children("ul:first");
	var curLeft = Math.round($ul.position().left / sliderItemWidth) * sliderItemWidth; // IE correction
	var maxLeft = ($ul.children("li").length - 2 - sliderNrVisible) * sliderItemWidth;
	if (-curLeft <= maxLeft) {
		sliderIsAnimating = true;
		var newLeft = curLeft - sliderItemWidth;
		jQuery(".kausgallery span.left").removeClass("dsbl");
		if (-newLeft > maxLeft) {
			jQuery(".kausgallery span.right").addClass("dsbl");
		}
		$ul.animate({left: newLeft}, 400, function() {
			sliderIsAnimating = false;
		});
	}
	return false;
}
