// --------------------
// TIME ZONE ADJUSTMENT
// --------------------
var msoffset=(new Date()).getTimezoneOffset() * 60000;
var mos=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

// iteratively adjust each timestamp on the page
function timeadj() {
	var timeobj,i=0;
	var stamp,hr,ampm,min;
	while(timeobj=document.getElementById("time"+i)) {
		stamp=new Date(timeobj.innerHTML);
		stamp.setTime(stamp.getTime() - msoffset);
		if(timeobj.innerHTML.indexOf(":")>-1) {
			hr=stamp.getHours();
			ampm="PM"; if(hr<12) ampm="AM";
			if(hr>12) hr-=12; if(hr==0) hr=12;
			min=stamp.getMinutes(); if(min<10) min="0"+min;
			timeobj.innerHTML=mos[stamp.getMonth()] + " " + stamp.getDate() + ", " + stamp.getFullYear() + " " + hr + ":" + min + " " + ampm;
		} else {
			timeobj.innerHTML=mos[stamp.getMonth()] + " " + stamp.getDate() + ", " + stamp.getFullYear();
		}
		i++;
	}
}

// -------------------
// ANIMATION FUNCTIONS
// -------------------
var framerate=24; // script will attempt to animate at this rate
var k1=10; var k2=3; // constants used for animation movement
var sizecon; // reference to size measuring element
var objarr=new Array(); // array of animation object references

// strip "px" out of a pixel measurement
function getpx(strpx) {
	if(strpx.indexOf("px"))
		strpx=strpx.substring(0,strpx.indexOf("px"));
	if(!strpx) strpx=0;
	return eval(strpx);
}

// find the height of an element
// requires a measuring element id "sizecheck"
function getheight(text) {
	sizecon.innerHTML=text;
	var h=sizecon.offsetHeight;
	sizecon.innerHTML="";
	return h;
}

// moves animation object to endx,endy (animation type 0)
function moveto() {
	var dx=0; var dy=0;
	if(this.left!=this.endx) {
		var px=this.endx-this.left;
		if(px!=0) dx=k1*px/Math.abs(px)+px/k2;
		this.left+=dx;
		px=this.endx-this.left;
		if((px>0)!=(dx>0) || (px<0)!=(dx<0)) this.left=this.endx;
		if(Math.round(this.left)==this.endx) this.left=this.endx;
	}
	if(this.top!=this.endy) {
		var py=this.endy-this.top;
		if(py!=0) dy=k1*py/Math.abs(py)+py/k2;
		this.top+=dy;
		py=this.endy-this.top;
		if((py>0)!=(dy>0) || (py<0)!=(dy<0)) this.top=this.endy;
		if(Math.round(this.top)==this.endy) this.top=this.endy;
	}
	if(dx!=0) this.obj.style.left=Math.round(this.left)+"px";
	if(dy!=0) this.obj.style.top=Math.round(this.top)+"px";
	if(dx==0 && dy==0) this.obj.style.visibility="visible";
	return(dx!=0 && dy!=0);
}

// moves animation object to destination object (animation type 1)
function movetrack() {
	var dest=this.track;
	if(dest.offsetParent) {
		this.endx=dest.offsetLeft;
		this.endy=dest.offsetTop;
		while(dest=dest.offsetParent) {
			this.endx+=dest.offsetLeft;
			this.endy+=dest.offsetTop;
		}
	}
	return this.move();
}

// resizes animation object to endw x endh (animation type 2)
function resize() {
	var dw=0; var dh=0;
	if(this.width!=this.endw && this.endw!="-") {
		var pw=this.endw-this.width;
		if(pw!=0) dw=k1*pw/Math.abs(pw)+pw/k2;
		this.width+=dw;
		pw=this.endw-this.width;
		if((pw>0)!=(dw>0) || (pw<0)!=(dw<0)) this.width=this.endw;
		if(Math.round(this.width)==this.endw) this.width=this.endw;
	} if(this.height!=this.endh && this.endh!="-") {
		var ph=this.endh-this.height;
		if(ph!=0) dh=k1*ph/Math.abs(ph)+ph/k2;
		this.height+=dh;
		ph=this.endh-this.height;
		if((ph>0)!=(dh>0) || (ph<0)!=(dh<0)) this.height=this.endh;
		if(Math.round(this.height)==this.endh) this.height=this.endh;
	}
	if(this.sizetype=="s" && this.endw!=0 && this.width==this.endw)
		this.obj.style.width="auto";
	else if(dw!=0) {
		if(this.sizetype=="o") this.obj.width=Math.round(this.width);
		else this.obj.style.width=Math.round(this.width)+"px";
	}
	if(this.sizetype=="s" && this.baseh==false && this.height==this.endh)
		this.obj.style.height="auto";
	else if(dh!=0) {
		if(this.sizetype=="o") this.obj.height=Math.round(this.height);
		else this.obj.style.height=Math.round(this.height)+"px";
	}
	if(this.width==0 || this.height==0) {
		this.obj.style.visibility="hidden";
		this.obj.style.display="none";
	} else {
		this.obj.style.visibility="visible";
		this.obj.style.display="block";
	}
	return(dw!=0 && dh!=0);
}

