//
// JSHttpRequest v1.11. (C) Dmitry Koterov, 2005-01-27. 
// http://forum.dklab.ru/users/DmitryKoterov/
//
// Do not remove this comment if you want to use script!
// Не удаляйте данный комментарий, если вы хотите использовать скрипт!
//
function JSHttpRequest() {}
(function() {
  var count       = 0;
  var pending     = {};
  var cache       = {};
  var spanReuse   = null;
  
  // Uncomment if you want to switch on <SCRIPT> reusing.
  // But be carefull: seems FireFox does not work with reusing correctly
  // (long delay on fast data reloading via existed <SCRIPT>).
  // spanReuse   = [];
  

  // Called by server script on data load.
  JSHttpRequest.dataReady = function(id, text, js) {
    var undef;
    var th = pending[id];
    if (th) {
      if (th.caching) cache[th.hash] = [text, js];
      th._dataReady(text, js);
    } else if (typeof(th) != typeof(undef)) {
      alert("ScriptLoader: unknown pending id: "+id);
    }
  }
  
  JSHttpRequest.prototype = {
    // Standard properties.
    onreadystatechange: null,
    readyState: false,
    responseText: null,
    responseXML: null,
    status: 200,
    statusText: "OK",
    // Additional properties.
    responseJS: null, 
    caching: false,
    SID: "nopt=08743a66c257e6fa75baf466591b0e24",
    // Internals.
    _span: null,
    _id: null,
      
    abort: 
     function() { 
          this.readyState = false;
          var span = this._span;
          if (span) {
            this._span = null;
            setTimeout(function() {
              // without setTimeout - crash in IE 5.0!
              span.parentNode.removeChild(span);
            }, 100);
          }
          return false;
     },
    
    send: 
      function(content, url) {
          var id = count++;
          var query = [];
          this.url = url;
          
          if (content instanceof Object) {
            for (var k in content) {
              query[query.length] = escape(k) + "=" + escape(content[k]);
            }
          } else {
            query = [content];
          }
          
          if (this.SID) query[query.length] = this.SID;
          
          var qs = query.join('&');
          query = id + '&' + qs;
          
          var href = this.url + (this.url.indexOf('?')>=0? '&' : '?') + 'jshttprequest=' + query;
          var hash = this.url + '?' + qs;
          
          this.hash = hash;
          if (this.caching && cache[hash]) {
            var c = cache[hash];
            this._dataReady(c[0], c[1]);
            return false;
          }
          
          this._obtainScript(id, href);
          return true;
      },


    _dataReady: 
        function(text, js) {
            with (this) {
                  if (text !== null || js !== null) {
                    readyState = true;
                    responseText = responseXML = text;
                    responseJS = js;
                  } else {
                    readyState = false;
                    responseText = responseXML = responseJS = null;
                  }
            onreadystatechange();
            
            _cleanupScript();
            }
        },

    _obtainScript:
        function(id, href) {
          with (document) {
              var span = null;
              
              if (spanReuse == null || !spanReuse.length) {
                span = body.appendChild(createElement("SPAN"));
                span.style.display = 'none';
                span.innerHTML = 'Text for stupid IE.<script></' + 'script>';
              } else {
                span = spanReuse[spanReuse.length-1];
                spanReuse[spanReuse.length-1] = null;
                spanReuse.length--;
              }
              
              pending[id] = this;
              setTimeout(  function() {
                              var s = span.getElementsByTagName("script")[0];
                              s.language = "JavaScript";
                              s.type = "text/javascript";
                              if (s.setAttribute) s.setAttribute('src', href); else s.src = href;
                           }, 10);
              this._id = id;
              this._span = span;
           }
        },

    _cleanupScript:
          function() {
              var span = this._span;
              if (span) {
                    this._span = null;
                    
                    setTimeout(function() {
                      if (spanReuse != null) {
                        spanReuse[spanReuse.length] = span;
                      } else {
                        // without setTimeout - crash in IE 5.0!
                        span.parentNode.removeChild(span);
                      }
                    }, 50);
              }
              //window.status = document.body.childNodes.length + " - " + (spanReuse? spanReuse.length : 'no span reusing')
              return false;
         }
  }
  
})();
