if (window.NTorus === undefined)
           window.NTorus = new Object();

if (window.NTorus.Ajax === undefined)
           window.NTorus.Ajax = new Object();

if (window.NTorus.Event === undefined)
           window.NTorus.Event = new Object();

if (window.NTorus.Dom === undefined)
    window.NTorus.Dom = new Object();

if (window.NTorus.Utility === undefined)
    window.NTorus.Utility = new Object();

//-----------------------------------------------------------------------------
// Ajax
//-----------------------------------------------------------------------------

//--------------------------------------------------------------------
//NtXmlHttpRequest Object
//--------------------------------------------------------------------
window.NTorus.Ajax.NtXmlHttpRequest = function(verb, resourceUrl, body, handler, async) {

    //Properties
    this.verb = verb ?  verb : "GET";
    this.resourceUrl = resourceUrl ? resourceUrl : null;
    this.body = body ? body : null;
    this.handler = handler ? handler : null;
    this.async = async ? true : false;
    this._nativeObj = null;

    if (typeof XMLHttpRequest != "undefined") {
        this._nativeObj = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        this._nativeObj = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        throw new Error("XMLHttpRequest not supported");
    }
};

//--------------------------------------------------------------------
//NtXmlHttpRequest Method
//--------------------------------------------------------------------
window.NTorus.Ajax.NtXmlHttpRequest.prototype.getResponseText = function() {

    if (this.async) {
        if (this.handler) {
            var _this = this;
            this._nativeObj.onreadystatechange = function() {
                if (_this._nativeObj.readyState != 4) return;
                _this.handler(_this._nativeObj.responseText);
            }
        }
   }
   this._nativeObj.open(this.verb, this.resourceUrl, this.async);
   this._nativeObj.send(this.body);
   if (!this.async) return this._nativeObj.responseText;
};

//-----------------------------------------------------------------------------
// Event
//-----------------------------------------------------------------------------
//TODO: unbindAll function and bindlist.

//--------------------------------------------------------------------
//Event Static Method
//--------------------------------------------------------------------
window.NTorus.Event.bindEvent =
    document.addEventListener ?
    function(elm, eventName, listener) {
            elm.addEventListener(eventName, listener, false);
            return listener;
    }
    : document.attachEvent ?
    function(elm, eventName, listener) {
        var listenerFN = function() { listener(window.event); };
        elm.attachEvent('on' + eventName, listenerFN);
        return listenerFN;
    }
    :
    function() {
        throw new Error("Event binding not supported");
    };

//--------------------------------------------------------------------
//Event Static Method
//--------------------------------------------------------------------
window.NTorus.Event.unbindEvent =
    document.removeEventListener ?
    function(elm, eventName, listener) {
        elm.removeEventListener(eventName, listener, false);
    }
    : document.detachEvent ?
    function(elm, eventName, listener) {
       elm.detachEvent('on' + eventName, listener);
    }
    :
    function() {
        throw new Error("Event unbinding not supported");
    };

//-----------------------------------------------------------------------------
// Dom
//-----------------------------------------------------------------------------

//--------------------------------------------------------------------
//Dom Static Method
//--------------------------------------------------------------------
window.NTorus.Dom.SetStyleClass = function(elm, className) {
    elm.setAttribute("class",className);
    elm.setAttribute("className",className);
};

//-----------------------------------------------------------------------------
// Utility Static Method
//-----------------------------------------------------------------------------
window.NTorus.Utility.getQueryVariable = function(variable) {

   var query = window.location.search.substring(1);
   var vars = query.split("&");

   for (var i=0;i<vars.length;i++) {
       var pair = vars[i].split("=");
       if (pair[0] == variable) {
           return pair[1];
       }
   }
}


//-----------------------------------------------------------------------------
window.NTorus.Utility.setCookie = function(name,value,days) {

  var expires = "";

  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }

  document.cookie = name + "=" + value + expires + "; path=/";
}

//-----------------------------------------------------------------------------
window.NTorus.Utility.getCookie = function(name) {

  var nameEQ = name + "=";
  var ca = document.cookie.split(';');

  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }

  return null;
}

//-----------------------------------------------------------------------------
window.NTorus.Utility.eraseCookie = function(name) {
  createCookie(name,"",-1);
}