// array of function pointers (for render)
var renderfunc=new Array(moveto,movetrack,resize);

// parses input arguments for creating/updating an animation object
function parseargs(func,args) {
if(func==0) { // moveto
	this.left=getpx(this.obj.style.left);
	this.top=getpx(this.obj.style.top);
	this.endx=0; this.endy=0;
	var dest=document.getElementById(args);
	if(dest.offsetParent) {
		this.endx=dest.offsetLeft;
		this.endy=dest.offsetTop;
		while(dest=dest.offsetParent) {
			this.endx+=dest.offsetLeft;
			this.endy+=dest.offsetTop;
		}
	}
} else if(func==1) { // movetrack
	this.left=getpx(this.obj.style.left);
	this.top=getpx(this.obj.style.top);
	this.track=document.getElementById(args);
	this.move=moveto;
} else if(func==2) { // resize
	this.baseh=false;
	var argarr=args.split(",");
	this.sizetype=argarr[0];
	if(this.sizetype=="o")
		this.width=this.obj.width;
	else if(this.obj.style.width!="auto")
		this.width=getpx(this.obj.style.width);
	if(this.sizetype=="o")
		this.height=this.obj.height;
	else if(this.obj.style.height!="auto")
		this.height=getpx(this.obj.style.height);
	else this.height=getheight(this.obj.innerHTML);

	if(argarr[1]!="-") this.endw=eval(argarr[1]);
	else this.endw="-";
	if(argarr[2].substr(argarr[2].length-1,1)=="t") {
		var toggle=0;
		var cut=document.getElementById("cut-"+this.id);
		var upcut=document.getElementById("upcut-"+this.id);
		if(argarr[2].length>1)
			toggle=eval(argarr[2].substr(0,argarr[2].length-1));
		if(this.height>toggle) {
			this.endh=toggle;
			this.baseh=true;
			if(cut) {
			cut.innerHTML=cut.innerHTML.replace(/hide filters/,"show filters");
			cut.innerHTML=cut.innerHTML.replace(/hide/,"read more");
			if(upcut)
				upcut.innerHTML="";
			}
		} else {
			this.endh=getheight(this.obj.innerHTML);
			if(cut) {
			cut.innerHTML=cut.innerHTML.replace(/show filters/,"hide filters");
			cut.innerHTML=cut.innerHTML.replace(/read more/,"hide");
			if(upcut)
				upcut.innerHTML="<img src='/img/more.gif'> Hide";
			}
		}
	} else if(argarr[2]!="-") this.endh=eval(argarr[2]);
	else this.endh="-";
}
return true;
}

// create new animation object
function animobj(objid,func,args) {
	this.id=objid;
	this.obj=document.getElementById(objid);
	this.render=renderfunc[func];
	this.parse=parseargs;
	this.parse(func,args);
	return true;
}

// handler function: update existing object, or create new one
function addobj(objid,func,args) {
	var i;
	for(i=0;i<objarr.length;++i)
		if(objarr[i]) if(objarr[i].id==objid) {
			objarr[i].render=renderfunc[func];
			objarr[i].parse(func,args);
			return true;
		}
	return objarr.push(new animobj(objid,func,args));
}

// animation loop
function animate() {
	var i;
	for(i=0;i<objarr.length;++i)
		objarr[i].render();
	setTimeout("animate()",1000/framerate);
	return true;
}

// initialize animation variables and processes
function initanimation() {
	sizecon=document.getElementById("sizecheck");
	animate();
	return true;
}

// ---------------
// OTHER FUNCTIONS
// ---------------


