// {{MadCap}} //////////////////////////////////////////////////////////////////
// Copyright: MadCap Software, Inc - www.madcapsoftware.com ////////////////////
////////////////////////////////////////////////////////////////////////////////
// <version>5.0.1.0</version>
////////////////////////////////////////////////////////////////////////////////

function Default_WindowOnload()
{
	var framesForBridge	= ["body", "navigation"];
	
	for ( var i = 0; i < framesForBridge.length; i++ )
	{
		var frameName	= framesForBridge[i];
		
		frames[frameName].parentSandboxBridge = {};
		
		for ( var key in frames["bridge"].childSandboxBridge )
		{
			frames[frameName].parentSandboxBridge[key] = frames["bridge"].childSandboxBridge[key];
		}
	}
}

function CheckCSH()
{
	var hash	= document.location.hash.substring( 1 );
	
	if ( hash != "" )
	{
		if ( FMCIsSafari() )
		{
			hash = hash.replace( /%23/g, "#" );
		}

		var cshParts	= hash.split( "|" );
		
		for ( var i = 0; i < cshParts.length; i++ )
		{
			var pair	= cshParts[i].split( "=" );
			
			if ( pair[0] == "CSHID" )
			{
				gCSHID = decodeURIComponent( pair[1] );
			}
			else if ( pair[0] == "StartTopic" )
			{
				gStartTopic = decodeURIComponent( pair[1] );
			}
			else if ( pair[0] == "SkinName" )
			{
				gSkinFolder = "Data/Skin" + pair[1] + "/";
			}
		}
	}
}

function Default_Init()
{
	FMCPreloadImage( MCGlobals.SkinTemplateFolder + "Images/Loading.gif" );
	
	Default_LoadSkin();
	
	gInit = true;
}

function Default_LoadSkin()
{
    var xmlDoc		= CMCXmlParser.GetXmlDoc( gRootFolder + gSkinFolder + "Skin.xml", false, null, null );
    var xmlHead		= xmlDoc.getElementsByTagName( "CatapultSkin" )[0];
    var caption		= xmlHead.getAttribute( "Title" );
    
    if ( caption == null )
    {
		var masterHS	= FMCGetHelpSystem();

		if ( masterHS.IsWebHelpPlus )
		{
			caption = "WebHelp Plus";
		}
		else
		{
			caption = "WebHelp";
		}
    }
    
    document.title = caption;
    
    //
    
    Default_LoadWebHelpOptions( xmlDoc );
    
    if ( document.location.hash == null || document.location.hash.indexOf( "OpenType=Javascript" ) == -1 )
    {
		LoadSize( xmlDoc );
    }
}

function LoadSize( xmlDoc )
{
    try
    {
        var doc = frames["body"].document;
    }
    catch ( err )
    {
        return;
    }
    
    var xmlHead			= xmlDoc.documentElement;
    var useDefaultSize	= FMCGetAttributeBool( xmlHead, "UseBrowserDefaultSize", false );
    
    if ( useDefaultSize )
    {
		return;
    }
    
    var topPx		= FMCConvertToPx( frames["body"].document, xmlHead.getAttribute( "Top" ), null, 0 );
    var leftPx		= FMCConvertToPx( frames["body"].document, xmlHead.getAttribute( "Left" ), null, 0 );
    var bottomPx	= FMCConvertToPx( frames["body"].document, xmlHead.getAttribute( "Bottom" ), null, 0 );
    var rightPx		= FMCConvertToPx( frames["body"].document, xmlHead.getAttribute( "Right" ), null, 0 );
    var widthPx		= FMCConvertToPx( frames["body"].document, xmlHead.getAttribute( "Width" ), "Width", 800 );
    var heightPx	= FMCConvertToPx( frames["body"].document, xmlHead.getAttribute( "Height" ), "Height", 600 );
    
    var anchors = xmlHead.getAttribute( "Anchors" );
    
    if ( anchors )
    {
        var aTop    = (anchors.indexOf( "Top" ) > -1)    ? true : false;
        var aLeft   = (anchors.indexOf( "Left" ) > -1)   ? true : false;
        var aBottom = (anchors.indexOf( "Bottom" ) > -1) ? true : false;
        var aRight  = (anchors.indexOf( "Right" ) > -1)  ? true : false;
        var aWidth  = (anchors.indexOf( "Width" ) > -1)  ? true : false;
        var aHeight = (anchors.indexOf( "Height" ) > -1) ? true : false;
    }
    
    if ( aLeft && aRight )
    {
        widthPx = screen.availWidth - (leftPx + rightPx);
    }
    else if ( !aLeft && aRight )
    {
        leftPx = screen.availWidth - (widthPx + rightPx);
    }
    else if ( aWidth )
    {
        leftPx = (screen.availWidth / 2) - (widthPx / 2);
    }
    
    if ( aTop && aBottom )
    {
        heightPx = screen.availHeight - (topPx + bottomPx);
    }
    else if ( !aTop && aBottom )
    {
        topPx = screen.availHeight - (heightPx + bottomPx);
    }
    else if ( aHeight )
    {
        topPx = (screen.availHeight / 2) - (heightPx / 2);
    }
    
	if ( window == top )
	{
		try
		{
			// This is in a try/catch block because there seems to be a bug in IE where if the window loses focus
			// immediately before these statements are executed, IE will produce an "Access is denied" error.
			
			window.resizeTo( widthPx, heightPx );
			window.moveTo( leftPx, topPx );
		}
		catch ( err )
		{
		}
	}
}

