/**
* simple deeplinker v 0.1 
*
* @autor Andriy.Oblivantsev@gmail.com
*/
document.visLinker = new function()
{
	this.intervalTime 	= 350; // update each intervalTime
	this.hash 			= "";  // current deeplink value

	var $this 			= this;
	var timer 			= null;
	var lastHash 		= null;
	var listeners 		= [];

	this.addEvent = function(listenerFunction){
		listeners.push(listenerFunction);
	}

	this.removeEvent = function(listenerFunction){
		for(var i in listeners){
			if(listeners[i] == listenerFunction){
				listeners.splice(i, 1);
				break;
			}
		}
	}
	
	this.detect = function()
	{
		var f = location.href.match(/#(.+?)$/i);
		f = f == null?"":f[1];
		if(lastHash != f){ // if hash changed
			lastHash = f;
			$this.hash = f;
			for(var i in listeners){
				listeners[i]($this.hash);
			}
		}
	}
	
	this.startUpdating = function(){
		if(timer != null)
			$this.stopUpdating();
		timer = window.setInterval($this.detect, $this.intervalTime);
	}

	this.stopUpdating = function(){
		if(timer != null)
			window.clearInterval(timer);
		
		timer = null;
	}
};
