/*

	dojo's browser.undo stripped down and re-written by thruflo to depend on prototype & utils.js
	
	a)	we change the src of an iframe when we change location via AJAX
	b)	then listen for changes to the iframe src that are caused by the back & forward buttons
	c)	when we hear one, we tell the AJAX app to update its view

	
*/
var ChasteHistory = Class.create();
ChasteHistory.prototype = {
	// @@ edit as appropriate
	'config': {
		'iframe_container': 'history_container',
		'iframe_id': 'history-iframe',
		'iframe_path': 'http://www.myartinfo.com/static/html/history.htm'
	},
	// this should call one or more external methods, which update the page / view appropriately
	// @@ edit as appropriate - *meant to be app specific*
	'notify_observers': function (obj) { 
		hextra_load(obj['slot'], null, null, obj['url']);
		/*
		//alert($H(obj).keys());
		if ($H(obj).keys().include('url')) {
			//alert(obj['url']);
			window.location.href = obj['url'];
			// hextra_load(obj['slot'], null, null, obj['url']);
		}
		else {
			//alert(obj['target'].readAttribute('href'));
			hextra_load.bind(obj['target'])(obj['slot']);
		}
		*/
	},
	'set_history': function (obj) {
		var timestamp = get_timestamp();
		// flag to ignore the iframe src change - seeings how we're deliberately changing it
		this['ignore_location_change'] = true;
		// update the src of the iframe with the new timestamp
		set_iframe_src(
			window.frames[this.config.iframe_id], 
			this['config']['iframe_path'] + '?' + timestamp, 
			false
		);
		// make a note of the timestamp and corresponding object (the specifics of which chaste cares nothing about)
		this['history_stack'].push( {
				'timestamp': timestamp, 
				'obj': obj
			}
		);
	},
	'handle_history_frame_location_change': function (evt, history_frame_location) {
		// alert('ChasteHistory.handle_history_frame_location_change(' + history_frame_location + ')');
		if (!Prototype.Browser.Opera) {
			if (this['ignore_location_change']) {
				// alert('ignore!');
				this['ignore_location_change'] = false;
				return;
			}
			var query = parse_url(history_frame_location.href)['query'];
			if (query == '' || query == null) {
				// alert('no query');
				return;
			}
			else {
				// alert('we have a query');
				var len = this['history_stack'].length;
				if (len >= 2 && query == this['history_stack'][len - 2]['timestamp']) {
					this['handle_back_button']();
				}
				else {
					len = this['forward_stack'].length;
					if (len > 0 && query == this['forward_stack'][len - 1]['timestamp']) {
						this.handle_forward_button();
					}
				}
			}
		}
	},
	'handle_back_button': function () {
		var current = this['history_stack'].pop();
		if (!current) {
			return;
		}
		var target_slot = current['obj']['slot'];
		var last_state = this['history_stack'].reverse().find(
			function (item) {
				if (item['obj']['slot'] == target_slot) {
					return true;
				}
				return false;
			}
		);
		this['history_stack'].reverse();
		if (!last_state) {
			this['notify_observers']( {
					'slot': target_slot,
					'url': this['base_url'].sub('browse', 'z_scripts') + 'mt'
				}
			);
		}
		else {
			this['notify_observers'](last_state['obj']);
		}
		// this['notify_observers'](this['history_stack'].last()['obj']);
		this['forward_stack'].push(current);
	},
	'handle_forward_button': function () {
		// alert('handle fwd btn');
		var next = this['forward_stack'].pop();
		if (!next) {
			return;
		}
		this['notify_observers'](next.obj);
		this['history_stack'].push(next);
	},
	'initialize': function () {
		if ((typeof Prototype == 'undefined') || parseFloat(Prototype['Version'].split(".")[0] + "." + Prototype['Version'].split(".")[1]) < 1.5) {
			throw('chaste requires the Prototype JavaScript framework >= 1.5.0');
		}
		if (typeof Utils == 'undefined') {
			throw('chaste requires utils.js');
		}
		this['history_stack'] = new Array();
		this['forward_stack'] = new Array();
		this['ignore_location_change'] = false;
		if (!$(this['config']['iframe_container'])) {
			new Insertion.Bottom($$('body').first(), '<div id="' + this['config']['iframe_container'] + '"></div>');
		}
		new Insertion.Bottom(
			this['config']['iframe_container'], 
			'<iframe width="1px" \
				name="' + this['config']['iframe_id'] + '" \
				id="' + this['config']['iframe_id'] + '" \
				src="' + this['config']['iframe_path'] + '"> \
			</iframe>'
		);
		this.set_history( {
				'url': this['base_url'] + this['initial_history']['section'],
				'slot': this['initial_history']['slot']
			}
		);
	}
};
/*
Event.observe(
	window,
	'load',
	function () {
		window['chaste_history_controller'] = new ChasteHistory();
	}
);
*/