var base_url = 'http://' + window.location.host;

$(function() {

	$('#navigation a').click(function() {
		$('#navigation a').address();
	});
	
	$('#subnavigation a').click(function() {
		$('#subnavigation a').address();
	});	
	
	$.address.init(function(event) {
		$('#navigation a:not([href^=http])').address();
		$('#subnavigation a:not([href^=http])').address();		
	}).change(function(event) {
		if (event.value != '/') {
			$('#content').hide();
			loadContent(event.value);
		}
		return false;
	});
	
	$('#main').hover(function() {
		if($('#navigation').hasClass('opened')) {
			$('#navigation').stop();
		} else {
			$('#navigation').animate({
				left: -30
			}, 'fast', function() {
				$('#navigation').removeClass('closed');
				$('#navigation').addClass('opened');
			});
		}
		$('#content').stop(true, true);
		/*
		if($('#content').hasClass('opened')) {
			$('#content').stop(true, true);
		} else {
			$('#content').delay(3000).fadeOut('slow', function() {
				$.address.value('/');
				$('#content').html('');
				$('#content').removeClass('opened');
			});
		}*/
	}, function() {
		$('#navigation').animate({
			left: 100
		}, 'fast', function() {
			$('#navigation').removeClass('opened');
			$('#navigation').addClass('closed');
			$('#content').delay(3000).fadeOut('slow', function() {
				$.address.value('/');
				$('#content').html('');				
			});
		});
	});
	
	$('#navigation').hover(function() {
		if($('#navigation').hasClass('opened')) {
			$('#navigation').stop();
		}
		$('#content').stop(true, true);
	}, function() {
		$('#navigation').animate({
			left: 100
		}, 'fast', function() {
			$('#navigation').removeClass('opened');
			$('#navigation').addClass('closed');
			$('#content').delay(3000).fadeOut('slow', function() {
				$.address.value('/');
				$('#content').html('');				
			});
		});
	});
	
	$('#subnavigation').hover(function() {
		$('#content').stop(true, true);
	}, function() {
		$('#content').delay(3000).fadeOut('slow', function() {
			$.address.value('/');
			$('#content').html('');
		});
	});
	
	$('#slider').cycle();
	$('#navigation').corner();
});

function loadContent(current) {
	$('#content').load(base_url + current + '/', function() {
		$('#content').show();
		$('.text').jScrollPane();
		if($('#navigation').hasClass('opened')) {
			$('#navigation').stop();
		} else {
			if ((current.indexOf('links') != -1) || (current.indexOf('impressum') != -1)) {
				console.log('hier');
			} else {
				$('#navigation').animate({
					left: -30
				}, 'fast', function() {
					$('#navigation').removeClass('closed');
					$('#navigation').addClass('opened');
				});
			}
		}
		if ((current.indexOf('musik') != -1)) {
			initmusic();
		}
	});
}

function initmusic() {
	var	my_jPlayer = $("#jquery_jplayer"),
		my_trackName = $("#jp_container .track-name"),
		my_playState = $("#jp_container .play-state"),
		my_extraPlayInfo = $("#jp_container .extra-play-info");

	var	opt_text_playing = "Gespielt wird";

	$.jPlayer.timeFormat.padMin = false;
	$.jPlayer.timeFormat.padSec = false;
	$.jPlayer.timeFormat.sepMin = ":";
	$.jPlayer.timeFormat.sepSec = " Min";

	my_jPlayer.jPlayer({
		timeupdate: function(event) {
			my_extraPlayInfo.text(parseInt(event.jPlayer.status.currentPercentAbsolute, 10) + "%");
		},
		play: function(event) {
			$("#track-status").show();
			my_playState.text(opt_text_playing);
		},
		pause: function(event) {
			$("#track-status").hide();
			my_playState.text('');
		},
		ended: function(event) {
			$("#track-status").hide();
			my_playState.text('');
		},
		swfPath: "js",
		cssSelectorAncestor: "#jp_container",
		supplied: "mp3",
		wmode: "window"
	});

	$("#jp_container .track").click(function(e) {
		if($(this).hasClass('playing')) {
			my_jPlayer.jPlayer("stop");
			$(this).removeClass('playing');
		} else {
			$('#jp_container a').removeClass('playing');
			my_jPlayer.jPlayer("setMedia", {
				mp3: $(this).attr("href")
			});
			my_jPlayer.jPlayer("play");
			$(this).addClass('playing');
		}
		my_trackName.text($(this).text());
		return false;
	});
}
