$(document).ready(function() {
	//global variables
	var winHeight = $(window).height();
		winWidth = $(window).width();
		objWidth = $(".circle").width() + 2; //extra 2 is for border width
		objHeight = $(".circle").height() + 2;
		baseMaxTime = Math.floor((winHeight * winWidth)/100)+3000; //calculated max time for circle movement
		baseMinTime = Math.floor((winHeight * winWidth)/100)+1000; //calculated min time for circle movement
		maxTime = baseMaxTime;
		minTime = baseMinTime;
		distanceMax = 150; //variable for tracking listening distance of cursor
		selectedCount = 0; //variable for counting number of selected audio tracks
		beginAnimationCount = 1; //variable for tracking the number of circles in animation
		selectedQueue = 0; //variable for tracking which item in the queue is selected
		queueIsPlaying = 0; //variable to track if queue is playing
		settingsOpen = 1; //tracks whether or not a popup window is open
		initialized = 0; //tracks whether or not the piece has been started
	
	//arrays for audio circles - (filename, track name, artist, category, if used)
	var audioArr = [
		["acshutdown","AC Shutdown","John Barber","ambience",0],
		//["armstrongmoon","Armstrong on the Moon","John Barber","vocals",0],
		//["ball-bok-karawane","Ball 1","John Barber","vocals",0],
		//["ball-osmond-karawane","Ball 2","John Barber","vocals",0],
		//["coughsniff","Cough+Sniff","John Barber","baseline",0],
		//["abbeyroadsoundscape","Abbey Road Soundscape","John Barber","ambience",0],
		//["airtrafficcontrollers","Air Traffic Controllers","John Barber","ambience",0],
		//["amenairraid","Amen Air Raid","John Barber","vocals",0],
		//["another-cougar-first-down","Cougar First Down","John Barber","baseline",0],
		//["arrivals","Arrivals","John Barber","vocals",0],
		["asheville-hotel","Asheville Hotel","John Barber","ambience",0],
		//["asheville-soundwalk","Asheville Soundwalk","John Barber","ambience",0],
		["blackhelicopter","Black Helicopter","John Barber","baseline",0],
		["carrally","Car Rally","John Barber","baseline",0],
		["catseating","Cats Eating","John Barber","ambience",0],
		//["charingcrossstation","Charing Cross Station","John Barber","vocals",0],
		["chipper","Chipper","John Barber","baseline",0],
		//["cohoferry-redux","Coho Ferry - Redux","John Barber","baseline",0],
		["copenhagensoundscape1","Copenhagen Soundscape 1","John Barber","ambience",0],
		["copenhagensoundscape2","Copenhagen Soundscape 2","John Barber","ambience",0],
		["copenhagensoundscape3","Copenhagen Soundscape 3","John Barber","ambience",0],
		["cruising","Cruising","John Barber","baseline",0],
		["curlew-soundscape","Curlew Soundscape","John Barber","ambience",0],
		["dancewithtweekswhistles","Dance with Tweeks Whistles","John Barber","baseline",0],
		//["darkness","Darkness","John Barber","ambience",0],
		//["dhsi2014-cohoferry","DHSI 2014 - Coho Ferry","John Barber","ambience",0],
		//["dhsi2014-courseartifact","DHSI 2014 - Course Artifact","John Barber","baseline",0],
		//["dhsi2014-firstday","DHSI 2014 - First Day","John Barber","baseline",0],
		//["dhsi2014-siemens","DHSI 2014 - Siemens","John Barber","vocals",0],
		//["doowapweatherreport","Doowap Weather Report","John Barber","vocals",0],
		["eastwind911","Eastwind 911","John Barber","vocals",0],
		["elo2014-milwaukeesounds","ELO 2014 - Milwaukee Sounds","John Barber","baseline",0],
		["estershortclock","Ester Short Clock","John Barber","ambience",0],
		//["fakto","Fakto","John Barber","vocals",0],
		["fortbell","Fort Bell","John Barber","baseline",0],
		["frogchorus","Frog Chorus","John Barber","vocals",0],
		//["ghoststory","Ghost Story","John Barber","vocals",0],
		["goldsmithssoundscape","Goldsmith Soundscape","John Barber","drums",0],
		["granularambience","Granular Ambience","John Barber","drums",0],
		//["interlude","Interlude","John Barber","baseline",0],
		["jfbresume","JFB Resume","John Barber","vocals",0],
		["july4","July 4","John Barber","drums",0],
		["langleytrain","Langley Train","John Barber","drums",0],
		["makerfaire","Maker Faire","John Barber","vocals",0],
		["makermusic","Maker Music","John Barber","baseline",0],
		//["mindyourstep","Mind your Step","John Barber","drums",0],
		["newcrossstation","New Cross Station","John Barber","drums",0],
		//["opening","Opening","John Barber","drums",0],
		//["palaceguards","Palace Guards","John Barber","drums",0],
		["phonemessage","Phone Message","John Barber","vocals",0],
		["primemeridian","Prime Meridian","John Barber","drums",0],
		["printer","Printer","John Barber","baseline",0],
		["publastcall","Pub Last Call","John Barber","vocals",0],
		["radiogaga","Radio Gaga","John Barber","drums",0],
		["rainchain","Rain Chain","John Barber","drums",0],
		["rainonskylight","Rain on Skylight","John Barber","ambience",0],
		["rainonvent","Rain on Vent","John Barber","ambience",0],
		["santafebells","Santa Fe Bells","John Barber","drums",0],
		["santafethunderstorm","Santa Fe Thunder Storm","John Barber","drums",0],
		["santafewalk","Santa Fe Walk","John Barber","drums",0],
		//["savage-guidelost","Savage Guide Lost","John Barber","drums",0],
		["singingbowls","Singing Bowls","John Barber","drums",0],
		//["speakerscorner","Speaker's Corner","John Barber","vocals",0],
		["steelyardpassage","Steelyard Passage","John Barber","drums",0],
		["stevensontrain","Stevenson Train","John Barber","drums",0],
		["sundaymorningnoise","Sunday Morning Noise","John Barber","baseline",0],
		["symphonyfortractors","Symphony for Tractors","John Barber","ambience",0],
		["tinysounds","Tiny Sounds","John Barber","ambience",0],
		["underground","Underground","John Barber","drums",0],
		["undermilleniumbridge","Under Millenium Bridge","John Barber","vocals",0],
		["voiceoffair","Voice Of the Fair","John Barber","baseline",0],
		["voicesandspaces","Voices and Spaces","John Barber","vocals",0]
	];
	
	var usedAudioCount = audioArr.length; //for tracking the number of used sounds
	var categoryArr = ["drums","baseline","vocals","ambience"]; //array for list of sound categories
	var queueArr = []; //array for storing queued audio variables
	var listenArr = []; //array for storing audio that mousehover listens to
	
	
	
	
	

	
	//arrays for color schemes
	var colorsArr = [
	["24A385","FD7400","FFE11A","BEDB39","007396"],
	["D92525","F25C05","F29F05","88A61B","0E3D59"],
	];
	
	//fade in when piece is loaded
	$("#fadescreen").fadeOut(800);
	
	//edges of window
	var maxValY = winHeight - objHeight;
	var maxValX = winWidth - objWidth;
	var minValY = 0;
	var minValX = 0;
	
	//calculate starting x of circles
	var circleStartX = (winWidth / 3) - (objWidth / 2);
	$(".circle").css("left",circleStartX+"px");
	var circleStartY = parseInt($(".circle").css("bottom"));
	
	//set height and width of skyline div, generate calculated number of lights, and hide off-screen animated lights
	$("#skyline").css({'height' : winHeight,'width' : winWidth});
	
	var lightsWidth = Math.ceil(winWidth / 960);
	$("#light-container").html("");
	for (var z=1; z<=(lightsWidth*11); z++) {
		$("#light-container").append('<div id="light'+z+'" class="skyline-light"></div>');
	}
	
	$(".skyline-light").each(function( index ) {
		var tempLeft = parseInt($(this).css("left")) + parseInt($(this).css("width"));
		if (tempLeft < winWidth) {
			$(this).css("display","block");
		}
	});
	
	
	
	
	
	
	//**********************************
	//WINDOW RESIZE FUNCTIONS - recalculate edges, circle movement time, etc.
	//**********************************
	$( window ).resize(function() {
		winHeight = $(window).height();
		winWidth = $(window).width();
		objWidth = $(".circle").width();
		objHeight = $(".circle").height();
		maxValY = winHeight - objHeight;
		maxValX = winWidth - objWidth;
		
		//set height and width of skyline div, calculate number of lights, show/hide animated lights
		$("#skyline").css({'height' : winHeight,'width' : winWidth});
		
		var lightsWidth = Math.ceil(winWidth / 960);
		$("#light-container").html("");
		for (var z=1; z<=(lightsWidth*11); z++) {
			$("#light-container").append('<div id="light'+z+'" class="skyline-light"></div>');
		}
		
		$(".skyline-light").each(function( index ) {
			var tempLeft = parseInt($(this).css("left")) + parseInt($(this).css("width"));
		
			if (tempLeft > winWidth) {
				$(this).css("display","none");
			}
			else {
				$(this).css("display","block");
			}
		});
		
		//maxTime = Math.floor((winHeight * winWidth)/100)+3000;
		//minTime = Math.floor((winHeight * winWidth)/100)+1000;
	});
	
	

	//**********************************
	//GENERATE RANDOM INTEGER FUNCTION - function for randomly choosing integers
	//**********************************
	function getRandomInt(min, max) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}
	
	
	//**********************************
	//CHOOSE SIDE FUNCTION - determine which side the circle is touching and which side to touch next
	//**********************************
	function chooseSide(objectName) {
		var x = 0;
		var tempNum = getRandomInt(1,3);
	
		//if at max y
		if ($("#"+objectName).css("bottom") == maxValY+"px") {
			if (tempNum == 1) {
				x = 2;
			}
			else if (tempNum == 2) {
				x = 3;
			}
			else if (tempNum == 3) {
				x = 4;
			}
		}
		
		//if at max x
		else if ($("#"+objectName).css("left") == maxValX+"px") {
			if (tempNum == 1) {
				x = 1;
			}
			else if (tempNum == 2) {
				x = 3;
			}
			else if (tempNum == 3) {
				x = 4;
			}
		}
		
		//if at min y
		else if ($("#"+objectName).css("bottom") == "0px") {
			if (tempNum == 1) {
				x = 1;
			}
			else if (tempNum == 2) {
				x = 2;
			}
			else if (tempNum == 3) {
				x = 4;
			}
		}
		
		//if at min x
		else if ($("#"+objectName).css("left") == "0px") {
			if (tempNum == 1) {
				x = 1;
			}
			else if (tempNum == 2) {
				x = 2;
			}
			else if (tempNum == 3) {
				x = 3;
			}
		}

		return x;
	}
	
	
	//**********************************
	//STANDARD CIRCLE ANIMATION - for bouncing randomization and single animations
	//**********************************
	function circleAnimation(circleName,start) {
		var tempX = 0;
		var tempY = 0;
		var whichSide = 0;
		var tempTime = getRandomInt(minTime,maxTime); //random time based on size of screen
		
		//check for animation type - 1 is new single random, else is for bouncing
		if (start == 1) {
			whichSide = getRandomInt(1,4);
		}
		else {
			whichSide = chooseSide(circleName);
		}
	
		//choose numbers for side bouncing
		if (whichSide == 1) {
			tempX = getRandomInt(0,maxValX);
			tempY = maxValY;
		}
		else if (whichSide == 2) {
			tempX = maxValX;
			tempY = getRandomInt(0,maxValY);
		}
		else if (whichSide == 3) {
			tempX = getRandomInt(0,maxValX);
			tempY = 0;
		}
		else if (whichSide == 4) {
			tempX = 0;
			tempY = getRandomInt(0,maxValY);
		}
		
		$("#"+circleName).animate({
				left: tempX+"px",
				bottom: tempY+"px"
			}, tempTime, "linear", function() {
			
			//check to see if currently playing
			if ($("#"+circleName).hasClass("selected") == false) {
				
				//check for recast - if not, continue animation
				// if ($("#"+circleName).data("audio-reset") == 1) {
// 				
// 					//fadeout circle before resetting position and audio choice
// 					$("#"+circleName).fadeOut(500);
// 					
// 					setTimeout(function(){
// 						randomizeAudioChoice(circleName.slice(3));
// 						$("#"+circleName).css({'bottom' : circleStartY,'left' : circleStartX});
// 						$("#"+circleName).css("display","block").data("audio-reset",0);
// 						circleAnimation(circleName,1);	
// 					}, 1000);
// 					
// 				}
// 				else {
// 					circleAnimation(circleName,0);
// 				}
				circleAnimation(circleName,0);
			}
			
		});
	}
	
	
	
	//**********************************
	//BEGINNING ANIMATION FUNCTION - calculate which direction the circles should head in
	//**********************************
	function beginningAnimation(circleName) {
		var tempX = 0;
		var tempY = 0;
		
		//case check four all eight circles in wave
		if (beginAnimationCount == 1) {
			tempX = 0;
			tempY = maxValY;
		}
		else if (beginAnimationCount == 2) {
			tempX = maxValX / 2;
			tempY = maxValY;
		}
		else if (beginAnimationCount == 3) {
			tempX = maxValX;
			tempY = maxValY;
		}
		else if (beginAnimationCount == 4) {
			tempX = maxValX;
			tempY = maxValY / 2;
		}
		else if (beginAnimationCount == 5) {
			tempX = maxValX;
			tempY = 0;
		}
		else if (beginAnimationCount == 6) {
			tempX = maxValX / 2;
			tempY = 0;
		}
		else if (beginAnimationCount == 7) {
			tempX = 0;
			tempY = 0;
		}
		else if (beginAnimationCount == 8) {
			tempX = 0;
			tempY = maxValY / 2;
		}
		
		
		//delay corners for circle effect
		if (beginAnimationCount == 1 || beginAnimationCount == 3 || beginAnimationCount == 5 || beginAnimationCount == 7) {
			$("#"+circleName).css("display","block");
			beginAnimationCount++;
			$("#"+circleName).animate({
					left: tempX+"px",
					bottom: tempY+"px"
				}, baseMaxTime/3+1500, "linear", function() {
			
				//check to see if currently playing
				if ($("#"+circleName).hasClass("selected") == false) {
					circleAnimation(circleName,0);
				}
			
			});
		}
		else {
			$("#"+circleName).css("display","block");
			beginAnimationCount++;
			$("#"+circleName).animate({
					left: tempX+"px",
					bottom: tempY+"px"
				}, baseMaxTime/3, "linear", function() {
			
				//check to see if currently playing
				if ($("#"+circleName).hasClass("selected") == false) {
					circleAnimation(circleName,0);
				}
			
			});
		}
		
	}
	
	
	
	//**********************************
	//INITIALIZE AUDIO FUNCTION - reset audio array checks, populate circles with audio, beginning wave animation
	//**********************************
	function initializeAudio() {
	
		//reset all of the audio array's used checks
		for (var i=0;i<audioArr.length;i++) {
			audioArr[i][4] = 0;
		}
	
		//delay waves of audio circles
		setTimeout(function(){
			for (var i=1;i<=8;i++) {
				//reset classes and animations
				randomizeAudioChoice(i);
				beginningAnimation("cir"+i,1);
			}
		}, 3000);
		
		setTimeout(function(){
			beginAnimationCount = 1;
			for (var i=9;i<=16;i++) {
				//reset classes and animations
				randomizeAudioChoice(i);
				beginningAnimation("cir"+i,1);
			}
		}, 33000);
		
		setTimeout(function(){
			beginAnimationCount = 1;
			for (var i=17;i<=24;i++) {
				//reset classes and animations
				randomizeAudioChoice(i);
				beginningAnimation("cir"+i,1);
			}
		}, 78000);
		
		/*setTimeout(function(){
			beginAnimationCount = 1;
			for (var i=25;i<=32;i++) {
				//reset classes and animations
				randomizeAudioChoice(i);
				beginningAnimation("cir"+i,1);
			}
		}, 18000);*/
	}
	
	
	//**********************************
	//RANDOMIZE AUDIO FUNCTION - for choosing random array entry from remaining unused entries
	//**********************************
	function randomizeAudioChoice(i) {
		//randomize audio selection from array
		tempCheck = 0;
		while (tempCheck == 0) {
			tempAudio = getRandomInt(0,audioArr.length-1);
			if (audioArr[tempAudio][4] == 0) {
				//wipe all classes from circle div
				for (var x=0;x<categoryArr.length;x++) {
					$("#cir"+i).removeClass(categoryArr[x]);
				}
				
				//set data attributes from audio array
				$("#cir"+i).addClass(audioArr[tempAudio][3]);
				$("#cir"+i).data("audio-file",audioArr[tempAudio][0]);
				$("#cir"+i).data("audio-name",audioArr[tempAudio][1]);
				$("#cir"+i).data("audio-artist",audioArr[tempAudio][2]);
				
				//add class to circle, mark used check, exit loop
				$("#cir"+i).addClass(audioArr[tempAudio][3]);
				audioArr[tempAudio][4] = 1;
				tempCheck = 1;
				
				//add audio file to proximity array
				var a = $("#cir"+i).data("audio-file");
				var proxAudio = new Audio("audio/"+a+".mp3");
				listenArr.push(proxAudio);
			}
		}
		usedAudioCount--;
		
		//check to see if audio array clear is needed
		if (usedAudioCount == 2) {
			//reset all of the audio array's used checks
			for (var i=0;i<audioArr.length;i++) {
				audioArr[i][4] = 0;
			}
			usedAudioCount = audioArr.length;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	//**********************************
	//CIRCLE CLICK FUNCTION - for when clicking on circle object
	//**********************************
	$(".circle").click(function(){
		
		//get id of audio circle div
		var tempID = $(this).attr("id");
		
		//check to see if in queue - send audio circle to queue if not
		if ($(this).hasClass("selected") == false) {
		
			//check to see if 5 tracks have already been selected
			if (selectedCount < 5) {
			
				//clear current class and hide circle audio controls from queued items
				$(".selected").removeClass("current");
				$(this).addClass("current");
				$(".circle-controls span").css("display","none");
				$("#"+tempID+" .circle-controls span").css("display","block");
				$("#queue-stop").css("display","block");
		
				//calculate position inside of selected container and track count and queue placement
				var tempSelectPos = selectedCount * (objWidth + 35) + 35;
				$(this).data("audio-queue",selectedCount);
				selectedCount++;
				
				//place audio metadata in html and store queue data in circle div
				$("#queue-info-name").html($(this).data("audio-name"));
				$("#queue-info-artist").html($(this).data("audio-artist"));
				selectedQueue = $(this).data("audio-queue");
	
				//suspend animation, animate circle to queue container
				$(this).addClass("selected").stop(true,false).css('z-index','12');
				$(this).animate({
						left: tempSelectPos+"px",
						bottom: "15px"
					}, 1000, "linear", function() {
		
					//shift left in queue if audio in queue finished before this animation completed
					for (var i=1;i<33;i++) {
						if ($("#cir"+i).hasClass("selected") == true) {
							tempPos = $("#cir"+i).data("audio-queue") * (objWidth + 35) + 35;
							$("#cir"+i).css("left",tempPos+"px");
						}
					}
		
				});
				
				//retrieve audio file data and create new audio variable
				var z = $(this).data("audio-file");
				var tempAudio = new Audio("audio/"+z+".mp3");
				
				//store information about circle and audio variable in queue array
				queueArr.push([tempAudio,tempID]);
				
				//pause audio from proximity listening array
				listenArr[selectedQueue].pause();
				listenArr[selectedQueue].currentTime = 0;
				//listenArr[selectedQueue][0].pause();
				//listenArr[selectedQueue][0].currentTime = 0;
		
				//end of audio events, remove audio from queue
				tempAudio.addEventListener("ended", function(){
					removeFromQueue(tempAudio,tempID);
					
					//check for empty queue
					if (queueArr.length != 0) {
						//jump to next item in queue
						if (queueIsPlaying == 1) {
							queueArr[0][0].play();
						}

						var metaTempID = queueArr[0][1];
						$(".selected").removeClass("current");
						$("#"+metaTempID).addClass("current");
						$(".circle-controls span").css("display","none");
						$("#"+metaTempID+" .circle-controls span").css("display","block");

						//place audio metadata in html and store queue data in circle div
						$("#queue-info-name").html($("#"+metaTempID).data("audio-name"));
						$("#queue-info-artist").html($("#"+metaTempID).data("audio-artist"));
						selectedQueue = $("#"+metaTempID).data("audio-queue");
					}
					else {
						//end of queue has been reached
						queueIsPlaying = 0;
						$(".control-play").removeClass("activeControl");
						$(".control-stop").addClass("activeControl");
					}
				});
			}
		}
		else {
			//clear current class and hide circle audio controls from queued items
			$(".selected").removeClass("current");
			$(this).addClass("current");
			$(".circle-controls span").css("display","none");
			$("#"+tempID+" .circle-controls span").css("display","block");
			$("#queue-stop").css("display","block");
			
			//place audio metadata in html and store queue data in circle div
			$("#queue-info-name").html($(this).data("audio-name"));
			$("#queue-info-artist").html($(this).data("audio-artist"));
			selectedQueue = $(this).data("audio-queue");
			
		}//end of queue check if/else
		
	});
	
	
	
	//**********************************
	//CIRCLE MOUSEOVER FUNCTION - for mousing over audio circle in queue
	//**********************************
	$(".circle").mouseover(function(){
	
		//check to see if in queue - send audio circle to queue if not
		if ($(this).hasClass("selected") == true) {
		
			//clear current class and hide circle audio controls from queued items
			var tempID = $(this).attr("id");
			$(".selected").removeClass("current");
			$(this).addClass("current");
			$(".circle-controls span").css("display","none");
			$("#"+tempID+" .circle-controls span").css("display","block");
			$("#queue-stop").css("display","block");
		
			//place audio metadata in html and store queue data in circle div
			$("#queue-info-name").html($(this).data("audio-name"));
			$("#queue-info-artist").html($(this).data("audio-artist"));
			selectedQueue = $(this).data("audio-queue");
		}
		
	});
	
	
	
	
	//**********************************
	//REMOVE FROM QUEUE FUNCTION - for removing audio circle from queue, both at end of track and on stop click
	//**********************************
	function removeFromQueue(audioObj,id) {
		audioObj.currentTime = 0;
					
		//remove audio object from queueArr
		tempQueue = $("#"+id).data("audio-queue");
		queueArr.splice(tempQueue, 1);
		
		if ($("#"+id).hasClass("current") == true) {
			$(".circle-controls span").css("display","none");
			$("#queue-info-name").html("");
			$("#queue-info-artist").html("");
			$("#queue-stop").css("display","none");
		}
		$("#"+id).css("z-index","3").removeClass("selected current");
		$("#"+id).removeData("audio-queue");
	 
		//move track queue to the left
		for (var i=1;i<33;i++) {
			if ($("#cir"+i).hasClass("selected") == true && $("#cir"+i).data("audio-queue") > tempQueue) {
				$("#cir"+i).css("left","-="+(objWidth + 35)+"px");
				var newQueue = $("#cir"+i).data("audio-queue") - 1;
				$("#cir"+i).data("audio-queue",newQueue);
			}
		}
		
		//check to see if end of queue
		if (queueArr.length != 0) {
			//checking for playing queue, play next
			if (queueIsPlaying == 1) {
				//jump to next item in queue
				queueArr[0][0].play();
			}

			var metaTempID = queueArr[0][1];
			$(".selected").removeClass("current");
			$("#"+metaTempID).addClass("current");
			$(".circle-controls span").css("display","none");
			$("#"+metaTempID+" .circle-controls span").css("display","block");

			//place audio metadata in html and store queue data in circle div
			$("#queue-info-name").html($("#"+metaTempID).data("audio-name"));
			$("#queue-info-artist").html($("#"+metaTempID).data("audio-artist"));
			selectedQueue = $("#"+metaTempID).data("audio-queue");
		}
		else {
			//end of queue has been reached
			queueIsPlaying = 0;
			$(".control-play").removeClass("activeControl");
			$(".control-stop").addClass("activeControl");
		}
	
		//restart animation bounce loop and remove from selected array
		circleAnimation(id,1);
		selectedCount--;
		selectedQueue = -1;
	}
	
	
	
	//**********************************
	//LEFT AND RIGHT CIRCLE AUDIO CONTROLS - for controlling audio of selected audio circle
	//**********************************
	$(".left").click(function(){
		selectedQueue = $(this).closest(".circle").data("audio-queue");
		var tempVol = queueArr[selectedQueue][0].volume;
		if (tempVol > 0.1) {
			queueArr[selectedQueue][0].volume = tempVol - 0.1;
		}
	});
	
	$(".right").click(function(){
		selectedQueue = $(this).closest(".circle").data("audio-queue");
		var tempVol = queueArr[selectedQueue][0].volume;
		if (tempVol < 1) {
			queueArr[selectedQueue][0].volume = tempVol + 0.1;
		}
	});
	
	
	
	//**********************************
	//PLAY FUNCTION - for playing audio queue after selecting spheres
	//**********************************
	$(".control-play").click(function(){
		//check for empty queue
		if (queueArr.length != 0) {
			queueArr[0][0].play();
			
			//track playing queue
			queueIsPlaying = 1;
			
			//shut off all proximity audio if queue begins to play
			for (i=0;i<listenArr.length;i++) {
				listenArr[i].pause();
				$("#cir"+i).removeClass("prox-active");
			}
			
			//change metadata to beginning of playlist
			var metaTempID = queueArr[0][1];
			$(".selected").removeClass("current");
			$("#"+metaTempID).addClass("current");
			$(".circle-controls span").css("display","none");
			$("#"+metaTempID+" .circle-controls span").css("display","block");

			//place audio metadata in html and store queue data in circle div
			$("#queue-info-name").html($("#"+metaTempID).data("audio-name"));
			$("#queue-info-artist").html($("#"+metaTempID).data("audio-artist"));
			selectedQueue = $("#"+metaTempID).data("audio-queue");
			
			//change css of play/stop buttons
			$(this).addClass("activeControl");
			$(".control-stop").removeClass("activeControl");
		}
	});
	
	
	
	
	//**********************************
	//STOP FUNCTION - for when stopping queue
	//**********************************
	$(".control-stop").click(function(){
		//track queue being stopped
		queueIsPlaying = 0;
	
		if (queueArr.length != 0) {
			queueArr[0][0].pause();
		}
		
		//change css of play/stop buttons
		$(this).addClass("activeControl");
		$(".control-play").removeClass("activeControl");
	});
	
	
	
	
	//**********************************
	//EJECT FUNCTION - for when ejecting audio tracks in queue
	//**********************************
	$(".control-eject").click(function(){
		//check to see if no queued audio is selected
		if (selectedQueue != -1) {
			queueArr[selectedQueue][0].pause();
			queueArr[selectedQueue][0].currentTime = 0;
		
			//remove from queue and tracking arrays, restart animations
			removeFromQueue(queueArr[selectedQueue][0],queueArr[selectedQueue][1]);
			
			//set selectedQueue to beginning of queue
			selectedQueue = 0;
		}
	});
	
	
	
	
	//**********************************
	//AUDIO POOL RECAST - for swapping out single audio track circles periodically
	//**********************************
	// function singleRecast() {
// 		var tempCheck = 0;
// 		var tempCircle = 0;
// 		
// 		//force choose circle that is not currently in queue
// 		while (tempCheck == 0) {
// 			//randomly choose active circle, check to see if in queue
// 			tempCircle = getRandomInt(1,32);
// 			
// 			if ($("#cir"+tempCircle).hasClass("selected") == false) {
// 				$("#cir"+tempCircle).data("audio-reset",1);
// 				tempCheck = 1;
// 			}
// 		}
// 		
// 		//set timeout before recursion
// 		setTimeout(function(){
// 			singleRecast();
// 		}, maxTime * 1.5);
// 	}
// 	
// 	setTimeout(function(){
// 		singleRecast();
// 	}, maxTime * 1.5);
	
	
	
	
	
	
	//**********************************
	//SETTINGS FUNCTIONS - for when clicking on settings menu items
	//**********************************
	$("#settings i").click(function() {
		$("#settings-list").animate({
			height: "toggle"
			}, 500, function() {
			// Animation complete.
		});
	});
	
	//clears all current sounds and resets animations
	$("#set-refresh").click(function() {
		$("#fadescreen").fadeIn(800);
		setTimeout(function(){
			location.reload();
		}, 1000);
	});
	
	
	//settings info
	$("#statement-close").click(function(){
		$("#statement-popup").fadeOut(400);
		settingsOpen = 0;
	});
	
	$("#set-info").click(function(){
		$("#statement-popup").fadeIn(400);
		settingsOpen = 1;
		
		$("#settings-list").animate({
			height: "toggle"
			}, 500, function() {
			// Animation complete.
		});
	});
	
	//settings instructions
	$("#instructions-close").click(function(){
		$("#instructions-popup").fadeOut(400);
		if (initialized == 0) {
			initializeAudio();
			initialized = 1;
		}
		settingsOpen = 0;
	});
	
	$("#set-instructions").click(function(){
		$("#instructions-popup").fadeIn(400);
		settingsOpen = 1;
		
		$("#settings-list").animate({
			height: "toggle"
			}, 500, function() {
			// Animation complete.
		});
	});
	
	//settings info
	$("#bios-close").click(function(){
		$("#bios-popup").fadeOut(400);
		settingsOpen = 0;
	});
	
	$("#set-bios").click(function(){
		$("#bios-popup").fadeIn(400);
		settingsOpen = 1;
		
		$("#settings-list").animate({
			height: "toggle"
			}, 500, function() {
			// Animation complete.
		});
	});
	
	
	//settings speed
	$("#speed-close").click(function(){
		$("#speed-popup").fadeOut(400);
		settingsOpen = 0;
	});
	
	$("#set-speed").click(function(){
		$("#speed-popup").fadeIn(400);
		settingsOpen = 1;
		
		$("#settings-list").animate({
			height: "toggle"
			}, 500, function() {
			// Animation complete.
		});
	});
	
	$("#speed-list ul li").click(function(){
		$("#speed-list ul li").removeClass("speed-select");
		$(this).addClass("speed-select");
		maxTime = baseMaxTime / parseFloat($(this).html());
		minTime = baseMinTime / parseFloat($(this).html());
	});
	
	
	//settings distance
	$("#distance-close").click(function(){
		$("#distance-popup").fadeOut(400);
		settingsOpen = 0;
	});
	
	$("#set-distance").click(function(){
		$("#distance-popup").fadeIn(400);
		settingsOpen = 1;
		
		$("#settings-list").animate({
			height: "toggle"
			}, 500, function() {
			// Animation complete.
		});
	});
	
	$("#distance-list ul li").click(function(){
		$("#distance-list ul li").removeClass("distance-select");
		$(this).addClass("distance-select");
		
		var tempChoice = $(this).html();
		
		//change distance variable
		if (tempChoice == "Close") {
			distanceMax = 80;
		}
		else if (tempChoice == "Medium") {
			distanceMax = 150;
		}
		else if (tempChoice == "Distant") {
			distanceMax = 210;
		}
	});
	
	
	
	//prevent img dragging
	$('img').on('dragstart', function(event) { event.preventDefault(); });
	
	
	
	
	//**********************************
	//PROXIMITY CHECKS AND INTERVALS - track position of mouse and play sounds nearby
	//**********************************
	var mousePosition = {'x': 0, 'y': 0};
    $(document).bind('mousemove', function(e) {
        mousePosition = {'x': e.pageX, 'y': e.pageY};
    });

    setInterval(function () {
    	//check to see if queue is currently playing and settings aren't open
    	if (queueIsPlaying == 0) {
    	
    		for (i=1;i<(listenArr.length+1);i++) {
    			//calculate distance between mouse and circle
    			if (settingsOpen == 0) {
    				var x = Math.round(Math.abs($('#cir'+i).offset().left - mousePosition.x));
					var y = Math.round(Math.abs($('#cir'+i).offset().top - mousePosition.y));
    			}

				//if distance is within set distance and settings aren't open, then play audio
				if (x < distanceMax && y < distanceMax && settingsOpen == 0) {
			
					//only play audio if not in queue
					if ($('#cir'+i).hasClass("selected") == false) {
						listenArr[i-1].play();
						$("#cir"+i).addClass("prox-active");
					}
				}
				else {
					listenArr[i-1].pause();
					$("#cir"+i).removeClass("prox-active");
				}
    		}

		}
    }, 200);
    
    
    
    
    
    
    //**********************************
	//MOUSE IDLE CHECK - reload page if user is idle for two minutes
	//**********************************
    var timeout = null;

	$(document).on('mousemove', function() {
		clearTimeout(timeout);

		timeout = setTimeout(function() {
			if (queueIsPlaying == 0) {
				$("#fadescreen").fadeIn(800);
				setTimeout(function(){
					location.reload();
				}, 1000);
			}
		}, 90000);
	});
	
	
});