var _pageNavigator;
var _LinkHome, _LinkProducts, _LinkServices, _LinkAbout, _LinkContact, _LinkForum, _LinkLogin, _LinkLogoff, _LinkRegister, _UserIndic, _LinkBuyA, _LinkSef, _AdSpace;
var _NavTab, _NavRow;
var _selectedMainMenuOption = null;
var _pageTitle = null;
var _sideThing = null, _sideThing2=null;
var _footer = null; 
var _pageTopRef;
var _contentNavigator = null;
var _contentList = null;
var _ivalContentNav = null;

function showNavigator()
{
	_pageNavigator = document.createElement("DIV");
	_pageNavigator.className="pageNavigator";
	
	_NavTab = document.createElement("TABLE");
	_NavTab.cellPadding=0;
	_NavTab.cellSpacing=0;
	_NavTab.border=0;

	_NavRow = _NavTab.insertRow(0);

	_LinkHome = _NavRow.insertCell(0);
	_LinkHome.className="mainOption";
	_LinkHome.innerHTML = "News/Home";
	_LinkHome.onmouseover = setMainMenuOptionOver;
	_LinkHome.onmouseout = setMainMenuOptionOut;
	_LinkHome.onclick = selectMainMenuOption;
	_LinkHome.id = "NEWS";

	_LinkProducts = _NavRow.insertCell(1);
	_LinkProducts.className="mainOption";
	_LinkProducts.innerHTML = "Products";
	_LinkProducts.onmouseover = setMainMenuOptionOver;
	_LinkProducts.onmouseout = setMainMenuOptionOut;
	_LinkProducts.onclick = selectMainMenuOption;
	_LinkProducts.id = "PRODUCTS";

	_LinkServices= _NavRow.insertCell(2);
	_LinkServices.className="mainOption";
	_LinkServices.innerHTML = "Services";
	_LinkServices.onmouseover = setMainMenuOptionOver;
	_LinkServices.onmouseout = setMainMenuOptionOut;
	_LinkServices.onclick = selectMainMenuOption;
	_LinkServices.id = "SERVICES";

	_LinkAbout= _NavRow.insertCell(3);
	_LinkAbout.className="mainOption";
	_LinkAbout.innerHTML = "About";
	_LinkAbout.onmouseover = setMainMenuOptionOver;
	_LinkAbout.onmouseout = setMainMenuOptionOut;
	_LinkAbout.onclick = selectMainMenuOption;
	_LinkAbout.id = "ABOUT";

	_LinkContact= _NavRow.insertCell(4);
	_LinkContact.className="mainOption";
	_LinkContact.innerHTML = "Contact";
	_LinkContact.onmouseover = setMainMenuOptionOver;
	_LinkContact.onmouseout = setMainMenuOptionOut;
	_LinkContact.onclick = selectMainMenuOption;
	_LinkContact.id = "CONTACT";

	_LinkForum= _NavRow.insertCell(5);
	_LinkForum.className="mainOption";
	_LinkForum.innerHTML = "Discussion Board";
	_LinkForum.onmouseover = setMainMenuOptionOver;
	_LinkForum.onmouseout = setMainMenuOptionOut;
	_LinkForum.onclick = selectMainMenuOption;
	_LinkForum.id = "FORUM";

    _LinkLogin= document.createElement("DIV"); 
	_LinkLogin.className="mainOption";
	_LinkLogin.innerHTML = "Login";
	_LinkLogin.style.position="absolute";
	_LinkLogin.style.top="360px";
	_LinkLogin.style.width="75px";
	_LinkLogin.style.left="735px";
	_LinkLogin.style.zIndex="10";
	_LinkLogin.onmouseover = setMainMenuOptionOver;
	_LinkLogin.onmouseout = setMainMenuOptionOut;
	_LinkLogin.onclick = selectMainMenuOption;
	_LinkLogin.id = "LOGIN";
    
    document.body.appendChild(_LinkLogin);

    _LinkLogoff= document.createElement("DIV"); 
	_LinkLogoff.className="mainOption";
	_LinkLogoff.innerHTML = "Logout";
	_LinkLogoff.style.position="absolute";
	_LinkLogoff.style.top="360px";
	_LinkLogoff.style.width="75px";
	_LinkLogoff.style.left="735px";
	_LinkLogoff.style.zIndex="10";
	_LinkLogoff.style.display="none";
	_LinkLogoff.onmouseover = setMainMenuOptionOver;
	_LinkLogoff.onmouseout = setMainMenuOptionOut;
	_LinkLogoff.onclick = selectMainMenuOption;
	_LinkLogoff.id = "LOGOFF";
    
    document.body.appendChild(_LinkLogoff);


	_LinkRegister= document.createElement("DIV"); 
	_LinkRegister.className="mainOption";
	_LinkRegister.innerHTML = "Register";
	_LinkRegister.style.position="absolute";
	_LinkRegister.style.top="380px";
	_LinkRegister.style.width="80px";	
	_LinkRegister.style.left="735px";
	_LinkRegister.style.zIndex="10";
	_LinkRegister.onmouseover = setMainMenuOptionOver;
	_LinkRegister.onmouseout = setMainMenuOptionOut;
	_LinkRegister.onclick = selectMainMenuOption;
	_LinkRegister.id = "REGISTER";
    
    document.body.appendChild(_LinkRegister);
    
    
    _LinkBuyA = document.createElement("DIV");
    _LinkBuyA.style.display="none";
	_LinkBuyA.className="mainOption";
	_LinkBuyA.innerHTML = "Purchase Approved";
	_LinkBuyA.onmouseover = setMainMenuOptionOver;
	_LinkBuyA.onmouseout = setMainMenuOptionOut;
	_LinkBuyA.onclick = selectMainMenuOption;
	_LinkBuyA.id = "BUY-A";

    document.body.appendChild(_LinkBuyA);
    
    _LinkBuyP = _NavRow.insertCell(0);
    _LinkBuyP.style.display="none";
	_LinkBuyP.className="mainOption";
	_LinkBuyP.innerHTML = "Purchase Pending";
	_LinkBuyP.onmouseover = setMainMenuOptionOver;
	_LinkBuyP.onmouseout = setMainMenuOptionOut;
	_LinkBuyP.onclick = selectMainMenuOption;
	_LinkBuyP.id = "BUY-P";

    document.body.appendChild(_LinkBuyP);
    
    _LinkSef = document.createElement("DIV");
    _LinkSef.className = "sefOff";
    _LinkSef.onmouseover = function() { this.className="sefOn"; }
    _LinkSef.onmouseout = function() { this.className="sefOff"; }
    _LinkSef.onclick = function() { 
        hideContentNavigator();
		setPageTitle("SEF");
		Comm.sendCommand("<CMD name='IsLoggedIn'></CMD>", cb_isLoggedInForSEF, "Accessing SEF...");
    }
    
    document.body.appendChild(_LinkSef);
    

    _AdSpace = document.createElement("DIV");
    _AdSpace.className = "adSpace";
    _AdSpace.cnt= document.createElement("IFRAME");
    _AdSpace.cnt.className = "adSpaceContent";
    _AdSpace.cnt.src = "ads/AdMain.aspx";
    _AdSpace.cnt.frameBorder="0";
    document.body.appendChild(_AdSpace);    
    document.body.appendChild(_AdSpace.cnt);

	_UserIndic = document.createElement("DIV");
	_UserIndic.className = "UserIndicator";
	document.body.appendChild(_UserIndic);
	
		
	_pageNavigator.appendChild(_NavTab);

	document.body.appendChild(_pageNavigator);

	_sideThing = document.createElement("DIV");
	_sideThing.className = "sideThing";

	document.body.appendChild(_sideThing);

    _sideThingA = document.createElement("DIV");
	_sideThingA.className = "sideThingA";

	document.body.appendChild(_sideThingA);

    _sideThingB = document.createElement("DIV");
	_sideThingB.className = "sideThingB";
	//_sideThingB.style.setExpression("height",eval(screen.availHeight));

	document.body.appendChild(_sideThingB);

    _sideThingC = document.createElement("DIV");
	_sideThingC.className = "sideThingC";

	document.body.appendChild(_sideThingC);

	_sideThing2 = document.createElement("DIV");
	_sideThing2.className = "sideThing2";

	document.body.appendChild(_sideThing2);

	_sideThing0 = document.createElement("DIV");
	_sideThing0.className = "sideThingSky";

	document.body.appendChild(_sideThing0);

	_sideThing3= document.createElement("DIV");
	_sideThing3.className = "sideThing3";

	document.body.appendChild(_sideThing3);

	_sideThing4= document.createElement("DIV");
	_sideThing4.className = "sideThing4";
	
	_pageTopRef = _sideThing0;

	document.body.appendChild(_sideThing4);

    updateUserIndicator();

}

