﻿
/***************************************************************

    Created By: Asa Sherrill <asa.sherrill@definition6.com>
    Date: 12/18/2006
    
    Directory Based Highlighting uses the directory of an
    existing web page to determine which style to apply
    to registered elements.
    
    To use this package, simply register directory names with
    their corresping elements. Additionally, you will want to
    designate on/off state classes to be applied to the 
    designated elements.
    
    1. declareStateClasses ( OnState, OffState ) 
    Simply pass strings of the requested classes. By default these
    will be DirectoryBasedHighlight_On and _Off.
    
    2. addRelation ( DirectoryName, IDName ) 
    This statement adds a relation to the list. When a page with
    this script enabled is loaded, if the page exists within the 
    indicated directory, the indicated ID will be granted the 
    ON CSS Class.
    
    Use "root" for the root directory
        
*****************************************************************/

    var MAXIMUM_DIRECTORIES = 30;
    var GROWTH_CONSTANT = 5;

    var arr_DirectoryList = new Array( MAXIMUM_DIRECTORIES );
    var arr_RelatedIDs = new Array( MAXIMUM_DIRECTORIES );
    
    var int_ArrayPointer = 0;
    
    var str_OnClass = "DirectoryBasedHighlighting_On";
    var str_OffClass = "DirectoryBasedHighlighting_Off";
    
    
    /*********************************************************/
    
    function highlight() {
        var str_Directory = getDirectory();
        var int_A_seeker = 0;
        
        if ( navigator.appName != "Microsoft Internet Explorer" ) {
            int_A_seeker = 1;
        }

        for ( var v = 0; v < int_ArrayPointer; v ++ ) {

            if ( arr_DirectoryList[v] == str_Directory ) {
                
                document.getElementById( arr_RelatedIDs[v] ).childNodes[0].className = str_OnClass;

            } else { 
                document.getElementById( arr_RelatedIDs[v] ).childNodes[0].className = str_OffClass;
            }
            
        }

   }

    /***********************************************************/
    
    
    
    function declareStateClasses ( OnState, OffState ) {
    /* PRE: two strings referring to defined classes
       POST: sets the variables to be used in processing */
 
        str_OnClass = OnState;
        str_OffClass = OffState;
    
        

    }
    
    function addHomeRelation ( IDName ) {
    /* PRE: the DOM-Object that should relate to
            the root of the website
       POST: creates a relation with "root" as the
             directory
    */         
            addRelation ( "root", IDName );       

    }
    
    
    function addRelation ( DirectoryName, IDName ) {
    /* PRE: a directory name and an ID that should be
            reactive when a page is loaded within the 
            declared directory.
       
       POST: adds a relation element to the internal list
    */ 
    
        if ( int_ArrayPointer >= MAXIMUM_DIRECTORIES ) {  
        
            growRelationLists();
            
        }
       
        arr_DirectoryList[int_ArrayPointer] = DirectoryName;
        arr_RelatedIDs[int_ArrayPointer ++] = IDName;    

    }
    
    
    
    
    function getDirectory () {
    /* PRE: none
       POST: returns the directory that the existing page
             lives in. If the page sits on the root of the 
             website, returns a string equalling "root"
   */

        var urlString = document.URL.split("/");
        var directory = urlString[ urlString.length - 2 ];
        
       
        if ( directory != urlString[2] && 
             directory.indexOf('www') < 0 && 
             directory.indexOf('localhost') < 0 &&
             directory != 'quantumradiology' &&
             directory != 'QuantumRadiology' &&
             directory != 'stg.sb.def6.com' ) {

            return directory;
            
        } else {
        
            return "root";
            
        }
        
    }
    
    function growRelationLists () {
    
        var tmp_Directories = arr_DirectoryList;
        var tmp_RelatedIDs = arr_RelatedIDs;
        
        MAXIMUM_DIRECTORIES += GROWTH_CONSTANT;
        
        arr_DirectoryList = new Array ( MAXIMUM_DIRECTORIES );
        arr_RelatedIDs = new Array ( MAXIMUM_DIRECTORIES );
        
        for ( var x = 0; x < int_ArrayPointer; x ++ ) {
        
            arr_DirectoryList[x] = tmp_Directories[x];
            arr_RelatedIDs[x] = tmp_RelatedIDs[x];
        
        }
        
    }
    



