﻿
var bb_count = 0;
var bb_curr_idx = "";
var bb_cache = new Array;
var bb_iframe_script = "framecounter.htm";
var bb_iframe_loaded = false;
var bb_target_div = "";

// Run from the interval timer (every 300 mseconds), this function reads a cache index
// value that is stored in the DIV element of the child IFRAME.

// If this extracted cache index differs from the current cache index, then the
// back button was pressed.  In this case, we pull the corresponding data from the
// cache and update the page.

function bb_check_state()
{
    if (bb_iframe_loaded == false)
	    return;

	var doc = window.frames['bbFrame'].document;
	var new_idx;

	if (doc.getElementById('divFrameCount') == null)
	    new_idx = null;
	else
        new_idx = doc.getElementById('divFrameCount').innerHTML;

    //alert("check state: " + new_idx + ", " + bb_curr_idx);
    
    if (new_idx != bb_curr_idx) {
      
    	// Pull a previous state from the cache (if it exists).
	    if (bb_cache[new_idx])
	    {
	        var divBody = document.getElementById(bb_target_div);
	        divBody.innerHTML = bb_cache[new_idx];
        }

        bb_curr_idx = new_idx;
    }
}

// Called by child IFRAME
function bb_done_loading()
{
    bb_iframe_loaded = true;
}

// Update the hidden IFRAME.
function bb_loadframe()
{
    var bbFrame = document.getElementById("bbFrame");
    bb_iframe_loaded = false;
    bbFrame.src = bb_iframe_script + "?" + bb_count;
}

// When requested, save the current state in a cache.
function bb_save_state() {
    // Store the new contents in the cache.
    var div_to_cache = document.getElementById(bb_target_div);
    
    if (div_to_cache == null)
        return;

    bb_count++;
    bb_cache[bb_count] = div_to_cache.innerHTML;
    //alert("STORED[" + bb_count + "]: ");

    // Load new page into iframe.
    bb_loadframe();

    bb_curr_idx = bb_count;
}

function bb_save_first_state() {
    // Store the new contents in the cache.
    var div_to_cache = document.getElementById(bb_target_div);

    if (div_to_cache == null)
        return;

    bb_cache[bb_count] = div_to_cache.innerHTML;
    //alert("STORED[" + bb_count + "]: ");
}

// Load the hidden IFRAME and start an interval timer.
function bb_init(div_name)
{
    bb_target_div = div_name;
    bb_loadframe();
    bb_curr_idx = 0;
    //alert("init and starting timer");
    bb_save_first_state();
    window.setInterval('bb_check_state()', 300);
}


