/*  
power by dcboy  
method:cross domain to get JSON  
example:  
 
var proxy=new ScriptTagProxy();  
proxy.loadUrl('http://jungli.chinatrust-hotel.com.cn/gate/gb/search.yahooapis.com/ImageSearchService/V1/imageSearch?', 'appid=YahooDemo&query=Madonna&output=json', window,process);  
function process(a){  
  alert('Server return:'+a)  
}  
return a is [Object object]  
*/  
var ScriptTagProxys={TRANS_ID:1000};
//var ScriptTagProxy=Class.create();
ScriptTagProxy=function(){}
ScriptTagProxy.prototype={
    initialize:function(){this.head = document.getElementsByTagName("head")[0];},
    loadUrl : function(url, params, caller,onComplete, onFailure){
       transId=++ScriptTagProxys.TRANS_ID;
       var trans ={   
          id : transId,   
          cb : "stcCallback"+transId,   
          scriptId : "stcScript"+transId,   
          url : url,   
          params : params,   
          caller : caller,    
          onComplete : onComplete,   
          onFailure :onFailure       
       };   
       var conn = this;   
       window[trans.cb] = function(o){conn.handleResponse(o, trans);};   
        params='callback='+trans.cb+'&'+params;   
  
        var script = document.createElement("script");   
        script.setAttribute("src", url+params);   
        script.setAttribute("type", "text/javascript");   
        script.setAttribute("id", trans.scriptId);   
        //trans.timeoutId=setTimeout(this.handleFailure.bind(conn,trans), 20000);   
		//trans.timeoutId=setTimeout(this.handleFailure(trans), 20000);   
        this.head.appendChild(script);   
  },   
  handleResponse : function(o, trans){   
    this.destroyTrans(trans, true);   
    trans.onComplete.call(trans.caller||window, o);   
  },   
  handleFailure : function(trans){   
    this.destroyTrans(trans, false);   
    if(trans.onFailure){ trans.onFailure.call(trans.caller||window, null);   
    }else{alert('Load Timeout');}   
  },   
  destroyTrans : function(trans, isLoaded){   
    this.head.removeChild(document.getElementById(trans.scriptId));   
    clearTimeout(trans.timeoutId);   
    if(isLoaded){window[trans.cb] = undefined;   
      try{delete window[trans.cb];}catch(e){}   
    }else{   
      window[trans.cb] = function(){window[trans.cb] = undefined;   
      try{delete window[trans.cb];}catch(e){}   
      };    
    }   
  }   
}  

