/** 
project:Ourbrander.DeepLink 
version 1.1
update:2010/10/29
document:http://www.ourbrander.com/p/deeplink
autor:liuyi
*/

function deeplink(target_url){

	var targetId;
	var currentHash;
	var updateObj=setInterval
	var duration=90;

	
	if(target_url!=null){
		
		var str=String(target_url)
		if(str.indexOf('title:')==0){
			setTitle(str.replace("title:",""))
		}else if (str.indexOf('url:')==0){
			setURL(str.replace('url:',""))
		}else if(str.indexOf('init:')==0){
			return (hash()!="")?hash():"#";
		}
	}
	
	function setTitle(str){
		window.document.title=str
	}
	function setURL(str){
		document.location.hash=str;
	 
	}
	function hash() {
		return document.location.hash;
	}
	
	function getURL(){
		var str=document.location.hash;
		return str.substring(2).split("/");
		 
	}
	
	this.init=function(id){
		targetId=id;
		updateObj=setInterval(onUpdate,duration);
		
	}
	
	var onChanged=function(){
		flash(targetId).deeplink_onLinkChanged(currentHash);
	}
	
	var onInit=function(){
		flash(targetId).deeplink_onLinkOpened(currentHash);
		
	}
	this.setURL=function(str){
		document.location.hash=str;
	}

	var onUpdate=function (){
		if(currentHash==null || currentHash==undefined){
			currentHash=(hash()!="")?hash():"#";
		}else{
			var tmpHash=(hash()=="")?"#":hash();
			
			if(tmpHash!=currentHash){
				currentHash=tmpHash
				onChanged();
			}
		}
		if(String(hash()).indexOf("debug:close")>=0){stop()}
	}
	
	this.stop=function(){
		clearInterval(updateObj)
	}
	this.resume=function(){
		updateObj=setInterval(onUpdate,duration)
	}
	function flash(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }
	 
}