function setMainMenuOptionOver()
{
	if(_selectedMainMenuOption!=this) this.className = "mainOptionHover";
}

function setMainMenuOptionOut()
{
	if(_selectedMainMenuOption!=this) this.className = "mainOption";
}

function showContentNavigator()
{
    //_ivalContentNav = setInterval("i_showContentNavigator(false);",100);
    if(!_sectionLoaded) setTimeout("showContentNavigator();",20);
    else i_showContentNavigator(false);
}


function i_showContentNavigator(bShowPanel)
{

    if(!_sectionLoaded) return;
    
    //clearInterval(_ivalContentNav);
    
    var i, tmp;
    
    if(_contentNavigator==null && bShowPanel) {
        _contentNavigator = document.createElement("DIV");
        _contentNavigator.className = "contentNavigator";
        
        tmp = document.createElement("DIV");
        tmp.id="indexdiv";
        tmp.style.top="100px";
        tmp.style.left="-15px";
        tmp.style.position="absolute";
        
        _contentNavigator.appendChild(tmp);
        
        _contentList = document.createElement("UL");
        _contentList.id="indexul";
        _contentList.className="UList";
        tmp.appendChild(_contentList);
        
        document.body.appendChild(_contentNavigator);
    }
    if(bShowPanel) _contentNavigator.style.display="block";
    
    if(_contentArea!=null && bShowPanel) {
        tmp = _contentList.childNodes.length;
        for(i=0;i<tmp;i++) _contentList.removeChild(_contentList.childNodes[0]);
        _contentArea.style.width="550px";
        try { _contentNavigator.style.setExpression("height",_contentArea.scrollHeight); } //offsetHeight+10); 
        catch(e) {}
    }
    
    //gatherIndex();
    //setOnScrollEvent();
    itemNavigator();
    //getSubContentNavigator();
}

