﻿$(document).ready(function() {

	$(".input").each(function () {
		$(this).val($(this).attr("title"));
		if ($.trim($(this).val()) == "")
			$(this).val($(this).attr("title"));
		$(this).focus(function () {
			if ($(this).val() == $(this).attr("title")) $(this).val("");
		})
			.blur(function () {
				if ($.trim($(this).val()) == "")
					$(this).val($(this).attr("title"));
			});
		});

	$('.email').defuscate();
		
	jQuery('#mycarousel').jcarousel({
		wrap: 'circular',
		auto: 5,
		scroll: 5,
		initCallback: mycarousel_initCallback,
		easing: 'swing',
		animation: 1600

	});
	if (!$.browser.msie) {
		var container = $("#chromepagewrapper");
		container.hide();
		container.fadeIn(1000);
		$(".fadeLink").live('click', function(e) {
			//e.preventDefault();
			var link = this;
			container.fadeOut(1000, function() {
				document.location = link.href;
			});
		});
	}
	var $menu = $("#menu");
	var $menuLinks = $("#menu a.scrollable");
	$menuLinks.click(scrollPage);

	$("#footer_sitemap a.scrollable").click(scrollPage);
	


	setInterval(setRightNavElement, 100);

	//if we enter a URL containing a hash, make it active and scroll to it
	var $active = $("#menu a[href='" + window.location.hash + "']");
	$active.addClass("active");
	$(window).load(function() {
		$active.click();
	});

	$.cookie("lastIndex", null);


	$(".customerimage_start").hover(
		 function() {
		 	this.src = this.src.replace("_rgb_", "_sw_");
		 },
		 function() {
		 	this.src = this.src.replace("_sw_", "_rgb_");
		 }
		);

});

function mycarousel_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

function scrollPage(e) {
	e.preventDefault(e);
	var $menuLinks = $("#menu a");
	$menuLinks.removeClass("active");
	e.target.className += "active";
	var whereTo = $(this).attr("href");
	var targetLink = whereTo.substring(1); //remove hash

	//		$("#" + targetLink + " .sectionheadertitle > h1").addClass("active");
	if (targetLink != "impressum") {
		var offset = parseInt($("#headercontent").css("height"));
//		if (targetLink != "kontact") {
//			var pos = $("#" + targetLink).position().top - 169;
//		}
//		else
//			var pos = $("#" + targetLink).position().top;

		$.scrollTo($("#" + targetLink), 1000, { axis: 'y', easing: 'swing',
			onAfter: function(whereTo) {
				setTimeout(function() {
					window.location.hash = targetLink;
					hash = window.location.hash;
				}, 100);
			}
		});
	}

}

function setRightNavElement() {
	var $menuLinks = $("#menu a");

	for (var i = 0; i < $menuLinks.length - 1; i++) {

		var lasttarget = $menuLinks[i];
		var lasttargetwhereTo = $(lasttarget).attr("href");
		var lasttargetwhereToLink = lasttargetwhereTo.substring(1);
		var actPos = $(window).scrollTop();
		var lastMenuPoint = $("#" + lasttargetwhereToLink).offset().top;

		if (i + 1 < $menuLinks.length - 1) {
			var nexttarget = $menuLinks[i + 1];
			var nexttargetwhereTo = $(nexttarget).attr("href");
			var nexttargetwhereToLink = nexttargetwhereTo.substring(1);

			var nextMenuPoint = $("#" + nexttargetwhereToLink).offset().top;

			if ((actPos >= lastMenuPoint) && (actPos < nextMenuPoint)) {
				var $menuLinks = $("#menu a");
				$menuLinks.removeClass("active");

				//					$(".sectionheadertitle h1").removeClass("active");

				$("#menu a[href='#" + lasttargetwhereToLink + "']").addClass("active");

				//					$("#" + lasttargetwhereToLink + " .sectionheadertitle > h1").addClass("active");

				break;
			}

		}
		else {
			$menuLinks.removeClass("active");
			//				$(".sectionheadertitle h1").removeClass("active");
			$("#menu a[href='#" + lasttargetwhereToLink + "']").addClass("active");
			//				$("#" + lasttargetwhereToLink + " .sectionheadertitle > h1").addClass("active");
			break;
		}
	}
}
