/** 
   Copyright (c) 2006, Nils Millahn, nils@hub124.co.uk
   http://www.hub124.co.uk
   
   Permission is hereby granted, free of charge, to any person obtaining 
   a copy of this software and associated documentation files (the "Software"), 
   to deal in the Software without restriction, including without limitation 
   the rights to use, copy, modify, merge, publish, distribute, sublicense, 
   and/or sell copies of the Software, and to permit persons to whom the 
   Software is furnished to do so, subject to the following conditions:
   
   The above copyright notice and this permission notice shall be 
   included in all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 
   OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
   THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 * DESCRIPTION:
 * Integrates the dhtmlHistory javascript library with a Flash application.
 *
 * USAGE:
 * This file and dhtmlHistory.js must be included in the same document.
 * Then call 'initFlashHistory("swfidname")' in HTML.
 *
 * Use externalInterface in Flash to call flashAddHistory(location, pageTitle);
 * And use ExternalInterface.addCallback() to add a callback to the 'historyLocationChanged' function.
 *
 * NOTE:
 * Will only work in combination with Flash 8 and browsers that support ExternalInterface.
 *
**/

FlashHistoryIntegrator = new Object();

// default SWF id
FlashHistoryIntegrator.swfID = "myMovie";

// default page title
FlashHistoryIntegrator.pageTitle = "";

// initialisation function
FlashHistoryIntegrator.init = function(id, pageTitle)
{
	// save
	this.swfID = id;
	
	if(pageTitle)
		this.pageTitle = pageTitle;

	// initialize our DHTML history
	dhtmlHistory.initialize();
    
    // subscribe to DHTML history change events
    var changeFn = function(loc,data){FlashHistoryIntegrator.historyChanged(loc, data);};
    dhtmlHistory.addListener(changeFn);
};

// add a history item - event handler from flash
// defined as a window level function to work with ExternalInterface
flashAddHistory = function (location, title)
{
	if(location)
		FlashHistoryIntegrator.addHistory(location);
	
	if(title)
		FlashHistoryIntegrator.setTitle(title);
};

FlashHistoryIntegrator.addHistory = function(location)
{
	dhtmlHistory.add(location);
};

// set the document title
FlashHistoryIntegrator.setTitle = function(title)
{
	if(document.title)
		document.title = this.pageTitle + title;
};

// history change event handler
FlashHistoryIntegrator.historyChanged = function(newLocation, historyData)
{
	this.notifyFlash(newLocation);
};

// notify the flash movie
FlashHistoryIntegrator.notifyFlash = function (msg)
{
	var myFlashMovie = document.getElementById(this.swfID);
	if(myFlashMovie.historyLocationChanged)
	{
		myFlashMovie.historyLocationChanged(msg);
	}
};