// used for addgame (parallels CGI title filtering)

var roman=new Array("0",
	"i","ii","iii","iv","v","vi","vii","viii","ix","x",
	"xi","xii","xiii","xiv","xv","xvi","xvii","xviii","xix","xx",
	"xxi","xxii","xxiii","xxiv","xxv","xxvi","xxvii","xxviii","xxix","xxx");

function filtertitle(title) {
	var i;
	var str=" " + title.toLowerCase() + " ";
	str=str.replace(/'s/,"s");
	str=str.replace(/^ (a|an|the) +/," ");
	str=str.replace(/: (a|an|the) +/g,": ");
	for(i=1;i<roman.length;++i) {
		var re=new RegExp(" ([^a-z0-9]*)" + roman[i] + "([^a-z0-9]*) ","gi");
		str=str.replace(re," $1" + i + "$2 ");
	}
	str=str.replace(/[^a-z0-9_]/g," ");
	str=str.replace(/ +/g," ");
	str=str.replace(/^ /,""); str=str.replace(/ $/,"");
	if(str.length>64) {
		var abbrev="";
		while(str.length+1+abbrev.length>64 && str.indexOf(" ")>=0) {
			var l=str.lastIndexOf(" ");
			abbrev=str.substr(l+1,1) + abbrev;
			str=str.substring(0,l);
		}
		str+=" " + abbrev;
	}
	str=str.replace(/ /g,"_");
	return str;
}

// collapse/expand all entries on page

function toggleall() {
	var i;
	for(i=0;i<entries.length;++i)
		addobj(entries[i],2,'s,-,100t');
	return true;
}

// ----------------
// CORE INITIALIZER
// ----------------

var entries=new Array();

function init() {
	var i;
	timeadj();
	initanimation();
	for(i=0;i<entries.length;++i) {
		if(getheight(document.getElementById(entries[i]).innerHTML)>100)
			document.getElementById("cut-"+entries[i]).innerHTML="<img src='/img/more.gif'> Click to read more<br>";
		else {
			document.getElementById(entries[i]).style.height="auto";
			entries.splice(i,1); --i;
		}
	}
	return true;
}

// ------------
// STATUS LISTS
// ------------

function listselect(type) {
	if(type=="a") {
		document.getElementById("asel").style.fontWeight="bold";
		document.getElementById("alist").style.height=300;
		document.getElementById("alist").style.display="block";
		document.getElementById("psel").style.fontWeight="normal";
		document.getElementById("plist").style.height=0;
		document.getElementById("plist").style.display="none";
		document.getElementById("csel").style.fontWeight="normal";
		document.getElementById("clist").style.height=0;
		document.getElementById("clist").style.display="none";
		return true;
	} else if(type=="p") {
		document.getElementById("asel").style.fontWeight="normal";
		document.getElementById("alist").style.height=0;
		document.getElementById("alist").style.display="none";
		document.getElementById("psel").style.fontWeight="bold";
		document.getElementById("plist").style.height=300;
		document.getElementById("plist").style.display="block";
		document.getElementById("csel").style.fontWeight="normal";
		document.getElementById("clist").style.height=0;
		document.getElementById("clist").style.display="none";
		return true;
	} else if(type=="c") {
		document.getElementById("asel").style.fontWeight="normal";
		document.getElementById("alist").style.height=0;
		document.getElementById("alist").style.display="none";
		document.getElementById("psel").style.fontWeight="normal";
		document.getElementById("plist").style.height=0;
		document.getElementById("plist").style.display="none";
		document.getElementById("csel").style.fontWeight="bold";
		document.getElementById("clist").style.height=300;
		document.getElementById("clist").style.display="block";
		return true;
	}
	return false;
}

// -----------------
// AJAT PREVIEW CODE
// -----------------

var req;
var preview;
var prev_count=0;

function reqChange() {
	if(req.readyState==4) {
		if(req.status==200)
			preview.innerHTML=req.responseText;
		else
			preview.innerHTML="<div align=center>An error has occurred: "+req.statusText+"</div>";
	}
	delete req;
	return true;
}

function sendreq(input,output) {
	preview=document.getElementById(output);
	req=new XMLHttpRequest();
	req.onreadystatechange=reqChange;
	req.open("POST","preview.cgi",true);
	req.send(prev_count+"|"+escape(input.value));
	prev_count++;
	return true;
}
