/**
 * 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 b = navigator.appName;
	var ie = false;
	var o = this;
	var flashObj;
	var currentHash = window.location.hash;
	var lastReceivedState = window.location.hash;
	
	if(b == "Microsoft Internet Explorer"){
		ie = true;
	}else{
		ie = false;
	}
	
	this.setObject = function(obj){
		o.flashObj = obj;
	}
	o.changeState = function(newState){
		if(!ie){
			window.location.hash="/"+newState;
			o.lastReceivedState = newState;
		}
	}
	o.sendState = function(){
		if(!ie){
			hashValue = decodeURI(window.location.hash).substring(2);
			o.currentHash = hashValue;
			if (hashValue){
				document.getElementById(o.flashObj).receiveState(hashValue);
			}
		}
	}
	o.listenForURLChange = function(){
		if(!ie){
			// 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)
		if(!ie){
			o.sendState();
		}
	}
	
	if(!ie){
		setInterval(o.listenForURLChange, 50);
	}
}
DeepLink = new f6design.DeepLink();