function Default_LoadWebHelpOptions( xmlDoc )
{
    var webHelpOptions	= xmlDoc.getElementsByTagName( "WebHelpOptions" )[0];
    
    if ( webHelpOptions )
    {
		if ( webHelpOptions.getAttribute( "NavigationPanePosition" ) )
		{
			gNavPosition = webHelpOptions.getAttribute( "NavigationPanePosition" );
		}
		
        if ( webHelpOptions.getAttribute( "NavigationPaneWidth" ) )
        {
            var navWidth    = webHelpOptions.getAttribute( "NavigationPaneWidth" );
            
            if ( navWidth != "0" )
            {
				var hideNavStartup	= FMCGetAttributeBool( webHelpOptions, "HideNavigationOnStartup", false );
				
				if ( !hideNavStartup )
				{
					if ( gNavPosition == "Left" )
					{
						document.getElementsByTagName( "frameset" )[1].cols = navWidth + ", *";
					}
					else if ( gNavPosition == "Right" )
					{
						document.getElementsByTagName( "frameset" )[1].cols = "*, " + navWidth;
					}
					else if ( gNavPosition == "Top" )
					{
						var resizeBarHeight = 7;
	                    
						document.getElementsByTagName( "frameset" )[0].rows = navWidth + ", " + resizeBarHeight + ", *";
					}
					else if ( gNavPosition == "Bottom" )
					{
						document.getElementsByTagName( "frameset" )[0].rows = "*, " + navWidth;
					}
                }
            }
        }
        
		gHideNavStartup = FMCGetAttributeBool( webHelpOptions, "HideNavigationOnStartup", false );

		if ( gHideNavStartup )
		{
			ShowHideNavigation( false );
		}
    }
    
    // Safari
    
    if ( FMCIsSafari() )
    {
		var frameNodes	= document.getElementsByTagName( "frame" );
		
		for ( var i = 0; i < frameNodes.length; i++ )
        {
            if ( frameNodes[i].name == "navigation" )
            {
				if ( gNavPosition == "Left" )
				{
					frameNodes[i].style.borderRight = "solid 1px #444444";
					
					break;
				}
				else if ( gNavPosition == "Right" )
				{
					frameNodes[i].style.borderLeft = "solid 1px #444444";
                    
                    break;
				}
            }
        }
    }
}

function ShowHideNavigation( slide )
{
	if ( gInnerFrameset == null || gOuterFrameset == null || gBodyFrameNode == null )
	{
		return;
	}
	
    if ( gChanging )
    {
        return;
    }
    
    gChanging = true;
    gSlide = slide;
    
    if ( gNavigationState == "visible" )
    {
		gNavigationState = "hidden";
    }
    else if ( gNavigationState == "hidden" )
    {
		gNavigationState = "visible";
    }
    
	for ( var i = 0, length = gChangeNavigationStateStartedListeners.length; i < length; i++ )
	{
		gChangeNavigationStateStartedListeners[i]( gNavigationState, gNavPosition );
	}
    
    if ( gNavigationState == "hidden" )  // Hiding
	{            
		gNavigationFrameNode.tabIndex = "-1";
	}
	else                         // Showing
	{
		gNavigationFrameNode.tabIndex = "0";
	}
    
    if ( gNavPosition == "Left" || gNavPosition == "Right" )
    {
        ShowHideNavigationHorizontal();
    }
    else
    {
        ShowHideNavigationVertical();
    }
}

