﻿//// Create namespaces
if (!Creative)
	var Creative = { };
if (!Creative.Waad)
	Creative.Waad = { };
if (!Creative.Waad.Navigation)
	Creative.Waad.Navigation = { };


/**
 * Allows the programmer to perform navigation based on changes in the hash value of the url.
 **/
Creative.Waad.Navigation.HashNavigation = {
	/**
	 * Starts the polling of hash changes. 
	 *
	 * @param interval		the number of milliseconds between two checks (defaults to 75 if omitted).
	 *  
	 **/
	Start : function(interval) {
		if (typeof(interval) == "number")
			Creative.Waad.Navigation.HashNavigation.privateInterval = interval;
			
		if (typeof document.all !== "undefined" && !Creative.Waad.Navigation.HashNavigation.privateIframe) {
			if (typeof document.createElementNS != 'undefined')
				Creative.Waad.Navigation.HashNavigation.privateIframe = document.createElementNS('http://www.w3.org/1999/xhtml', 'iframe');
			else
				Creative.Waad.Navigation.HashNavigation.privateIframe = document.createElement('iframe');
			document.forms[0].appendChild(Creative.Waad.Navigation.HashNavigation.privateIframe);
			Creative.Waad.Navigation.HashNavigation.privateIframe.style.display = 'none';
			Creative.Waad.Navigation.HashNavigation.privateIframe.src = 'CreativeWaadHashNavigation.htm?DefaultCreativeWaadHashNavigationValue';
		}
			
		Creative.Waad.Navigation.HashNavigation.privateIntervalIdentifier = window.setInterval('Creative.Waad.Navigation.HashNavigation.privateCheckAddresses()', Creative.Waad.Navigation.HashNavigation.privateInterval);
	},
	
	/**
	 * Stops the polling of hash changes. 
	 **/
	Stop : function() {
		if (Creative.Waad.Navigation.HashNavigation.privateIntervalIdentifier) {
			window.clearInterval(Creative.Waad.Navigation.HashNavigation.privateIntervalIdentifier);
		}
	},
	
	/**
	 * Adds an event handler to the HashNavigation module.
	 *	This function is called whenever the current hash location changes. The function call gets one 
	 *	argument, an object with two properties:
	 *	- Previous: The previous hash value
	 *	- New: The current (new) hash value
	 **/
	AddListener : function(listener) {
		if (typeof(listener) == "function") {
			Creative.Waad.Events.AddEvent(Creative.Waad.Navigation.HashNavigation, 'locationchanged', listener);
		}
	},
	
	privateCheckAddresses : function() {
		var tCurrentHash = Creative.Waad.Navigation.HashNavigation.privateGetCurrentHash();
		var tLastKnown = Creative.Waad.Navigation.HashNavigation.privateLastKnownHash;
		if (Creative.Waad.Navigation.HashNavigation.privateIframe) {
			// IE
			var tCurrentIframeHash = Creative.Waad.Navigation.HashNavigation.privateGetCurrentIframeHash();
			var tLastKnownIframeHash = Creative.Waad.Navigation.HashNavigation.privateLastKnownIframeAddress;
			if (tCurrentIframeHash == "DefaultCreativeWaadHashNavigationValue") {
				// The Iframe has the default value => The url in the address bar is accurate
				if (tCurrentIframeHash != tCurrentHash) {
					// Iframe differs from the address bar hash, update the iframe
					Creative.Waad.Navigation.HashNavigation.privateIframe.src = "CreativeWaadHashNavigation.htm?" + tCurrentHash;
					Creative.Waad.Navigation.HashNavigation.privateLastKnownIframeAddress = tCurrentHash;
					Creative.Waad.Navigation.HashNavigation.privateLastKnownHash = tCurrentHash;
				}
				if (tLastKnown != tCurrentHash)
					Creative.Waad.Navigation.HashNavigation.privateRaiseEvent(tLastKnown, tCurrentHash);
			} else if (tCurrentIframeHash == tLastKnownIframeHash) {
				// The Iframe has the same value it had before => The url in the address bar is accurate
				if (tLastKnown != tCurrentHash) {
					Creative.Waad.Navigation.HashNavigation.privateIframe.src = "CreativeWaadHashNavigation.htm?" + tCurrentHash;
					Creative.Waad.Navigation.HashNavigation.privateLastKnownIframeAddress = tCurrentHash;
					Creative.Waad.Navigation.HashNavigation.privateLastKnownHash = tCurrentHash;
					Creative.Waad.Navigation.HashNavigation.privateRaiseEvent(tLastKnown, tCurrentHash);
				}
			} else if (tCurrentIframeHash != tLastKnownIframeHash) {
				// The Iframe value has changed => The IFrame is correct, the url in the address bar is wrong
				Creative.Waad.Navigation.HashNavigation.privateLastKnownIframeAddress = tCurrentIframeHash;
				Creative.Waad.Navigation.HashNavigation.privateLastKnownHash = tCurrentIframeHash;
				window.location.hash = tCurrentIframeHash;
				Creative.Waad.Navigation.HashNavigation.privateRaiseEvent(tLastKnownIframeHash, tCurrentIframeHash);
			}
		} else {
			// Not IE
			if (tCurrentHash != tLastKnown) {
				Creative.Waad.Navigation.HashNavigation.privateLastKnownHash = tCurrentHash;
				Creative.Waad.Navigation.HashNavigation.privateRaiseEvent(tLastKnown, tCurrentHash);
			}
		}
	},
	
	privateRaiseEvent : function(previousHash, newHash) {
		Creative.Waad.Events.FireEvent(Creative.Waad.Navigation.HashNavigation, 'locationchanged', { Previous : previousHash, New : newHash });
	},
	privateIntervalIdentifier : null,
	privateInterval : 75,
	privateLastKnownHash : "",
	privateLastKnownIframeAddress : "",
	privateIframe : null,
	privateGetCurrentHash : function() {
		var r = window.location.href;
		var i = r.indexOf("#");
		return (i >= 0 ? r.substr(i+1) : "");
	},
	privateGetCurrentIframeHash : function() {
		if (!Creative.Waad.Navigation.HashNavigation.privateIframe)
			return "";
		var doc = Creative.Waad.Navigation.HashNavigation.privateIframe.contentWindow.document;
		var hash = String(doc.location.search);
		if (hash.length == 1 && hash.charAt(0) == "?") {
			hash = "";
		}
		else if (hash.length >= 2 && hash.charAt(0) == "?") {
			hash = hash.substring(1);
		}
		return hash;
	}
}
