/**
 * DeepLink
 * (http://f6design.com/journal/2006/11/18/deeplink-flash-deep-linking/)
 *
 * By Jonathan Nicol (f6design.com)
 *
 * Inspired by the work of:
 * Theo Hultberg (http://blog.iconara.net/2006/06/21/bookmarkability-in-flash/) 
 * and Asual(http://www.asual.com/swfaddress/)
 *
 */
if(typeof f6design == "undefined") var f6design = new Object();
f6design.DeepLink = function() {
	var o = this;
	var flashObj;
	var currentHash = window.location.hash;
	var lastReceivedState = window.location.hash;
	this.setObject = function(obj){
		o.flashObj = obj;
	}
	o.changeState = function(newState){
		window.location.hash="bookmark="+newState;
		o.lastReceivedState = newState;
	}
	o.sendState = function(){
		hashValue = decodeURI(window.location.hash).substring(2);
		o.currentHash = hashValue;
		if (hashValue){
			document.getElementById(o.flashObj).receiveState(hashValue);
		}
	}
	o.listenForURLChange = function(){
		// if hash has changed due to user changing it, not flash
		if (window.location.hash != o.currentHash && decodeURI(window.location.hash).substring(2) != o.lastReceivedState){
			o.lastReceivedState = window.location.hash;
			// alert flash of change
			o.sendState();
		}
	}
	o.flashLoaded = function(){
		// send initial hash state to flash (in case initial URL contains deeplink)
		o.sendState();
	}
	setInterval(o.listenForURLChange, 50);
}
DeepLink = new f6design.DeepLink();
