$(function(){
	var naturalScroll = true;
	var menuOffset = 0;

	$(window).trigger('resize')
	$('#bigtext').bigtext();
	Cufon.replace('.bigtext-line0, .bigtext-line1, .bigtext-line2', {
		hover: true
	});
	Cufon.replace('h3, h4, h5, .intro, #social a', {
		hover: true
	})
	Cufon.replace('#player a, #player span', {
		hover:true
	})
	Cufon.CSS.ready(function() {
		menuOffset = $('#bigtext a:first').offset().top;
		$('#fadeout').css({
			height: $('#bigtext').outerHeight()
		})
	});
	$('#bigtext').css({
		position: 'absolute'
	})
	var zoeLeft = ($(window).width() / 2) - ($('#zoe').width() / 2)
	if($(window).scrollTop() > 0){
		$('#zoe').css({
			left: - zoeLeft -50
		})
	}
	$('#zoe').css({
		'margin-left': zoeLeft
	})
	$.address.externalChange(function(e){
		var targetName = e.pathNames[0]
		$('#bigtext a[href=#'+targetName+']').trigger('click')
	})
	$('body').bind('loop1', function(){
		var rand_opacity = Math.floor((70-30)*Math.random()) + 31;
		var rand_time = Math.floor((5000-3000)*Math.random()) + 3001;
		
		$('.zoe_2').animate({
			opacity: '0.'+rand_opacity
		}, rand_time, function(){
			$('body').trigger('loop1')
		})
	})
	$('body').bind('loop2', function(){
		var rand_opacity = Math.floor((99-40)*Math.random()) + 41;
		var rand_time = Math.floor((7000-3000)*Math.random()) + 3001;
				
		$('.zoe_1').animate({
			opacity: '0.'+rand_opacity
		}, rand_time, function(){
			$('body').trigger('loop2')
		})
	})
	
/*
	$('body').trigger('loop1')
	$('body').trigger('loop2')
*/
	$('#bigtext a').click(function(e){
		$(this).addClass('active').siblings('.active').removeClass('active')
		Cufon.replace('.bigtext-line0, .bigtext-line1, .bigtext-line2');
		var targetName = $(this).attr('href').split('#')[1];
		if($('#section_'+targetName).length){
			naturalScroll = false;
			$.scrollTo($('#section_'+targetName), 1000, {
				offset: - $('#bigtext a:first').height() * 1.2,
				onAfter: function(){
					naturalScroll = true;
				}
			})
		} else {
			$.scrollTo(0, 1000, {
				onAfter: function(){
					naturalScroll = true;
				}
			});
		}
		$.address.value(targetName)
		e.preventDefault();
	})
	$(window).scroll(function(){
		// move zoe
		if($(window).scrollTop() < zoeLeft + 50){
			$('#zoe').css({
				left: -$(window).scrollTop()
			})
		} else {
			$('#zoe').css({
				left: -zoeLeft - 50
			})
		}
		// keep menu up
		if($(window).scrollTop() > menuOffset){
			$('#bigtext, #fadeout').css({
				position: 'fixed',
				top: -menuOffset,
				left: 0
			})
		} else {
			$('#bigtext, #fadeout').css({
				position: 'absolute',
				top: 0,
				left: 0
			})
		}
		// push twitter down
		if($(window).scrollTop() < ($('#twitter').height() + 15)){
			$('#aside').css({
				bottom: -$(window).scrollTop() + 15
			})
		} else {
			$('#aside').css({
				bottom: -$('#twitter').height()
			})
		}
		if(naturalScroll){
			$('.section').each(function(){
				if($(this).offset().top > ($(window).scrollTop() - 100) && $(this).offset().top < ($(window).scrollTop() + 140)){
					var targetName = $(this).attr('id').split('section_')[1];
					if(!$('#bigtext a[href=#'+ targetName +']').is('.active')){
						$('#bigtext a[href=#'+ targetName +']').addClass('active').siblings('.active').removeClass('active');
						Cufon.replace('.bigtext-line0, .bigtext-line1, .bigtext-line2');
						$.address.value(targetName)
					}
				}
			})
		}
	})
	
	$('#members_links li:not(.active) a').live('mouseover', function(){
		$(this).stop().animate({'opacity': 1.0})
	}).live('mouseout', function(){
		$(this).stop().animate({'opacity': 0.4})
	}).live('click', function(e){
		$(this).parent().addClass('active').siblings('.active').removeClass('active').find('a').removeAttr('style')
		$('#members_content li').eq($(this).parent().index()).addClass('active').siblings('.active').removeClass('active')
		e.preventDefault()
	})
	$('#members_links li.active a').live('click', function(e){
		e.preventDefault()
	})
	$('.contact_link').click(function(e){
		naturalScroll = false;
		$.scrollTo($('.contact'), 1000, {
			offset: - $('#bigtext a:first').height() * 1.2,
			onAfter: function(){
				naturalScroll = true;
			}
		})
		e.preventDefault();
	})
});
$(window).resize(function(){
	$('#section_home').css({
		'height': $(window).height() + 20
	})
	if($(window).height() < $('#zoe').height()){
		$('#zoe').css({
			bottom: $(window).height() - $('#zoe').height()
		})
	}
	$('.section').each(function(){
		$(this).css({
			'min-height': $(window).height() - $('#fadeout').height()
		})
	})
})

