var fps = 30;
var timeinterval = Math.round(1000/fps);
var secs = 40; //seconds x 10

var obj1time = 0;
var obj1top = 0;
var obj1left = 0;
var footrun = 1;
var footcoltime = 0;
var footrbase = 0;
var footgbase = 0;
var footbbase = 0;
var footr = 0;
var footg = 0;
var footb = 0;


//main function to make everything work
function runner(){
	//move logo around
	if (window.obj1time == 0){
		window.obj1time = Math.round(((Math.random()*secs))*(fps/10))+1;
		setobj1();
		document.getElementById('div1').style.top = parseFloat(document.getElementById('div1').style.top) + window.obj1top + '%';
		document.getElementById('div1').style.left = parseFloat(document.getElementById('div1').style.left) + window.obj1left + '%';

	}else{
		document.getElementById('div1').style.top = parseFloat(document.getElementById('div1').style.top) + window.obj1top + '%';
		document.getElementById('div1').style.left = parseFloat(document.getElementById('div1').style.left) + window.obj1left + '%';
	}
	
	//cycle footer color
	if(window.footrun==1){	
		if (window.footcoltime == 0){
			window.footcoltime = Math.round(((Math.random()*secs))*(fps/10))+1;
			footercolor();
			document.getElementById('footer').style.backgroundColor = 'rgb('+Math.round(window.footrbase)+", "+Math.round(window.footgbase)+", "+Math.round(window.footbbase)+")";
			window.footrbase = parseFloat(window.footrbase) + window.footr;
			window.footgbase = parseFloat(window.footgbase) + window.footg;
			window.footbbase = parseFloat(window.footbbase) + window.footb;
		}
	document.getElementById('footer').style.backgroundColor = 'rgb('+Math.round(window.footrbase)+", "+Math.round(window.footgbase)+", "+Math.round(window.footbbase)+")";
	window.footcoltime --;
	window.footrbase = parseFloat(window.footrbase) + window.footr;
	window.footgbase = parseFloat(window.footgbase) + window.footg;
	window.footbbase = parseFloat(window.footbbase) + window.footb;
	}
	
	//cycle box colors
	if (window.boxrun==0){
		backwhite();
	}else{
		if(window.boxtime==0){
			window.boxtime = Math.round(((Math.random()*secs))*(fps/10))+1;
			setbackcol();
			backcol();
		}
		backcol();
		window.boxtime --;
	}
	
	//cycle text on screen
	if (window.txtrun==1){
		fmouse();
		textfollow();
		window.txttime--;
	}
	if(window.txtrotate==1){
		rotatetext();
	}
	if(window.txtspecifytime>0){
		specifymove();
		window.txtspecifytime--;
	}
	
	setTimeout('runner()',timeinterval);
	window.obj1time --;	
	document.getElementById('colrgb').value = "PanelColor rr"+window.footrbase+" gg"+window.footgbase+" bb"+window.footbbase;
	document.getElementById('obj1pos').value = "Obj1pos xx"+parseFloat(document.getElementById('div1').style.top)+"yy"+parseFloat(document.getElementById('div1').style.left);
	document.getElementById('tictoc').value = "Logo "+window.obj1time+" PanelCol "+window.footcoltime+" BoxCols "+window.boxtime+" Txt "+window.txttime;
	txt1="";
		for (i=0;i<18;i++){
			txt1 += "Box"+(i+1)+" r"+Math.round(window.boxcolr[(i)])+" g"+Math.round(window.boxcolg[(i)])+" b"+Math.round(window.boxcolb[(i)])+"\n";
		}
		document.getElementById('bkcols').value = txt1;
	txt1="";
		for (i=0;i<18;i++){
			txt1 += "Box"+(i+19)+" r"+Math.round(window.boxcolr[(i+18)])+" g"+Math.round(window.boxcolg[(i+18)])+" b"+Math.round(window.boxcolb[(i+18)])+"\n";
		}
		document.getElementById('bkcols2').value = txt1;
	document.getElementById('settings').value = "FPS "+window.fps+" TI "+window.timeinterval;
	txt1=""
	txt1="style "+window.txtstyle+"\n";
	txt1+="boxnums "+window.txtboxnums+"\n";
	if (document.formpanel.followmouse[0].checked==true){
		txt1+="movement none\n";
	}else if(document.formpanel.followmouse[1].checked==true){
		txt1+="movement follow mouse\n";
	}else if(document.formpanel.followmouse[2].checked==true){
		txt1+="movement jump\n";
	}else {txt1+="movement smooth\n";}
	if(window.txtrun!=0){
		txt1+="X "+document.getElementById('lettercont0').style.left+" Y "+document.getElementById('lettercont0').style.top+"\n";
		if(window.tmpXoffset!=0 && window.tmpYoffset!=0){
			txt1+="Xoff "+Math.round(window.tmpXoffset*100)/100+" Yoff "+Math.round(window.tmpYoffset*100)/100+"\n";
		}
	}
	txt1+="let "+window.txtnum+"\n";
	if (window.txtrotate==1){txt1+="rotation\nx0 "+parseFloat(document.getElementById('letter0').style.left)+" y0 "+parseFloat(document.getElementById('letter0').style.top)+" xi "+window.Xinc+" yi "+window.Yinc+"\n"+"R "+window.txtr+" B "+window.txtd+"\n"+"Dir "+window.txtdir[0]+"\n";}
	document.getElementById('tvals').value = txt1;
}

