﻿var speed = 50; // Delay between increments.
var inc = 5; // Increment amount -- also changes speed
var pid = request("pid");
if (pid == '' || pid == undefined) {
	pid = 1;
}

oPid = pid;
if (oPid == 6) {
	oPid = 4;
}

window.openMenuID = 0;

var rnum = Math.floor(Math.random()*1000000)

function initPage() {
	if (oPid != 1) {
		initMenu(oPid);
	}
	loadPage(pid);
}

/* MENU FUNCTIONS */
function initMenu(id) {
	cnt = 0;
	curid = document.getElementById("cur").value;
	
	if (curid != 1) {
		document.getElementById("menu"+curid).style.visibility = "hidden";
		document.getElementById("menu"+curid).style.height = "1px";
		document.getElementById("menu"+curid).innerHTML = "";
	}

	if (document.getElementById("cnt"+id)) {
		cnt = document.getElementById("cnt"+id).value;
	}

	if (cnt > 0) {
		curh = cnt * 20;
		resize(curh,0,"menu"+id);
		writeMenu(id);
		document.getElementById("menu"+id).style.visibility = "visible";
	}	
	
	document.getElementById("cur").value = id;
}

function openMenu(id) {
//	curid = document.getElementById("cur").value;
	curid = window.openMenuID;
	
	if (curid != 1) {
		if (document.getElementById("menu"+curid)) {
			document.getElementById("menu"+curid).style.visibility = "hidden";
			document.getElementById("menu"+curid).style.display = "none";
//			document.getElementById("menu"+curid).style.height = "1px";
//			document.getElementById("menu"+curid).innerHTML = "";
		}
	}

	window.openMenuID = id;
	cnt = document.getElementById("cnt"+id).value;

	if (cnt > 0) {
		curh = cnt * 20;
		resize(curh,0,"menu"+id);
		writeMenu(id);
		document.getElementById("menu"+id).style.visibility = "visible";
		document.getElementById("menu"+id).style.display = "block";
		loadPage(id);
	} else {
		loadPage(id);
	}	
	
	document.getElementById("cur").value = id;
}

function writeMenu(id) {
//	window.cid = window.fPid;
	menuvars = document.getElementById("m"+id).value;
	fulloutput = '<table width="100%" cellpadding="0" cellspacing="0">';
	marray = menuvars.split("//");
	for (i in marray) {
		t = marray[i];
		itemarray = t.split("||");
		itemid = itemarray[0];
		itemname = itemarray[1];
		itemoption = itemarray[2];
		if (window.cid == itemid) {
			thisclass = 'menuActive';
			//document.getElementById('curmenuitem').value = itemid;
			pid = itemid;
		} else {
			thisclass = 'menu';
		}
		
		if (itemoption == 0) {
			thislink = 'javascript:loadPage('+itemid+')';
		} else {
			thislink = 'javascript:loadPage('+itemid+')';
			//thislink = 'index.asp?pid='+itemid;
		}
		fulloutput = fulloutput + '<tr><td class="'+thisclass+'" id="menuitem'+itemid+'" nowrap><a href="'+thislink+'" onfocus="blur()">'+itemname+'</a></td></tr><tr><td height="8"><img src="images/spacer.gif" width="1" height="1"></td></tr>';
	}
	fulloutput = fulloutput + '</table>';
	document.getElementById("menu"+id).innerHTML = fulloutput;
}


function loadMenu() {
	if (window.menuHTML != '' && window.menuHTML != undefined) {
		document.getElementById('menuDiv').innerHTML = window.menuHTML;
		activateMenuItem(window.fPid);
	} else {
		load('/pages/menu.html?rnum='+rnum,'',loadMenuProcess);
	}
}

function loadMenuProcess(fResult) {
	document.getElementById('menuDiv').innerHTML = fResult;
	activateMenuItem(window.fPid);
}

function storeMenu() {
	if (document.getElementById('menuDiv')) {
		window.menuHTML = document.getElementById('menuDiv').innerHTML;
	} else {
		window.menuHTML = '';
	}
}

