﻿///////////////////////////////////////////////////////
// Name: history.js
// Type: Javascript
// Version: 1.1.01
//
// Written by: Mattias Ekendahl (ME)
// Edited by: Mattias Ekendahl (ME)
// E-mail: mattias@tewonder.se (ME)
// Created: 2007-04-18
// Modified: 2007-04-20 (ME)
///////////////////////////////////////////////////////

var tewonderHistoryListeningFlashObjectsArray;
var tewonderHistoryCallNr;
var tewonderLastHistoryCallsArray;
var tewonderLastHistoryCallNr;
var tewonderHistoryIframe;
var tewonderHistoryFilePath;

function setHistoryIframe(iHistoryIframe, iHistoryFilePath) {
	//alert("setHistoryIframe");
	tewonderHistoryIframe = iHistoryIframe;
	tewonderHistoryFilePath = iHistoryFilePath;
}

function setHistory(iProjectName, iType, iData) {
	//alert("setHistory");
	//alert(tewonderHistoryFilePath + "?project=" + iProjectName + "&type=" + iType + "&data=" + iData);
	if(tewonderHistoryCallNr == undefined) {
		tewonderHistoryCallNr = 0;	
	}
	tewonderHistoryCallNr++;
	setLastHistoryCall(iProjectName, iType, iData, tewonderHistoryCallNr);
	tewonderHistoryIframe.src = tewonderHistoryFilePath + "?project=" + iProjectName + "&type=" + iType + "&data=" + iData + "&call=" + tewonderHistoryCallNr;
}

function reportInHistoryListeningFlash(iObject, iFlashVersion) {
	//alert("reportInHistoryListeningFlash");
	if(tewonderHistoryListeningFlashObjectsArray == undefined) {
		tewonderHistoryListeningFlashObjectsArray = new Array();
	}
	var newObject = new Object();
	newObject.theObject = iObject;
	if(iFlashVersion == undefined) {
		newObject.flashVersion = flash.version;	
	}
	else {
		newObject.flashVersion = iFlashVersion;
	}
	tewonderHistoryListeningFlashObjectsArray.push(newObject);
}

function reportHistoryChangeToFlash(iProjectName, iType, iData, iCallNr) {
	//alert("reportHistoryChangeToFlash");
	var isLastCall = isLastHistoryCallForProject(iProjectName, iType, iData);
	if(isLastCall) {
		if(tewonderHistoryCallNr == iCallNr) {
			tewonderLastHistoryCallNr = iCallNr;
			return;
		}
		else {
			if(iCallNr < tewonderLastHistoryCallNr)	{
				history.back();
				return;
			}
			else {
				history.forward();
				return;				
			}
		}
	}
	else {
		var currentArray = tewonderHistoryListeningFlashObjectsArray;
		for(var i = -1; ++i < currentArray.length;) {
			if((1*currentArray[i].flashVersion) < 8) {
				currentArray[i].theObject.SetVariable("historyCall", "project=" + iProjectName + "&type=" + iType + "&data=" + iData);
			}
			else {
				currentArray[i].theObject.historyCall(iProjectName, iType, iData);
			}
		}
		setLastHistoryCall(iProjectName, iType, iData, iCallNr);
	}
}

function isLastHistoryCallForProject(iProjectName, iType, iData) {
	//alert("isLastHistoryCallForProject");
	var currentArray = tewonderLastHistoryCallsArray;
	for(var i = -1; ++i < currentArray.length;) {
		if(currentArray[i].projectName == iProjectName)	{
			if((currentArray[i].type == iType) && (currentArray[i].data == iData)) {
				return true;
			}
			return false;
		}
	}
	//MENOTE: error
	return false;
}

function setLastHistoryCall(iProjectName, iType, iData, iCallNr) {
	//alert("setLastHistoryCall");
	tewonderLastHistoryCallNr = iCallNr;
	if(tewonderLastHistoryCallsArray == undefined) {
		tewonderLastHistoryCallsArray = new Array();
	}
	var currentArray = tewonderLastHistoryCallsArray;
	for(var i = -1; ++i < currentArray.length;) {
		if(currentArray[i].projectName == iProjectName)	{
			currentArray[i].type = iType;
			currentArray[i].data = iData;
			return;
		}
	}
	var newObject = new Object();
	newObject.projectName = iProjectName;
	newObject.type = iType;
	newObject.data = iData;
	tewonderLastHistoryCallsArray.push(newObject);
}

//MENOTE: flash 7 needs to call DoFSCommand instead of calling the javascript function direct
/*function flashObjectName_DoFSCommand(commandName, argumentsString) {
	if (commandName == "setHistory") {
		tewonderHistoryIframe.src = tewonderHistoryFilePath + "?" + argumentsString;
	}
}*/