function ShowHideNavigationHorizontal()
{
    if ( gNavigationState == "hidden" )  // Hiding
    {
    	// IE 8 bug: Frameset col and row value is incorrect after the frameset has been resized using the mouse. Instead, we'll now use the frame's clientWidth and clientHeight value.
//        var cols    = gInnerFrameset.cols;
//        
//        if ( gNavPosition == "Left" )
//        {
//            gNavigationWidth = parseInt( cols );
//        }
//        else if ( gNavPosition == "Right" )
//        {
//            gNavigationWidth = parseInt( cols.substring( cols.indexOf( "," ) + 1 ) );
//        }

    	gNavigationWidth = frames["navigation"].document.documentElement.clientWidth;
    	gCurrNavigationWidth = gNavigationWidth;
    }
    else                         // Showing
    {
        gInnerFrameset.setAttribute( "border", 4 );
        gInnerFrameset.setAttribute( "frameSpacing", 2 )
        gBodyFrameNode.setAttribute( "frameBorder", 1 );
    }
    
    gIntervalID = setInterval( ChangeNavigationHorizontal, 10 );
}

function ShowHideNavigationVertical()
{
    if ( gNavigationState == "hidden" )  // Hiding
    {
    	// IE 8 bug: Frameset col and row value is incorrect after the frameset has been resized using the mouse. Instead, we'll now use the frame's clientWidth and clientHeight value.
//        var rows    = gOuterFrameset.rows;
//        
//        if ( gNavPosition == "Top" )
//        {
//            gNavigationWidth = parseInt( rows );
//        }
//        else if ( gNavPosition == "Bottom" )
//        {
//            gNavigationWidth = parseInt( rows.substring( rows.indexOf( "," ) + 1 ) );
//        }

    	gNavigationWidth = frames["navigation"].document.documentElement.clientHeight;
    	gCurrNavigationWidth = gNavigationWidth;
    }
    else                         // Showing
    {
        if ( gNavPosition == "Bottom" )
        {
            gOuterFrameset.setAttribute( "border", 4 );
            gOuterFrameset.setAttribute( "frameSpacing", 2 )
        }
    }
    
    gIntervalID = setInterval( ChangeNavigationVertical, 10 );
}

function ChangeNavigationHorizontal()
{
	// IE 8 bug: Frameset col and row value is incorrect after the frameset has been resized using the mouse. Instead, we'll now use the frame's clientWidth and clientHeight value.
//    var cols    = gInnerFrameset.cols;
//    
//    if ( gNavPosition == "Left" )
//    {
//        var currWidth   = parseInt( cols );
//    }
//    else if ( gNavPosition == "Right" )
//    {
//        var currWidth   = parseInt( cols.substring( cols.indexOf( "," ) + 1 ) );
//    }
    
    if ( gSlide )
    {
    	gCurrNavigationWidth = Math.min(Math.max(gCurrNavigationWidth + gNavigationChangeStep, 0), gNavigationWidth);
    }
    else
    {
    	gCurrNavigationWidth = 0;
	}
    
    for ( var i = 0, length = gChangingNavigationStateListeners.length; i < length; i++ )
    {
    	gChangingNavigationStateListeners[i](gCurrNavigationWidth);
    }

    if (gCurrNavigationWidth <= 0 || gCurrNavigationWidth >= gNavigationWidth)
    {
        clearInterval( gIntervalID );
        
        for ( var i = 0, length = gChangeNavigationStateCompletedListeners.length; i < length; i++ )
		{
			gChangeNavigationStateCompletedListeners[i]( gNavigationState, gNavPosition );
		}
        
        if ( gNavigationState == "hidden" )  // Hiding
        {
        	gCurrNavigationWidth = 0;
            
            gInnerFrameset.setAttribute( "border", 0 );
            gInnerFrameset.setAttribute( "frameSpacing", 0 )
        }
        else                         // Showing
        {
        	gCurrNavigationWidth = gNavigationWidth;
        }
        
        gNavigationChangeStep *= -1;
        gChanging = false;
    }
    
    if ( gNavPosition == "Left" )
    {
    	gInnerFrameset.cols = gCurrNavigationWidth + ", *";
    }
    else if ( gNavPosition == "Right" )
    {
    	gInnerFrameset.cols = "*, " + gCurrNavigationWidth;
    }
}

