// @name: WindowLocationHash
// @type: Behaviour
// @author: Rocco Howard
// @version: 20080428

Bluora.WindowLocationHash = {
	initiated: false,
	hashObject: new Hash(),
	previousHash: '',
	currentHash: '',
	changeHash: function(HashType,NewHash) {
		if (Prototype.Browser.IE) {
			//$('WindowLocationHash').src = WEBROOT+'hash.php?hash='+newHash;
		} else {
			var hash_text = '';
			Bluora.WindowLocationHash.hashObject.set(HashType,NewHash);
			var pair_count = 0;
			var pair_length = Bluora.WindowLocationHash.hashObject.keys().length;
			Bluora.WindowLocationHash.hashObject.each(function(pair) {
				hash_text += pair.key+':'+pair.value;
				if (pair_count < pair_length-1) {
					hash_text += ';';
				}
				pair_count++;
			});
			location.hash = hash_text;
		}
	},
	checkHash: function() {
		if (Prototype.Browser.IE) {
			//if (Bluora.WindowLocationHash.currentHash != $('WindowLocationHash').document.body.innerHTML) {
			//	Bluora.WindowLocationHash.previousHash = Bluora.WindowLocationHash.currentHash
			//	Bluora.WindowLocationHash.currentHash = ('WindowLocationHash').document.body.innerHTML
			//	window.onhashchangeevent();
			//}
		} else {
			if (Bluora.WindowLocationHash.currentHash != window.location.hash.replace('#','')) {
				Bluora.WindowLocationHash.previousHash = Bluora.WindowLocationHash.currentHash
				Bluora.WindowLocationHash.currentHash = window.location.hash.replace('#','');
				var tmp = Bluora.WindowLocationHash.currentHash.replace('#','').split(';');
				Bluora.WindowLocationHash.hashObject = new Hash();
				tmp.each(
					function(section) {
						if (section) {
							var tmp1 = section.split(':');
							Bluora.WindowLocationHash.hashObject.set(tmp1[0],tmp1[1]);
						}
					}
				);
				window.onhashchangeevent();
			}
		}
	},
	getHash: function(HashType) {
		
		Bluora.log(window.location.hash.replace('#',''));
		Bluora.WindowLocationHash.currentHash = window.location.hash.replace('#','');
		var tmp = Bluora.WindowLocationHash.currentHash.replace('#','').split(';');
		Bluora.WindowLocationHash.hashObject = new Hash();
		tmp.each(
			function(section) {
				if (section) {
					var tmp1 = section.split(':');
					Bluora.WindowLocationHash.hashObject.set(tmp1[0],tmp1[1]);
				}
			}
		);
		return Bluora.WindowLocationHash.hashObject.get(HashType);
	},
	replaceLocation: function(newHash) {
		window.location.replace(window.location.protocol+'//'+window.location.hostname+window.location.pathname+window.location.search+'#'+newHash);
	},
	addOnHashChange: function(func){var onhashchangeevent=window.onhashchangeevent;if(typeof(window.onhashchangeevent)!='function'){window.onhashchangeevent=func;}else{window.onhashchangeevent=function(){if(onhashchangeevent){onhashchangeevent();}func();}}}
}

if (Bluora.WindowLocationHash.initiated == false) {
	Bluora.WindowLocationHash.initiated = true;
	Bluora.WindowLocationHash.currentHash = window.location.hash;
	if (!window.onhashchangeevent) {
		window.onhashchangeevent = function(){};
	}
	if (Bluora.isIE) {
		var scriptElem = document.createElement('iframe');
		scriptElem.setAttribute('src',WEBROOT+'hash.php');
		scriptElem.setAttribute('id','WindowLocationHash');
		//document.getElementsByTagName('body')[0].appendChild(scriptElem);
	}
	setInterval(Bluora.WindowLocationHash.checkHash,500);
}