/**
 * ajaxer.js
 * ajaxor class
 * prototype.js Ajax.Request class wrapper.
 * ** prototype.js が読み込まれたあとにインスタンスを生成すること **
 * 
 * @author okm
 */
  // Ajaxor class
  function Ajaxor(){
      var ref = this;

      /* private */
      this._available = true;

      /* public */
      this.method = "get";
      this.action = null;
      this.params = "";  // ex) "a=b&c=d&e=f"
      this.disableCache = true;

      this.timeout = 0; // msec, if 0, do nothing.
      this.avail_interval = 0; // msec, if 0, do nothing.

      /* public ajaxor events */
      this.onSuccess = null;
      this.onStart = null;
      this.onTimeout = null;
      this.onLoading = null;
      this.onComplete = null;
      this.onFailure = null;
      this.onNotAvailable = null;
      this.onException = null;

      /** functions **/
      this.execute = function(initObj){
          if(!this._available){
              alert("not in avail!");
              return;
          }

          for(var item in initObj){
              var value = initObj[item];
              this[item] = initObj[item];
          }

          if( this.action == null || this.action == "" ){
	      this.onFailureHandler("actionが指定されていません。");
	      return;
	  }
          if( this.method != "get" && this.method != "post" ){
	      this.onFailureHandler("methodの指定が間違っています。get/post");
	      return;
	  }

          if(this.disableCache){
              date = new Date();
              if(this.params != null) this.params += "&";
              else this.params = "";
              this.params += "cahce_=" + date.getTime();
          }

          /** add timeout **/
          if(ref.timeout > 0){
             // Register global responders that will occur on all AJAX requests
             Ajax.Responders.register({
                 onCreate: function(request) {
                     request['timeoutId'] = window.setTimeout(
                         function() {
                             // If we have hit the timeout and the AJAX request is active, abort it and let the user know
                             if (ref.callInProgress(request.transport)) {
                                 request.transport.abort();
                                 // Run the onFailure method
                                 ref.onTimeoutHandler();
                             }
                         },
                         ref.timeout
                     );
                 },
                 onComplete: function(request) {
                     // Clear the timeout, the request completed ok
                     window.clearTimeout(request['timeoutId']);
                 }
              });
          }

          /** execute ajax! **/
	  var ajaxRequest = new Ajax.Request(
					     this.action,
                                             {
						 "method": this.method,
						 "parameters": this.params,
						 onSuccess: function(request) {
						     ref.onSuccessHandler();
						 },
//                                                 onLoading: function(){
//                                                     ref.onLoadingHandler();
//                                                 },
                                                 onCreate: function(request) {
                                                       ref.onStartHandler();
                                                 },

                                                 onComplete: function(request) {   
						     //ref.onCompleteHandler(request.responseText);
						     ref.onCompleteHandler(request);
						 },
						 onFailure: function(request) {  
						     ref.onFailureHandler('読み込みに失敗しました');   
						 }
//						 onException: function (request, error) {
//						     ref.onExceptionHandler(request, error); 
//						 },
					     }
					     );

          if(this.avail_interval > 0){
              this._available = false;
              setTimeout(ref.setAvailable(true), ref.avail_interval);
          }
      }

      this.setAvailable = function(avail){
          this._available = true;
      }

      this.callInProgress = function(xmlhttp){
          switch (xmlhttp.readyState) {
              case 1: case 2: case 3:
                  return true;
                  break;
              // Case 4 and 0
              default:
                  return false;
                  break;
          }
      }

      /* Events */
      /* Ajax.Request Exents */
      this.onSuccessHandler = function(request){
          if(this.onSuccess) this.onSuccess(request);
      }
      this.onLoadingHandler = function(){
          if(this.onLoading) this.onLoading();
      }
      this.onCompleteHandler = function(request){
          // for safari
//          if(navigator.appVersion.indexOf( "KHTML" ) > -1){
//               var esc = escape(content);
//               loadText = (esc.indexOf("%u") < 0 && esc.indexOf("%") > -1) ? decodeURIComponent(esc) : loadText;
//          }
          // if responseText == "" or null, not complete
          if(request.responseText == "" || request.responseText == null) return;
          if(this.onComplete) this.onComplete(request);
      }
      this.onFailureHandler = function(failText){
          if(this.onFailure) this.onFailure(failText);
      },
      this.onTimeoutHandler = function(){
          if(this.onTimeout) this.onTimeout();
      }

      this.onExceptionHandler = function(request, eroor){
          if(this.onException) this.onException(request, error);
      }
      this.onStartHandler = function(){
          if(this.onStart) this.onStart();
      }
      /////////////////////////////////////////////////////////////////////////////////////////

  } // Ajaxor()

