/**
 * Class built to manage the back & forward button presses in an Ajax orientated site.
 * @author Keith Foster
 * @version 0.1
 * @required YUI
 *  
 */
function HashListener() {
	var Public = this;
	var previous_hash;
	var on_entry_page = true;
	/*
	 * initialising the class
	 */
	function __construct(landing_page){
		// set the hash for the landing page.
		//if ( !location.hash || location.hash == '#') location.hash = 'home';		
		previous_hash = location.hash;
	}
	function __makeIFrame(){
		var i_frame = document.createElement('iframe');
		i_frame.id = 'blank-frame';
		i_frame.name = 'blank-frame';
		document.body.appendChild(i_frame);
		
		return i_frame;
	}
	
	Public.get_hash = function(){
		return (location.href.indexOf(location.search) > location.href.indexOf(location.hash) && location.hash.indexOf(location.search) < 0)? 
			location.hash + location.search : location.hash;
	};
	Public.set_hash = function(hash){
		location.hash = hash;
		previous_hash = hash;

		if (YAHOO.env.ua.ie != 0) {
			window.frames['blank-frame'].document.title = '';
			window.frames['blank-frame'].document.location = '/blank.html?'+hash;
		}

		document.title = hash.replace(/-/g, ' ').trim('/').replace(/\//g, ' » ');
		
		return Public;
	};
	Public.handle_hash_change = function(){
		var hash = Public.get_hash().slice(1);
		Public.set_hash(hash);
		var index = hash!='' ? worldquant.get_section_index(hash) : 0;
		
		if(index==0) {
			worldquant.load_home();
		} else {
			worldquant.load_section(index, previous_hash, false);
		}
		
		on_entry_page = false;
	};
	Public.define_interval = function() {
		Public.interval = setInterval(function() {
			var current_hash = Public.get_hash();
			current_hash = current_hash.length > 1 ? current_hash : current_hash.slice(1)
			
			if (previous_hash === current_hash && ( previous_hash === '' || !on_entry_page)) return;
			
			if (YAHOO.env.ua.ie != 0)
			{
				window.frames['blank-frame'].document.title = current_hash;
				window.frames['blank-frame'].document.location = '/blank.html?'+current_hash.slice(1);
			}
			
			Public.handle_hash_change();
			previous_hash = current_hash;
			initial = false;
		}, 50);	
	}
	
	// when the hash changes we need to trigger a load of the worldquant.load_section with the new hash value.
	Public.initialise = function(trigger) {
		if (YAHOO.env.ua.ie != 0) {
			__makeIFrame().setAttribute('src', '/blank.html?'+previous_hash.slice(1));
		}
		Public.define_interval();
		return Public;
	}
	__construct.apply(this, arguments);
}