//alert( 'bdiusa_functions loading' );

//Contact John Aguinaldo (jagui99@hotmail.com) for any questions concerning the functions below
designer="John Aguinaldo\njagui99@hotmail.com";

/*
*******************************************************************************
***   TURN OFF ERROR REPORTING                                              ***
***                                                                         ***/
function handleError()
{
    return true;
}
// Uncomment the line below to suppress javascript errors
window.onerror = handleError;



/*
*******************************************************************************
***   BROWSER DETECTION                                                     ***
***                                                                         ***/
//abstract browser detection to seperate file
browser = navigator.appName;
version = navigator.appVersion;
version_major = version.slice( 0,1 );

ns4 = (document.layers) ? 1 : 0;
ie = (document.all) ? 1: 0;
ns6 = ( browser.indexOf("Netscape") != -1  &&  version_major == '5' ) ? 1 : 0;



/*
*******************************************************************************
***   HANDLE NETSCAPE RESIZE                                                ***
***                                                                         ***/

function handleResize() {
    location.reload();
    return false;
}

if (document.layers) {
    window.captureEvents(Event.RESIZE)
    window.onresize = handleResize
}




/*
*******************************************************************************
***   INSTANTIATE IMAGE OBJECTS AND ASSIGN SOURCE                           ***
***                                                                         ***/
//
//    DESCRIPTION       This function is used to preload images by creating
//                      image objects from the name (imageName) and source
//                      (imageSrc) passed in as parameters.
//                      A third parameter 'imageNameSuffix' is passed to
//                      append to the image name (i.e. if image name 'testImage'
//                      is passed and imageNameSuffix 'Out' is passed, then
//                      the image object reference will be: testImageOut.src
//
//    PARAMETERS
//
//    imageName:        The name to be used to reference the image object
//
//    imageSrc:         The file path/name of the image source assigned to the image object
//
//    imageNameSuffix:  The suffix to be appended to the name when naming the image object
//
//
function makeImageObj( imageName, imageSrc, imageNameSuffix )
{
    //IF NO IMAGENAMESUFFIX PASSED, THEN SET TO NULL
    if ( makeImageObj.arguments.length < 2 ) { imageNameSuffix = "" }
    eval( imageName + imageNameSuffix + ' = new Image()' );
    eval( imageName + imageNameSuffix + '.src = "' + imageSrc + '"' );
}



/*
*******************************************************************************
***   IMAGE CHANGE()                                                        ***
***                                                                         ***/
// Parameter 'div_layer_name' is optional.  It is the name of the positionable div
// element that the image is a child of.
//
function imageChange( image_name, new_image_name, div_layer_name )
{
    // IF IMAGE IS CHILD OF POSITIONABLE DIV LAYER AND BROWSER IS NS4
    if ( ns4 && div_layer_name )
    {
        document.layers[div_layer_name].document[image_name].src = new_image_name.src;
    }
    else
    {
        document[image_name].src = new_image_name.src;
    }
}



/*
*******************************************************************************
***   SET IMAGE()                                                           ***
***                                                                         ***/
//
function setImage( imageName, action, frozenImgVar, unfreezeImgSufx, divName, frozenDivVar )
{
    // Evaluate frozenImgVar once and store for later reuse - for efficiency
    frozenImgVarStr =  eval(frozenImgVar);

    // Unless image is currently selected (frozen), then change image source to 'action'
    if ( imageName != frozenImgVarStr )
    {
        document[imageName].src = eval( imageName + action + '.src' );

        //  This section controls 'sticky' images - image stays set when clicked
        //  and is unset when another image is clicked.
        //  'unfreezeImgSufx' is only used when setting sticky images,
        //  so we test for its existence to continue with this block
        if ( unfreezeImgSufx )
        {
            // Unless this is the very first click, unset the source of the
            // frozen image using the user specified source suffix
            if ( frozenImgVarStr != '' )
            {
                document[ frozenImgVarStr ].src =  eval( frozenImgVarStr + unfreezeImgSufx + '.src' );
            }

            // Set current image to frozen
            eval( frozenImgVar + ' = imageName' );

            // If using div option, then call showDiv() to reveal target div and conceal frozen div
            if ( divName && frozenDivVar )
            {
                showDiv( divName, frozenDivVar );
            }
        }
    }
}



/*
*******************************************************************************
***   NEXT_PREV()                                                           ***
***                                                                         ***/
//    DESCRIPTION:  This function accepts an array (reference_array), the name
//                  of an element within that array (active_var) and an offset
//                  number (positive or negative integer).  This function returns
//                  the name of the array element existing at the offset location
//                  relative to the location of active_var within the array.
//                  Essentially, this function retrieves the 'next' or 'previous'
//                  element in the array relative to active_var and offset.
//
//    PRECONDITIONS:reference_array must be a cross-referenced array
//                  constructed as follows:
//                      reference_array[0] = 'zero';
//                      reference_array['zero'] = 0;
//                      reference_array[1] = 'one';
//                      reference_array['one'] = 1;
//                      ...
//
function prev_next( reference_array, active_element, offset )
{
    //DETERMINE INDEX OF ACTIVE ELEMENT
    current_index = reference_array[ active_element ];

    //FORCE TO NUMBER
    current_index = parseInt( current_index );

    //TEST LIMIT AND WRAP AROUND TO BEGINNING OR END OF ARRAY
    if( offset < 0 && current_index == 0 )
    {
        current_index = reference_array.length;
    }
    else if( offset > 0 && current_index == reference_array.length - 1 )
    {
        current_index = -1;
    }

    //DETERMINE NAME OF ELEMENT AT OFFSET INDEX
    offset_element_name = reference_array[current_index + offset];

    //RETURN NAME
    return offset_element_name;
}