function ChangeNavigationVertical()
{
	// IE 8 bug: Frameset col and row value is incorrect after the frameset has been resized using the mouse. Instead, we'll now use the frame's clientWidth and clientHeight value.
//    var rows    = gOuterFrameset.rows;
//    
//    if ( gNavPosition == "Top" )
//    {
//        var currHeight  = parseInt( rows );
//    }
//    else if ( gNavPosition == "Bottom" )
//    {
//        var currHeight  = parseInt( rows.substring( rows.indexOf( "," ) + 1 ) );
//    }

	gCurrNavigationWidth = gSlide ? gCurrNavigationWidth + gNavigationChangeStep : 0;
	
    var resizeBarHeight = 7;

    if (gCurrNavigationWidth <= 0 || gCurrNavigationWidth >= gNavigationWidth)
    {
        clearInterval( gIntervalID );
        
        for ( var i = 0, length = gChangeNavigationStateCompletedListeners.length; i < length; i++ )
		{
			gChangeNavigationStateCompletedListeners[i]( gNavigationState, gNavPosition );
		}
        
        if ( gNavigationState == "hidden" )  // Hiding
        {
        	gCurrNavigationWidth = 0;
            resizeBarHeight = 0;
            
            if ( gNavPosition == "Bottom" )
            {
                gOuterFrameset.setAttribute( "border", 0 );
                gOuterFrameset.setAttribute( "frameSpacing", 0 )
            }
        }
        else                         // Showing
        {
        	gCurrNavigationWidth = gNavigationWidth;
            resizeBarHeight = 7;
        }
        
        gNavigationChangeStep *= -1;
        gChanging = false;
    }
    
    if ( gNavPosition == "Top" )
    {
    	gOuterFrameset.rows = gCurrNavigationWidth + ", " + resizeBarHeight + ", *";
    }
    else if ( gNavPosition == "Bottom" )
    {
    	gOuterFrameset.rows = "*, " + gCurrNavigationWidth;
    }
}

if ( gRuntimeFileType == "Default" )
{

var gInit					= false;
var gRootFolder				= FMCGetRootFolder( document.location );
var gStartTopic				= gDefaultStartTopic;
var gCSHID					= null;
var gLoadingLabel			= "LOADING";
var gLoadingAlternateText	= "Loading";
var gOuterFrameset			= null;
var gInnerFrameset			= null;
var gBodyFrameNode			= null;
var gNavigationWidth;
var gCurrNavigationWidth;
var gNavPosition			= "Left";
var gNavigationState		= "visible";
var gNavigationChangeStep	= -30;
var gSlide					= true;
var gChanging				= false;
var gChangeNavigationStateStartedListeners = new Array();
var gChangeNavigationStateCompletedListeners = new Array();
var gChangingNavigationStateListeners = new Array();
var gHideNavStartup			= false;

if ( FMCIsWebHelpAIR() )
{
	gOnloadFuncs.push( Default_WindowOnload );
}

window.onresize = function()
{
	// Firefox on Mac: might trigger this event before everything is finished being loaded.
	
	var indexFrame	= frames["navigation"].frames["index"];
	
	if ( indexFrame )
	{
		indexFrame.RefreshIndex();
	}
};

CheckCSH();

gOnloadFuncs.push( 
	function()
	{
		var framesetNodes = document.getElementsByTagName( "frameset" );
		
		gOuterFrameset = framesetNodes[0];
		gInnerFrameset = framesetNodes[1];

		var frameNodes = MCGlobals.RootFrame.document.getElementsByTagName( "frame" );
	    
		for ( var i = 0; i < frameNodes.length; i++ )
		{
			var currName    = frameNodes[i].name;
	        
			switch ( currName )
			{
				case "mctoolbar":
					gToolbarFrameNode = frameNodes[i];
					break;
				case "navigation":
					gNavigationFrameNode = frameNodes[i];
					break;
				case "body":
					gBodyFrameNode = frameNodes[i];
					break;
			}
		}
	}
);

gOnloadFuncs.push( Default_Init );

}

