/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Original Author: Eric Pollmann <pollmann@google.com>
 *
 * With thanks to the many public crossbrowser toolkits, techniques of
 * which are reused herein.
 */

var cb_isExplorer = /MSIE (\d+\.\d+);/.test(navigator.userAgent)
var cb_isWebkit = navigator.userAgent.match(/webkit/i)
var cb_isGecko = /Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)

// Standardize getElementsByClassName
if(!document.getElementsByClassName) {
  document.getElementsByClassName = function(className, parentElement) {
    var children = (parentElement || document.body).getElementsByTagName('*')
    var elements = [], child
    for (var i=0, length = children.length; i < length; i++) {
      child = children[i];
      if (child.className &&
          child.className == className) { // .toUpperCase(), and deal with ' '
        elements.push(child)
      }
    }
    return elements
  }
}

// Standardize Array.indexOf
if (!Array.indexOf) {
  Array.prototype.indexOf = function(el, start) {
    var start = start || 0;
    for ( var i=0; i < this.length; ++i ) {
      if ( this[i] === el ) {
        return i;
      }
    }
    return -1;
  }
}

// Standardize XMLHttpRequest
var cb_oldXHR = window.XMLHttpRequest
function cb_XHR() {
  if(cb_oldXHR && (!cb_isExplorer ||
                   (cb_isExplorer &&
                    String(location.href).indexOf('file')!=0))) {
    this.xhr=new cb_oldXHR
  } else {
    this.xhr=new window.ActiveXObject('Microsoft.XMLHTTP')
  }
}
cb_XHR.prototype.readyState = 0
cb_XHR.prototype.responseText = ''
cb_XHR.prototype.responseXML = null
cb_XHR.prototype.status = 0
cb_XHR.prototype.statusText = ''
cb_XHR.prototype.cb_ = true
cb_XHR.prototype.send=function (text) { this.xhr.send(text) }
cb_XHR.prototype.open=function (method, url, async, user, password){
   var inst=this
   var ready=this.readyState
   if(cb_isExplorer && async) {window.attachEvent('onunload', inst.abort)}
   this.xhr.onreadystatechange=function () {
     if(cb_isGecko && !async) {return}
     inst.readyState=inst.xhr.readyState
     if(inst.readyState){
       try{inst.responseText=inst.xhr.responseText} catch(e){}
       try{inst.responseXML=inst.xhr.responseXML} catch(e){}
       try{inst.status=inst.xhr.status} catch(e){}
       try{inst.statusText=inst.xhr.statusText} catch(e){}
     }
     if(inst.interrupt_){
       inst.readyState=0
       return
     }
     if(inst.readyState==4){
       inst.abort(inst)
       if(inst.onload){
         inst.onload.apply(inst)
       }
       if(cb_isExplorer && async) {window.detachEvent('onunload',inst.abort)}
     }  
     if(ready != inst.readyState){
       if(inst.onreadystatechange){
         if(inst.xhr.async && inst.readyState == 4){
           setTimeout(function () {inst.onreadystatechange.apply(inst)}, 0)
         } else {
           inst.onreadystatechange.apply(inst)
         }
       }  
     }
     ready = inst.readyState // ???
   }
   this.xhr.open(method, url, async, user, password)
 }  
cb_XHR.prototype.abort=function (){
  if(this.xhr.readyState==4) {return}
  if(this.readyState>0) {this.interrupt_=true}
  this.xhr.abort()
  if (cb_isExplorer) {this.xhr.onreadystatechange=new window.Function}
}
window.XMLHttpRequest = cb_XHR

// Standardize DOMParser
if(!window.DOMParser) {
  DOMParser = new Function
  DOMParser.prototype.baseURI=null
  DOMParser.prototype.parseFromString = function (text) {
    var parser = new ActiveXObject('Microsoft.XMLDOM')
    try{
      parser.setProperty('SelectionLanguage', 'XPath')
    } catch(e){}
    parser.async=false
    parser.preserveWhiteSpace=true
    parser.validateOnParse=false
    parser.resolveExternals=false
    if(cb_isExplorer) {
      var docStart=text.indexOf('<?xml ')
      if(docStart >= 0) {text=text.substr(text.indexOf('?>',docStart + 1) + 2)}
    }
    parser.loadXML(text)
    return parser
  }
}
