var minTop = 0;
var minLeft = 0;
var theMiddle = 0;
var TheLeftEdge = 0;
var theActualHeight = 0;
var idealWidth = 989;
var theMinimumHeight = 500;
var M_contentJax ;
var timer="";
var imageJax ;
var isIE = navigator.appName.indexOf("Microsoft") != -1;
var moz=document.getElementById&&!document.all

function reBox() {
	var TheCheat = document.getElementById('cheater');
	var TheCBox = document.getElementById('truC');
	var RealSizeH = TheCheat.offsetHeight;
	var RealSizeW = TheCheat.offsetWidth;
	var RealCSizeH = TheCBox.offsetHeight;
	var RealCSizeW = TheCBox.offsetWidth;
	theMiddle = (RealSizeW / 2);
	TheLeftEdge = (theMiddle - (idealWidth / 2));
	var TheRightEdge = (theMiddle + (idealWidth / 2));
	if (TheLeftEdge <= minLeft) {
		TheLeftEdge = 0;
		TheRightEdge = idealWidth;
		theMiddle = idealWidth / 2;
	}	
	var TheContentHeight = RealCSizeH + document.getElementById('imagePane').offsetHeight + document.getElementById('footer').offsetHeight;	
	var nuMinimumHeight = theMinimumHeight;		
	if (TheContentHeight > nuMinimumHeight ) { nuMinimumHeight = TheContentHeight; }
	if ( nuMinimumHeight >= RealSizeH ) { var realHeight= nuMinimumHeight; } else { var realHeight= RealSizeH; }	
	theActualHeight = realHeight-5;
	document.getElementById('CFrame').style.height  = realHeight+"px";
	document.getElementById('leftNav').style.height = realHeight+"px";
	document.getElementById('rightNav').style.height= realHeight-32-220+"px";
	document.getElementById('copyright').style.top 	= realHeight-30+"px";
	document.getElementById('khaoMS').style.top 	= realHeight-30+"px";
	document.getElementById('spareDiv1').style.left = TheLeftEdge+1+"px";
	document.getElementById('spareDiv2').style.left = TheLeftEdge+2+"px";
	document.getElementById('topBar').style.left 	= TheLeftEdge+2+"px";
	document.getElementById('topBar').style.width 	= idealWidth-4+"px";	
	document.getElementById('CFrame').style.left 	= TheLeftEdge+273+"px";
	document.getElementById('CFrame').style.top   	= 0+"px";
	document.getElementById('rightNav').style.left 	= TheRightEdge-190+"px";
	document.getElementById('leftNav').style.top 	= 0+"px";
	document.getElementById('leftNav').style.left 	= TheLeftEdge-10+"px";
	document.getElementById('leftFar').style.top 	= 0+"px";
	document.getElementById('leftFar').style.left 	= TheLeftEdge-28+"px";
	document.getElementById('address').style.width 	= 590+"px";
	document.getElementById('address').style.left 	= TheRightEdge-605+"px";
	document.getElementById('address').style.top 	= 1+"px";
	document.getElementById('copyright').style.width = 450+"px";
	document.getElementById('copyright').style.left = TheRightEdge-460+"px";
	document.getElementById('khaoMS').style.width 	= 300+"px";
	document.getElementById('khaoMS').style.left 	= TheLeftEdge+10+"px";
	document.getElementById('cheater').style.zIndex = 1;
	document.getElementById('footer').style.top 	= document.getElementById('CFrame').offsetHeight-document.getElementById('footer').offsetHeight+"px";
	document.getElementById('footer').style.left 	= TheLeftEdge+273+"px";
	document.getElementById('bigTit').style.top 	= document.getElementById('imagePane').offsetHeight-35+"px";
}

var isLogo=0;
var logoPos=0;
function setLogo(posID){
	if (isLogo == 0) {
		var theLogo = new SWFObject("logo3.swf", "theLogoBox", 264, 52, "9.0", "#ffffff");
		theLogo.addParam("wmode", "transparent");
		theLogo.write("logoBox");
		isLogo = 1;
	}
	if(posID==0){
		 document.getElementById('logoBox').style.top 	= 60+"px";
		 document.getElementById('logoBox').style.left 	= TheLeftEdge+"px";
		document.getElementById('theLogoBox').style.height = 52+"px";
		document.getElementById('theLogoBox').style.width 	= 264+"px";		
	} else if(posID==1) {
		 document.getElementById('logoBox').style.top 	= 30+"px";
		 document.getElementById('logoBox').style.left 	= TheLeftEdge+247+"px";
		document.getElementById('theLogoBox').style.height = 90+"px";
		document.getElementById('theLogoBox').style.width 	= 464+"px";
	} else { alert("Error Code: 215 Please contact a site administrator if this continues.")}
	
}
function setImage(toWhat){
	if (toWhat) {
		if (document.getElementById('imagePane').innerHTML.indexOf(toWhat) == -1) { 
			document.getElementById('imagePane').innerHTML="<img src='images/uploads/banners/"+toWhat+"'> ";
			reload = setTimeout("reBox()",1000);
			// alert(document.getElementById('imagePane').innerHTML);
		}
	}
}
// go home on logo click
function do_goHome(sumVar) { window.location.href="index.cfm"; return true; }
// place main Nav
function navLeft(){ return TheLeftEdge+100; }
// Confirm Delete
function confirmdelete() { if (confirm("Are you sure you want to delete? - Delete is PERMANENT")) { return true; } else { return false;}}

