
// =====================================================================//
// Author: Mark Qian <markqian@hotmail.com>                             //
// WWW: http://www.coolshare.com/                                       //
// All rights reserved (2006)                                           //
//                                                                      //
// You must contact Mark Qian to get a permission of use                //
// in case you want to make any use of the codes besides viewing it     //
// on Mark's site.                                                      //
//======================================================================//



function DSCommunicator() {
  this.id = ""+(idCount++);
  var pp = this;
  ///////////////////////////////////////////////////////////////////
  //
  // Pattern "Template Method" is used here: implement its own concrete
  // "send" here. 
  //
  /////////////////////////////////////////////////////////////////////
  this.send = function () {
      var id = "ss_"+this.id;
	  var script=document.getElementById(id);
	  if (script) {    
	    script.parentNode.removeChild(script);
	  }  
	  script=document.createElement("SCRIPT");
	  script.id=id;
	  script.src=this.url;
	  var head=document.getElementsByTagName("HEAD")[0];
	  head.appendChild(script);    
	  this.state = 2;
    
  }
  
  	  ///////////////// Wrapping the concrete callback ///////////
	  //
	  // I don't invoke the real callback function, "handler",
	  // directly when dynamic script onloading. Instead, I wrapped
	  // the "handler" with a wrapper, "process" so
	  // that I can do some system level tasks before and
	  // after the "handler" is carried out. 
	  //
	  ////////////////////////////////////////////////////////////
  this.process = (function() {
           var res = function () {
	       // do the preprocess here...
	         pp.handler();
	       // do any clear up here...
	         pp.state = 0;
	      }
	      return res;
	  })();
}

///////////////////////////////////////////////////////////////
//
// Subclass Communicator
//
//////////////////////////////////////////////////////////////
DSCommunicator.prototype = new Communicator();

