
var HistoryController = Class.create();

HistoryController.prototype = {
	historyFrame: {},
	historyArray: [],
	
	initialize: function(){
		
		document.body.historyController = this;
		
		this.historyFrame = document.createElement("iframe");		
		document.body.appendChild(this.historyFrame);
		
		/* if you make it invisible safari wont "see" the iframe*/
		this.historyFrame.style.position = "absolute";
		this.historyFrame.style.width = "1px";
		this.historyFrame.style.height = "1px"; 
		this.historyFrame.style.visibility = "hidden";
		
		
		this.historyFrameLocation = "history.html";
		
		
		this.historyFrame.src = this.historyFrameLocation+"?index=0";
		
		this.historyArray = [];
		this.index = 0;
		
	},
	
	addToHistory: function(path, callbackObject){
		//check if it is the same as you have already
		if(this.historyArray.length > 0){
			if(path.pageId == this.historyArray[this.index].path.pageId) return;
		}
		
		//check if you are on last item...if not cut array
		if(this.index<(this.historyArray.length-1)){
			this.historyArray = this.historyArray.slice(0, parseInt(this.index+1))
		}
		
		    
		//if not add it
	    this.historyArray.push({path: path, callbackObject: callbackObject});
		this.index = this.historyArray.length-1;
		
		this.historyFrame.src = this.historyFrameLocation+"?index="+this.index;
	},
	
	moveBack: function(){
		this.gotoIndex(this.index-1);
	},
	
	moveForeward: function(){
		this.gotoIndex(this.index+1);
	},
	
	gotoIndex: function(index){
		
		if(index == this.index) return;
		
		//return if index is too high or low
		if(index >= this.historyArray.length) return;
		if (index < 0) return;
		
		this.index = parseInt(index);
		
		var handlerObject = this.historyArray[index].callbackObject;
		var path = this.historyArray[index].path;

		handlerObject.gotoPath(path);
		
	}
	
};