function activateMenuItem(id) {
//	alert(window.cid+'\n'+id);
	if (window.cid != undefined) {
		if (document.getElementById('menuitem'+window.cid)) {
			document.getElementById('menuitem'+window.cid).className = 'menu';
		}
	}
	
	if (document.getElementById('menuitem'+id)) {
		document.getElementById('menuitem'+id).className = 'menuActive';
	}
	
	window.cid = id;
}

/* END MENU FUNCTIONS */


function ScaleToFit(id) {
	var obj= document.getElementById(id);
	resize(obj.scrollWidth, obj.scrollHeight, 0, id);
}

function resize(height, current, id) {
	var obj = document.getElementById(id);
	
	if (current <= height) {
		i = current + inc;
		obj.style.height = i;
	}

	if (current <= height) {
		var t = window.setTimeout("resize("+height+", "+i+", '"+id+"')", speed);
	} else {
		obj.style.height = height;
	}
}

function loadPage(fpid) {
	window.fPid = fpid;
	storeMenu();
	loadTemplate(fpid);
}

function loadTemplate(fPid) {
	
	if (fPid == 1) {
		fType = 'home';
	} else if (fPid == 6) {
		fType = 'photo';
	} else {
		fType = 'inside';
	}
	if (document.getElementById('templateID')) {
		curTemplate = document.getElementById('templateID').value;
	} else {
		curTemplate = 'none';
	}
	
	if (curTemplate != fType) {
		if (fType == 'home') {
			load('/home.html?rnum='+rnum,'',loadTemplateProcess);
		} else if (fType == 'photo') {
			load('/phototemplate.html?rnum='+rnum,'',loadTemplateProcess);
		} else if (fType == 'inside') {
			load('/inside.html?rnum='+rnum,'',loadTemplateProcess);
		}
	} else {
		loadPageByAjax(fPid);
	}
}

function loadTemplateProcess(fResult) {
	document.getElementById('bodyDiv').innerHTML = fResult;
	if (window.fPid == 1) {
		loadHomeNews();
		loadHomeDownloads();
		replaceDivWithFlash('flash/slideshow.swf?rnum='+rnum,'homeflash',612,294,'',9,true,'homeFlashDiv');
		replaceDivWithFlash('flash/map.swf?rnum='+rnum,'mapp',307,161,'',9,true,'homeMapDiv');
	} else if (window.fPid == 6) {
		replaceDivWithFlash('flash/bottomleft.swf?rnum='+rnum,'botlef',307,284,'',9,true,'insideMapDiv');
		replaceDivWithFlash('flash/portfolio.swf?rnum='+rnum,'slideshow',663,639,'',9,true,'presentationDiv');
		replaceDivWithFlash('flash/footer.swf?rnum='+rnum,'footer',970,50,'',9,true,'footerDiv');
	} else {
		replaceDivWithFlash('flash/bottomleft.swf?rnum='+rnum,'botlef',307,284,'',9,true,'insideMapDiv');
		replaceDivWithFlash('flash/footer.swf?rnum='+rnum,'footer',970,50,'',9,true,'footerDiv');
	}
	loadMenu();
	loadPageByAjax(window.fPid);
}

function loadPageByAjax(fPid) {
	if (fPid == 1) {
		//replacedivwithflash here
	} else if (fPid == 6) {
		//replacedivwithflash here
	} else {
		document.getElementById('main').innerHTML = '<img src="images/loader.gif">';
		load('/files/pages/'+fPid+'.html?rnum='+rnum,'',loadPageProcess);
		activateMenuItem(window.fPid);
	}
}

function loadPageProcess(fResult) {
	document.getElementById('main').innerHTML = fResult;
}

function loadHomeNews() {
	load('/files/pages/news.html?rnum='+rnum,'newsreleases','');
}

function loadHomeDownloads() {
	load('/files/pages/downloads.html?rnum='+rnum,'downloads','');
}

document.onload = initPage();
