var curPage = "";
function Floater(src)
{
	this.d = document.createElement("div");
	this.o = document.createElement("img");
	this.o.src = src;
	this.d.className = "floater";
	this.d.appendChild(this.o);	
	document.body.appendChild(this.d);
	this.set();
}

Floater.prototype.set = function()
{
	this.x = Math.floor(Math.random() * (document.documentElement.clientWidth - 400) + 200);
	this.y = Math.floor(Math.random() * (window.screen.height - 300));
	this.dx = (Math.random() * 4 + 1) * (Math.floor(Math.random() * 2) == 1 ? 1 : -1);
	this.sx = Math.floor(Math.random() * 2 + 1);
	this.d.style.left = Math.floor(this.x);
	this.d.style.top = this.y;	
	this.step = 0;
}

Floater.prototype.reset = function()
{
	this.d.style.width = "";
	this.dx = (Math.random() * 4 + 1) * (Math.floor(Math.random() * 2) == 1 ? 1 : -1);
	if(this.dx > 0)
	{
		this.d.style.width = "";
		this.x = 0 - this.o.width;
	}
	else
	{
		this.x = document.documentElement.clientWidth;
		this.d.style.width = 0;
	}
	this.y = Math.floor(Math.random() * (window.screen.height - 300));
	this.sx = Math.floor(Math.random() * 2 + 1);
	this.d.style.left = Math.floor(this.x);
	this.d.style.top = this.y;	
	this.step = 0;	
}

Floater.prototype.update = function()
{
	this.step++;
	if(this.step == this.sx)
	{
		this.step = 0;
	}
	else
		return;		
	this.x += this.dx;	
	if(this.x <= 0 && this.dx < 0)
	{
		if(this.x <= 0 - this.o.width)		
			this.reset();
	}
	else if(this.x + this.o.width >= document.documentElement.clientWidth - 15 && this.dx > 0)
	{
		if(this.x + this.dx >= document.documentElement.clientWidth - 5)		
			this.reset();	
		else
		{
			this.d.style.width = document.documentElement.clientWidth - Math.floor(this.x);
		}
	}
	else if(this.x + this.o.width >= document.documentElement.clientWidth - 15)
	{
		if(this.x + this.o.width >= document.documentElement.clientWidth)
			this.d.style.width = document.documentElement.clientWidth - Math.floor(this.x);
		else
			this.d.style.width = '';
	}
	this.d.style.left = Math.floor(this.x) + "px";
}

var floaters;
var links;
var tug;
var dtug;
var tugwidth;
var bannerSrc;
function init()
{
	if(document.location.href.indexOf("loaded") == -1 && navigator.userAgent.indexOf("MSIE") == -1)
	{
		dtug = document.createElement("div");
		tug = document.createElement("img");
		dtug.appendChild(tug);
		tug.src = "images/tug.png";
		tug.id = 'tug';
		dtug.style.display = "none";
		dtug.style.position = "absolute";
		dtug.style.top = "50%";
		dtug.style.left = "0";
		dtug.style.overflow = "hidden";
		dtug.style.zIndex = 110;	
		document.body.appendChild(dtug);	
		tug.onload = tugStart;
		dtug.width = 0;
		dtug.style.width = "0px";
	}
	else
	{
		if(navigator.userAgent.indexOf("MSIE") != -1)
		{
			document.getElementById('bodytext').innerHTML = "<div id='IEWarn'>For the full website experience, please use a more standards compliant browser such as Google Chrome or Mozilla Firefox.</div>" + document.getElementById('bodytext').innerHTML;
		}
		ajaxCaller.getPlainText(getPageLoc() + ".html",setPageText);	
		document.getElementById('logo').style.display = "";
		document.getElementById('bodytext').style.display = "";
		addClouds();
	}
	setTimeout("detectPageChange()", 200);
}

function detectPageChange()
{
	document.title = "Welcome to the LASA Robotics BEST 2008 Web Site!";
	if(curPage != getPageLoc())
		goPage();
	setTimeout("detectPageChange()", 200);
}

function addClouds()
{
	floaters = Array(	new Floater("images/cloud1.png"),
						new Floater("images/cloud2.png"),
						new Floater("images/cloud3.png"),
						new Floater("images/cloud4.png"),
						new Floater("images/cloud5.png"),
						new Floater("images/cloud6.png"),
						new Floater("images/cloud1.png"),
						new Floater("images/cloud2.png"),
						new Floater("images/cloud3.png"));
	setTimeout("updateFloaters()", 30);
	if(document.location.href.indexOf("#loaded") == -1)
	{
		document.location = "#loaded_home";
	}
}

function goPage()
{
	document.title = "Welcome to the LASA Robotics BEST 2008 Web Site!";
	setTimeout("goPageReal()", 30);
}

function getPageLoc()
{
	var loc = "home";
	if(document.location.href.indexOf("#loaded_") != -1)
		loc = document.location.href.substr(document.location.href.indexOf("#loaded_")).replace("#loaded_", "");
	curPage = loc;
	return loc;
}

function goPageReal()
{
	document.title = "Welcome to the LASA Robotics BEST 2008 Web Site!";
	ajaxCaller.getPlainText(getPageLoc() + ".html", fadePageText);	
}

var fadeTo;
var opacity = 1;
function fadePageText(str)
{
	fadeTo = str;
	setTimeout("fadePageTextStep1()", 20);
	setTimeout("document.getElementById('text').style.opacity = 1;", 2000);
}

