
var out1 = 1;
var up = 1;
var waiter = 0;
var int_countswitched = 0;
var i2 = 0;
var p = true;
var topspeed = 0;
var iteration = 0;
var nextcounter=1000;
var mytime2;
var mytime3;
var maxlength = 0;
var messages = new Array();
var symbolArray = new Array();
var bgshade = 0;
var order = 1;
var showcolour=0;

int_whichmessage=0;
/***********************************************************************************************************/
function findindex(str_in, char_find, int_start) {
	if (!int_start) int_start = 0;

	while(int_start++ < str_in.length) {
		if (str_in.substring(int_start,int_start+1) == char_find) return(int_start);
	}
	if (int_start == str_in.length) return(str_in.length);
}

function dec2hex2 ( textString ) {
  var hexequiv = new Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
  return hexequiv[(textString >> 4) & 0xF] + hexequiv[textString & 0xF];
}

function switchcase(str_input) {
	if(str_input.toUpperCase() === str_input) str_output = str_input.toLowerCase();
	else if(str_input.toLowerCase() === str_input) str_output = str_input.toUpperCase();

	return(str_output);
}
/***********************************************************************************************************/
function strpad(str_input, int_length, str_with, str_which) {
	//alert(str_input+" \n "+ int_length+" \n "+ str_with+" \n "+str_which);
	var str_pad_right = "";
	var str_pad_left = "";
	if(str_which == "center") {
		for (i=0; i < Math.floor(int_length/2); i++) str_pad_left += ""+str_with;
		for (i=0; i <= Math.floor((int_length-1)/2); i++) str_pad_right += ""+str_with;
	} else if(str_which == "left") {
		for (i=0; i < int_length; i++) str_pad_left += ""+str_with;
	} if(str_which == "right") {
		for (i=0; i < int_length; i++) str_pad_right += ""+str_with;
	}

	str_output = str_pad_left+str_input;
	str_output = str_output+str_pad_right;
	//alert("-"+str_output+"-"+str_output.length);
	return(str_output);

}
/***********************************************************************************************************/
function mutate(theMessage, theOldMessage, inTarget) {
	var theMessage2 = "";
	//alert(theMessage+" "+ theOldMessage);
	currentmessage = strpad(messages[inTarget][theMessage], maxlength-messages[inTarget][theMessage].length, " ", "center");

	for(var ind2=0; ind2< currentmessage.length;ind2++)	{
		var oneletter = currentmessage.substring(ind2, ind2+1);
		var heads=Math.floor(Math.random()*currentmessage.length);
		if (messages[inTarget][theOldMessage]) var oneoldletter = messages[inTarget][theOldMessage].substring(ind2, ind2+1);
		else oneoldletter = "?";


		//if (oneletter == '\n') theMessage2 += '<br>';
dobold = "";
		if (symbolArray[inTarget][ind2] == null) {
			whichletter = oneoldletter;
			if (theOldMessage%2 == 0) dobold = "font-weight:bold;";
			else dooldbold = "";

		} else {
			whichletter = oneletter;
			if (theMessage%2 == 1) dobold = "";
			else dobold = "font-weight:bold;";
		}
		var doitalic = "";
		if (symbolArray[inTarget][ind2] === 1) doitalic = "font-style:italic;";
//		else if (symbolArray[inTarget][ind2] === 0) doitalic = "";

		var prefix = "", postfix="";
		if (dobold+doitalic != "") {
			prefix = "<span style='"+dobold+doitalic+"'>";
			postfix = "</span>";
		}

		if (heads === 0) { theMessage2 += prefix+"¿"+postfix; }
		else if (heads === 1) { theMessage2 +=  prefix+"?"+postfix; }
		else if (heads > 1) { theMessage2 += prefix+whichletter+postfix; }

/*
null = unselected
0 = on message 0;
1 = on message 1;
-1 = italic.
*/
	}

	return(theMessage2);
}
/***********************************************************************************************************/
function addmessage(message, target) {
	message = message.replace(/ /g, " ");

	if(!messages[target]) messages[target] = new Array();
	symbolArray[target] = new Array();

	messages[messages.length] = target;

	if (message.length > maxlength) {
		maxlength = message.length;
		window.document.getElementById(target).style.width = "100%";
	//	alert(maxlength+" "+window.document.getElementById(target).style.width);
	}

	//alert(messages.length);
/*
	if (messages.length > 1) {
		//alert(messages[target].length+" "+messages.length+ " "+messages[messages.length-1]);
		//alert(messages.length+" len0:"+messages[target][messages.length-2].length);
		var tmp_diff = (messages[target][messages.length-2].length - message.length);
		templength = 0;
		if (tmp_diff > 0) templength = tmp_diff;
		//this needs to be redone, to pad the messages AFTER all the messages have been added, to determine the max length!
	}
*/
	messages[target][messages[target].length] = message
	//alert(target+"\n"+messages.length+"\n"+messages[target].length+"\n"+messages[target][messages[target].length-1]+"!\n"+messages[target][messages[target].length-2]+"!");
	//window.document.getElementById(target).innerHTML += "\n<br>"+messages[target][messages[target].length-1];
//	alert(window.document.getElementById("div").innerHTML);
}

