// JavaScript Document
var currPageLoc;
var pageTimer;
var win;
var maxTime = 250;
var fullPage;
var thePage;
function winget()
{
	win = window.location.toString();
}
function checkpage()
{
	winget();
	//alert(currPageLoc + " - " + win);
	if (currPageLoc != win)
	{
		clearTimeout(pageTimer);
		newPage();	
	}
	else
	{
		clearTimeout(pageTimer);
		pageTimer = self.setTimeout('checkpage()', maxTime);
	}
}

function changeLocation(page)
{
	winget();
	clearTimeout(pageTimer);
	if (win.indexOf('#') == -1)
	{
		window.location = win + '#' + page;
	}
	else
	{
		window.location = win.substr(0, win.lastIndexOf('#')) + '#' + page;
	}
	newPage();
}

function newPage()
{
	clearTimeout(pageTimer);
	winget();
	thePage = win;
	if(thePage.indexOf('#') == -1)
	{
		sendToActionScript('p0');
		changePage('main.php');
	}
	else
	{
		thePage = thePage.substr(thePage.lastIndexOf('#')+1);
		fullPage = thePage;
		thePage = thePage.substr(0, 4);
		sendToActionScript(thePage);
		
		switch(thePage)
		{
			case "p1": thePage = "spotlight.php";
			break;
			case "p2-0": thePage = "projects_web.php";
			break;
			case "p2-1": thePage = "projects_flash.php";
			break;
			case "p2-2": thePage = "projects_learn.php";
			break;
			case "p2-3": thePage = "projects_other.php";
			break;
			case "p3": thePage = "resume.php";
			break;
			case "p4": thePage = "about.php";
			break;
			case "p5": thePage = "contact.php";
			break;
			default: thePage = "main.php";
			break;
			
		}
		//thePage = thePage + '.php';
		changePage(thePage);
	}
}

function changePage(page)
{
	
	xmlhttp.open("GET", page, true);
	xmlhttp.onreadystatechange = stateChangeNav;
	xmlhttp.send(null);
}

function stateChangeNav()
{
	if (xmlhttp.readyState==1)
	{
		document.getElementById('content').innerHTML = '<div style="font-size:15px; font-weight:bold; font-family:Geneva, Arial, Helvetica, sans-serif; position:absolute; top:-12px; left:405px;">Loading...</div>';
		winget();
		currPageLoc = win;
  	} 
	if (xmlhttp.readyState==4)
	{
		document.getElementById('content').innerHTML = xmlhttp.responseText;
		
		if (thePage.indexOf('projects') >= 0)
		{
			
			var list = document.getElementById('projitems');
			if(fullPage.length < 7)
			{
				fullPage = list.childNodes[1].childNodes[1].getAttribute('id').toString();
				
			}
			document.getElementById(fullPage).style.background = "url(images/thumbback.jpg) repeat-x";
			changeProject(fullPage);
		}
		pageTimer = self.setTimeout('checkpage()', maxTime);
  	}
}

function updateProject(page)
{
	clearTimeout(pageTimer);
	winget();
	if (win.indexOf('#') == -1)
	{
		window.location = win + '#' + page;
	}
	else
	{
		window.location = win.substr(0, win.lastIndexOf('#')) + '#' + page;
	}
	winget();
	currPageLoc = win;
	changeProject(page);
	pageTimer = self.setTimeout('checkpage()', maxTime);
}
function changeProject(proj)
{
	var page = "project.php?page=" + proj;
	fullPage = proj;
	
	xmlhttp2.open("GET", page, true);
	xmlhttp2.onreadystatechange = stateChangeProj;
	xmlhttp2.send(null);
}

function stateChangeProj()
{
	if (xmlhttp2.readyState==1)
	{
		document.getElementById('projright').innerHTML = "<span class='getpage'>Getting Project Please Wait...</span>";
		var items = document.getElementsByName("thumbs");
		for (i=0; i<items.length; i++)
		{
			items[i].style.background = "transparent";	
		}
  	} 
	if (xmlhttp2.readyState==4)
	{
		//alert(xmlhttp.responseText);
		document.getElementById('projright').innerHTML = xmlhttp2.responseText;
		document.getElementById(fullPage).style.background = "url(images/thumbback.jpg) repeat-x";
  	}
}

//Flash Callback setup
var jsReady = false;
function isReady()
{
	return jsReady;
}
function pageInit()
{
	jsReady = true;
	//alert(ubrow);
	newPage();
	//alert(window["navFlash"]);
}
/*function thisMovie(movieName)
{
	if ("<?=$ubrow?>" == "MSIE")
	{
		return document[movieName];
	}
	else
	{
		return document[movieName];
	}
}*/
function sendToActionScript(value)
{
 	var mov = document["navFlash"];
	mov.sendToActionScript(value);
}



//create Ajax object
var xmlhttp = false;
var xmlhttp2 = false;
var xmlhttpform = false;
try
{
	xmlhttp = new XMLHttpRequest();
	xmlhttp2 = new XMLHttpRequest();
	xmlhttpform = new XMLHttpRequest();
}
catch(e)
{
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		xmlhttp2 = new ActiveXObject("Msxml2.XMLHTTP");
		xmlhttpform = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try 
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	
			xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
			xmlhttpform = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			alert("no ajax");
		}
	}
}
