$(function(){
	$("#navigation ul.level_1 li").hover(function(){
		$(this).children("ul.level_2").toggle();
	});
	// zentrieren :rolleyes:
	$("#wrapper").css("margin-top", function() {
		if((window.innerHeight-575)/2-30 > 0) {
			return (window.innerHeight-575)/2-30;
		} else {
			return 20;
		}
	});
	if($("#gallery").length > 0) {
		var scroller = window.setInterval("scrollGallery(richtung, speed)", 10);
		$("#gallery a").click(function(){
			$("#mainimg img").attr("src", $(this).attr("href"));
			/* Mit überblendung später 
			href = $(this).attr("href");
			$("#mainimg").animate({opacity: 0} , { duration: 500 , complete: switchImg(href)})
			.animate({opacity: 1} , { duration: 500}); */
			return false;
		});
		/*
		function switchImg(url){
			$("#mainimg img").attr("src", url);
		}
		*/
		$("#gallery").css("top", "90px");
		$("#galleryfade").mouseenter(function() {
			$("#gallery").animate({top: 20}, {queue: false});
		}).mouseleave(function(){
			$("#gallery").animate({top: 90}, {queue: false});
		});
		$("#gallery").mousemove(function(e) {
			x = e.pageX - $("#gallery").offset().left;
			if (x > 435) {
				richtung = 'left';
				speed = 1;
			} else if (x < 335) {
				richtung = 'right';
				speed = 1;
			}
			if (x > 700 || x < 100) {
				speed = 5;
			}
			if (x > 335 && x < 435) {
				richtung = 'hold';
			}
		}).mouseout(function(){
			richtung = 'hold';
		});

		scrollzero = $("#scrolly").offset().left;
		
		$("div#container").append("<div id='previous'></div><div id='next'></div>");
		$("#previous, #next").hover(function() {
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
		});
		$("#previous").click(function(){
			if(currentpic > 1) {
				newpic = currentpic-1;
				newurl = $("#scrolly div:nth-child("+newpic+") a").attr("href");
				$("#mainimg img").attr("src", newurl);
				currentpic--;
			}
		});
		$("#next").click(function(){
			maxpic = $("#gallery img").size();
			if(currentpic < maxpic) {
				newpic = currentpic+1;
				newurl = $("#scrolly div:nth-child("+newpic+") a").attr("href");
				$("#mainimg img").attr("src", newurl);
				currentpic++;
				//$("#debug").show().append(", right: "+newpic +" - "+newurl);
			}
		});
		
	}
	//bgcolorselect
	$("#colorselect img").each(function(){
		$(this).click(function(){
			$("body").css("background-color","#"+$(this).attr("class"));
			$.cookie('bgColor', $(this).attr("class"));
		});
	});
	if ($.cookie('bgColor')) {
		$("body").css("background-color","#"+$.cookie('bgColor'));
	}
});

richtung = "hold";
speed = 1;
currentpic = 1;

function scrollGallery(richtung, speed) {
	// abfrage nach Position und richtung
	gallerywidth = $("#gallery img").size() * 94 - 800;
	if (richtung == 'left' && $("#scrolly").offset().left > scrollzero + gallerywidth*-1) {
		$("#scrolly").animate({left: '-='+speed}, 0);
	} else if(richtung == 'right' && $("#scrolly").offset().left < scrollzero) {
		$("#scrolly").animate({left: '+='+speed}, 0);
	}
	$("#debug").html("breite: "+gallerywidth+", richtung: "+richtung+ ", scroller-x: "+$("#scrolly").offset().left);

}