/*
idea: instead of random bold, turn the CHANGED letter bold only
make them randomly italic?
*/
var number;
/***********************************************************************************************************/
function random_replace(inTarget) { //str_message1, str_message2, str_replacearray[],
	tmp_thismessage = strpad(messages[inTarget][int_whichmessage], maxlength-messages[inTarget][int_whichmessage].length, " ", "center");

	var newMessage;
	var nexttime;

	if (p === true) {
		number=Math.floor(Math.random()*tmp_thismessage.length);
		nexttime = Math.ceil(nextcounter/10);

		if(symbolArray[inTarget][number] == null) { int_countswitched++; }
		if(symbolArray[inTarget][number] === 0) { symbolArray[inTarget][number] = 1;	}
		else { symbolArray[inTarget][number] = 0; }
	} else {
		nexttime = nextcounter;
		if (nextcounter > 100) { nextcounter -= Math.ceil(nextcounter*1/33); }
		topspeed += nextcounter;
		var debug = "<br>"+(iteration++)+" "+ nextcounter + " " + topspeed;

//		alert(window.document.getElementById(inTarget).id+" "+window.document.getElementById(inTarget).parentNode.style.width);

//		var parwid = window.document.getElementById(inTarget).parentNode.style.width;
//		var parwid2 = Number(parwid.substr(0, parwid.length-1))
		//alert(parwid+" "+parwid2);

//		if(parwid2 > 0) {
//			window.document.getElementById(inTarget).parentNode.style.width = (parwid2-1)+"%";
//			window.document.getElementById(inTarget).parentNode.style.height = (parwid2-1)+"%";
//		}

		//alert(parwid);
		// = 1px;
	}
	var letter = switchcase(tmp_thismessage.substring(number,number+1));
	tmp_thismessage = tmp_thismessage.substring(0,number)+ letter + tmp_thismessage.substring(number+1,tmp_thismessage.length);
	messages[inTarget][int_whichmessage] = tmp_thismessage;

	//alert(newMessage);

	newMessage = mutate(int_whichmessage,int_whichmessage-1, inTarget);

	if (waiter == 0) {
		countbar = strpad("", int_countswitched, "!", "left");
		countbar = strpad(countbar, maxlength - int_countswitched, "?", "right");
	} else {
		countbar = strpad("", Math.floor(waiter/2), "?", "left");
		countbar = strpad(countbar, maxlength - Math.floor(waiter/2), "!", "right");
	}

	window.document.getElementById(inTarget).innerHTML = newMessage+"<br>"+countbar;
	if (int_countswitched == tmp_thismessage.length) {
		if (waiter <= tmp_thismessage.length * 2) { waiter++ }
		else {
			waiter=0;
			int_countswitched = 0;
			i2 = 0;
			topspeed = 0;
			iteration = 0;
			//nextcounter = 1000;
			//nexttime = 100;

			for (i=0; i < messages[inTarget][int_whichmessage].length; i++) {
				//alert(symbolArray[inTarget][i]);
				symbolArray[inTarget][i] = null;
				delete symbolArray[inTarget][i];
			}
			if (messages[inTarget][int_whichmessage+1] ) int_whichmessage++;
			else {
				messages[inTarget][-1] = messages[inTarget][int_whichmessage];
				int_whichmessage=0;
			}
		}
	}

	if (p) {
		ha();
		bgshade += order;
		if ((bgshade == 255) || (bgshade == 0)) {
			order = order * -1;
		}
		docolours(inTarget);

	}

	//nexttime=100;
	p = !p;
	outTarget=inTarget;
	window.clearTimeout(mytime2); //this should stop multiple instances right?
	mytime2 = window.setTimeout(function(){random_replace(outTarget);}, nexttime);
}
var randcolour1 = 0;
var randcolour2 = 0;
var randind = 0;

