//****************************************************************
// TBI Menu System
// Author: Simon James Fox
// &copy;Copyright Total Business Integration Ltd. 2006
// All rights reserved.
//***************************************************************

var CurrentSectionId = "Start";
var CurrentElementId = "";

var SectionsHidden = 0;

//------------------------------------------------------------------
// Sections Array()
//
// Each Sub Array contains:        Parent , Title, Num
// e.g.                new Array("Section00", "Home", 3 );
//------------------------------------------------------------------
var Sections = new Array();

ButtonOver  = "url(../img/button.png)";
ButtonOut   = "url(../img/button.png)";

OpacityOver = 1.0;
OpacityOut  = 1.0;

Sections["Section00"] = new Array("Section00", "Products", 3 );
Sections["Section01"] = new Array("Section01", "Resources", 5 );
Sections["Section02"] = new Array("Section02", "Contact", 0 );
Sections["Section03"] = new Array("Section03", "About", 2 );
NumSections = 4;

function HideStatus()
{
	window.status='TBiConnect';
	return true;
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
//---------------------------------------------------
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
//-------------------------------------------------------
if (!IE)
{
	document.captureEvents(Event.MOUSEOVER);
	document.captureEvents(Event.CLICK);
}

document.onmouseover = MouseOver;
document.onclick     = MouseClick;

function MouseOver(evt)
{
 	var evt = (evt) ? evt : ((window.event) ? window.event : "");
	var elem = (evt.target) ? evt.target : evt.srcElement;
	
	CurrentElementId = elem.id;
	
	// Picks up XY position
	//-----------------------------------------------------
	tempX = evt.pageX;
	tempY = evt.pageY;
	
	//document.getElementById("Test").value = CurrentElementId + " : " + tempX + " : " +tempY;
	//-------------------------------------------------------------

	// This is a Section we're over
	//-----------------------------
	if( CurrentElementId.length == 9 && CurrentElementId.substr(0, 7) == "Section" )
	{
		SectionsHidden = 1;
		
		if(CurrentSectionId == "Start")
		{
			CurrentSectionId = elem.id;
		}
		
		if( CurrentSectionId != elem.id ) // We're over a new Section, so close old, open new
		{
 			SubSections = Sections[CurrentSectionId][2];
			
			for(var i = 1; i <= SubSections; i++)
			{
				var SubSection = CurrentSectionId + "-" + i; // e.g. Section00-1
				Opacity(SubSection, parseInt(OpacityOver*100), 0, 800);
			}

			if(!IE)
			{
				document.getElementById(CurrentSectionId).style.opacity= OpacityOut;
			}
			else
			{
                document.getElementById(CurrentSectionId).style.filter="Alpha(opacity=" + OpacityOut*100 + ")";
			}
            
			document.getElementById(CurrentSectionId).style.backgroundImage= ButtonOut;
			CurrentSectionId = elem.id;
		}
		
    	SubSections = Sections[CurrentElementId][2];

		for(var i = 1; i <= SubSections; i++)
		{
		      var SubSection = elem.id + "-" + i; // e.g. Section00-1
		      document.getElementById(SubSection).style.visibility = "visible";
			  Opacity(SubSection, 0, parseInt(OpacityOver*100), 200);
		}
		elem.style.backgroundImage= ButtonOver;
		elem.style.opacity= OpacityOver;
		elem.style.filter="Alpha(opacity=" + parseInt(OpacityOver*100) + ")";
	}
	else if( CurrentElementId.length == 11 && CurrentElementId.substr(0, 7) == "Section" ) // Sub Section
	{
		// Set all subsections to same opacity
		//------------------------------------
		SectionId = CurrentElementId.substr(0, 9);
		SubSections = Sections[SectionId][2];
		
		for(var i = 1; i <= SubSections; i++)
		{
		      var SubSection = SectionId + "-" + i; // e.g. Section00-1
		      document.getElementById(SubSection).style.opacity= OpacityOver;
		      document.getElementById(SubSection).style.filter="Alpha(opacity=" + OpacityOver*100 + ")";
		}
		elem.style.opacity=1.0;
		elem.style.filter="Alpha(opacity=100)";
	}
	else if (SectionsHidden == 1 ) // Not over a Section or SubSection AND they are currently not hidden
	{						
		for(var i = 0; i < (NumSections); i++)
		{
			var SectionId = "Section0" + i;

			// Now Revert Section
			//--------------------
			document.getElementById(Sections[SectionId][0]).style.backgroundImage= ButtonOut;
		    document.getElementById(Sections[SectionId][0]).style.opacity= OpacityOut;
		    document.getElementById(Sections[SectionId][0]).style.filter="Alpha(opacity=" + parseInt(OpacityOut*100) + ")";
			
			// Now for each hide the Subsections
			//-----------------------------------
	    	SubSections = Sections[SectionId][2];
	
			for(var j = 1; j <= SubSections; j++)
			{
				  var SubSection = SectionId + "-" + j; // e.g. Section00-1
				  //alert(SubSections + " : " + SubSection);
			      document.getElementById(SubSection).style.opacity = OpacityOut;
			      document.getElementById(SubSection).style.filter = "Alpha(opacity=" + parseInt(OpacityOut*100) + ")";
			      document.getElementById(SubSection).style.visibility = "hidden";
			}
		}
		
	}
}


function MouseClick(evt)
{
 	var evt = (evt) ? evt : ((window.event) ? window.event : "");
	var elem = (evt.target) ? evt.target : evt.srcElement;

	CurrentElementId = elem.id;
}

function Opacity(Id, Start, End, MilliSec) 
{
    //speed for each frame
	var FadeSpeed = 80; // Smaller = slower
    var Speed = Math.round(MilliSec / FadeSpeed);
    var Tm = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(Start > End) 
	{
        for(var i = Start; i >= End; i--) 
		{
            setTimeout("ChangeOpac(" + i + ",'" + Id + "')",(Tm * Speed));
            Tm++;
        }
    } 
	else if(Start < End) 
	{
        for(var i = Start; i <= End; i++)
        {
            setTimeout("ChangeOpac(" + i + ",'" + Id + "')",(Tm * Speed));
            Tm++;
        }
    }
}

//change the opacity for different browsers
function ChangeOpac(Opacity, Id) 
{
    var object = document.getElementById(Id).style;
    object.opacity = (Opacity / 100);
    object.MozOpacity = (Opacity / 100);
    object.KhtmlOpacity = (Opacity / 100);
    object.filter = "alpha(Opacity=" + Opacity + ")";
}

//==========================================================================================

var UseableWidth = 0;
var UseableHeight = 0;
var PageWidth = 0;
var PageLeft = 0;

//==========================================================================================
function Layout()
{
	// Set Positions, Widths & Heights of components
	//----------------------------------------------
	   with (Math)
	{
		// Only used onloading or when Window size has changed
		//----------------------------------------------------
		if( UseableWidth != document.body.clientWidth )
		{
			UseableWidth = document.body.clientWidth - 20;
			alert("Client Width: " + UseableWidth);
			//if( UseableWidth > 800)
			//{
				PageWidth = 700;
				PageLeft = parseInt((UseableWidth - PageWidth) / 2);
                alert("PageWidth: " + PageWidth + "PageLeft: " + PageLeft);                
				document.getElementById("mainwrapper").style.left = PageLeft;
			//}
		}
	}

	document.getElementById("mainwrapper").style.visibility = "visible";
}
//==========================================================================================

/* ---------------------------------------------------------------------------------------------------
Video control functions
------------------------------------------------------------------------------------------------------ */
function StartMeUp ()
{
    document.getElementById("MediaPlayer").URL = "tc4014-v.wmv";
}

function screenfull ()
{
               if (document.getElementById("MediaPlayer").playState == 3) 
                  document.getElementById("MediaPlayer").fullScreen = 'true';
}

function ShutMeDown ()
{
    document.getElementById("MediaPlayer").controls.stop();
}
//==========================================================================================