/**
 * DeepLink
 * (http://f6design.com/journal/2006/11/18/deeplink-flash-deep-linking/)
 *
 * By Jonathan Nicol (f6design.com)
 * Edited by Aleksandar Bogdanovic
 *
 * 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="/"+newState;
		o.lastReceivedState = "#/" + newState;
	}
	o.sendState = function(){
		hashValue = decodeURI(window.location.hash).substring(2);
		o.currentHash = hashValue;
		if (hashValue){
			var c_flashObj = document.getElementById(o.flashObj);
			if (c_flashObj == null) c_flashObj = document.getElementsByName(o.flashObj)[0];
			//alert("send state, flashID: " + o.flashObj + "; object: " + c_flashObj);
			c_flashObj.receiveState(hashValue);
		}
	}
	o.listenForURLChange = function(){
		// if hash has changed due to user changing it, not flash
		if (window.location.hash != o.currentHash && window.location.hash != 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();
		//alert('flash loaded');
		setInterval(o.listenForURLChange, 50);
	}
	//setInterval(o.listenForURLChange, 50);
}
DeepLink = new f6design.DeepLink();