function docolours(inTarget) {
		thisshade1 = dec2hex2(bgshade);
		thisshade2 = dec2hex2(255-bgshade);
		thisshade3 = dec2hex2(Math.ceil(bgshade/2));
		thisshade4 = dec2hex2(Math.ceil((255-bgshade)/2));

		if (randind==0) {
			thisshade1 = thisshade1+thisshade1+thisshade1;
			thisshade2 = thisshade2+thisshade2+thisshade2;
		} else if (randind==1) {
			thisshade1 = thisshade1+randcolour1+randcolour2;
			thisshade2 = thisshade2+randcolour1+randcolour2;

		} else if (randind==2) {
			thisshade1 = randcolour1+thisshade1+randcolour2;
			thisshade2 = randcolour1+thisshade2+randcolour2;

		} else if (randind==3) {
			thisshade1 = randcolour1+randcolour2+thisshade1;
			thisshade2 = randcolour1+randcolour2+thisshade2;

		}

		window.document.body.style.backgroundColor = "#"+thisshade1 //thisshade;
		window.document.getElementById(inTarget).style.color = "#"+thisshade2; //thisshade2;
		//window.document.getElementById("bob").innerHTML = bgshade +"<br>"+(255-bgshade)+"<br>"+ thisshade +"<br>"+ thisshade2;
}
function newcolour() {
	showcolour++
	if( showcolour == 8) showcolour = 0;

		randcolour1 = dec2hex2(Math.floor(Math.random()*255));
		randcolour2 = dec2hex2(Math.floor(Math.random()*255));
		if (randind == 0) randind = Math.floor(1+Math.random()*2);
 		else randind = Math.floor(Math.random()*3);

		if (randind==0) {
			midshade = "808080";
			fullshade = "000000";

		} else if (randind==1) {
			midshade = "80"+randcolour1+randcolour2;
			fullshade = "00"+randcolour1+randcolour2;

		} else if (randind==2) {
			midshade = randcolour1+"80"+randcolour2;
			fullshade = randcolour1+"00"+randcolour2;

		} else if (randind==3) {
			midshade = randcolour1+randcolour2+"80";
			fullshade = randcolour1+randcolour2+"00";

		}

	for(i=0; (a = document.getElementsByTagName("a")[i]); i++) a.style.color = "#"+midshade;
	window.document.getElementById("ll").style.color = "#"+midshade;


	window.document.getElementById("bl").style.borderColor = "transparent transparent "+fullshade+" "+fullshade;
	window.document.getElementById("br").style.borderColor = "transparent "+fullshade+" "+fullshade+" transparent";
	window.document.getElementById("tl").style.borderColor = fullshade+" transparent transparent "+fullshade;
	window.document.getElementById("tr").style.borderColor = fullshade+" "+fullshade+" transparent transparent";

	docolours("div1"); //hardcoded id name, bad
	if (window.document.getElementById("div2")) docolours("div2");
}

var int_aboutcount = 0;

