// include appropriate javascript.  NOTE: these scripts won't be loaded until onPageLoad is called
includeScript( "scripts/shared/utilities.js" );
includeScript( "scripts/shared/imageutil.js" );
includeScript( "scripts/shared/collapse.js" );
includeScript( "scripts/shared/tabview.js" );
includeScript( "scripts/shared/ajaxwrapper.js" );
includeScript( "scripts/sgisite/sgiscript.js" );
includeScript( "scripts/sgisite/sound.js" );
includeScript( "scripts/external/soundmanager.js" );

// globals
var g_ContentTabs = null;

var g_tabIndexToHTML = [ "embednews.html", "embedabout.html", "embedcareers.html", "embedcontact.html" ];
var g_tabIndexToTitle = [ "News", "About", "Careers", "Contact" ];
var g_tabIndexToHash = [ "#news", "#about", "#careers", "#contact" ];
var g_tabHTMLContent = new Array(4);

function scriptMain()
{
	soundSetup( "muteButton" );
	
	var mainbody = document.getElementById( "mainbody" );
	autoAlignElemToWindowHorozontal( mainbody, -488, 0 );
	
	// setup the on mouse hover any hover images
	setupHoverImages( "(swap_hover)|(link_hover)" );
	
	// check to see if we are hot-linking to a sub-page
	var startTab = 0;
	var i;
	for ( i = 0; i != g_tabIndexToHash.length; ++i )
	{
		if ( window.location.hash.match( g_tabIndexToHash[i] ) )
		{
			startTab = i;
			break;
		}
	}
	
	// initialize loaded content
	for( i = 0; i != 4; ++i )
	{
		g_tabHTMLContent[i] = new Object();
		g_tabHTMLContent[i].loaded = false;
		g_tabHTMLContent[i].contentElem = "";
	}

	var tabButtonIds = [ "i1", "i2", "i3", "i4" ];
	var tabButtonActiveImages = [ "images/content_tabs_05.gif", "images/content_tabs_07.gif", "images/content_tabs_09.gif", "images/content_tabs_11.gif" ];
	g_ContentTabs = new TabView( tabButtonIds, tabButtonActiveImages, startTab, onTabChangeCB );
	
	// random title swap
	setTimeout( titleSwapRandom, Math.random()*5000 + 5000 ); // random time from 5 to 10 seconds
	
	// load sound, then images
	var sndWait = new WaitForSoundToLoad( soundLoadedCB );
}

function soundLoadedCB()
{
	// make the body visible after loading all of the document images
	var wait = new WaitForImagesToLoad( onImagesLoadedCB );
}

function onImagesLoadedCB()
{
	// hide the loading image
	var loading = document.getElementById("loading");
	loading.style.display = "none";

	// hide the loading image
	var body = document.getElementById("mainbody");
	body.style.display = "block";
	
	preloadContentFromFile( "preload.xml", preloadDone );

    var musicCB = bindCB( sndPlayMusic, "background" );
    setTimeout( musicCB, 1000 );
}

function preloadDone()
{
	
}
    
function titleSwapRandom()
{
	var titleElem = document.getElementById("iHome");
	if ( titleElem )
	{
		titleElem.src = "images/content_02.gif";
		
		setTimeout( titleStaticRestore, 3000 );
	}
}

function titleStaticRestore()
{
	var titleElem = document.getElementById("iHome");
	if ( titleElem )
	{
		titleElem.src = "images/content_02.jpg";
		setTimeout( titleSwapRandom, Math.random()*5000 + 5000 ); // random time from 5 to 10 seconds
	}
}

function onTabChangeCB( newTabIndex )
{
	// clear the existing content
	contentClear();
    
    if ( g_tabHTMLContent[ newTabIndex ].loaded == true )
    {
		// use the cached page
		contentSet( g_tabHTMLContent[ newTabIndex ].contentElem, newTabIndex );
		return;
    }
    
    var tabIndex = newTabIndex;
    
    if ( !getUrlDocumentText( g_tabIndexToHTML[newTabIndex], onContentDownloaded ) )
    {
		// redirect the browser to the embedded page directly on failure to download
		window.location.replace( "index.html" );
		return;
    }
    
    function onContentDownloaded( resultsText )
	{
		// create a container div
		var contentElem = document.createElement("div");
		contentElem.innerHTML = resultsText;
		
		g_tabHTMLContent[ tabIndex ].loaded = true;
		g_tabHTMLContent[ tabIndex ].contentElem = contentElem;
		
		contentSet( contentElem, tabIndex );
	}
}

var g_collapseObjList = new Array();

function bindCollapseContent( newContentElem )
{
	var i;
	for ( i=0; i != newContentElem.childNodes.length; ++i )
	{
		if ( newContentElem.childNodes[i].tagName == "DIV" )
		{
			var j;
			for ( j = i+1; j != newContentElem.childNodes.length; ++j )
			{
				if ( newContentElem.childNodes[j].tagName == "DIV" )
				{
					var header = newContentElem.childNodes[i];
					var content = newContentElem.childNodes[j]
					
					g_collapseObjList.push( new CollapseElem( header, content ) );
					
					i = j;
					break;
				}
			}
		}
	}
}

function unbindCollapseContent()
{
	if ( g_collapseObjList )
	{
		var i;
		for ( i = 0; i != g_collapseObjList.length; ++i )
		{
			// remove any DHTML additions and event bindings
			g_collapseObjList[i].dispose();
			g_collapseObjList[i] = null;
		}
		g_collapseObjList.length = 0;
	}
}


function contentSet( newContentElem, pageIndex )
{
	var collapsediv = findChildElemWithId( newContentElem, "collapsediv" );
	// setup the collapsable regions, currently the news is the only section that uses this
	if ( collapsediv )
		bindCollapseContent( collapsediv );
	
	var targetElem = document.getElementById( "pagecontent" );
	targetElem.appendChild( newContentElem );
	// change the URL location to link to the new hash location
	var newLocation = window.location.protocol + "//" + window.location.host + window.location.pathname + g_tabIndexToHash[pageIndex];
	window.location.replace( newLocation );
	// update the document Title
	document.title = "Smoking Gun Interactive - " + g_tabIndexToTitle[pageIndex];
}

function contentClear()
{
	unbindCollapseContent();
	
	var targetElem = document.getElementById( "pagecontent" );
	if ( targetElem.firstChild )
	{
		targetElem.removeChild( targetElem.firstChild );
	}
}