/*Figured the main javascript file was a good place to put a message.  All html, css, js, php, etc done in notepad++ or emacs.  I'm not much of a commenter, so if you want me to explain anything drop me an email at info@jownow.com.  If you have any suggestions for me please do the same.*/

ftTimer = setTimeout("",1);
tbhTimer = setTimeout("",1);
gbwTimer = setTimeout("",1);
gbhTimer = setTimeout("",1);

MIN_WIDTH = 10;
MIN_HEIGHT = 20;

function initbox(mybox, delay) {
	//setup
	obox=document.getElementById(mybox); //outer div
	ibox=document.getElementById(mybox+"txt");  //div containing box contents
	maxH = ibox.offsetHeight;  //destination height -- offsetHeight?  doesn't give the right values in safari
	maxW = obox.offsetWidth;   //destination width

	//impose min box dims.  Safari sucks.
	maxH = Math.max(maxH,MIN_HEIGHT);
	maxW = Math.max(maxW,MIN_WIDTH);

	ibox.style.height="10px";  //inner div height, so that rounded corners don't dissapear
	obox.style.width="0px";    //outer div width, so rounded corners start close in on left
	setTimeout("growBoxW(\""+mybox+"\",0,\""+maxW+"\",\""+maxH+"\",false);",delay);
	//fade in text
	ibox.style.color="#8388ff";
	setTimeout("fadeText(\""+mybox+"txt\",\"#8388ff\",\"#000000\")",500+delay);
}

/*
function prepElem(elemName) {
	elem = document.getElementById(elemName);
	elemc = document.getElementById(elemName+"txt");
	elemc.style.height="10px";
	elem.style.width="0px";
	//elem.style.visibility="visible";
}*/

function stripPX(str) {  //returns css-returned "px" dimension without the "px"
	return str.substr(0,str.length-2);
}

function tween1(t, tf) {
	//motion type 1
	mS=75;
	if (tf-t > mS) s = Math.exp((t-8)/10);
	else if (tf-t>36) s=(6/7)*(tf-t)-8;
	else s=(tf-t)/3;
	if (s>tf-t) {s=tf-t;}
	if (s>mS) {s=mS;}
	return s;
}

function tween2(t, tf) {
	//motion type 2
	mS=150;
	if (tf-t > mS+10) s = Math.exp((t-2)/5);
	else if (tf-t>20) s= 15;
	else s=(tf-t)/3;
	if (s>tf-t) {s=tf-t;}
	if (s>mS) {s=mS;}
	return s;
}

function slowTween(t,tf) {
	//slow motion, used for text fade-in
	s=(tf-t)/30;
	s = (s<0)?Math.floor(s):Math.ceil(s);
	return s;
}

function linTween(ts,tf) {
	//linear motion
	s = (tf-ts)/40;
	return s;
}

function fastTween(ts,t,tf) {
	//faster motion
	dtf = Math.abs(tf-t);
	dfs = Math.abs(t-ts);
	dir = (tf>ts)?1:-1;
	mS=150;

	if (dtf > mS+10) s = Math.exp((dfs)/5);
	else if (dtf>20) s= dtf/2;
	else s=(dtf)/3;

	if (s>dtf) {s=dtf;}
	if (s>mS) {s=mS;}

	s = Math.ceil(s);	
	s *=dir;
	return s;
}


function tweenBoxH(bname,sH,tH) {
	box = document.getElementById(bname);
	cH = stripPX(box.style.height)*1;
	if (cH == tH) {
		box.style.height="";
		return;
	}
	cH+=fastTween(sH,cH,tH);
	box.style.height=cH+"px";
	tbhTimer = setTimeout("tweenBoxH(\""+bname+"\","+sH+","+tH+")",25);
}

function growBoxH(boxName, cH, maxH) {
	//expand box's height by tween1
	box=document.getElementById(boxName);
	if (cH < maxH-1) {
		//not there yet, keep growing
		nH = cH + tween1(cH,maxH);
		if (nH>maxH) {nH=maxH};
		box.style.height = Math.floor(nH)+"px";
		gbhTimer = setTimeout("growBoxH(\""+boxName+"\","+nH+","+maxH+");",25);
	} else {
		//reset height style to default (resizes with window)
		box.style.height="";
	}
}

function growBoxW(boxName, cW, maxW, maxH, thresh) {
	//expand box's width by tween2
	//if past threshhold, start gorwin box's height
	if (!thresh && maxW-cW < (.4*maxW)) {
		growBoxH(boxName+"txt",10,maxH);
		thresh=true;
	}
	
	box=document.getElementById(boxName);
	if (cW < maxW-1) {
		//not there yet, keep growing
		nW = cW + tween2(cW,maxW);
		if (nW>maxW) {nW=maxW};
		box.style.width = Math.floor(nW)+"px";
		gbwTimer = setTimeout("growBoxW(\""+boxName+"\","+nW+","+maxW+","+maxH+","+thresh+");",25);
	} else {
		//reset box's width style so that it resizes with window
		box.style.width="";
	}
}/**/

function fadeText(boxName, sRGB, tRGB) {
	//fade text from sRGB to tRGB (hex strings without #)
	
	//get R, G, B values
	sR=sRGB.substr(1,2);
	sG=sRGB.substr(3,2);
	sB=sRGB.substr(5,2);
	tR=tRGB.substr(1,2);
	tG=tRGB.substr(3,2);
	tB=tRGB.substr(5,2);
	
	//set color
	box=document.getElementById(boxName);
	box.style.color=colStr(sR,sG,sB);
	
	if (sRGB == tRGB) return;   //if we're done
	//else calculate next call:
	
	//convert hex strings to decimal values
	sRv = parseInt(sR,16);
	sGv = parseInt(sG,16);
	sBv = parseInt(sB,16);
	tRv = parseInt(tR,16);
	tGv = parseInt(tG,16);
	tBv = parseInt(tB,16);
	
	//calculate next color value
	sRv+=slowTween(sRv,tRv);
	sGv+=slowTween(sGv,tGv);
	sBv+=slowTween(sBv,tBv);

	//convert decimal values to hex
	sR=d2h(sRv);
	sG=d2h(sGv);
	sB=d2h(sBv);
	tR=d2h(tRv);
	tG=d2h(tGv);
	tB=d2h(tBv);
	
	//set up next call to fadeText
	sCol=colStr(sR,sG,sB);
	tCol=colStr(tR,tG,tB);
	clearTimeout(ftTimer);
	ftTimer = setTimeout("fadeText(\""+boxName+"\",\""+sCol+"\",\""+tCol+"\");",25);
}

function colStr(r,g,b){
	//take r, g, b, value, make hex color code
	return "#"+r+""+g+""+b;
}

var hD="0123456789ABCDEF"
function d2h(d) { //SOURCE: about.com, Stephen Chapman, modified for 2-hexit output only
	var h=hD.substr(d&15,1);
	while(d>15) {d>>=4; h=hD.substr(d&15,1)+h;}
	if (h.length==1) h="0"+h;
	return h;
}

function alertme() {
	alert("Alerting you.");
}