// Copyright (c) 2000-2007 Araxis Ltd. All rights reserved.

var g_fShowHideInitialized = false;
var g_crumbsShowHideControls;
var g_sShowHideCookieName = "dxShowHideSections_v1"; // "v1" suffix allows easy versioning in case we need to change format


function initShowHide()
{
	g_crumbsShowHideControls =  new CrumbsCtor(getPageFilenameWithoutExtension(), g_sShowHideCookieName);
	g_fShowHideInitialized = true;
}


function showBlock(elementId, fPersistCollapsedState)
{
	if (!getElementUsingId(elementId + "Block") || !getElementUsingId(elementId + "Block").className)
		return;
		
	if (fPersistCollapsedState)
    	g_crumbsShowHideControls.setCrumb(elementId, 1);
	
    var element = getElementUsingId(elementId + "Block"); 
    if (element != null && element.className != "shown")
    	element.className = "shown";
  
    element = getElementUsingId(elementId + "WhenHidden");
    if (element != null && element.className != "hidden")
    	element.className = "hidden";
  
    element = getElementUsingId(elementId + "WhenShown");
    if (element != null && element.className != "shown")
    	element.className = "shown";
  
    element = getElementUsingId(elementId + "Img");
    if (element != null && element.src.substring(element.src.length - 9) != "Minus.gif")
    	element.src = g_styleDir +"/Img/Minus.gif";
}


function hideBlock(elementId, fPersistCollapsedState)
{
	if (!getElementUsingId(elementId + "Block") || !getElementUsingId(elementId + "Block").className)
		return;
		
	if (fPersistCollapsedState)
	    g_crumbsShowHideControls.setCrumb(elementId, 0);

	var element = getElementUsingId(elementId + "Block"); 
	if (element != null && element.className !="hidden")
		element.className = "hidden";
	  
	element = getElementUsingId(elementId + "WhenHidden"); 
	if (element != null && element.className !="shown")
		element.className = "shown";
	  
	element = getElementUsingId(elementId + "WhenShown");
	if (element != null && element.className !="hidden")
		element.className = "hidden";
	  
	element = getElementUsingId(elementId + "Img"); 
	if (element != null && element.src.substring(element.src.length - 8) != "Plus.gif")
		element.src = g_styleDir + "/Img/Plus.gif";
}


function showHideBlock(blockId, fPersistCollapsedState)
{
	if (!getElementUsingId(blockId + "Block") || !getElementUsingId(blockId + "Block").className)
		return false;
	
    if (!g_fShowHideInitialized)
        return false;
    
	var blockElement = getElementUsingId(blockId + "Block");
	var  fIsCollapsed = (blockElement.className == "hidden");
	if (fIsCollapsed)
		showBlock(blockId, fPersistCollapsedState);	   
	else
		hideBlock(blockId, fPersistCollapsedState);

	return false;
}


function showHideBlockKey(evt, blockId, fPersistCollapsedState)
{
	if (!getElementUsingId(blockId + "Block") || !getElementUsingId(blockId + "Block").className || !evt || !evt.keyCode)
		return false;
	
	if (!g_fShowHideInitialized)
    	return false;

	var blockElement = getElementUsingId(blockId + "Block");
	var fIsCollapsed = (blockElement.className == "hidden");
  
	switch (evt.keyCode)
	{
		case 9: // tab
			return true; // allow default handling of this event
	    case 13: // enter
	    case 32: // space
	      if (fIsCollapsed)
	          showBlock(blockId, fPersistCollapsedState);
	      else
	          hideBlock(blockId, fPersistCollapsedState);
	    break;
	    case 37: // left  arrow
	        if (!fIsCollapsed)
	        {
	            hideBlock(blockId, fPersistCollapsedState);
	        }
	        break;
	    case 39: // right arrow
	        if (fIsCollapsed)
	        {
	            showBlock(blockId, fPersistCollapsedState);
	        }
	        break;
	    default:
	        break;
	}
	return false;
}


function saveShowHideStateChanges()
{
	if (typeof g_crumbsShowHideControls == "undefined" || !g_fEnableShowHideAndTabControlStatePersistence)
		return;
		
	g_crumbsShowHideControls.storePageSpecificCrumbsInCookie();
}


function setShowHidesFromPersistedState()
{
	if (typeof g_fEnableShowHideAndTabControlStatePersistence == "undefined" || !g_fEnableShowHideAndTabControlStatePersistence)
		return;
		
	if (!areCookiesEnabled())
		return;

	var aCrumbs = g_crumbsShowHideControls.toArray();
	for (var i = 0; i < aCrumbs.length; i++)
    {
    	var aFragmentComponents = aCrumbs[i].split("@");
    	if (typeof aFragmentComponents[1] != "undefined")
    	{
			if (aFragmentComponents[1] == "0")
				hideBlock(aFragmentComponents[0], true);
			else
				showBlock(aFragmentComponents[0], true);
    	}
	}
}


