//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//                          Initialise variables, etc.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


// function to call when page has loaded.  Detect whether the help video link should be displayed.
window.addEvent( 'domready', function(  )
	{
		setTimeout( 'isJangoPresent(  )', 500 );
		setTimeout( 'isMontecitoPresent(  )', 500 );
		setTimeout( 'isNeedaPresent(  )', 500 );
		/*setTimeout( 'showHiddenArtifacts(  )', 500 );*/
	} );

// hide the contents until the page has loaded.  Makes it a bit nicer aesthetically.
window.onload = showHiddenArtifacts;



// flag to control whether this page can be closed (navigate away or close the tab/browser).
var warnBeforeLeaving = false;

// prevent premature closing of this page.
window.onbeforeunload = beforeUnload;

function beforeUnload(  )
{
	if ( warnBeforeLeaving )
    {
    	// note: no text need be returned as
		return "Warning: Leaving this page will lose any unsaved work.";
	}
}



// erro handling variables
onerror = handleErr;
var txt = "";

// detect an IE mobile.
if ( !isIE6orBetter )
{
	var browserVersion = navigator.appVersion;
	var isIEMobile = false;

	// look for these strings to discover if the current browser is a mobile device.
	if ( ( browserVersion.indexOf( "IEMobile" ) != -1 ) || ( browserVersion.indexOf( "PPC" ) != -1 ) )
	{
		isIEMobile = true;
	}

	// jump to the classic interface if this is a mobile device.
	if ( true == isIEMobile )
	{
		window.location = SERIES1_GO_HERE;
	}
}
else
{
	isIEMobile = false;
}

//alert( "isIE6orBetter = " + isIE6orBetter + ", isOther = " + isOther + ", isIEMobile = " + isIEMobile );

// simply amazing code from http://en.design-noir.de/webdev/JS/XMLHttpRequest-IE/ that 
//  is commented out but uses Microsoft's 'Conditional Compilation' for being visible to
//  MSIE. if (!window.XMLHttpRequest) checks if XMLHttpRequest really doesn't exist yet.
//  That won't be the case for IE versions up to 6 but for IE7, which will provide a
//  native XMLHTTPRequest object. I'm not using the ordinary try/catch with new
//  ActiveXObject('Msxml2.XMLHTTP') because new ActiveXObject('Microsoft.XMLHTTP') does
//  the job for all IE (Win) versions from 5 and should use Msxml2 automatically, if available.

/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/

// assign a request as appropriate - depends on the browser.
var jangoRequest = ( !isIEMobile && ( isIE6orBetter || isOther ) ) ? new XMLHttpRequest(  ) : "";
var montecitoRequest = ( !isIEMobile && ( isIE6orBetter || isOther ) ) ? new XMLHttpRequest(  ) : "";
var needaRequest = ( !isIEMobile && ( isIE6orBetter || isOther ) ) ? new XMLHttpRequest(  ) : "";

// holds the jango application's DIV reference.
var jangoID;

// holds the montecito application's DIV reference.
var montecitoID;

// holds the needa application's DIV reference.
var needaID;

