//pops up the contact page
function popup_contact() {
	window.open('contact.php','Contact','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=540,height=560');
	return false;
}

//pops up any page when the url is passed
function popup(thePage) {
	window.open(thePage,'','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=580');
	return false;
}

function primClick(which) {
	var num = which.id.substring(2);
	
	for (var i=0; i<secondSelected.length; i++) {
		//Removes the inline style of previously selected secondary links
		clearSecondStyle(secondSelected[i]);
	}
	secondSelected.length = 0;
	
	for (var i=0; i<primSelected.length; i++) {
		primSelected[i].style.cssText = "";
	}
	primSelected.length = 0;
	
	changeSeconds(num);
	changePrims(num);
	
}

function secondClick(which) {
	//if the to-be-selected secondary link is of the same group, don't do anything
	if (which.id != curID) {
		var primaryID = which.id.substring(2).split("-")[0];
		
		if (which.id.substring(2).split("-")[0] != curID.split("-")[0]) {
			for (var i=0; i<primSelected.length; i++) {
				//Removes the inline style of previously selected primary links
				clearPrimStyle(primSelected[i]);
			}
			//resets the length (clearing the array) so a new array of selected links can be populated
			primSelected.length = 0;
			
			for (var i=0; i<secondSelected.length; i++) {
				secondSelected[i].style.cssText = "";
			}
			secondSelected.length = 0;
			
			changePrims(primaryID);
			changeSeconds(primaryID);
		}
		
		loadContent(which.id);
		addAmoeba();
	}
}

function changePrims(theNum) {
	var primary = document.getElementById("primary-links");
	for (var i=0; i<primary.childNodes.length; i++) {
		if (primary.childNodes[i].nodeType == 1) {
			if (primary.childNodes[i].id.substring(2) == theNum) {
				primSelected.push(primary.childNodes[i]);
				changePrimStyle(primary.childNodes[i]);
			}
		}
	}
}

function changeSeconds(theNum) {
	var second = document.getElementById("secondary-links");
	for (var i=0; i<second.childNodes.length; i++) {
		if (second.childNodes[i].nodeType == 1) {
			if (second.childNodes[i].id.substring(2).split("-")[0] == theNum) {
				secondSelected.push(second.childNodes[i]);
				changeSecondStyle(second.childNodes[i]);
			}
		}
	}
}

function primRoll(which) {
	$(which).animate({
	  opacity: "1"
	}, 500 );
}

function primOut(which) {
	$(which).animate({
	  opacity: ".7"
	}, 500 );
}

function changePrimStyle(which) {
	which.style.backgroundColor = "rgb(70,104,54)";
	which.style.color = "rgb(254,254,254)";
}
function clearPrimStyle(which) {
	which.style.backgroundColor = "rgb(204,204,204)";
	which.style.color = "rgb(51,51,51)";
}

function changeSecondStyle(which) {
	$(which).show("slow");
}
function clearSecondStyle(which) {
	$(which).hide("slow");
}

var primSelected = new Array();
var secondSelected = new Array();

//IE's failure to support setAttribute properly led to the creation of amoebaStyle as a fix
var amoebaStyle = new Array();

var curID = "0-0";
//amoeba counter
var amoebaNum = 1;

function loadContent(theID) {
	var prim = theID.substring(2).split("-")[0];
	var sec = theID.substring(2).split("-")[1];
	
	newQ = document.createElement("h1");
	newQ.setAttribute("id", "q-"+theID); 
	$(newQ).hide();
	$(newQ).append(theData["primary"][prim-1]["secondary"][sec-1]["q"]);
	
	newA = document.createElement("h1");
	newA.setAttribute("id", "a-"+theID);
	$(newA).hide();
	$(newA).append(theData["primary"][prim-1]["secondary"][sec-1]["a"]);
	
	$(curQ).slideUp("slow", function(){ $(this).remove(); });
	$(curA).slideUp("slow", function(){ $(this).remove(); });
	
	$('#questionText').append($(newQ));
	$('#answerText').append($(newA));
	$(newQ).slideDown("slow");
	$(newA).slideDown("slow");
	
	curID = theID;
	curQ = newQ;
	curA = newA;
}


