// namespace:
var AARF_PDX = AARF_PDX || {};

(function()
{
	// grab the utils namespace:
	AARF_PDX.UTILS = AARF_PDX.UTILS || {};



/*
* { DeepLinkManager } js class to manage deep linking functionality:
* @return : null
* instantiate this manager class like:  var dl = new AARF_PDX.UTILS.DeepLinkManager();
*/
AARF_PDX.UTILS.DeepLinkManager = function (){
	this.lastHash = "";	// page we are expecting to move to
	this.movieName = "flashFile";
	this.currentPage = null;	// check against expected page to make sure we aren't already there
	this.locationTimerID = null;
	this.siteTitle = document.title || "[TITLE]";
	// ie history var:
	this.ieHistory;
	
	
/****************************************************************************
* 	{ getPageID } desc
*	@return : desc
****************************************************************************/
	this.getPageID = function (){
		// return the pageID located after the hash.  If no values after hash, return "home"
		return window.location.hash.split("#")[1] || "#";
	}	
	
	
	
/****************************************************************************
* 	{ setPageTitle } desc
*	@return : desc
****************************************************************************/
	this.setPageTitle = function (strNewTitle){
		// set page title to strNewTitle value
		document.title =  AARF_PDX.UTILS.capitalizeAllWords(strNewTitle) + " - " + this.siteTitle;
	}	
	
	
	
	
/****************************************************************************
*	{ SetValue } called from Flash Movie
*	@return : return bool indicating whether new value was set
****************************************************************************/

	this.SetValue = function(o){
		var m = window.location.hash;
		//alert("SetValue _"+o+" "+m.replace("#","")+" "+this.lastHash+" "+this.currentPage);
		/* do whatever you need to do to get this into the browser history here */
		
		// check to see if the current page is the requested page, if so do nothing
		//if(o == this.currentPage) return;
		
		
		// check if this.lastHash is null - if so this is the first visit to the page (hopefully).
		//if (this.lastHash != o)
		//{
			if(o == "home")
			{
				this.lastHash = this.currentPage = "#";
				window.location.hash = "#";
				
				/*@cc_on
				this.updateIEHistory("#");
				@*/
			}
			else if(o == "#")
			{
				this.lastHash = o;
				this.currentPage = o;
				/*@cc_on
				//this.updateIEHistory(o);
				@*/
			}
			else
			{
				window.location.hash = o;
  				this.lastHash = o;
				this.currentPage = o;
				/*@cc_on
				this.updateIEHistory(o);
				@*/
			}
			
			
			// set new title value:
			this.setPageTitle(o);
			/* after that, call the 'UrlOnChange' function in the flash movie */
			this.thisMovie(this.movieName).UrlOnChange(o);
			return true;
		//}
		
		
		
		
		// did not set a new value, return false:
		return false;
	}		// END SetValue()
	
/****************************************************************************
* 	{ GetValue } called from flash movie.
* 	@return : return url/deep link value to flash movie
****************************************************************************/
	this.GetValue = function (){
	// check the location hash for page name / section name		
		if(this.currentPage)
		{
			//return this.currentPage;
			var hash = window.location.hash || "home";
			return this.currentPage = hash.replace("#","");
		}
		else 
		{
			var hash = window.location.hash || "home";
			
			return this.currentPage = hash.replace("#","");
		}
			
		
		// return this.currentPage
		
	}
/****************************************************************************
* 	{ thisMovie } called from script to target the flash movie being linked to
*	@return : name of flash movie object in the HTML DOM
****************************************************************************/
	// 
	this.thisMovie = function (movieName) {
    	if (navigator.appName.indexOf("Microsoft") != -1) {
        	return window[movieName]
    	}
    	else {
        	return document[movieName]
    	}
	}

/****************************************************************************
* 	{ checkLoc } desc
*	@return : desc
****************************************************************************/
	this.checkLoc = function (flag){
		
		// let us clear the checkLoc interval if needed call: checkLock(0)
		if(flag == false)
		{
			window.clearInterval(this.locationTimerID);
			this.locationTimerID = null;
			return false;
		}
		
		// check the loc, if the URL shown does not match current location... update!
		// if the URL changes, getPageID will be different then the value stored in this.currentPage:
		
		var curState = this.getPageID();
		if(curState != this.currentPage )
		{
			if(this.currentPage == "home" && curState == "#") return;
			// change app state:
			this.SetValue(curState);
		}
		
		
		
	}



/****************************************************************************
* 	{ init } call from flash file to start URL polling for back/forward buttons
*	@return : desc
****************************************************************************/
	this.init = function(){
		// start polling the URL
		this.locationTimerID = window.setInterval("AARF_PDX.DEEPLINK_MANAGER.checkLoc()",200);
		/*@cc_on
			
     	 		//alert("IE Browser ");
				this.ieHistory = document.createElement("iframe");
				this.ieHistory.setAttribute("id","AARF_PDX-deeplink-ieHistory");
				this.ieHistory.setAttribute("marginheight","0");
				this.ieHistory.setAttribute("scrolling","no");
				this.ieHistory.setAttribute("align","top");
				this.ieHistory.style.height="80px";
				this.ieHistory.style.width ="100px";
				this.ieHistory.style.position = "absolute";
				this.ieHistory.style.top = "100px";
				this.ieHistory.style.left = "980px";
				this.ieHistory.style.display = "none";
				document.body.appendChild(this.ieHistory);
		   		this.updateIEHistory(this.getPageID(),false);
				window.clearInterval(this.locationTimerID);
		@*/
		return true;
	}
	
/****************************************************************************
* 	{ updateIEHistory } call from js file to trigger history entries for IE
*	@return : null
****************************************************************************/

	this.updateIEHistory = function(pageID,bFirstVisit)
	{
		/*@cc_on 	
		//	ie trickery here:  
		//	when init is called from flash within IE an IFRAME is added to the page
		//	we need to write our history values to that iframe for IE to register
		//	the history entries and enable back/forward functionality:
			
		// make sure we have our history Iframe before doing anything with it:
		if(AARF_PDX.UTILS.trim(pageID) == "#" || AARF_PDX.UTILS.trim(pageID) == "")
		{
			pageID = "home";
		}
		
		var ifr = this.ieHistory.contentWindow.document;	
		if(ifr)
		{
			ifr.open();
			ifr.write('<script>parent.document.title = "' + AARF_PDX.UTILS.capitalizeAllWords(pageID) + '"; parent.document.location.hash = "' + pageID + '";parent.AARF_PDX.DEEPLINK_MANAGER.thisMovie(parent.AARF_PDX.DEEPLINK_MANAGER.movieName).UrlOnChange("'+pageID+'");</script>');
			ifr.write('pageID >> ' + pageID );
			ifr.close();
			
		}
		@*/		  
				
	}





	
// end DeepLinkManager constructor
}
AARF_PDX.DEEPLINK_MANAGER = new AARF_PDX.UTILS.DeepLinkManager();
})();