function showHiddenArtifacts(  )
{
	var mainWorkSpace = document.getElementById( 'MAIN_WORKSPACE' );

	mainWorkSpace.style.display = "block";
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//                          Handle tab switching.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function activateTab( tabId )
{
	var divId = '';

	if ( tabId == 'HIGH_SPEED_TAB' )
	{
		divId = 'HIGH_SPEED_DIV';
	}
	else if ( tabId == 'LOW_SPEED_TAB' )
	{
		divId = 'LOW_SPEED_DIV';
	}

	var tab1 = document.getElementById( 'HIGH_SPEED_TAB' );
	var tab2 = document.getElementById( 'LOW_SPEED_TAB' );

	tab1.className = '';
	tab2.className = '';

	var div1 = document.getElementById( 'HIGH_SPEED_DIV' );
	var div2 = document.getElementById( 'LOW_SPEED_DIV' );

	div1.className = '';
	div2.className = '';
	
	var selectedTab = document.getElementById( tabId );
	var selectedDiv = document.getElementById( divId );

	selectedTab.className = 'tabActive';
	selectedDiv.className = 'divActive';

	return false;
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//                          Handle unexpected errors.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function handleErr( msg, url, l )
{
	txt =  "There was an error on this page.\n\n";
	txt += "Error: " + msg + "\n";
	txt += "URL: " + url + "\n";
	txt += "Line: " + l + "\n\n";
	txt += "Click OK to continue.\n\n";
	
	alert( txt );
	return true;
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//         Show jango link if the folder is present.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// discover whether Jango is present or not.
function isJangoPresent(  )
{
	//alert( "calling isJangoPresent function" );
	
	// make sure the browser is isIE6orBetter or something else (not IE5)
	if ( !isIEMobile && ( isIE6orBetter || isOther ) )
	{
		try
		{
			// create and send the JSON request.
			jangoRequest.open( "GET",  "/jango/index.html", true ); 
			jangoRequest.onreadystatechange = isJangoPresentResponse;  // response handler.
			//jangoRequest.setRequestHeader( "Connection", "close" );  // close the connection immediately after the reposnse is sent.
			jangoRequest.send( null );
		}
		catch ( e3 )
		{
			alert( "Fatal error when communicating with the logger 3: " + e3.message );
		}
	}
}


// handle the response from the logger.  Use the response to hide or show the jango link.
function isJangoPresentResponse(  )
{
	// check that the request has been finished.
	if ( 4 == jangoRequest.readyState )
	{// yes; the request is finished.
		
		// define the jango div.
		jangoID = document.getElementById( "jango" );

		try
		{
			// check which type of reply was recieved and act upon it as appropriate.
			switch( jangoRequest.status )
			{
				case 404:
					// file not found.
					//alert( "isJangoPresentResponse(  ), 404 error: " + montecitoRequest.status );
					jangoID.style.display = "none";
					break;

				default:
					//assume that the help file is present.
					//alert( "isJangoPresentResponse(  ), other result: " + montecitoRequest.status );
					jangoID.style.display = "block";
					break;
			}
		}
		catch ( e )
		{
			// an error was received, so assume that the help file is present.
			//alert( "isJangoPresentResponse(  ), error caught: " + e.message + ".  jangoRequest.status = " + montecitoRequest.status );
			
			jangoID.style.display = "block";
		}
	}
}




//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//         Show montecito link if the folder is present.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// discover whether Montecito is present or not.
function isMontecitoPresent(  )
{
	//alert( "calling isMontecitoPresent function" );
	
	// make sure the browser is isIE6orBetter or something else (not IE5)
	if ( !isIEMobile && ( isIE6orBetter || isOther ) )
	{
		try
		{
			// create and send the JSON request.
			montecitoRequest.open( "GET",  "/flash/index.html", true ); 
			montecitoRequest.onreadystatechange = isMontecitoPresentResponse;  // response handler.
			//montecitoRequest.setRequestHeader( "Connection", "close" );  // close the connection immediately after the reposnse is sent.
			montecitoRequest.send( null );
		}
		catch ( e2 )
		{
			alert( "Fatal error when communicating with the logger 2: " + e2.message );
		}
	}
}


// handle the response from the logger.  Use the response to hide or show the montecito link.
function isMontecitoPresentResponse(  )
{
	// check that the request has been finished.
	if ( 4 == montecitoRequest.readyState )
	{// yes; the request is finished.
		
		// define the montectito div.
		montecitoID = document.getElementById( "montecito" );

		try
		{
			// check which type of reply was received and act upon it as appropriate.
			switch( montecitoRequest.status )
			{
				case 404:
					// file not found.
					//alert( "isMontecitoPresentResponse(  ), 404 error: " + montecitoRequest.status );
					montecitoID.style.display = "none";
					break;

				default:
					//assume that the help file is present.
					//alert( "isMontecitoPresentResponse(  ), other result: " + montecitoRequest.status );
					montecitoID.style.display = "block";
					break;
			}
		}
		catch ( e )
		{
			// an error was received, so assume that the help file is present.
			//alert( "isMontecitoPresentResponse(  ), error caught: " + e.message + ".  montecitoRequest.status = " + montecitoRequest.status );
			
			montecitoID.style.display = "block";
		}
	}
}



//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//         Show needa link if the folder is present.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// discover whether needa app is present or not.
function isNeedaPresent(  )
{
	//alert( "calling isNeedaPresent function" );
	
	// make sure the browser is isIE6orBetter or something else (not IE5)
	if ( !isIEMobile && ( isIE6orBetter || isOther ) )
	{
		try
		{
			// create and send the JSON request.
			needaRequest.open( "GET",  "/needa/index.html", true ); 
			needaRequest.onreadystatechange = isNeedaPresentResponse;  // response handler.
			//needaRequest.setRequestHeader( "Connection", "close" );  // close the connection immediately after the reposnse is sent.
			needaRequest.send( null );
		}
		catch ( e )
		{
			alert( "Fatal error when communicating with the logger 3: " + e.message );
		}
	}
}


// handle the response from the logger.  Use the response to hide or show the Needa app link.
function isNeedaPresentResponse(  )
{
	// check that the request has been finished.
	if ( 4 == needaRequest.readyState )
	{// yes; the request is finished.
		
		// define the intro needa div.
		needaID = document.getElementById( "needa" );

		try
		{
			// check which type of reply was recieved and act upon it as appropriate.
			switch( needaRequest.status )
			{
				case 404:
					// file not found.
					//alert( "isNeedaPresentResponse(  ), 404 error: " + needaRequest.status );
					needaID.style.display = "none";
					break;

				default:
					//assume that the help file is present.
					//alert( "isNeedaPresentResponse(  ), other result: " + needaRequest.status );
					needaID.style.display = "block";
					break;
			}
		}
		catch ( e )
		{
			// an error was received, so assume that the help file is present.
			//alert( "isNeedaPresentResponse(  ), error caught: " + e.message + ".  needaRequest.status = " + needaRequest.status );
			
			needaID.style.display = "block";
		}
	}
}



//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//                           Menu (mouse) event handlers
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function onMouseOverHandler( className, id )
{
	if (document.layers) document.layers[''+id+''].visibility = "show"
	else if (document.all) document.all[''+id+''].style.visibility = "visible"
	else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible";
	
	cursorToLink(  );
	
	//className.style.backgroundColor = '#FFFFFF';
}

function onMouseOutHandler( className, id )
{
	if (document.layers) document.layers[''+id+''].visibility = "hide"
	else if (document.all) document.all[''+id+''].style.visibility = "hidden"
	else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden";
	
	cursorToArrow(  );
	
	//className.style.backgroundColor = '';
}

// Changes the cursor to a link cursor
function cursorToLink(  )
{
	document.body.style.cursor = 'pointer';
}

// Changes the cursor to an arrow cursor
function cursorToArrow(  )
{
	document.body.style.cursor = 'default';
}



//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//                           Multibox event handlers
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function onClickHandlerIntroVideo(  )
{
	var mb = new MultiBox( 'mbj1', { useOverlay: true, showNumbers: false, showControls: false } );
	var myBox = $( 'MBJ_1' );
	
	//myBox.setProperty( 'href', ( '../videoHelp/introduction.html' ) );
	
	mb.open( myBox );
}

function onClickHandlerNeeda(  )
{
	var mb = new MultiBox( 'mbj2', { useOverlay: true, showNumbers: false, showControls: false } );
	var myBox = $( 'MBJ_2' );
	
	//myBox.setProperty( 'href', ( '../needa/Needa.html' ) );
	
	mb.open( myBox );
}