function aboutloop(inTarget) {
	outTarget = inTarget;
	int_aboutcount++

	if (int_aboutcount == 0) {
		window.document.getElementById(inTarget).style.borderLeft="3px dotted black";
	} else if (int_aboutcount == 1) {
		window.document.getElementById(inTarget).style.borderRight="4px dotted black";
	} else if (int_aboutcount == 2) {
		window.document.getElementById(inTarget).style.borderTop="3px dotted black";
	} else if (int_aboutcount == 3) {
		window.document.getElementById(inTarget).style.borderBottom="4px dotted black";
	} else if (int_aboutcount == 4) {

	} else if (int_aboutcount == 5) {

	} else if (int_aboutcount == 6) {

	} else if (int_aboutcount == 7) {

	}

	nexttime = 1000;
	window.clearTimeout(mytime3); //this should stop multiple instances right?
	mytime3 = window.setTimeout(function(){aboutloop(outTarget);}, nexttime);

}
/***********************************************************************************************************/
function go() {
	if (mytime2) {
		alert("fuck off");
		return;
	}

	var whichId1 = "div1";
	var text1 = window.document.getElementById(whichId1).innerHTML;
//	text = text.substr(0, text.length-1);
	var re = new RegExp("<BR>", 'gi');
	text1 = text1.replace(re, "\n");
	//window.document.getElementById(whichId1).innerHTML = "<div id='"+whichId+"'>"+text+"</div>";

	var text2 = "Welcome to nympholept.com. Hope you enjoy the show.";
	var text3 = "Coming \"soon\": Actual content.";
	var text4 = "Seriously, I've been working on some very special things.";

	addmessage(text1,whichId1);
	addmessage(text2,whichId1);
	addmessage(text3,whichId1);
	addmessage(text4,whichId1);
	//repad();
 	mytime2 = window.setTimeout(function(){random_replace(whichId1);}, 0);
	//window.document.getElementById("div1").style.width = "100%";
}
function go2() {
	if (mytime3) {
		window.clearTimeout(mytime3);
	}
	if (mytime2) {
		alert("fuck off");
		return;
	}
	window.document.getElementById("boring").style.display="none";

	var whichId1 = "div1";
	var whichId2 = "div2";

	var text1 = window.document.getElementById(whichId2).innerHTML;
	window.document.getElementById(whichId2).style.display="none";
	var re = new RegExp("<BR>\n", 'gi');
	text1 = text1.replace(re, "\n");
	var re = new RegExp("\n\n", 'gi');
	text1 = text1.replace(re, "\n");

	int_start = 0;
	while (int_start < text1.length) {
		int_end = findindex(text1, '\n', int_start);
		this_text = text1.substring(int_start,int_end);

		if (this_text.length > 1) addmessage(this_text,whichId1); //check is no longer required
		int_start = int_end + 1;
	}
 	mytime2 = window.setTimeout(function(){random_replace(whichId1);}, 0);
}

function go3() {
	whichId2 = "div2";

	mytime3 = window.setTimeout(function(){aboutloop(whichId2);}, 0);
}


function ha() {
//	var in1 = document.getElementById('topleft').style.borderWidth;
//	out1 = in1.substring(0,in1.length-2);
//alert(out1);
	if (up == 1) out1++;
	else out1 = (1*out1)-3;

	clw = document.body.clientWidth;
	clh = document.body.clientHeight;
	//alert(out1);
	if ((out1 >= (clw-20)/4) || (out1 >= (clh-40)/4) ) { up = 0; out1--; }
	if (out1 <= 0) { up = 1; out1 = 0;

	//alert((clh-40)/4);
	}

	//document.getElementById('menu1').style.width = (out1*2);

	document.getElementById('bl').style.borderWidth = out1;

	document.getElementById('br').style.borderWidth = out1;
	document.getElementById('tl').style.borderWidth = out1;
	document.getElementById('tr').style.borderWidth = out1;

	//document.getElementById('table1').style.width = (out1*2);

//clearInterval(to3);
}