function addAmoeba() {
	if (amoebaNum < 6) {
		var amoebaDiv = document.getElementById("amoebas");
		//keeps count of the number of amoebas in the most recently created row
		var rowCount = 1;
		//adds new amoebas based on the number of amoebas in the last row
		for (var i=0; i<freshAmoebas.length; i++) {
			//error handling at its finest...
			if (freshAmoebas[i] != null) {
				var newAm1 = document.createElement("img");
				var src1;
				var src2;
				if (amoebaNum == 1) {
					src1="am-sm2"; src2="am-sm1";
				} else if (amoebaNum == 2) {
					src1="am-sm3"; src2="am-min1";
				} else if (amoebaNum > 2) {
					src1="am-tiny1"; src2="am-tiny2";
				}
				newAm1.setAttribute("src", "images/amoebas/"+src1+".png");
				newAm1.setAttribute("id", "am-"+amoebaNum+"-"+rowCount);
				rowCount ++;
				newAm1.setAttribute("style", "position: absolute; opacity: 0; filter: alpha(opacity=0); bottom:"+freshAmoebas[i].style.bottom+"; right:"+(parseInt(freshAmoebas[i].style.right.split("p")[0])/*+Math.abs(freshAmoebas[i].offsetLeft)*.75*/)+"px;");
				//creates the array of amoeba styles based on the current freshAmoebas
				var styleArray = {"position":"absolute", "opacity":"0", "filter":"alpha(opacity=0)", "bottom":freshAmoebas[i].style.bottom, "right":parseInt(freshAmoebas[i].style.right.split("p")[0])+"px"};
				//adds the style to the amoebaStyle array
				amoebaStyle.push(styleArray);
				
				var newAm2 = document.createElement("img");
				newAm2.setAttribute("src", "images/amoebas/"+src2+".png");
				newAm2.setAttribute("id", "am-"+amoebaNum+"-"+rowCount);
				rowCount ++;
				newAm2.setAttribute("style", "position: absolute; opacity: 0; filter: alpha(opacity=0); bottom:"+freshAmoebas[i].style.bottom+"; right:"+(freshAmoebas[i].style.right.split("p")[0]/*+Math.abs(freshAmoebas[i].offsetLeft)*.75*/)+"px;");
				//adds the style to the amoebaStyle array
				amoebaStyle.push(styleArray);
				
				amoebaDiv.appendChild(newAm1);
				amoebaDiv.appendChild(newAm2);
			}
		}
		//gets the width so new amoebas can be created in the center of the parent amoeba
		var parentWidth = freshAmoebas[0].offsetWidth;
		//clear amoeba length
		freshAmoebas.length = 0;
		//create new array of fresh amoebas
		for (var i=1; i<rowCount; i++) {
			freshAmoebas.push(document.getElementById("am-"+amoebaNum+"-"+(i)));
			//fix for IE, so styles get applied
			freshAmoebas[freshAmoebas.length-1].style.position = amoebaStyle[freshAmoebas.length-1]["position"];
			freshAmoebas[freshAmoebas.length-1].style.opacity = amoebaStyle[freshAmoebas.length-1]["opacity"];
			freshAmoebas[freshAmoebas.length-1].style.filter = amoebaStyle[freshAmoebas.length-1]["filter"];
			freshAmoebas[freshAmoebas.length-1].style.bottom = amoebaStyle[freshAmoebas.length-1]["bottom"];
			freshAmoebas[freshAmoebas.length-1].style.right = amoebaStyle[freshAmoebas.length-1]["right"];
			
			if (isEven(i) ) {
				$(freshAmoebas[freshAmoebas.length-1]).animate({
				  right: "+="+Math.floor(((90/amoebaNum)+(parentWidth/2)+((Math.random()*8)-4)))+"px",
				  bottom: "-="+Math.floor((((Math.random()*8)-4)+30))+"px",
				  opacity: "1"
				}, 1000, "linear", function() {
					//after it's done fading in, remove the opacity from the inline style. helps in IE
					$(this).css('opacity','');
				});
				//alert((90/amoebaNum)+(parentWidth/2)+freshAmoebas[freshAmoebas.length-1].offsetWidth);
			}
			else {
				$(freshAmoebas[freshAmoebas.length-1]).animate({
				  right: "-="+Math.floor(((90/amoebaNum)+(parentWidth/2)+((Math.random()*8)-4)))+"px",
				  bottom: "-="+Math.floor((((Math.random()*8)-4)+30))+"px",
				  opacity: "1"
				}, 1000, "linear", function() {
					//after it's done fading in, remove the opacity from the inline style. helps in IE
					$(this).css('opacity','');
				});
			}
		}
		//keeps track of the number of rows
		amoebaNum ++;
		//clear amoeba style
		amoebaStyle.length = 0;
	}
	if (amoebaNum == 5) {
		$("#dialog-message").hide().dialog( "open" ).fadeIn("slow");
		$(".ui-dialog-overlay").hide().fadeIn("slow");
	}
}

function isEven(value){
	if (value%2 == 0)
		return true;
	else
		return false;
}
