﻿//the path to the file that should be loaded into the iframe
var IFRAME_PAGE_URL = '/ajaxnavigator.htm';
//the iframe variable to append to the url
var IFRAME_URL_INDEX_VARIABLE_NAME = 'i';
//the iframe variable to append to the url
var IFRAME_URL_TITLE_VARIABLE_NAME = 't';
//the ajax iframe to use for history
var IFRAME_NAME = 'ajaxIFrame';
//array to hold the different actions
var _ajaxArrActions = null;
//stores the list of titles for all pages
var _ajaxArrTitles=null;
//an idex to point to the index of the current action
var _ajaxCurrentIndex = -1;

//adds a new ajax action  - this doesn't adjust the index until the item is saved to history
function AjaxRegisterAction(action, pageTitle)
{
    var index=AjaxSaveAction(action, pageTitle);
    AjaxSaveActionToHistory(index);
}
//checks if action already exists, and if not we save it
//if action already exists we update the title just in case it has changed
//the function returns the location the action is stored in the array
function AjaxSaveAction(action, pageTitle)
{
    if (_ajaxArrActions == null)
    {
        _ajaxArrActions = new Array(action);
        _ajaxArrTitles = new Array(pageTitle);
        return 0;
    }
    //checks if action already exists
    var count = _ajaxArrActions.length;
    for(index=0; index<count; index++)
    {
        if (_ajaxArrActions[index]==action)
        {
            _ajaxArrTitles[index]=pageTitle;
            return index;
        }
    }
    
    //action doesn't exist, so we add it
    _ajaxArrActions.push(action);
    _ajaxArrTitles.push(pageTitle);
    return (_ajaxArrActions.length-1)
}
//loads the page with the action index into the iframe
function AjaxSaveActionToHistory(index)
{    
    //if current index is already on index there is nothing to do
    if(_ajaxCurrentIndex == index)
        return;
        
    _ajaxCurrentIndex = index; 
    var urlVariables = IFRAME_URL_INDEX_VARIABLE_NAME + '=' + _ajaxCurrentIndex;              
    var src = IFRAME_PAGE_URL + '?' + urlVariables;
    var execFunction = 'AjaxUpdateIFrame(\'' + src + '\');'
    self.setTimeout(execFunction, 1);
}
function AjaxUpdateIFrame(src)
{
    var iframe = document.getElementById(IFRAME_NAME);
    iframe.src = src;
}
//this function should be called by the iframe source page when it's loads
//the function would update the title of the page, and if index supplied is not the current index
//it would execute the action specified
function AjaxOnIFrameLoad(iframePageWindow)
{
   var actionIndex = -1;
   var indexString = AjaxGetURLKeyValue(iframePageWindow, IFRAME_URL_INDEX_VARIABLE_NAME);
   if (indexString=='')
        return;
        
   try{actionIndex = parseInt(indexString);}
   catch(e)
   {return;}
   
   if (actionIndex < 0 || actionIndex >= _ajaxArrActions.length)
        return;
    
   var pageTitle = _ajaxArrTitles[actionIndex];
   iframePageWindow.document.title = pageTitle;
   
   //if this was just saved there is nothing to do
   if ( actionIndex == _ajaxCurrentIndex)
        return;
      
   var action = _ajaxArrActions[actionIndex];
   try{eval(action);}
   catch(e){}
}
//gets a windows object and a key and returns the value of that key from the url
function AjaxGetURLKeyValue(objWindow, key)
{
    var regexS = '[\\?&]' + key + '=([^&#]*)';
    var regex = new RegExp( regexS );
    var tmpURL = objWindow.location.href;
    var results = regex.exec(tmpURL);
    if( results == null )
        return '';
    else
        return results[1];
}