// ###############################
// AJAX interactivity
// ###############################
function doM_Content(DocID) {  // engine for pulling Documents
	// doSubNav('up');
	fadeContent();
	M_contentJax=GetXmlHttpObject();
	var url="CMS/apps/_content.cfm";
	url=url+"?docid="+DocID;
	url=url+"&randInt="+Math.random();
	M_contentJax.onreadystatechange=doContentView;
	M_contentJax.open("GET",url,true);
	M_contentJax.send(null);
}
function doUpContent(DocID) {  // engine for pulling Documents from within Documents
	// doSubNav('up');
	fadeContent();
	M_contentJax=GetXmlHttpObject();
	var url="_content.cfm";
	url=url+"?docid="+DocID;
	url=url+"&randInt="+Math.random();
	M_contentJax.onreadystatechange=doContentView;
	M_contentJax.open("GET",url,true);
	M_contentJax.send(null);
}
function doM_FContent(FID) { // engine for pulling Folders
	// doSubNav('up');
	fadeContent();
	M_contentJax=GetXmlHttpObject();
	var url="CMS/apps/_content.cfm";
	url=url+"?fid="+FID;
	url=url+"&randInt="+Math.random();
	M_contentJax.onreadystatechange=doContentView;
	M_contentJax.open("GET",url,true);
	M_contentJax.send(null);
}
 function doF_Content(DocID) { // engine for pulling a path from anywhere to the main content window.
	// doSubNav('up');
	fadeContent();
	M_contentJax=GetXmlHttpObject();
	if (DocID.indexOf('?') == -1){	var url=DocID+"?randInt="+Math.random(); } else { var url=DocID+"&randInt="+Math.random(); }
	M_contentJax.onreadystatechange=doContentView;
	M_contentJax.open("GET",url,true);
	M_contentJax.send(null);
}

