/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
/* Pee or Poo - Break the Taboo
Script edited by AQS ===> Amazing Scripter of 06,07,08 */

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname //automatically determine root domain name
var bustcacheparameter=""
var curURL="";
var extendy = 0;

function ajaxpage(url, containerid){
var dontloader = true;
if (url.substr(0,4) != "side") {
	curURL = url;
	dontloader = false;
}
var page_request = false;
if (window.XMLHttpRequest) // if Mozilla, Safari etc
	page_request = new XMLHttpRequest();
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
else 
	return false;
page_request.onreadystatechange=function(){
loadpage(page_request, containerid, dontloader);
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null);
}

function loadpage(page_request, containerid, dontloader){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
{
document.getElementById(containerid).innerHTML=page_request.responseText;
	externalLinks();
if (!dontloader) {
	document.getElementById('loading').style.display = 'none';
	loader(curURL);
	}
else {
	document.getElementById('loading2').style.display = 'none';
	randomFact();
	displayShortcut();
	randomFeed();
	testYourself();
}
}
else //display 'loading' logo using DOM :)
{
	var temp = document.getElementById(containerid);
	while(temp.firstChild)
		temp.removeChild(temp.firstChild);
	if (!dontloader) 
		document.getElementById('loading').style.display = 'inline';
	else
		document.getElementById('loading2').style.display = 'inline';
}
}

function loadobjs(){ //loadobjs('.css', '.js', ...) 
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

function getCookie(Name) { 
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return null
}

function setCookie(name, value, days) {
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete cookie, respectively
var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function printfriendly()
{	
	window.open(curURL, "_blank");
}

function tellFriend()
{
	var initialsubj = "Your Friend Wants You To Check Out This Site!";
	var initialmsg = "Your friend would like you to visit our ThinkQuest Entry, Pee or Poo - Break the Taboo! It is hosted at http:\/\/library.thinkquest.org !";
	window.location = "mailto:" + "?subject=" + initialsubj + "&body=" + initialmsg;
}

//To replace target attribute which is deprecated... -.-
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

function popup(mylink, windowname, width, height)
{
	var hasRequiredVer = DetectFlashVer(8,0,0);
	if (hasRequiredVer)
		window.open(mylink, null, "width="+width+",height="+height+"scrollbars=no");
	else {
		alert("You do not have a valid flash plugin, you'll have to download it to view the content");
		window.open('http://www.adobe.com/go/getflashplayer');
	}
}

function repeaty() {
	if (document.getElementById("calc").offsetHeight > (document.getElementById("contenttop").offsetHeight+198)) {
		document.getElementById("contentoverflow").style.height = document.getElementById("calc").offsetHeight - document.getElementById("contenttop").offsetHeight + 92 + 'px';
		document.getElementById("background").style.height = document.getElementById("calc").offsetHeight + document.getElementById("content").offsetTop + 92 + 'px';
	}
	else {
		document.getElementById("contentoverflow").style.height = extendy + 'px';
		document.getElementById("background").style.height = extendy + 'px';
	}
}

function displayShortcut() {	
	if (getCookie("myShortcut") != null && document.getElementById("shortcut")) {
		var div = document.getElementById("shortcut");
		var shortcuts = getCookie("myShortcut").split(',');
		while (div.firstChild)
			div.removeChild(div.firstChild);
		var a = new Array(8);
		for (i=0; i<8; i++) {
			a[i] = document.createElement('a');
			a[i].appendChild(document.createTextNode(shortcuts[i]));
			a[i].onclick = function(e){ajaxpage(shortcuts[i],'contentpage');};
			div.appendChild(a[i]);
			div.appendChild(document.createElement('br'));
		}
	}
}

function addShortcut() {
	if (document.getElementById)
	{
		var div = document.getElementById('shortcut');
		var a = document.createElement('a');
		var atext = document.createTextNode(curURL);
		var br = document.createElement("br");
		a.appendChild(atext);
		a.onclick = function(e){ajaxpage(curURL,'contentpage');};
		var value = curURL + ',';
		for (i=0; i<div.childNodes.length-3; i++)
		{
			if ((i%2) == 0) 
				value += div.childNodes[i].firstChild.nodeValue + ',';
		}
		setCookie("myShortcut", value, 88);
		displayShortcut();
	}
}

function randomImg() {
	var index = Math.floor(Math.random() * 7 + 1);
	var placeholder = document.getElementById("randomimg");
	placeholder.setAttribute("src","pictures/random"+index+".jpg");
}

function switchDisplay(objectA, objectB)
{
	if (objectA.style.display == 'none') {
		objectB.style.display = 'none';
		objectA.style.display = 'inline';
	}
	else {
		objectB.style.display = 'inline';
		objectA.style.display = 'none';
	}
}

//For this site, functions to load whenever a page refreshes
function loader(url)
{
	if (url == 'results.html')
	{
		search_form();
		num_jse();
		out_jse();
	}
	else if (url == 'home.html')
	{
		randomImg();
	}
	fixAllLinks();
	textsize();
	repeaty();
	var atag = document.getElementsByTagName('a');
	var objs = new Array();
	for (i=0; i<atag.length; i++) {
		if (atag[i].getAttribute("id") && atag[i].getAttribute("id").substr(0,8) == "showhide" && atag[i].getAttribute("id").charAt(11) == "a") {
			objs[parseInt(atag[i].getAttribute("id").substr(8,3))] = new animatedcollapse("showhide"+atag[i].getAttribute("id").substr(8,3)+'c', 700, false, "block");
			atag[i].nextSibling.style.display = "none";
			atag[i].style.display = 'inline';
			atag[i].onclick = function(e){objs[parseInt(this.getAttribute("id").substr(8,3))].slideup();switchDisplay(this, this.nextSibling);};
			atag[i].nextSibling.onclick = function(e){objs[parseInt(this.getAttribute("id").substr(8,3))].slidedown();switchDisplay(this, this.previousSibling);};
		}
	}
	for (i=0; i<atag.length; i++) {
		if (atag[i].getAttribute('rel') && atag[i].getAttribute('rel').substr(0,8) == 'lightbox') {
			initLightbox();
			break;
		}
	}
}

function firstload() {
	if (document.getElementById) {
		extendy = document.body.offsetHeight - document.getElementById("contenttop").offsetHeight - document.getElementById("content").offsetTop;
		ajaxpage("home.html", "contentpage");
		ajaxpage("side_default.html", "panelpage");
		document.getElementById("q2").style.display = "none";
		Drag.init(document.getElementById("quicktools"));
		if(BrowserDetect.browser == "Explorer" && parseInt(BrowserDetect.version) == 6 && window.location.href.substr(window.location.href.length-8,8) != "ie6.html") {
			alert("You are using a non-standards compliant browser, and may experience styling problems. To correct this, please download the latest version of your browser. If you are unable to do so, click ok and we redirect you to a seperate page that does not contain extensive styling");
			location.replace("ie6.html"	);
		}
	}
	else
		return;
}
addLoadEvent(firstload);