$(document).ready(function() {
	// Print
	$("#command-buttons a.print").click(function() {
		window.print();
		return false;
	});

	// Toggle "tip a friend"
	$("#tipafriend").hide();
	$("#command-buttons a.tipafriend, #tip_cancel a").click(function() {
		$("#tipafriend").toggle();
		return false;
	});

	// Language menu
	$("#language").click(function() {
		if ($(this).hasClass("expanded")) {
			$("#language-menu").hide();
			$(this).removeClass("expanded");
		} else {
			$("#language-menu").show();
			$(this).addClass("expanded");
		}
		return false;
	});

	// Startpage slider
	$("#startslider .pager a").click(function() {
		sliderSwitch($(this).closest("li"));
		$(document).stopTime("slidertimer");
		return false;
	});

	// Auto-rotate startpage slides
	$(document).everyTime(5000, "slidertimer", function(i) {
		var activate = $("#startslider .pager li.active").next("li");
		if (activate.length == 0)
			activate = $("#startslider .pager li:first");
		sliderSwitch(activate);
	});

	// Startpage ticker
	$("#newsticker li").css("opacity", 0).css("display", "list-item");
	toggleTicker(0);

	// Startpage tabbox
	$("#tabbox .tabs li").click(function() {
		var index = $(this).prevAll().length;
		var count = $(this).siblings().length;
		$("#tabbox .tabs li").attr("class", "");
		$(this).addClass("active");
		$("#tabbox .tabs li:first").addClass("first" + (index == 0 ? "-active" : ""));
		$("#tabbox .tabs li:last").addClass("last" + (index == count ? "-active" : ""));
		$("#tabbox .tabs li:eq(" + (index - 1) + ")").addClass("beforeactive");
		$("#tabbox .tabcontents .tab").hide();
		$("#tabbox .tabcontents ." + $(this).attr("id")).show();
	});
});

function sliderSwitch(slideItemButton) {
	var index = slideItemButton.prevAll().length;
	var slideWidth = 940;
	var scrollPos = -1 * index * slideWidth;
	$("#startslider .slides").animate({ left: scrollPos }, { queue: false, duration: 500 });
	$("#startslider .pager li").removeClass("active");
	slideItemButton.addClass("active");
}

function toggleTicker(index) {
	$("#newsticker li:not(:eq(" + index + "))").animate({ "opacity": 0 }, function() { $(this).hide(); });
	$("#newsticker li:eq(" + index + ")").show().animate({ "opacity": "1" });
	var tickerCount = $("#newsticker li").length;
	var nextIndex = index + 1;
	if (nextIndex >= tickerCount)
		nextIndex = 0;
	setTimeout(function() { toggleTicker(nextIndex); }, 4000);
}