// check AJAX and define an AJAX object.
function GetXmlHttpObject() {
	var MyAjax=null;
	try {// Firefox, Opera 8.0+, Safari
  		MyAjax=new XMLHttpRequest();
  	}
	catch (e) {	// Internet Explorer
  		try { MyAjax=new ActiveXObject("Msxml2.XMLHTTP"); }
  		catch (e) { MyAjax=new ActiveXObject("Microsoft.XMLHTTP"); }
  	}
	return MyAjax;
} 
// content flipper
function doContentView() { 
	if (M_contentJax.readyState == 1) { document.getElementById('bigTit').innerHTML="Loading...";
	} else if (M_contentJax.readyState == 2) { document.getElementById('bigTit').innerHTML="Requesting...";
	} else if (M_contentJax.readyState == 3) { document.getElementById('bigTit').innerHTML="Receiving...";
	} else if (M_contentJax.readyState == 4) { 
		if ( M_contentJax.responseText.indexOf('<!-- split -->') != -1 ) {
			theSplit = M_contentJax.responseText.split('<!-- split -->');
			if (theSplit.length == 6){
				
				document.getElementById("bigTit").innerHTML=theSplit[2];
				document.title = "CHMIC - "+theSplit[2];
				document.getElementById("truC").innerHTML=theSplit[3]
				setLogo(theSplit[4]); // logo
				setImage(theSplit[5]); //image
				
			} else if (theSplit.length == 4){	
				document.getElementById("bigTit").innerHTML=theSplit[0];
				document.title = "CHMIC - "+theSplit[0];
				document.getElementById("truC").innerHTML=theSplit[1];
				setLogo(theSplit[2]); // logo
				setImage(theSplit[3]); //image
				
				
			}  else {
			document.getElementById("truC").innerHTML = "an error occured with the request, please retry. If this problem persists, please contact a site administrator<br><br>102b - bad data return: "+M_contentJax.responseText; 
			//			+"bad data: "+theSplit[0]+"____-____"+theSplit[1]+"____-____"+theSplit[2]+"____-____"+theSplit[3]+"____-____"+theSplit[4];
			}
		} else {
			document.getElementById("truC").innerHTML = "an error occured with the request, please retry. If this problem persists, please contact a site administrator<br><br>102a - bad data return:<br> "+M_contentJax.responseText;
			// 	";
		}
		fadeContent('up');
	} // else {
		// document.getElementById("truC").innerHTML = "an error occured with the request, please retry. If this problem persists, please contact a site administrator";
	// }
}
// older and 'bit' rollover action
function doMe(myDiv,myID){
	if(myID == 1){ myDiv.style.background=overColor; myDiv.style.color=txtOnCol;  // might be unused....
	} else if (myID == 2){	myDiv.style.background="#cee1ad"; myDiv.style.color="#FFFFFF";	myDiv.style.cursor="pointer"	// Used for Calendar day overs.
	
	} else if (myID == 3){	myDiv.style.backgroundImage = "url('images/LnavItemOn.gif')"; undoFMe();						// Used for Left Navigation Buttons	
	} else if (myID == 4){	myDiv.style.color="#990000";	myDiv.style.cursor="pointer";									// Used for bottom of Calendar Buttons
	} else if (myID == 5){	myDiv.style.background="#ffffcd"; myDiv.style.color="#000000";									// Used for LeftNav Subs
	} else if (myID == 6){	myDiv.style.background="#ebe8c2"; myDiv.style.border="#990000 1px solid"; 						// Used for Folder View
	} else if (myID == 7){	myDiv.src="images/bak_on.gif";			// Used for Back Roll Over
	} else if (myID == 8){	myDiv.src="images/fwd_on.gif";			// Used for FWD Roll Over
	} else if (myID == 9){	myDiv.src="images/login_on.gif";			// Used for LOGIN Roll Over
	}
}
function undoMe(myDiv,myID){
	if(myID == 1){ myDiv.style.background=baseColor; myDiv.style.color=txtOffCol;
	} else if (myID == 2){	myDiv.style.background="#ffffff"; myDiv.style.color="#336699";									// Used for Calendar day overs.
	
	} else if (myID == 3){	myDiv.style.backgroundImage = "url('images/LnavItemOff.gif')"; undoFMe();						// Used for Left Navigation Buttons
	} else if (myID == 4){	myDiv.style.color="#FFFFFF";																	// Used for bottom of Calendar Buttons
	} else if (myID == 5){	myDiv.style.background="#e7e3ad"; myDiv.style.color="#666666";									// Used for LeftNav Subs
	} else if (myID == 6){	myDiv.style.background="#ffffff"; myDiv.style.border="0px"; 									// Used for Folder View
	} else if (myID == 7){	myDiv.src="images/bak_off.gif";																	// Used for Back Roll Off
	} else if (myID == 8){	myDiv.src="images/fwd_off.gif";																	// Used for FWD Roll Off
	} else if (myID == 9){	myDiv.src="images/login_off.gif";			// Used for LOGIN Roll Off
	}
}


// ##############################
//  START ANIMATIONS
// ##############################
function initRNav() {	
	var baseHeight = 20;
	// alert('boing');
	var animElements = document.getElementById("rightNav").getElementsByTagName("div");
	for(var i=0; i<animElements.length; i++) { 
			animElements[i].onmouseover = heightChange;
		 	animElements[i].onmouseout = heightRestore;
		}
	function heightChange() {
		if (!this.currentHeight) { this.currentHeight = baseHeight; } 
		if (this.heightChangeInt) { window.clearInterval(this.heightChangeInt);	 }
			var toHeight = 30;
			doSubNavOver(this,this.currentHeight,toHeight,10,1,1);
		}
	function heightRestore() {
		if (!this.currentHeight) { return; 
		} else { 
			var toHeight = 20; 
			doSubNavOver(this,this.currentHeight,toHeight,10,3,.3); } // (elem,startHeight,endHeight,steps,intervals,powr) (minValue,maxValue,totalSteps,actualStep,powr)
		}
}

