/**Generated on 03-09-2010 - 4:06 pm**/ 
function drawPage(){
	encloseDivsByName("content");
	rs = "";
	blockJs();
	fixSub();
	ajaxpage("/users/checkLogin.php?u="+getCookie("username"),"login");
	ajaxpage("/sidebar.php?u="+getCookie("username"),"sidebar-content");
	//ajaxpage("/js/setPrefs.php?colorscheme=red","mainContent");
	setHandling();
}

function blockJs(){
	document.body.innerHTML += '<iframe name="serverOutput" id="serverOutput" src="" style="display:none;"></iframe>';
	serverOutput = document.getElementById('serverOutput');
	serverOutput.src="/js/blockJs.php";
	setTimeout(function(){serverOutput.src = "";},200);
}

function encloseDivsByName(eName){
	divs = document.getElementsByTagName("div");
	dName="";
	out="";
	da=new Array();
	for(k=divs.length; k>0; k--){
		theDiv = divs.item(k-1);
		dName = theDiv.getAttribute("name");
		if(dName == eName){
			dm = "";
			if(theDiv.parentNode.getElementsByTagName("div").item(0) == theDiv)dm="0px";
			encloseElement(theDiv, dm);
		}
	}
}
function encloseElement(e, tMargin){
	initStr = '<div class="cap"><div class="tl"><div class="tr"><div class="top"></div></div></div></div><div class="sl"><div class="sr">';
	closeStr = '</div></div><div class="cap"><div class="bl"><div class="br"><div class="bottom"></div></div></div></div>';
	cDiv = e.cloneNode(true);
	var ctDiv = document.createElement('div');
	cDiv.style.marginTop = "0px";
	ctDiv.className = "container";
	if(tMargin != ""){
		ctDiv.style.marginTop = tMargin;
	}
	ctDiv.appendChild(cDiv);
	ctData = initStr + ctDiv.innerHTML + closeStr;
	ctDiv.innerHTML = ctData;
	e.parentNode.replaceChild(ctDiv, e);
}

function replaceTitle(txt){
	theTitle = document.getElementsByTagName("title").item(0);
	ctDiv = document.createElement('title');
	ctDiv.innerHTML = txt;
	theTitle.parentNode.replaceChild(ctDiv, theTitle);
}

function handleScrolling(){
	client = navigator.appName;
	if(client=="Microsoft Internet Explorer"){
		//document.body.scrollTop = 1;
		yPos = document.documentElement.scrollTop;
	}
	else{
		yPos = parseInt(window.pageYOffset);
	}
	//alert(yPos);
	offset = parseInt(document.getElementById("sideNav").className) - 5;
	if(yPos >= offset){
		document.getElementById("sideNav").style.top = 5 + "px";
		document.getElementById("sideNav").style.position = "fixed";
	}
	if(yPos < offset){
		document.getElementById("sideNav").style.position = "static";
	}
	//alert(String(parseInt(absPos) + parseInt(yPos)));
}

function clickHandler(){
	getPage(this.href);
	return false;
}
function setHandling(){
	var aTags = document.getElementsByTagName("a");
	for(i=0; i<aTags.length; i++){
		var href = aTags.item(i).href;
		if(href.indexOf("#") == -1 && href.indexOf("perpetualtechnologies") > -1){
			aTags.item(i).onclick = clickHandler;
		}
	}
}

function fixSub(){
	offs = 0;
	if(window.innerHeight < document.height){
		offs = -8;
	}
	sidebarHeight = document.getElementById("sidebar").offsetHeight;
	if(document.getElementById("sideNav") != null){
		document.getElementById("sideNav").className = parseInt(document.getElementById("sideNav").offsetTop);
	}
	//document.getElementById("sidebar").style.top=((parseInt(window.innerHeight) - parseInt(sidebarHeight)) / 6) + "px";
	document.getElementById("sidebar").style.top=8 + "px";
}

function showNav(){
	handleNav(250, parseInt(document.getElementById("ctSide").offsetHeight));
	setTimeout(function(){document.getElementById("sideHandle").onclick = hideNav;}, 1000);
}

function hideNav(){
	handleNav(11, 40);
	setTimeout(function(){document.getElementById("sideHandle").onclick = showNav;}, 1000);
}

function handleNav(tWidth, tHeight){
	minHeight = 90;
	if(tHeight < minHeight && tHeight > 40){
		tHeight = minHeight;
	}
	dimStr = document.getElementById("sidebar").attributes['name'].value;
	dimVars = dimStr.split("_");
	initWidth = 11;
	initHeight = 40;
	cWidth = parseInt(document.getElementById("sidebar").style.width);
	cHeight = parseInt(document.getElementById("sideContent").style.height);
	steps = 10;
	speed = ((Math.PI) / steps);
	ellipseWidth = tWidth - cWidth;
	ellipseHeight = tHeight - cHeight;
	linedist = Math.sqrt(Math.pow(ellipseWidth, 2) + Math.pow(ellipseHeight, 2));
	incline = Math.atan(ellipseWidth / ellipseHeight);
	//incline = Math.asin(ellipseWidth / linedist);
	//incline = Math.acos(ellipseHeight / linedist);
	dir = Math.abs(ellipseWidth) / ellipseWidth;
	// Vertical Direction (Inverts Path): set related to var dir or set dir as constant
	vDir = -dir;
	initAngle = (Math.PI/2) * -dir;
	/////////////
	i=0;
	thisAngle = initAngle;
	while (i<=steps){
		rX = linedist / 2;
		//Curve Height
		rY = 30;
		xPos = rX + (Math.sin(thisAngle) * rX);
		yPos = (Math.cos(thisAngle) * rY) * vDir;
		
		// rotate point (fix problem w/ negative yPos)
		dist = Math.sqrt(Math.pow(xPos, 2) + Math.pow(yPos, 2));
		cAngle = Math.atan(xPos / yPos);
		newAngle = (-initAngle) + (cAngle + incline); //problem with initangle
		newX = Math.sin(newAngle) * dist * vDir;
		newY = Math.cos(newAngle) * dist * vDir;
		if(newX < 0)newX = 0;
		if(newY < 0)newY = 0;
		if(newX > Math.abs(ellipseWidth))newX = Math.abs(ellipseWidth);
		if(newY > Math.abs(ellipseHeight))newY = ellipseHeight;
		setSideSize(newX + initWidth, newY + initHeight, (i + 1) * dir);
		thisAngle -= speed;
		i++;
	}
}

function setSideSize(daWidth, daHeight, timeout){
	setTimeout(function(){
					dir = timeout / Math.abs(timeout);
					theAlpha = 0;
					if(dir == -1){
						theAlpha = 100 - (10 * Math.abs(timeout));
					}
					else{
						theAlpha = (10 * Math.abs(timeout));
					}
					document.getElementById("sidebar").style.width = daWidth + "px";
					document.getElementById("sideContent").style.height = daHeight + "px";
					document.getElementById("ctSide").style.opacity = theAlpha / 100;
					document.getElementById("ctSide").style.filter = "alpha(opacity=" + theAlpha + ")";

						}, 50 * Math.abs(timeout));
}

function checkField(field){
	fname = field.attributes['name']
	if(fname == "email"){
		//check email with regex
	}
	if(fname == "username"){
		responseDiv = "response-" + fname;
		ajaxpage("lookup.php?f=" + fname, responseDiv);
	}
}
window.onload = drawPage;
window.onscroll = handleScrolling;
window.onresize = fixSub;
//window.onunload = blockJs;