var DynScript=new Class({
  initialize:function(id,fn,ft,st){
    this.f=typeof fn=='string'?fn:'';
    switch(st){
      case'head':{
        this.t=document.getElementsByTagName("head")[0];
      } break;
      default:{
        this.t=document.id(st)?document.id(st):document.getElementsByTagName("head")[0];
      } break;
    }
    this.running=false;
    this.src={'p':this,'id':id,'t':this.t};
    switch(ft){
      case'js':{
        this.src=document.createElement('script');
        this.src.setAttribute("type","text/javascript");
        this.src.setAttribute("src", this.f);
        this.src.setAttribute('id',id);
      } break;
      case 'css':{
        this.src=document.createElement('link');
        this.src.setAttribute('rel','stylesheet');
        this.src.setAttribute('type','text/css');
        this.src.setAttribute('href',fn);
        this.src.setAttribute('id',id);
      } break;
    }
  },
  run:function(){
    if(this.running){
      return 2;
    } else {
      if(this.src&&this.t)this.t.appendChild(this.src);
      this.running=this.t.childNodes.indexOf(this.src)>-1;
      if(this.running&&console)console.log('dynScript '+this.id+' running with '+this.f);
    }
    return this.running;
  },
  stop:function(){
    if(this.src){
      this.running=this.t.childNodes.indexOf(this.src)>-1;
      if(this.running){
        this.running=!this.t.removeChild(this.src);
      }
    }
    this.running=this.running&&!document.id(this.src?this.src.getAttribute('id'):null);
    if(this.running&&console)console.log('dynScript not stopped. unloading failed');
    return !this.running;
  }
});

