var sizer = 0;
var captionMover = 0;
var badgesizer = 0;
var badgeYposHack = 15;
var badgeStatus = 0;
var currBadgeTitle = "";
var currBadgeBlurb = "";
var currBadgeNav = "";
var sizeSpeed = 17;
//if (navigator.appName=="Netscape"){sizeSpeed=10}; // speed things up for pokey Mozilla
var sizeInc = 1.5708 / sizeSpeed;
var origW = 0;
var origH = 0;
var isNetscape = (document.layers) ? true : false;
var currSpan = "";
var specialData = "";
var currAnim = "";
var tow = 0;
var toh = 0;
var toUrl = "";
var currCaption = "";
var bgSat = 25	;
var bgVal = 50;
var magicBoxSat = 75;
var magicBoxVal = 30;
var newHue = 100;
var currHue = 100;
var alphaArr = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
/////////////////////////////////////////////////////////////////
function randomlistitem(listname) { return listname[Math.round(Math.random()*(listname.length-1))]; }
/////////////////////////////////////////////////////////////////
function randomint(taco) { return Math.round(Math.random()*(taco-1)); }
/////////////////////////////////////////////////////////////////
function DEC_to_HEX(dec) {var n_ = Math.floor(dec / 16); var _n = dec - n_ * 16; return alphaArr[n_] + alphaArr[_n];}
/////////////////////////////////////////////////////////////////
function HEX_from_HSV(h,s,v) {
	// convert values. h = max value of 360; s = max 100; v = max 100 (photoshop-style values for convenience)
	h = (h * 255) / 360;
	s = (s * 255) / 100;
	v = (v * 255) / 100;
	h *= 6; 
	range = h % 255; 
	phase = Math.floor(h / 255); 
	sat = s / 255;
	val = v / 255;

	var tmp = new Array(3);
	var rgbt = new Array(3);
	var mid = (7 - phase) % 3;
	tmp[Math.floor((phase + 7) / 2) % 3] = 255;
	tmp[(Math.floor(phase / 2) + 5) % 3] = 0;
	if ((phase % 2) == 1) tmp[mid] = 255 - range;
	else tmp[mid] = range;
	for (i=0; i<3; i++) 
		{
		tmp[i] = 255 - ((255 - tmp[i]) * sat);
		tmp[i] *= val;
		rgbt[i] = Math.round(tmp[i]);
		}
	return DEC_to_HEX(rgbt[0]) + DEC_to_HEX(rgbt[1]) + DEC_to_HEX(rgbt[2]);
}/////////////////////////////////////////////////////////////////
function initColors() {
	setCellColor('magicBoxCell', HEX_from_HSV(newHue, magicBoxSat, magicBoxVal));
	document.bgColor = HEX_from_HSV(newHue, bgSat, bgVal);
}
/////////////////////////////////////////////////////////////////
function setCellColor(whichCell, whichColor) {
	document.getElementById(whichCell).style.backgroundColor = whichColor;
}
/////////////////////////////////////////////////////////////////
function getMagicBoxSize() {
	origW = document.Bshadow.width;
	origH = document.Rshadow.height;
}
/////////////////////////////////////////////////////////////////
function loadLayer(orig, copy) {
	document.getElementById(copy).innerHTML = document.getElementById(orig).innerHTML;
}
/////////////////////////////////////////////////////////////////
function launchFrame(w, h, whichHue, url) {
	if(sizer != 0)
		{
			return;
		} else {
			tow = w;
			toh = h;
			currHue = whichHue;
			toUrl = url;
			changelayercontent('magicBoxCell', "");
			animFrame();
		}

}
/////////////////////////////////////////////////////////////////
function animFrame () {
	if(sizer >= 1.5708)
		{
			sizer = 0;
			resizeBox(tow,toh);
			newHue = currHue;
			newCellColor = HEX_from_HSV(newHue, magicBoxSat, magicBoxVal);
			setCellColor('magicBoxCell',newCellColor);
			document.bgColor = HEX_from_HSV(newHue, bgSat, bgVal);
			origW = tow;
			origH = toh;
	
			str = "\<iframe name=\"mainFrame\" id=\"mainFrame\" src=\"" + toUrl+ "\" scrolling=no frameborder=0 width=" + tow + " height=" + toh + " align=\"center\" style=\"background-color:transparent;\"\>\<\/iframe\>"
			changelayercontent('magicBoxCell', str);
			toUrl = "";			

		} else {
			resizeBox(origW + ((tow - origW) * Math.sin(sizer)), origH + ((toh - origH) * Math.sin(sizer * 1.000)));
			thisHue = newHue + ((currHue - newHue) * Math.sin(sizer));
			setCellColor('magicBoxCell', HEX_from_HSV(thisHue, magicBoxSat, magicBoxVal));
			
			document.bgColor = HEX_from_HSV(thisHue, bgSat, bgVal);
			sizer = sizer + sizeInc;
			setTimeout('animFrame()',10);
		}
}
/////////////////////////////////////////////////////////////////
function initFrame() {
		frames['mainFrame'].document.bgColor = HEX_from_HSV(currHue, magicBoxSat, magicBoxVal);
}
/////////////////////////////////////////////////////////////////
function launchCaption(title, blurb, nav){
	changelayercontent('captiontitle', title);
	changelayercontent('captionblurb',blurb);
	changelayercontent('captionnav', nav);
	animCaption();
}
/////////////////////////////////////////////////////////////////
function animCaption(){
	divh = divHeight("captionBadge");
	desty = ((windowHeight()-toh)/2)/1.4-divh; // OH MY GOD TOTAL HACK HERE
	if(captionMover >= 1.5708)
		{
			captionMover = 0;
			movediv("captionBadge", (windowWidth() - divWidth("captionBadge"))/2, desty); // hack
			badgeStatus = 1;
		} else {
			byy = ((desty + divh) * Math.sin(sizer)) - divh;
			movediv("captionBadge", (windowWidth() - divWidth("captionBadge"))/2, byy); // hack
			captionMover = captionMover + sizeInc;
			setTimeout('animCaption()', 10);
		}
}
/////////////////////////////////////////////////////////////////
function hideCaptionBadge(){
	divh = divHeight("captionBadge");
	desty = ((windowHeight()-origH)/2)/1.4 - divh; // OH MY GOD TOTAL HACK HERE
	if(captionMover >= 1.5708)
		{
			captionMover = 0;
			movediv("captionBadge", (windowWidth() - divWidth("captionBadge"))/2, -(divh+badgeYposHack)); // hack
			badgeStatus = 0;
		} else {
			byy = desty - ((desty + divh + badgeYposHack) * Math.sin(sizer));
			movediv("captionBadge", (windowWidth() - divWidth("captionBadge"))/2, byy); // hack
			captionMover = captionMover + (sizeInc * 2);
			setTimeout('hideCaptionBadge()', 10);
		}
}
/////////////////////////////////////////////////////////////////
function fixCaptionBadge() {
	if (badgeStatus == 0){return};
	divh = divHeight("captionBadge");
	desty = ((windowHeight()-origH)/2)/1.4-divh-badgeYposHack; // OH MY GOD TOTAL HACK HERE
	movediv("captionBadge", (windowWidth() - divWidth("captionBadge"))/2, desty); // hack
}
/////////////////////////////////////////////////////////////////
function resizeBox(w, h) {
	document.Bshadow.width= w;
	document.Rshadow.height= h;
}
/////////////////////////////////////////////////////////////////
function windowWidth(){
	return (isNetscape) ? (window.innerWidth + document.scrollLeft) : (document.body.clientWidth + document.body.scrollLeft);
}
/////////////////////////////////////////////////////////////////
function windowHeight(){
	return (isNetscape) ? (window.innerHeight + document.scrollTop) : (document.body.clientHeight + document.body.scrollTop);
}
/////////////////////////////////////////////////////////////////
function movediv(who, tox, toy) {
	getDiv(who).left = tox;
	getDiv(who).top = toy;
}
/////////////////////////////////////////////////////////////////
function divWidth(who) {
	return parseInt(getDiv(who).width);
}
/////////////////////////////////////////////////////////////////
function divHeight(who) {
	return parseInt(getDiv(who).height);
}
/////////////////////////////////////////////////////////////////
function getDiv(who){
	return document.getElementById(who).style;
}
/////////////////////////////////////////////////////////////////
function changelayercontent(who,what) {
	document.getElementById(who).innerHTML = what;
}
/////////////////////////////////////////////////////////////////
function show(id) {
	document.getElementById(id).style.visibility = "visible";
}
/////////////////////////////////////////////////////////////////
function hide(id) {
	document.getElementById(id).style.visibility = "hidden";
}
/////////////////////////////////////////////////////////////////
function togglevis(id){
	if (document.getElementById(id).style.visibility == "visible"){
		document.getElementById(id).style.visibility = "hidden";
	} else {
		document.getElementById(id).style.visibility = "visible";
	}
}
/////////////////////////////////////////////////////////////////
function loadMainMenu(){
	launchFrame(210, 400, 200, 'magicsquare\/mainMenu.htm');
}
/////////////////////////////////////////////////////////////////
function loadMainMenuArchive(){
	launchFrame(210, 400, 200, 'magicsquare\/mainMenu_archive.htm');
}
/////////////////////////////////////////////////////////////////
function loadDesignsMenu(){
	launchFrame(500, 370, 245, 'magicsquare\/design.htm');
}
/////////////////////////////////////////////////////////////////
function loadWritingsMenu(){
	launchFrame(400, 500, 170, 'magicsquare\/writings.htm');
}
/////////////////////////////////////////////////////////////////
function loadGamesMenu(){
	launchFrame(330, 330, 90, 'magicsquare\/games.htm');
}
/////////////////////////////////////////////////////////////////
function loadStudiesMenu(){
	launchFrame(400, 460, 0, 'magicsquare\/studies.htm');
}
/////////////////////////////////////////////////////////////////
function loadNotionsMenu(){
	launchFrame(460, 320, 300, 'magicsquare\/notions.htm');
}
/////////////////////////////////////////////////////////////////
function loadAboutMenu(){
	launchFrame(400, 350, 38, 'magicsquare\/about.htm');
}
/////////////////////////////////////////////////////////////////
function loadChumsMenu(){
	launchFrame(450, 300, 350, 'magicsquare\/chums.htm');
}
/////////////////////////////////////////////////////////////////
function loadNewsMenu(){
//	launchFrame(700, 2000, 100, 'magicsquare\/sample.htm');
		testURL = '';
		testURL = document.location;
		testURL += ' ';
//		alert(testURL);
	 if (testURL.indexOf("www")!=-1) {
		launchFrame(700, 3000, 100, 'http:\/\/www.davidyoon.com\/blog\/blogger.htm'); // have to check url to account for MSIE security permissions paranoia
	} else {
		launchFrame(700, 3000, 100, 'http:\/\/davidyoon.com\/blog\/blogger.htm');
	}
}
/////////////////////////////////////////////////////////////////
function loadFotoFlow(){
	launchFrame(900, 580, 100, 'http:\/\/www.davidyoon.com\/fotoFlow\/index.htm');
}
/////////////////////////////////////////////////////////////////
function loadFlickrshow(){
	launchFrame(900, 760, 100, 'flickrshow.htm');
}
/////////////////////////////////////////////////////////////////
function launchIM2LevelCodes(){
	window.open('magicsquare/im2levelCodes.htm','codes','height=375, width=300, toolbar=no, menubar=no');
}
/////////////////////////////////////////////////////////////////
onresize = fixCaptionBadge;
 /////////////////////////////////////////////////////////////////
 function autoResize(){
 	var id='mainFrame';
    var newheight;

    if (!window.opera && !document.mimeType && document.all && document.getElementById){
        newheight=document.getElementById(id).contentWindow.document .body.offsetHeight;
    }else if(document.getElementById){
        newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
    }
	
	var hack=16;  // original value
//	var hack=64;
    document.getElementById(id).height= (newheight + hack) + "px";
	alert(newheight);
}
 /////////////////////////////////////////////////////////////////
function autoIframe(frameId){
        try{
                frame = document.getElementById(frameId);
                innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
                objToResize = (frame.style) ? frame.style : frame;
                objToResize.height = innerDoc.body.scrollHeight + 30;
        }
        catch(err){
                window.status = err.message;
        }
		
		initFrame();
}