// ##############################
// Main & Content Frame Animation
// ##############################
function makeMotion(get2){
	//var theObj = document.getElementById('imagePane');
	//doChangeFrame(theObj.offsetHeight,get2,100,10,2);
}
function doChangeFrame(startHeight,endHeight,steps,intervals,powr) {
	var elem1 = document.getElementById('imagePane');
	var elem2 = document.getElementById('bigTit');
	var elem3 = document.getElementById('truC');
	// SmImgr('off');
	if (elem1.heightChangeInt) { window.clearInterval(elem1.heightChangeInt); }
	var actStep = 0;
	elem1.heightChangeInt = window.setInterval(
		function() {
			document.getElementById('rightNav').style.top = document.getElementById('imagePane').offsetHeight+10+150+"px";
			document.getElementById('truC').style.height = theActualHeight-document.getElementById('imagePane').offsetHeight+15+"px";
			elem1.currentHeight = easeInOut(startHeight,endHeight,steps,actStep,powr);
			elem1.style.height = elem1.currentHeight+"px";
			elem2.style.top = elem1.currentHeight+50+"px";
			elem3.style.top = elem1.currentHeight+10+"px";
			actStep++;
			
			if ((startHeight-1 >= endHeight) && (actStep > steps)) {				// shrank and done
				// SmImgr('on');
				window.clearInterval(elem1.heightChangeInt);
				reBox();
			} else if ((endHeight >= startHeight-1) && (actStep > steps)) {			//grew and done
				window.clearInterval(elem1.heightChangeInt);
				reBox();
			} else if (actStep > steps) { 
				window.clearInterval(elem1.heightChangeInt);
				reBox();
			}
		}
				,intervals)
}
function doChangeLogo(startHeight,endHeight,steps,intervals,powr) {
	var elem1 = document.getElementById('theLogoBox');
	
	if (elem1.heightChangeInt) { window.clearInterval(elem1.heightChangeInt); }
	if (elem1.widthChangeInt) { window.clearInterval(elem1.widthChangeInt); }
	if (elem1.topChangeInt) { window.clearInterval(elem1.topChangeInt); }
	if (elem1.leftChangeInt) { window.clearInterval(elem1.leftChangeInt); }
	var actStep = 0;
	elem1.heightChangeInt = window.setInterval(
		function() {
			elem1.currentHeight = easeInOut(startHeight,endHeight,steps,actStep,powr);
			elem1.style.height = elem1.currentHeight+"px";
			actStep++;
			
			if ((startHeight-1 >= endHeight) && (actStep > steps)) {				// shrank and done
				// SmImgr('on');
				window.clearInterval(elem1.heightChangeInt);
				reBox();
			} else if ((endHeight >= startHeight-1) && (actStep > steps)) {			// grew and done
				window.clearInterval(elem1.heightChangeInt);
				reBox();
			} else if (actStep > steps) { 
				window.clearInterval(elem1.heightChangeInt);
			}
			
			// need if here
				reBox();
			
			
		}
				,intervals)
}

function fadeContent(move) {
	elem = document.getElementById('truC');
	if (move == 'up') { 
		// alert('p');
		elem.currentAlpha = 0;
		toAlpha = 100; 
		fromAlpha=0; 
		elem.style.filter = "alpha(opacity=0"+fromAlpha+")";
		elem.style.KHTMLOpacity = fromAlpha/100;
		elem.style.MozOpacity = fromAlpha/100;
		elem.style.opacity = fromAlpha/100;
	} else { 
		toAlpha = 0; 
		fromAlpha=100;
	}	
	doAlphaChange(document.getElementById("truC"),fromAlpha,toAlpha,130,10,1);
}


// ##############################
// general Animation scripts
//###############################
function doAlphaChange(elem,startAlpha,endAlpha,steps,intervals,powr) {
	if (elem.alphaChangeInt) window.clearInterval(elem.alphaChangeInt);
	var actStep = 0;
	elem.alphaChangeInt = window.setInterval(
		function() {
			elem.currentAlpha = easeInOut(startAlpha,endAlpha,steps,actStep,powr);
			elem.style.filter = "alpha(opacity="+elem.currentAlpha+")";
			elem.style.KHTMLOpacity = elem.currentAlpha/100;
			elem.style.MozOpacity = elem.currentAlpha/100;
			elem.style.opacity = elem.currentAlpha/100;
			//elem.style.alpha = elem.currentAlpha+"px";
			actStep++;
			if (actStep > steps) { window.clearInterval(elem.alphaChangeInt); }
		},intervals)
}
function doHeightChange(elem,startHeight,endHeight,steps,intervals,powr) {
	if (elem.heightChangeInt) window.clearInterval(elem.heightChangeInt);
	var actStep = 0;
	elem.heightChangeInt = window.setInterval(
		function() {
			elem.currentHeight = easeInOut(startHeight,endHeight,steps,actStep,powr);
			elem.style.height = elem.currentHeight+"px";
			actStep++;
			if (actStep > steps) { window.clearInterval(elem.heightChangeInt); }
		},intervals)
}
function doWidthChange(elem,startHeight,endHeight,steps,intervals,powr) {
	if (elem.widthChangeInt) window.clearInterval(elem.widthChangeInt);
	var actStep = 0;
	elem.widthChangeInt = window.setInterval(
		function() {
			elem.currentWidth = easeInOut(startWidth,endWidth,steps,actStep,powr);
			elem.style.width = elem.currentWidth+"px";
			actStep++;
			if (actStep > steps) { window.clearInterval(elem.widthChangeInt); }
		},intervals)
}
function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}