function fadePageTextStep1()
{
	var d = .05;
	if(document.getElementById('text').style.opacity - d <= 0)
	{
		opacity = 0;
		document.getElementById('text').style.opacity = 0;
		document.getElementById('text').innerHTML = fadeTo;
		setTimeout("fadePageTextStep2()", 20);		
	}
	else
	{
		opacity -= d;
		document.getElementById('text').style.opacity = opacity;
		setTimeout("fadePageTextStep1()", 20);
	}
}

function fadePageTextStep2()
{
	var d = .05;
	if(document.getElementById('text').style.opacity + d >= 1)
	{
		opacity = 1;
		document.getElementById('text').style.opacity = 1;
		myLightbox.updateImageList();
		document.title = "Welcome to the LASA Robotics BEST 2008 Web Site!";
	}
	else
	{
		opacity += d;
		document.getElementById('text').style.opacity = opacity;
		setTimeout("fadePageTextStep2()", 20);
	}
}

function tugStart()
{
	tugwidth = tug.width;
	ajaxCaller.getPlainText(getPageLoc() + ".html",setTugPageText);	
}

function setPageText(str)
{
	document.getElementById('text').innerHTML = str;
}

var setting = 0;
var pageText = '';
function setTugPageText(str)
{
	pageText = str;
	setTimeout("tugit()", 30);
}

var transiting = 0;
var block;
var added = false;
var adjusted = false
function tugit()
{
	if(!transiting) // Just began
	{
		dtug.style.width = 0;
		dtug.style.left = document.documentElement.clientWidth + "px";		
		dtug.style.display = '';
		transiting = true;
		block = document.createElement('div');
		block.style.zIndex = 75;
		block.innerHTML = 
'<div class="bodytext"><table id="nav">' +
'<tr>' +
'<td><a href="#loaded_home" onclick="goPage(this)">Home</a></td>' +
'<td><a href="#loaded_manu" onclick="goPage(this)">Plane Manufacturing</a></td>' +
'<td><a href="#loaded_best" onclick="goPage(this)">About BEST</a></td>' +
'<td><a href="#loaded_org" onclick="goPage(this)">Team Organization</a></td>' +
'<td><a href="#loaded_proc" onclick="goPage(this)">Design Process</a></td>' +
'<td><a href="#loaded_mem" onclick="goPage(this)">Memories</a></td>' +
'</tr>' +
'</table><div class="text">' +
pageText +
'</div></div>';		
		block.style.position = 'absolute';
		block.style.left = document.documentElement.clientWidth + "px";
		block.style.bottom = "0px";
		block.style.height = "82%";
		block.style.width = "0px";
		block.style.borderTop = "#000 2px solid";
		block.width = 0;
		block.style.overflow = "hidden";
		added = false;
	}
	else if(dtug.offsetLeft >= document.documentElement.clientWidth - tugwidth)
	{
		dtug.style.left = (dtug.offsetLeft - 30) + "px";
		dtug.style.width = document.documentElement.clientWidth - dtug.offsetLeft;
	}
	else if(dtug.offsetLeft + tugwidth <= 0)
	{
		transiting = false;
		adjusted = false;
		setTimeout("addBanner()", 30);	
		return;
	}
	else if(dtug.offsetLeft + tugwidth <= (document.documentElement.clientWidth / 2) - 280 && !adjusted)
	{
		document.getElementById('text').innerHTML = pageText;
		adjusted = true;
		document.getElementById('bodytext').style.display = "";
		document.body.removeChild(block);
	}
	else
	{
		dtug.style.left = (dtug.offsetLeft - 30) + "px";
		block.style.left = (dtug.offsetLeft + tugwidth - 0) + "px";
	}
	
	if(block.offsetLeft <= document.documentElement.clientWidth && block.width < 615)
	{
		if(!added)
		{
			document.body.appendChild(block);
			added = true;
		}
		block.width = document.documentElement.clientWidth - block.offsetLeft;
		block.style.width = block.width + "px";
		block.style.display = '';
	}
	else if(block.width > 615)
	{
		block.width = 615;
		block.style.width = "615px";
	}
	
	setTimeout("tugit()", 20);
}

function onclickreflex(id)
{
	links[id].go();
}

var b;
function addBanner()
{
	b = document.createElement("img");
	b.src = document.getElementById('logo').src;
	b.style.position = "absolute";
	b.onload = finishAddBanner;
}

function finishAddBanner()
{
	b.style.left = 0 - b.width;
	b.style.top = "5px";
	b.style.zIndex = 100;
	document.body.appendChild(b);
	bannerSrc = document.getElementById('logo').src;
	document.getElementById('logo').onload = finishAddBanner2;
	document.getElementById('logo').src = "./images/spacer.gif";
}

function finishAddBanner2()
{
	document.getElementById('logo').style.display = "";
	setTimeout("flyBanner()", 30);
}

function flyBanner()
{
	if(b.offsetLeft >= document.getElementById('logo').offsetLeft)
	{
		document.body.removeChild(b);
		document.getElementById('logo').src = bannerSrc;
		document.getElementById('logo').style.display = "";
		setTimeout("addClouds()", 30);	
		myLightbox.updateImageList();
	}
	else
	{
		b.style.left = (b.offsetLeft + 15) + "px";
		setTimeout("flyBanner()", 30);
	}
}

function updateFloaters()
{
	for(i = 0; i < floaters.length; i++)
		floaters[i].update();
	setTimeout("updateFloaters()", 30);
}