$(document).ready(function(){

	var Playlist = function(instance, playlist, options) {
		var self = this;
		this.instance = instance; // String: To associate specific HTML with this playlist
		this.playlist = playlist; // Array of Objects: The playlist
		this.options = options; // Object: The jPlayer constructor options for this playlist

		this.current = 0;

		this.cssId = {
			jPlayer: "jquery_jplayer_",
			interface: "player",
			playlist: "playlist"
		};
		this.cssSelector = {
			play: '.play',
			pause: '.pause'
		};

		$.each(this.cssId, function(entity, id) {
			self.cssSelector[entity] = "#" + id + self.instance;
		});

		if(!this.options.cssSelectorAncestor) {
			this.options.cssSelectorAncestor = this.cssSelector.interface;
		}
		$('#player_cont').jPlayer(this.options);
		$(".prev").click(function() {
			self.playlistPrev();
			$(this).blur();
			return false;
		});

		$(".next").click(function() {
			self.playlistNext();
			$(this).blur();
			return false;
		});
	};

	Playlist.prototype = {
		displayPlaylist: function() {
			var self = this;
			$(this.cssSelector.playlist + " ul").empty();
			for (i=0; i < this.playlist.length; i++) {
				var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last'>" : "<li>";
				if(this.playlist[i].mp3){
					listItem += "<a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i +"' tabindex='1'>";
					listItem += "<span class='icon'></span><span class='play_text'>Play</span><span class='pause_text'>Pause</span> / ";
					listItem += this.playlist[i].name +"</a>";
				} else {
					listItem += this.playlist[i].name;
				}

				listItem += "</li>";

				// Associate playlist items with their media
				$(this.cssSelector.playlist + " ul").append(listItem);
				$(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {
					var index = $(this).data("index");
					if(self.current !== index) {
						self.playlistChange(index);
					} else if($('body').is('.playing')){
						$('#player_cont').jPlayer("pause");
					} else {
						$('#player_cont').jPlayer("play");
					}
					$(this).blur();
					return false;
				});

			}
		},
		playlistInit: function(autoplay) {
			if(autoplay) {
				this.playlistChange(this.current);
			} else {
				this.playlistConfig(this.current);
			}
		},
		playlistConfig: function(index) {
			$(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
			$(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
			this.current = index;
			$('.current_song').text(this.playlist[this.current].name)
			$('#player_cont').jPlayer("setMedia", this.playlist[this.current]);
			Cufon.replace('#player a, #player span')
		},
		playlistChange: function(index) {
			this.playlistConfig(index);
			$('#player_cont').jPlayer("play");
		},
		playlistNext: function() {
			findNext(this.current, this.playlist.length)
			function findNext(thisCurrent, thisPlaylistLength){
				index = (thisCurrent + 1 < thisPlaylistLength) ? thisCurrent + 1 : 0;
				if(!$('#playlist_item_'+index).length){
					findNext(index, thisPlaylistLength)
				}
			}
			this.playlistChange(index);
		},
		playlistPrev: function() {
			findPrev(this.current, this.playlist.length)
			function findPrev(thisCurrent, thisPlaylistLength){
				index = (thisCurrent - 1 >= 0) ? thisCurrent - 1 : thisPlaylistLength - 1;			
				if(!$('#playlist_item_'+index).length){
					findPrev(index, thisPlaylistLength)
				}
			}
			this.playlistChange(index);
		},
		playlistPlayPause: function() {
			if($('body').is('.playing')){
				$('#player_cont').jPlayer('pause')
			} else {
				$('#player_cont').jPlayer('play')				
			}
		}
	};
	// keyboard controls
		var captured = false;
		var isIE = navigator.appName.toLowerCase().indexOf("explorer") > -1;
		document.onkeydown = function(e){
			var ev = isIE?event:e;
			if(ev.charCode && ev.charCode == 32){
				audioPlaylist.playlistPlayPause();
				return false;
			}else{
				switch(ev.keyCode){
					case 32:
						audioPlaylist.playlistPlayPause();
						return false;
						break;
					case 39:
						audioPlaylist.playlistNext();
						debug.log('down?')
						break;
					case 37:
						audioPlaylist.playlistPrev();
						debug.log('up?')
						break;
				}
			}
		}

	var audioPlaylist = new Playlist("", [
		{
			name:"Dear Boss",
			mp3:"http://www.castelvandammequartet.com/wp-content/uploads/audio/Dear-Boss.mp3",
			oga:"http://www.castelvandammequartet.com/wp-content/uploads/audio/Dear-Boss.ogg"
		},
		{
			name: "Salzburg Syndrome"
		},
		{
			name:"Lou's Harbor",
			mp3:"http://www.castelvandammequartet.com/wp-content/uploads/audio/Lou_s-Harbor.mp3",
			oga:"http://www.castelvandammequartet.com/wp-content/uploads/audio/Lou_s-Harbor.ogg"
		},
		{
			name: "1937 Mules"
		},
		{
			name:"Zoid",
			mp3:"http://www.castelvandammequartet.com/wp-content/uploads/audio/Zoid.mp3",
			oga:"http://www.castelvandammequartet.com/wp-content/uploads/audio/Zoid.ogg"
		},
		{
			name:"Sandsong"
		},
		{
			name:"Auguste Gusteau"
		},
		{
			name:"Museum Bombers",
			mp3:"http://www.castelvandammequartet.com/wp-content/uploads/audio/Museum-Bombers.mp3",
			oga:"http://www.castelvandammequartet.com/wp-content/uploads/audio/Museum-Bombers.ogg"
		},
		{
			name:"Make Her Well"
		},
		{
			name:"Argentinian Freakshows"
		}
	], {
		ready: function() {
			audioPlaylist.displayPlaylist();
			audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
		},
		ended: function() {
			audioPlaylist.playlistNext();
		},
		play: function() {
			$(this).jPlayer("pauseOthers");
		},
		playing: function(){
			$('body').addClass('playing').removeClass('waiting')
		},
		waiting: function(){
			$('body').removeClass('playing').addClass('waiting')
		},
		pause: function() {
			$('body').removeClass('playing')
		},
		swfPath: "http://www.castelvandammequartet.com/wp-content/themes/castelvandamme/js",
		supplied: "oga, mp3",
		cssSelector: {
			play: '.play',
			pause: '.pause'
		},
		solution: "html, flash",
		errorAlerts: false,
		warningAlerts: false
	});
});