//----
// Fns below here are for the dynamic explain blocks used by Ketura
//----

function showHideDynamicBlockKey(evt, blockId, sURL)
{
    if (!getElementUsingId(blockId + "Block") || !getElementUsingId(blockId + "Block").className || !evt || !evt.keyCode)
        return false;
    
    if (!g_fShowHideInitialized)
        return false;

    var blockElement = getElementUsingId(blockId + "Block");
    var fIsCollapsed = (blockElement.className == "hidden");
  
    switch (evt.keyCode)
    {
        case 9: // tab
            return true; // allow default handling of this event
        case 13: // enter
        case 32: // space
          if (fIsCollapsed)
              sendXmlHttpRequestForDynamicBlock(blockId, sURL);
          else
              hideBlock(blockId, false);
        break;
        case 37: // left  arrow
            if (!fIsCollapsed)
            {
                hideBlock(blockId, false);
            }
            break;
        case 39: // right arrow
            if (fIsCollapsed)
            {
                sendXmlHttpRequestForDynamicBlock(blockId, sURL);
            }
            break;
        default:
            break;
    }
    return false;
}


function showHideDynamicBlock(blockId, sURL)
{
	// check that block exists
	if (!getElementUsingId(blockId + "Block") || !getElementUsingId(blockId + "Block").className)
		return false;
	
	// check that show hide functionality has been initialized
    if (!g_fShowHideInitialized)
        return false;
    
	var blockElement = getElementUsingId(blockId + "Block");
	var  fIsCollapsed = (blockElement.className == "hidden");
	if (!fIsCollapsed)
		hideBlock(blockId, false);
	else
	{
		sendXmlHttpRequestForDynamicBlock(blockId, sURL);
	}
	return false;
}


function sendXmlHttpRequestForDynamicBlock(blockId, sURL)
{
    // add new object for block to array of XMLHTTPRequest objects
    initXMLHTTPRequestObject("BLOCK_" + blockId);
    
    // if the browser does not support the XMLHTTRequest object, or the browser security does not allow its creation,
    // we just return without doing anything
    if (g_aobjXmlHttpRequests["BLOCK_" + blockId] == null)
        return false;
    
    // sets the event handler function that will be invoked when the request is complete
    g_aobjXmlHttpRequests["BLOCK_" + blockId].onreadystatechange = onXMLHTTPRequestEventForShowHideDynamicBlock;
    
    // make an asynchronous get request for the XML data
    g_aobjXmlHttpRequests["BLOCK_" + blockId].open("GET", sURL, true);
    g_aobjXmlHttpRequests["BLOCK_" + blockId].send(null);
}


function removeChildrenFromNode(node)
{
	if (typeof node == "undefined" || node == null)
		return;
   
	while (node.hasChildNodes())
	{
		node.removeChild(node.firstChild);
	}
}


function onXMLHTTPRequestEventForShowHideDynamicBlock()
{
    // pass through AJAX requests processing any that are for explanation blocks
    for (sKey in g_aobjXmlHttpRequests)
    {
        if (sKey.substring(0, "BLOCK_".length) == "BLOCK_")
        {
            // get the block element
            var sBlockId = sKey.substring(6);
            var blockElement = getElementUsingId(sBlockId + "Block");
        
            // check that block exists in the document
            if (!blockElement || !blockElement.className)
                continue;
            
            var xmlHttpRequest = g_aobjXmlHttpRequests[sKey];
            
            // check that the object is in the correct ready state and its status is complete before inserting HTML
            if (xmlHttpRequest.readyState == 4)
            {
                // if there was a problem with the request, continue to next id without doing anything
                if (xmlHttpRequest.status != 200)
                    continue;        
                
                // find the explanation div block, it is always the first child of the block
                var elementExplanationDiv = blockElement.firstChild;
        
                // remove existing child elements of the block
                removeChildrenFromNode(elementExplanationDiv);    
        
                // strip everything up to and including the the first <body> tag , and everything including and after the last </body> tag
                var sResponseText = new String(xmlHttpRequest.responseText);
                if (sResponseText.indexOf("<body") > -1)
                {
                    sResponseText = sResponseText.substr(sResponseText.indexOf("<body") + 5 );
                    if  (sResponseText.indexOf(">") > -1)
                    {
                        sResponseText = sResponseText.substr(sResponseText.indexOf(">") + 1);
                        if  (sResponseText.lastIndexOf("</body>") > -1)
                        {
                            sResponseText = sResponseText.substr(0, sResponseText.lastIndexOf("</body>"));
                        }
                    }
                }
        
                // write explanation into the page document
                elementExplanationDiv.innerHTML = sResponseText;
            
                // only reveal explanation once text has been inserted!
                showBlock(sBlockId, false);
                
                // we are done with request object, so we delete it from the store
                delete g_aobjXmlHttpRequests[sKey];
            }
        }
    }
}
