//these would be const but safari bitches.  oh well.
TSTEP = 25;
NUM_PICS = 3;
phase_dip = 0;
phase_lift = 1;
phase_drop = 2;
phase_raise = 3;
phase_done = 4;



/*---parameters:
 * each phase's final position (yf), movement multiplier (mm), direction (dir), and post-delay (pDly)--
 * {Dip, Lift, Drop, Raise}
 * Movement switches phase at final position
 */
yf = new Array();
mm = new Array();
dir = new Array();
pDly = new Array();

yf[phase_dip] = 15;
mm[phase_dip] = .6;
dir[phase_dip] = 1;
pDly[phase_dip] = 0;

yf[phase_lift] = -300;
mm[phase_lift] = 1.3;
dir[phase_lift] = -1;
pDly[phase_lift] = 400;

yf[phase_drop] = 10;
mm[phase_drop] = .7;
dir[phase_drop] = 1;
pDly[phase_drop] = 0;

yf[phase_raise] = 0;
mm[phase_raise] = .3;
dir[phase_raise] = -1;
pDly[phase_raise] = 0;

//Array of photo names
i=0;
photos = new Array();

photos[i++] = "cdr.jpg";
photos[i++] = "control.jpg";
photos[i++] = "keys.jpg";
photos[i++] = "keys1.jpg";

photos[i++] = "keys2.jpg";
photos[i++] = "knob.jpg";
photos[i++] = "knob2.jpg";
photos[i++] = "mic1.jpg";

photos[i++] = "mic2.jpg";
photos[i++] = "mixer.jpg";
photos[i++] = "monitor.jpg";
photos[i++] = "music2.jpg";

photos[i++] = "patch.jpg";
photos[i++] = "rubiks.jpg";
photos[i++] = "sliders.jpg";
photos[i++] = "tracks.jpg";

pic1= new Image(90,74); 

function randPic(banStr) {
	//Pic a random photo from the photos array.
	//banStr is a comma-seperated string of pics not allowed to be chosen
	if (banStr === undefined) banStr = "";
	banList = banStr.split(",");
	cPicList = new Array();
	for (ii=0; ii<photos.length; ii++) {
		banned = false;
		for (jj=0; jj<banList.length; jj++) {
			if (photos[ii] == banList[jj]) banned = true;
		}
		if (!banned) cPicList[cPicList.length] = photos[ii];
	}
	if (cPicList.length == 0) return ""
	
	picI = Math.floor(Math.random() * cPicList.length);
	return cPicList[picI];
}

function switchPics(cURL) {
	//switch the 3 pics in the top.  Depending on which page is being loaded, one of the
	//pic (random position) is determined, the other two are random.
	picName = new Array(2);
	fixedPic = '';
	
	//set fixed pic depending on page
	switch(cURL) {
		case "home.html":
			fixedPic = 'mic1.jpg';
		break;
		case "recording.html":
			fixedPic = 'tracks.jpg';
		break;
		case "mixing.html":
			fixedPic = 'mixer.jpg';
		break;
		case "songs.html":
			fixedPic = 'music2.jpg';
		break;
		case "about.html":
			fixedPic = 'control.jpg';
		break;
		case "contact.html":
			fixedPic = 'rubiks.jpg';
		break;
		default:
			fixedPic = randPic();
	}
	
	//randomly choose each pic
	picName[0] = randPic(fixedPic);
	picName[1] = randPic(fixedPic+","+picName[0]);
	picName[2] = randPic(fixedPic+","+picName[0]+","+picName[1]);
	
	//determine which pic to replace with fixedpic
	whichPic = Math.floor(Math.random()*3);
	picName[whichPic] = fixedPic;
	
	setTimeout("movePic('pic0', 0, phase_dip, 'photos/'+picName[0]);",1400);
	setTimeout("movePic('pic1', 0, phase_dip, 'photos/'+picName[1]);",1550);
	setTimeout("movePic('pic2', 0, phase_dip, 'photos/'+picName[2]);",1700);
}

function cyclePics(lastPic, curPics) {
	//continuously cycle a given picture at random intervals
	//against, would be const...but...safari....
	minTime = 1000;
	maxTime = 10000;
	//choose pic index to switch
	whichPic = choosePic(lastPic)
	pN = "pic" + whichPic
	//choose new pic source
	newSrc = randPic(curPics)
	//choose time until next switch
	distrib = 1-(Math.random()*Math.random());
	time = Math.floor(distrib * (maxTime - minTime)) + minTime;

	curPicArray = curPics.split(",");
	curPicArray[whichPic] = newSrc;
	curPics = curPicArray.join(",");
	
	//preload pic before calling for switch
	pic1.src=newSrc; 
	
	setTimeout("movePic(pN, 0, phase_dip, 'photos/'+newSrc);",time);
	setTimeout("cyclePics(whichPic, \""+curPics+"\");",time);
}

function choosePic(lastPic) {
	draw = Math.floor(Math.random()*NUM_PICS);
	if (draw == lastPic) draw = choosePic(-1); //reduce likelihood of picking the same pic twice, but still allow it.
	return draw;
}

function movePic(picName, y, phase, newSrc) {
	//function guiding the movement of the pic in 4 phases:
	//dip, lift, drop, raise
	p = document.getElementById(picName);


	//execute movement
	switch(phase) {
		case phase_dip:
			y += (yf[phase] - y) * mm[phase];
		break;
		case phase_lift:
			y -= (yf[phase_dip] - y + 1) * mm[phase];
		break;
		case phase_drop:
			y += (yf[phase] - y) * mm[phase];
		break;
		case phase_raise:
			y += (yf[phase] -  y) * mm[phase];
		break;
		case phase_done:
		break;
	}
 
	//set pic position
	p.style.top = Math.floor(y)+"px";

	tTimeout = TSTEP;
	//check if we should change phase, set up call to next movePic()
	if (dir[phase] * y > (dir[phase] * yf[phase] - 1)) {
		tTimeout += pDly[phase];
		y = yf[phase];
		phase++;
		if (phase == phase_drop) p.src = newSrc;
	}

	if (phase < phase_done) {
		setTimeout("movePic(\""+picName+"\","+y+","+phase+",\""+newSrc+"\")",tTimeout);
	}
}