$(function() {
	$('nav a').hover(function() {$(this).addClass('hover');},function() {$(this).removeClass('hover');});
		   
	function checkHeight() {
		if($(window).height() < 600) {
			$("div.outer").css("marginTop", -59);	
		} else {
			$("div.outer").css("marginTop", 0);			
		}; 
	};
	
	$(window).resize(checkHeight);	
	checkHeight();

		   
	$('a.video').click(function(e) {
		e.preventDefault();
		videoPlayer.playVideo(this.href, this.title);
	});
	if(window.location.hash == "#video") {
		$('a.video:eq(0)').trigger("click");
	};
	
	var bodyClass = document.body.className;
	
	switch (bodyClass) {
		case "HomePage":
			if($.flash.available) {
				$("#deko").html('').css("top", "20px").flash({
						swf: '/themes/hempen/assets/ani2.swf',
						height: '563',
						width: '405',
						params: {
							 wmode:'transparent'
						}});
				$("#statue").html('').flash({
						swf: '/themes/hempen/assets/100315_hempen_statue_nico.swf',
						height: '380',
						width: '444',
						params: {
							 wmode:'transparent'
						}});			
			}		
			break;
		case "GalleryPage":
			if($.flash.available) {
				$("div.content").html('').css('margin', 0).flash({
					swf: '/themes/hempen/assets/100315_hempen_bildergalerie_nico.swf',
					height: '487',
					width: '840'});			
			}			
			break;	
		case "IndicationPage":
			hNavigation.init();
			break;
	}
});

var hNavigation = {
	hNav: null,
	hNavWidth: 0,
	navItems: null,
	scrollBox: null,
	totalWidth: 0,
	mousePos: null,
	init: function() {
		this.hNav = $('div.hNav');
		this.hNavWidth = this.hNav.width();
		this.navItems = this.hNav.find("ul li");
		
		var size = 0;
		this.navItems.each(function() {
			size += $(this).outerWidth(true);
		});
		
		this.totalWidth = size;
		this.scrollBox = this.hNav.find("ul").width(size);
		
		setInterval(hNavigation.scrollHandler, 50);
		
		var pos = - this.navItems.find("a.current").position().left + 35
		hNavigation.moveTo(-1, pos);
		
		this.hNav.mousemove(hNavigation.scrollMouseMove);
	},
	scrollMouseMove: function(event) {
		hNavigation.mousePos = event.pageX - hNavigation.hNav.offset().left;
	},
	scrollHandler: function() {
	if(!hNavigation.mousePos) return;
	 // Richtung?
	 var sectionWidth = hNavigation.hNavWidth / 8;
	 var offset = 0;

	 if(hNavigation.mousePos < sectionWidth) {
	 offset = 10;
	 } else if(hNavigation.mousePos < (sectionWidth *2)) {
	 offset = 6;
	 } else if(hNavigation.mousePos < (sectionWidth *3)) {
	 offset = 4;
	 } else if(hNavigation.mousePos < (sectionWidth *4)) {
	 offset = 1;
	 } else if(hNavigation.mousePos < (sectionWidth *5)) {
	 offset = -1;
	 } else if(hNavigation.mousePos < (sectionWidth *6)) {
	 offset = -4;
	 } else if(hNavigation.mousePos < (sectionWidth *7)) {
	 offset = -6;
	 } else {
	 offset = -10;
	 }

	 var pos = hNavigation.scrollBox.position().left + offset;
	 hNavigation.moveTo(offset, pos);
 },
 moveTo: function(offset, pos) {
	 if(offset > 0) {
		 if((pos) > 0) {
			pos = 0;
		 };
	 } else {
		 if((pos + hNavigation.scrollBox.outerWidth(true)) < hNavigation.hNavWidth) {
			pos = hNavigation.hNavWidth - hNavigation.scrollBox.outerWidth(true);
		 };
	 }
 	hNavigation.scrollBox.css("left", pos + "px");	 
 }
};

var videoPlayer = {
	template: '<div id="videoPlayer"><div class="bg"></div><div class="popup"><div class="stage"></div><a class="close arrow">Video schlie&szlig;en</a></div></div>',
	innerTamplate: '<br /><br /><h1>Flash Player erforderlich</h1><br /><p>Um dieses Video abspielen zu k&ouml;nnen, ben&ouml;tigen Sie einen aktuellen Adobe Flash Player:<br /><br /><a href="http://get.adobe.com/flashplayer/" class="arrow" target="_blank">Flash installieren</a></p>',
	htmlRef: null,
	stageRef: null,
	playVideo: function(videoUrl, title) {
		videoPlayer.showPlayer();
		if($.flash.available) {
		videoPlayer.stageRef.html('').flash({
					swf: '/themes/hempen/assets/player.swf',
					height: '360',
					width: '640',
					hasVersion: 9,
					params: {
						allowfullscreen: 'true',
						flashvars: {   file: videoUrl,   title: title,   autostart: 'true', controlbar: 'over'  }
					}});
		}
	},
	hidePlayer: function() {
		videoPlayer.htmlRef.fadeOut('fast');
		videoPlayer.stageRef.html('');
	},
	showPlayer: function() {
		if(!videoPlayer.htmlRef) {
			videoPlayer.htmlRef = $(videoPlayer.template).appendTo(document.body);
			videoPlayer.htmlRef.find("div.bg, a.close").click(videoPlayer.hidePlayer);
		}
		videoPlayer.stageRef = videoPlayer.htmlRef.find("div.stage")
								.html(videoPlayer.innerTamplate);
		videoPlayer.htmlRef.fadeIn('fast');
	}
};
