YAHOO.si.HistoryManager =
{
    controller:YAHOO.si.Controller,
    bookmarkedPage:"",
    
    //queryPage:"",
    initPage:"",
    currentPage:"",
    areaid:"",
	pageid:"",
	page:{},
	initArea:function()
	{
		if(sitemap)
		{
			this.areaid= sitemap[this.language].homeareaid;
		}
		else
		{
			throw "Can't set the area. Check the file sitemap.js is not missing.";
		}
		return this.areaid;
	},
	initHomePage:function()
	{
		if(sitemap)
		{
			this.homePageId = sitemap[this.language].homepageid;
			this.pageid = this.homePageId;
			return this.pageid;
		}
		else
		{
			throw "Can't set the home page. Check the file sitemap.js is not missing.";
		}
	},
	/**
	 * This runs the first time you load the page.
	 */
	setup:function(language)
	{
		//alert("setting up history for language " + language);
		this.language =  language;
		//alert("history manager knows language is " + this.language);
		this.initArea();
		//alert("history manage knows areaid is " + this.areaid);
		this.initHomePage();
		//alert("pageid " + this.pageid);
		this.init();
	},
	getLanguage:function()
	{
		return YAHOO.util.History.getCurrentState("language");
	},
	updateState:function()
	{
		try
		{			
			this.language = YAHOO.util.History.getCurrentState("language");		
			this.areaid = YAHOO.util.History.getCurrentState("areaid");
			this.pageid = YAHOO.util.History.getCurrentState("pageid");
		}
		catch(e)
		{
			YAHOO.si.handleErrors(e);
		}		
	},
	onLanguageChange:function()
	{
		this.updateState();
		//this.controller.setLeftMenuLanguage();
	},
	onAreaChange:function()
	{
		try
		{
			this.updateState();
			this.controller.topNavBar.repaint();
			this.controller.updateLeftMenu();
		}
		catch(e)
		{
			e.contextMessage = "HistoryManager onAreaChange threw an error.";
			YAHOO.si.handleErrors(e);
		}
	},
	onPageChange:function()
	{
		this.updateState();
		//alert("page changed");
		this.page.reload(this.pageid);
		this.controller.breadcrumbs.render();
	},
	//register
	//void register ( module , initialState , onStateChange , obj , override ) 
	//Registers a new module. 
	//Parameters: 
	//module <string> Non-empty string uniquely identifying the module you wish to register. 
	//initialState <string> The initial state of the specified module corresponding to its earliest history entry. 
	//onStateChange <function> Callback called when the state of the specified module has changed. 
	//obj <object> An arbitrary object that will be passed as a parameter to the handler. 
	//override <boolean> If true, the obj passed in becomes the execution scope of the listener. 
	registerModules:function()
	{
		try
		{
		    YAHOO.util.History.register("language",	
		    		this.language,	
		    		this.onLanguageChange,
		    		this,
		    		true);	
		    YAHOO.util.History.register("areaid",	 	
		    		this.areaid,		
		    		this.onAreaChange,
		    		this,
		    		true);
		    YAHOO.util.History.register("pageid", 		
		    		this.pageid,		
		    		this.onPageChange,
		    		this,
		    		true);	
		}
		catch(e)
		{
			YAHOO.si.handleErrors(e);
		}
	},
	initState:function()
	{
		//alert("initState of history");
	    //bookmarkedLanguage = YAHOO.util.History.getBookmarkedState("language");
	    //bookmarkedAreaId = YAHOO.util.History.getBookmarkedState("areaid");
	    this.bookmarkedPageId = YAHOO.util.History.getBookmarkedState("pageid");
	    //I don't use the querystring at all.
	    //queryPage = YAHOO.util.History.getQueryStringParameter("pageid");
	    //If we arrive at the page from a url that points at a particular page
	    //we should open that instead
	    this.pageid = this.bookmarkedPageId ||  this.pageid;
	    //alert("initState " + this.pageid)
	    if(this.pageid!=YAHOO.si.Sitemap.pageid)
	    {
			//this.language = pagelookup[this.pageid].language;
			var url = pagelookup[this.pageid].url;
			this.areaid = urllookup[url].areaid;
			this.language = urllookup[url].language;
	    }
	    //alert("initState language " + this.language);
	},
	init:function()
	{
		this.initState();
		this.registerModules();
		YAHOO.util.History.onReady(function () {
			try
			{
				this.page = new YAHOO.si.Page();
				this.page.init(this.pageid);			
			}
			catch(e)
			{
				e.contextMessage = "The 'page.js' file may be missing.";
				YAHOO.si.handleErrors(e);
			}
		},this,true);
	    try {
	    	var field = YAHOO.util.Dom.inDocument("yui-history-field");
	    	var iframe = YAHOO.util.Dom.inDocument("yui-history-iframe");
	    	if(!field || !iframe)
	    	{
	    		throw new Error("Can't initialise the History Manager because " +
	    				"the yui-history-field or yui-history-iframe are missing.");
	    	}
	        YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
	    } 
	    catch (e) 
	    {
	    	YAHOO.si.handleErrors(e);
	    }
	},
	navigate:function(module,value)
	{
		YAHOO.util.History.navigate(module,value);
		return false;
	},	
	multiNavigate:function(state)
	{
		//if(window.location.hash!="")
		//{
			YAHOO.util.History.multiNavigate(state);		
		//}
		return false;
	},	
    toString:function()
    {
    	return "YAHOO.si.HistoryManager";
    }
    
};    

 