function getSubContentNavigator(opt)
{
    if(!_sectionLoaded) setTimeout("getSubContentNavigator('"+opt+"');",20);
    else cb_getSubContentNavigator(opt);
}

function getItemIndexes()
{
    if(!_sectionLoaded) setTimeout("getItemIndexes();",20);
    else gatherIndex();
    
}
   
function cb_getSubContentNavigator(opt)
{
    var dSubNav = document.createElement("DIV");
    
    dSubNav.className = "subItemNav";
    
    var bPApps, bPSols, bPLibs;
    
    bPApps = document.createElement("BUTTON");
    bPApps.innerHTML = "Applications";
    bPApps.id = "PRODUCTS-A";
    bPApps.className = (opt == "PRODUCTS-A" ? "btSuppS_on" : "btSuppS");
    bPApps.onclick = function() {
        //alert("me");
        //gotoPage("PRODUCTS-A");
        selectMainMenuOption(this.id);
        //this.className="btSuppS_on";
        //this.b1.className = "btSupps";
        //this.b2.className = "btSupps";
    }
    
    bPSols = document.createElement("BUTTON");
    bPSols.innerHTML = "Solutions";
    bPSols.className = "btSuppS";
    bPSols.id = "PRODUCTS-S";
    bPSols.className = (opt == "PRODUCTS-S" ? "btSuppS_on" : "btSuppS");
    bPSols.onclick = function() {
        //gotoPage("PRODUCTS-S");
        selectMainMenuOption(this.id);
        //this.className="btSuppS_on";
        //this.b1.className = "btSupps";
        //this.b2.className = "btSupps";
    }
    
    bPLibs = document.createElement("BUTTON");
    bPLibs.innerHTML = "Code Libraries";
    bPLibs.className = "btSuppS";
    bPLibs.style.letterSpacing = "1px";
    bPLibs.id = "PRODUCTS";
    bPLibs.className = (opt == "PRODUCTS" ? "btSuppS_on" : "btSuppS");
    bPLibs.onclick = function() {
        //gotoPage("PRODUCTS");
        selectMainMenuOption(this.id);
        //this.className="btSuppS_on";
        //this.b1.className = "btSupps";
        //this.b2.className = "btSupps";
    }

    bPApps.b1 = bPSols;
    bPApps.b2 = bPLibs;
    
    bPSols.b1 = bPApps;
    bPSols.b2 = bPLibs;
    
    bPLibs.b1 = bPSols;
    bPLibs.b2 = bPApps;
    
    dSubNav.appendChild(bPLibs);
    dSubNav.appendChild(bPApps);
    dSubNav.appendChild(bPSols);
    
    try { _contentArea.insertAdjacentElement("afterBegin", dSubNav); }
    catch(E) {
        _contentArea.insertBefore(dSubNav, _contentArea.firstChild);
    }
}

function hideContentNavigator()
{
    if(_contentNavigator!=null) _contentNavigator.style.display="none";
    if(_contentArea!=null) _contentArea.style.width="705px";
}

function setContentHeight(hgt)
{
    _contentArea.style.height = hgt;
}

