/*
The following code is used to handle hash inconsistencies.
Hashes can be inconsistent if a user comes to a page from an external link or manually modifies it.
If hash changes, load the appropriate page....
*/

function changeTitle(title) {
	document.title = title;
}

previousHash = "";
expectedHash = "";

function handleHistory() {
	
	docTitle = 'Disney Pirates of the Caribbean Online MMO game';
	if(document.title != docTitle) {
		changeTitle('Disney Pirates of the Caribbean Online');
	}
	
	//if (window.location.hash == "") return;
	if ((window.location.hash == "" || window.location.hash == "#1") && (getURLParams()["pageId"] == undefined || getURLParams()["pageId"] == "")) {
	//if (window.location.hash == ""){
		window.location.hash="#/community/community-home.html"
	}
	
	if ( window.location.hash != expectedHash ) {
		
		previousHash = expectedHash;
		expectedHash = window.location.hash;
		
		//alert(previousHash)
		//alert(expectedHash)
		
		if(expectedHash.length > 0 && expectedHash.substring(1,2) == "/") {
			
			var iHash = expectedHash.replace(/^#(.*?\.html).*/, "$1");
			var iMode = expectedHash.replace(/^#(.*?)(.mode=(.*))+$/, "$3");

			if (iMode.indexOf('&') > 0)
				iMode = iMode.substr(0, iMode.indexOf('&'));

			if(expectedHash.match(/mode=/)) {
				if(iMode == "pg_init") {
					loadContent(iHash, 'dynamicContent', iMode);
				} else if(iMode == "pgByUrl") {
					loadPGbyURL(iHash, 'pg_maincontent', iMode);
				} else if(iMode == "pgSearch") {
					if(window.ZoomSearch) {
						iSearchQuery = expectedHash.replace(/^.*?(.zoom.query=.*?).mode=.*/, "$1");
						//iSearchUrl = expectedHash.replace(/^#(.*?).mode=.*/, "$1");
						iSearchUrl = expectedHash.replace(/^#(.*?).zoom.query=.*/, "$1");
						loadContent(iSearchUrl, 'dynamicContent');
						ZoomSearch(iSearchQuery);
					}
				} else {
					loadContent(iHash, iMode);
				}
			} else {
				loadContent(iHash, 'dynamicContent');
			}
			
			
			//loadContent(expectedHash.substring(1), 'dynamicContent');
			//handle content loading here....
			
			
			
			
			
			
		}
		
	}
}

function loadPage(hash) {
		
		var iHash = hash.replace(/^#(.*?\.html).*/, "$1");
		var iMode = hash.replace(/^#(.*?)(.mode=(.*))+$/, "$3");
		
		if (iMode.indexOf('&') > 0)
			iMode = iMode.substr(0, iMode.indexOf('&'));

		if(hash.match(/mode=/)) {
			if(iMode == "pg_init") {
				loadContent(iHash, 'dynamicContent', iMode);
			} else if(iMode == "pgByUrl") {
				loadPGbyURL(iHash, 'dynamicContent', iMode);
			} else if(iMode == "pgSearch") {
				iSearchQuery = hash.replace(/^.*?(.zoom.query=.*?).mode=.*/, "$1");
				//iSearchUrl = hash.replace(/^#(.*?).mode=.*/, "$1");
				iSearchUrl = hash.replace(/^#(.*?).zoom.query=.*/, "$1");
				loadContent(iSearchUrl, 'dynamicContent');
				ZoomSearch(iSearchQuery);
			} else {
				loadContent(iHash, iMode);
			}
		} else {
			loadContent(iHash, 'dynamicContent');
		}
		
}

function backbutton() {
	pollHash();
}

function pollHash() {
	
	handleHistory();
	window.setInterval("handleHistory()", 100);
	//priti
	return true;
	
}





	/*
		Timer by Algorithm
		http://www.codingforums.com/archive/index.php/t-10531.html
	*/
	function Timer(){
		this.obj = (arguments.length)?arguments[0]:window;
		return this;
	}
	
	// The set functions should be called with:
	// - The name of the object method (as a string) (required)
	// - The millisecond delay (required)
	// - Any number of extra arguments, which will all be
	// passed to the method when it is evaluated.
	
	Timer.prototype.setInterval = function(func, msec){
		var i = Timer.getNew();
		var t = Timer.buildCall(this.obj, i, arguments);
		Timer.set[i].timer = window.setInterval(t,msec);
		return i;
	}
	Timer.prototype.setTimeout = function(func, msec){
		var i = Timer.getNew();
		Timer.buildCall(this.obj, i, arguments);
		Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
		return i;
	}
	
	// The clear functions should be called with
	// the return value from the equivalent set function.
	Timer.prototype.clearInterval = function(i){
		if(!Timer.set[i]) return;
		window.clearInterval(Timer.set[i].timer);
		Timer.set[i] = null;
	}
	Timer.prototype.clearTimeout = function(i){
		if(!Timer.set[i]) return;
		window.clearTimeout(Timer.set[i].timer);
		Timer.set[i] = null;
	}
	
	// Private data
	Timer.set = new Array();
	Timer.buildCall = function(obj, i, args){
		var t = "";
		Timer.set[i] = new Array();
		if(obj != window){
			Timer.set[i].obj = obj;
			t = "Timer.set["+i+"].obj.";
		}
		t += args[0]+"(";
		if(args.length > 2){
			Timer.set[i][0] = args[2];
			t += "Timer.set["+i+"][0]";
			for(var j=1; (j+2)<args.length; j++){
				Timer.set[i][j] = args[j+2];
				t += ", Timer.set["+i+"]["+j+"]";
			}
		}
		t += ");";
		Timer.set[i].call = t;
		return t;
	}
	Timer.callOnce = function(i){
		if(!Timer.set[i]) return;
		eval(Timer.set[i].call);
		Timer.set[i] = null;
	}
	Timer.getNew = function(){
		var i = 0;
		while(Timer.set[i]) i++;
		return i;
	}

    <!--
	/* CROSS-BROWSER EVENT HANDLER */
		function addEvent(obj, evType, fn){
			 if (obj.addEventListener){
				 obj.addEventListener(evType, fn, true);
				 return true;
			 } else if (obj.attachEvent){
				 var r = obj.attachEvent("on"+evType, fn);
				return r;
			 } else {
				return false;
			 }
		}
	/* END EVENT HANDLER */
	
	
	function doGetPage(i) {
		/*
		var iHash = i.replace(/^(.*?)(.container=.*)+$/, "$1");
		var iContainer = i.replace(/^(.*?)(.container=(.*))+$/, "$3");
		
		if(i.match(/container=/)) {
			loadContent(iHash, iContainer);
		} else {
			loadContent(iHash, 'dynamicContent');
		}
		*/
		/*
		switch(i) {
			case "/test/test.html":
				setContent("it works!!!");
				break;
			case "2":
				setContent("test2");
				break;
			case "3":
				setContent("test3");
				break;
			default:
				setContent("test");
				break;
		}
		*/
	}
		
		
	/* PAGELOCATOR */
		function PageLocator(propertyToUse, dividingCharacter) {
			this.propertyToUse = propertyToUse;
			this.defaultQS = 1;
			this.dividingCharacter = dividingCharacter;
		}
		PageLocator.prototype.getLocation = function() {
			return eval(this.propertyToUse);
		}
		PageLocator.prototype.getHash = function() {
			var url = this.getLocation();
			if(url.indexOf(this.dividingCharacter)>-1) {
				var url_elements = url.split(this.dividingCharacter);
				return url_elements[url_elements.length-1];
			} else {
				return this.defaultQS;
			}
		}
		PageLocator.prototype.getHref = function() {
			var url = this.getLocation();
			var url_elements = url.split(this.dividingCharacter)
			return url_elements[0];
		}
		PageLocator.prototype.makeNewLocation = function(new_qs) {
			return this.getHref() + this.dividingCharacter + new_qs;
		}
	/* END PAGELOCATOR */
	
	
	/* AjaxIframesFixer */
		function AjaxIframesFixer(iframeid) {
			this.iframeid = iframeid;
			if (document.getElementById('ajaxnav')) {
				this.fixLinks();
				
				this.locator = new PageLocator("document.frames['"+this.iframeid+"'].getLocation()", "?hash=");
				this.windowlocator = new PageLocator("window.location.href", "#");
				this.timer = new Timer(this);
				
				this.delayInit(); // required or IE doesn't fire
			}
		}
		AjaxIframesFixer.prototype.fixLinks = function (iframeid) {
			var links = document.getElementsByTagName("A");
			for(var i=0; i<links.length; i++) {
				var href = links[i].getAttribute("href");
				
				if(href.indexOf("hash=") > 0) {
					var hash = href.substr(href.indexOf("hash=")+5);
					links[i].setAttribute("href","javascript:document.getElementById('"+this.iframeid+"').setAttribute('src', '/pirates/v3/global/backbutton?hash="+hash+"');");
				}
				
			}
		}
		AjaxIframesFixer.prototype.delayInit = function(){
			this.timer.setTimeout("checkBookmark", 100, "");
		}
		AjaxIframesFixer.prototype.checkBookmark = function(){
			//window.location = this.windowlocator.makeNewLocation(this.locator.getHash()); // commented this out to fix i.e. bug
			this.checkWhetherChanged(0);
		}
		AjaxIframesFixer.prototype.checkWhetherChanged = function(location){
			if(this.locator.getHash() != location && location != 0) { // added location != 0 to fix i.e. bug
				//doGetPage(this.locator.getHash());
				window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
			}
			this.timer.setTimeout("checkWhetherChanged", 200, this.locator.getHash());
		}
	/* END AjaxIframesFixer */
	
	
	/* AjaxUrlFixer */
		function AjaxUrlFixer() {
			this.fixLinks();
			
			this.locator = new PageLocator("window.location.href", "#");
			this.timer = new Timer(this);
			this.checkWhetherChanged(0);
		}
		AjaxUrlFixer.prototype.fixLinks = function () {
			var links = document.getElementsByTagName("A");
			for(var i=0; i<links.length; i++) {
				var href = links[i].getAttribute("href");
				if(href.indexOf("hash=") > 0) {
					var hash = href.substr(href.indexOf("hash=")+5);
					links[i].setAttribute("href","#"+hash);
				}
			}
		}
		AjaxUrlFixer.prototype.checkWhetherChanged = function(location){
			if(this.locator.getHash() != location) {
				doGetPage(this.locator.getHash());
			}
			this.timer.setTimeout("checkWhetherChanged", 200, this.locator.getHash());
		}
	/* END AjaxUrlFixer */
	
	function setContent(new_content) {
		if(!document.getElementById || !document.getElementsByTagName) return;
		var container = document.getElementById("container");
		container.innerHTML = new_content;
	}
	function FixBackAndBookmarking() {
		if(!document.getElementById || !document.getElementsByTagName) return;
		if(document.iframesfix) {
			fix = new AjaxIframesFixer('ajaxnav');
		} else {
			fix = new AjaxUrlFixer();
		}
	}
	
	var detect = navigator.userAgent.toLowerCase();
	if(detect.indexOf("msie")>-1) document.iframesfix = true;
	addEvent(window, "load", FixBackAndBookmarking);
	
    -->