//set the new target position of the logo and increments to get there
function setobj1(){
	var ot = parseInt(document.getElementById('div1').style.top.substr(0,2));
	var ol = parseInt(document.getElementById('div1').style.left.substr(0,2));
	var nt = Math.round(Math.random()*80);
	var nl = Math.round(Math.random()*55);
	window.obj1top = (nt - ot) / window.obj1time;
	window.obj1left = (nl - ol) / window.obj1time;
}

//set the target color of the footer and increments to get there
function footercolor(){
	var colrgb = document.getElementById('footer').style.backgroundColor.substr(document.getElementById('footer').style.backgroundColor.indexOf("(")+1);
	window.footrbase = parseInt(colrgb);
	window.footgbase = parseInt(colrgb.substr(colrgb.indexOf(",")+1));
	window.footbbase = parseInt(colrgb.substr(colrgb.indexOf(",",colrgb.indexOf(",")+1)+1));
	var nr = Math.round(Math.random()*255);
	var ng = Math.round(Math.random()*255);
	var nb = Math.round(Math.random()*255);
	window.footr = (nr - footrbase) / window.footcoltime;
	window.footg = (ng - footgbase) / window.footcoltime;
	window.footb = (nb - footbbase) / window.footcoltime;
}

//button controls
//show and hide panel
function hideshow(){
	var fs = document.getElementById('framecontrol');
	if (fs.value=='Show Panel') {
		fs.value='Hide Panel';
		document.getElementById('footer').style.top = '55%';
		document.getElementById('footer').style.overflow = "auto";
		document.getElementById('letters').style.bottom = '45%';
	}else{
		fs.value='Show Panel';
		document.getElementById('footer').style.top = '95%';
		document.getElementById('footer').style.overflow = "hidden";
		document.getElementById('letters').style.bottom = '5%';
	}
}

//play song
function songplay(){
	if (document.getElementById('playsong').value=='Play Song'){
		if (document.getElementById('songtitle').value != 0) {
			document.getElementById('playsong').value='Stop Song';
			soundManager.play("selectedtrack", document.getElementById('songtitle').value);
		}
	}else{
		document.getElementById('playsong').value='Play Song';
		soundManager.stop('selectedtrack');
		soundManager.destroySound('selectedtrack');
	}
}

//show fields with code in them
function showcode(){
	if (document.getElementById('viewcode').value=='View Code Runners'){
		document.getElementById('viewcode').value='Hide Code Runners';
		document.getElementById('obj1pos').style.visibility='visible';
		document.getElementById('colrgb').style.visibility='visible';
		document.getElementById('bkcols').style.visibility='visible';
		document.getElementById('bkcols2').style.visibility='visible';
		document.getElementById('tictoc').style.visibility='visible';
		document.getElementById('boxnumrnd').style.visibility='visible';
		document.getElementById('settings').style.visibility='visible';
		document.getElementById('mousepos').style.visibility='visible';
		document.getElementById('tvals').style.visibility='visible';
	}else{
		document.getElementById('viewcode').value='View Code Runners';
		document.getElementById('obj1pos').style.visibility='hidden';
		document.getElementById('colrgb').style.visibility='hidden';
		document.getElementById('bkcols').style.visibility='hidden';
		document.getElementById('bkcols2').style.visibility='hidden';
		document.getElementById('tictoc').style.visibility='hidden';
		document.getElementById('boxnumrnd').style.visibility='hidden';
		document.getElementById('settings').style.visibility='hidden';
		document.getElementById('mousepos').style.visibility='hidden';
		document.getElementById('tvals').style.visibility='hidden';
	}
}

//show or hide logo
function showlogo(){
	if (document.getElementById('viewlogo').value=='Hide Logo'){
		document.getElementById('viewlogo').value='Show Logo';
		document.getElementById('div1').style.visibility='hidden';
	}else{
		document.getElementById('viewlogo').value='Hide Logo';
		document.getElementById('div1').style.visibility='visible';
	}
}

//background color
function backcolstart(){
	if (document.getElementById('addbackcol').value=='Add Background Color'){
		document.getElementById('addbackcol').value='Hide Background Color';
		document.getElementById('numboxestext').style.visibility='visible';
		document.getElementById('numboxes').style.visibility='visible';
		window.boxrun = 1;                      
	}else{
		document.getElementById('addbackcol').value='Add Background Color';
		document.getElementById('numboxestext').style.visibility='hidden';
		document.getElementById('numboxes').style.visibility='hidden'; 
		window.boxrun = 0;
	}
}

//fix panel/footer color
function fixfootcol(){
	if (document.getElementById('fixfoot').value=='Fix Panel Color'){
		document.getElementById('fixfoot').value='Cycle Panel Color';
		document.getElementById('footer').style.backgroundColor='rgb(204, 204, 204)';
		window.footrbase=204;
		window.footgbase=204;
		window.footbbase=204;
		window.footrun = 0;    
		window.footcoltime = 0;                  
	}else{
		document.getElementById('fixfoot').value='Fix Panel Color';
		window.footrun = 1;
	}
}

//change the frames per second
function fpsset(){
	window.fps = document.getElementById('fpsval').value;
	window.timeinterval = Math.round(1000/fps);
	window.boxtime = 1;
	window.footcoltime = 1;
	window.obj1time = 1;
	window.txttime = 1;
	window.txtrinit = 0;
	textput();
}