// ghost.js
// contains all subroutines needed by the ghost portion of the website

function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie55 = (this.version.indexOf('MSIE 5.5')>0)
	this.ieHi = ((this.version.indexOf('MSIE')>0) && !(this.ie4 || this.ie55))
	this.min = (this.ns||this.ie)
}
is = new BrowserCheck();
var ghostArray;
var ghostImg;
var ghostDiv;
var opacity;

function startGhosts() {
	// preload the images & give names, put on frames
	if (ghostArray == null) {
		var ghostSrc = new Array('/ghost/images/alanturing.gif', '/ghost/images/judyGarland.gif', '/ghost/images/eleanorRoosevelt.gif', '/ghost/images/oscarWilde.gif', '/ghost/images/ruthEllis.gif', '/ghost/images/karlUlrichs.gif');
		ghostArray = new Array(6);	
		for (i=0; i<6;i++) {
			ghostArray[i] = new Image;
			ghostArray[i].src = ghostSrc[i];
		}
	}

	// call other routines with timedelay
	ghostImg = document.getElementById('ghostImg');
	ghostDiv = document.getElementById('ghostDiv');
	chooseGhost();
}

function chooseGhost(){
	// randomly choose the specific image and location to show
	var x = Math.round(Math.random()*5)
	ghostImg.src = ghostArray[x].src;
	
	// find edges of the window (do this each time in case they have changed)
	H= document.body.clientHeight;
	W= document.body.clientWidth;
	OffsetX= document.body.scrollLeft;
	OffsetY= document.body.scrollTop;	

	// set location of ghost
	block=ghostDiv.style;
	block.xpos = parseInt(block.left);
	block.ypos = parseInt(block.top);
	block.visibility= "hidden";
	block.xpos = Math.round(Math.random()*(W-OffsetX-100)+OffsetX);
	block.left = block.xpos;
	block.ypos = Math.round(Math.random()*(H-OffsetY-100)+OffsetY);
	block.top = block.ypos;
	block.visibility= "visible";

	opacity=0;
	if (ghostImg) {
		highlighting=setInterval("highlightit()",500);
	}
}


function highlightit(){
	appearRate=1;

	// first make the image appear
	if (opacity < 25) {
		setOpacity(ghostImg, opacity)
		opacity += appearRate;
	}
	else if (opacity >= 25) {
		clearInterval(highlighting);
		highlighting=setInterval("vanish()",50);
	}
}

function vanish(){
	disappearRate=2;

	// make image disappear
	if (opacity > 0) {
		setOpacity(ghostImg,opacity);
		opacity -= disappearRate;
	}
	// kill routine, image is gone
	else if (opacity <= 0) {
		clearInterval(highlighting);
		// choose random time to wait
		setTimeout("chooseGhost()",Math.round(Math.random()*10000));
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win post 5.5
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// IE/Win 5.5
	obj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}


function preload(imgObj,imgSrc) {
	if (document.images) {
		eval(imgObj+' = new Image()')
		eval(imgObj+'.src = "'+imgSrc+'"')
	}
}

function changeImage(layer,imgName,imgObj) {
	if (document.images) {
		if (document.layers && layer!=null) eval('document.'+layer+'.document.images["'+imgName+'"].src = '+imgObj+'.src')
		else document.images[imgName].src = eval(imgObj+".src")
	}
}

function init() {
	preload('booksOn','/ghost/images/books_on.gif');
	preload('booksOff','/ghost/images/books_off.gif');
	preload('moviesOn','/ghost/images/movies_on.gif');
	preload('moviesOff','/ghost/images/movies_off.gif');
	preload('faqOn','/ghost/images/faq_on.gif');
	preload('faqOff','/ghost/images/faq_off.gif');
	preload('linksOn','/ghost/images/links_on.gif');
	preload('linksOff','/ghost/images/links_off.gif');
	preload('chatOn','/ghost/images/chat_on.gif');
	preload('chatOff','/ghost/images/chat_off.gif');
	preload('otherOn','/ghost/images/other_on.gif');
	preload('otherOff','/ghost/images/other_off.gif');

	startGhosts();
}