/*
*******************************************************************************
***   DYNAMIC_YSCROLL()                                                     ***
***                                                                         ***/
//
lastScrollY = 0;

function dynamic_yscroll( target_layer )
{
    if( !ie && !ns4 && !ns6 ) { return false; }
    offset = 0;

    function get_page_top()
    {
        if ( ie  )              { return document.body.scrollTop; }
        else if ( ns4 || ns6 )  { return self.pageYOffset; }
        else                    { return false; }
    }

    function get_layer_pos()
    {
        if ( ie )           { return document.getElementById( target_layer ).style.pixelTop; }
        else if ( ns4 )     { return document[ target_layer ].top }
        else if ( ns6 )     { return document.getElementById( target_layer ).style.top; }
        else                { return false; }
    }

    function set_layer_pos( move )
    {
        if ( ie )           { return document.getElementById( target_layer ).style.pixelTop += move; }
        else if ( ns4 )     { return document[ target_layer ].top += move; }
        else if ( ns6 )
        {
            //EXTRACT NUMBER FROM STRING VALUE [i.e. extract 100 from '100px']
            doc_top  = document.getElementById( target_layer ).style.top;
            top = parseInt( doc_top.substring( 0, doc_top.length -2 ) );
            document.getElementById( target_layer ).style.top = top + move;
        }
        else                { return false; }
    }

    diffY = get_page_top() + offset;

    if(diffY != lastScrollY + offset)
    {
        move_by = .1 * (diffY - lastScrollY);

        if( move_by > offset ) { move_by = Math.ceil(move_by); }
        else { move_by = Math.floor(move_by); }

        layer_pos = get_layer_pos();
        set_layer_pos( move_by );


        lastScrollY += move_by;
    }
}



/*
*******************************************************************************
***   FILE_PROGRESS_BAR()                                                   ***
***                                                                         ***/
// DESCRIPTION:     This function accepts an array if image names that are
//                  loading and determines when they have loaded.
//                  The load completion status is reported as a bar graph
//                  meter dynamically displayed in a floating layer.
//
// PRECONDITIONS:   Requires the following:
//                      1) Global variable 'intervalId'
//                      2) Bar graph contains exactly 10 images.
//                      3) Bar graph 'on' and 'off' images must be instantiated
//                          at the top of the page.
//
// POSTCONDITIONS:

complete = 0;

function file_progress_bar( image_names_array, div_layer, image_name_suffix )
{
    if(ns6){ return false; }

    //DISPLAY HIDDEN BAR GRAPH LAYER
    if( ns4 ){ eval( 'document.layers.' + div_layer + '.visibility = "visible"' ) }
    else if( ie ) { eval( 'document.all.' + div_layer + '.style.visibility = "visible"' ) }

    //IF IMAGE_NAMES_ARRAY IS SET TO ZERO, THEN STOP PROCESS
    if( !image_names_array ) { stopInterval(); }

    count = 0;

    //CHECK LOAD STATUS OF EACH IMAGE IN ARRAY
    for( i in image_names_array )
    {
        //IF IMAGE IS LOADED THEN INCREASE COUNTER
        if( eval( image_names_array[i] + image_name_suffix + '.complete' ) )
        {
            count++;
        }
    }

    //CALCULATE PERCENT OF IMAGES LOADED
    pct_complete = 10 * ( count/image_names_array.length );

    //FORCE TO INTEGER
    pct_complete = parseInt( pct_complete );

    //alert( 'total = ' + total + '\ncount = ' + count + '\npct_complete = ' + pct_complete );

    //TURN ON PROGRESS BAR ELEMENTS EQUAL TO PERCENT COMPLETE
    for( j=0; j<pct_complete; j++ )
    {
        if( ns4 ) { eval( 'document.layers[\'bar_graph\'].document.progress_bar' + j + '.src = progress_bar_on.src' ) }
        //else if( ie ){ eval( 'document.progress_bar' + j + '.src = progress_bar_on.src' ) }
        else if( ie ){ eval( 'document.progress_bar' + j + '.width = 50' ) }
    }

    // IF ALL IMAGES HAVE BEEN LOADED
    if( count == image_names_array.length )
    {
        //THIS IF/ELSE SECTION FORCES A DELAY BEFORE HIDING THE BAR GRAPH LAYER
        if( complete )
        {
            //STOP SETINTERVAL
            clearInterval( intervalId );

            //HIDE PROGRESS BAR METER
            if( ns4 ){ eval( 'document.layers.' + div_layer + '.visibility = "hidden"' ) }
            else if( ie ){ eval( 'document.all.' + div_layer + '.style.visibility = "hidden"' ) }
            return true;
        }
        else
        {
            //PAUSE X SECONDS - DELAYS HIDING PROGRESS BAR LAYER
            window.setTimeout( 'window.complete++', 1000 );
        }
        return true;
    }
}