function selectMainMenuOption(opt)
{
    showLoader();
    var selOpt = ((opt!=null && opt!="undefined" && typeof(opt)!="object")?  opt : this.id);
    
	if(_selectedMainMenuOption!=null) _selectedMainMenuOption.className = "mainOption";
	_selectedMainMenuOption = this;
	this.className = "mainOptionOn";
	
	try { closeLoginPrompt(); } catch(e) {}

  	switch(selOpt) {
		case "NEWS":
		    hideContentNavigator();
			setPageTitle("Home / News");
			fetchItemsForSection("HOME");
			break;
		case "PRODUCTS":
			setPageTitle("Products - Code Libraries");
			fetchItemsForSection("PRODUCTS");
			showContentNavigator(false);
			getSubContentNavigator("PRODUCTS");
			getItemIndexes();
			break;
		case "PRODUCTS-A":
			setPageTitle("Products - Applications");
			fetchItemsForSection("PRODUCTS-A");
			showContentNavigator(false);
			getSubContentNavigator("PRODUCTS-A");
			break;
		case "PRODUCTS-S":
			setPageTitle("Products - Solutions");
			fetchItemsForSection("PRODUCTS-S");
			showContentNavigator(false);
			getSubContentNavigator("PRODUCTS-S");
			break;
		case "SERVICES":
		    hideContentNavigator();
			setPageTitle("Services");
			fetchItemsForSection("SERVICES");
			break;
		case "ABOUT":
		    hideContentNavigator();
			setPageTitle("About SpindleScape");
			fetchItemsForSection("ABOUT");
			break;
		case "CONTACT":
		    hideContentNavigator();
			setPageTitle("Contact Us");
			fetchItemsForSection("CONTACT");
			break;
		case "FORUM":
		    hideContentNavigator();
			setPageTitle("Discussion Board");
			fetchItemsForSection("FORUM");
			break;
		case "LOGIN":
		    //_loaderOff = true; 
			showLoginPrompt();
			break;
		case "LOGOFF":
		    logoff();
		    hideContentNavigator();
			setPageTitle("Logout");
			fetchItemsForSection("LOGOFF");
			break;
		case "REGISTER":
		    hideContentNavigator();
			setPageTitle("Register");
			fetchItemsForSection("REGISTER");
			initRegister();
			break;
	    case "BUY-A":
	        //alert("buying...");
		    hideContentNavigator();
			setPageTitle("Product download");
			fetchItemsForSection("BUY-A");
			break;
		case "BUY-P":
		    hideContentNavigator();
			setPageTitle("Product download");
			fetchItemsForSection("BUY-P");
			break;
		default:
			setPageTitle("Welcome to SpindleScape");
			break;
	}
}

function selectDistinctMainOption(id)
{
	var rowref = ( typeof(_NavRow) == "object" ? _NavRow : _NavRow[0] );
	var i, n = rowref.cells.length;

	for(i=0;i<n;i++) {
		if(rowref.cells[i].id == id) {
			document.getElementById(id).onclick();
			_loaderOff = true;
			break;
		}
	}
}


function setPageTitle(text)
{
    var hex = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
	if(_pageTitle==null) {
		_pageTitle = document.createElement("DIV");
		_pageTitle.className = "pageTitle";
		document.body.appendChild(_pageTitle);
	}

    var strLen = text.length;
    var LowColor = 7, HighColor=13;
    var incr = (HighColor-LowColor)/strLen;
    var C, D;

    _pageTitle.innerHTML = "";
    
    for(var i=0;i<strLen;i++) {
        C = hex[LowColor+parseInt((i*incr))];
        C=C+"0";
        C=C+C+C;
        _pageTitle.innerHTML+="<span style='color: #"+C+";'>"+text.charAt(i)+"</span>";
    }
}

function genBackground()
{
	var n = 25;
	var i, d, el, dat, datval;

	for(i=0;i<n;i++) {
			dat = new Date();
			datval = dat.valueOf();

			el = document.createElement("DVI");
			el.style.position = "absolute";
			el.style.zIndex=0;
			el.style.width=el.style.height = 100+(Math.random(datval)*50);
			el.style.top = Math.random(datval)*document.body.clientHeight;
			el.style.left = Math.random(datval)*document.body.clientWidth;
			el.style.backgroundColor=((190+Math.random(datval)*55)<<16)+((80+Math.random(datval)*35)<<8)+((10+Math.random(datval)*55));

			document.body.appendChild(el);
	}

}

function setFooter()
{
	if(_footer==null) {
		_footer = document.createElement("DIV");
		_footer.className = "footer";
		document.body.appendChild(_footer);
	}
	
	_footer.innerHTML="<img src='visuals/images/sscape.gif'>"; //<br>Copyright (C) 2006 SpindleScape.<br>All rights reserved.";
}

