/**
 * GtfwAjax, wraps AdvAjax
 */
var GtfwAjax = {
   replaceContent: function(el_id, content) {
      var el = document.getElementById(el_id);
      if (el) {
         el.innerHTML = content;

         // aplly JS, since it won't be automatically exec'd
         this.applyXHRJs(el_id);

         // apply Behaviour
         if (Behaviour) // behaviour support, reapply rules for new content
            Behaviour.apply();

         // hides baloon
         if (BaloonManager)
            BaloonManager.hideBaloon();
      } else
         alert('cannot found: <div id="'+el_id+'">');

      //alert(el);
   },
   replaceContentWithUrl: function(el_id, url, method) {
      var self = this;
      self.el_id = el_id;
      eval('var this_callback = function(obj) { self.replaceContent("' + self.el_id + '", obj.responseText);}');

      if (method == 'get')
         this.xhrGet(url, this_callback);
      else
         this.xhrPost(url, this_callback);
   },
   postFormReplace: function(el_id, dest_id) {
      var el = document.getElementById(el_id);
      var self = this;
      self.dest_id = dest_id;
      eval('var this_callback = function(obj) { self.replaceContent("' + self.dest_id + '", obj.responseText);}');

      advAJAX.submit( document.getElementById(el_id), {
         onInitialization : function() {
            // loading status support
            if (XhrStatus)
               XhrStatus.start('loading-box-active', 50);
         },
         mimeType: 'text/plain',/*
         onFinalization : function() {
         },*/
         onSuccess : function(obj) {
            // loading status support
            if (XhrStatus) XhrStatus.stop("loading-box-active", 100);
            this_callback(obj);
         }
      });
   },
   postForm: function(el_id, callback) {
      var el = document.getElementById(el_id);
      var self = this;

      advAJAX.submit( document.getElementById(el_id), {
         onInitialization : function() {
            // loading status support
            if (XhrStatus)
               XhrStatus.start('loading-box-active', 50);
         },
         mimeType: 'text/plain',/*
         onFinalization : function() {
         },*/
         onSuccess : function(obj) {
            // loading status support
            if (XhrStatus) XhrStatus.stop("loading-box-active", 100);
            //callback(obj);
            if (callback)
               eval(callback + '(obj);');
            else
               self.defCallback(obj);
         }
      });
   },
   xhrGet: function(src, the_callback) {
      advAJAX.get({
         url: src,
         onInitialization : function() {
            // loading status support
            if (XhrStatus)
               XhrStatus.start('loading-box-active', 50);
         },
         mimeType: 'text/plain',
   //       onFinalization : function() {
   //       },
         onSuccess : function(obj) {
            the_callback(obj);
            // loading status support
            if (XhrStatus) XhrStatus.stop("loading-box-active", 100);
         }
      });
   },
   xhrPost: function(src, the_callback) {
      advAJAX.post({
         url: src,
         onInitialization : function() {
            // loading status support
            if (XhrStatus)
               XhrStatus.start('loading-box-active', 50);
         },
         mimeType: 'text/plain',
   //       onFinalization : function() {
   //       },
         onSuccess : function(obj) {
               the_callback(obj);
               // loading status support
               if (XhrStatus) XhrStatus.stop("loading-box-active", 100);
         }
      });
   },
   applyXHRJs: function(el_id) {
      // http://microformats.org/wiki/rest/ahah with eval tips from: http://service.zimki.com/user/blog/tomi/2006/08/07/javascript-eval
      /// NOTICE:
      /// Always use "/* */" comment instead of "//" within loaded html/js since it will be eval'd in one line.
      var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
      var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
      var bMoz = (navigator.appName == 'Netscape');

      var node = document.getElementById(el_id);
      var st = node.getElementsByTagName('SCRIPT');
      var strExec;

      for(var i=0;i<st.length; i++) {
         if (bSaf) {
            strExec = st[i].innerHTML;
         }
         else if (bOpera) {
            strExec = st[i].text;
         }
         else if (bMoz) {
            strExec = st[i].textContent;
         }
         else {
            strExec = st[i].text;
         }
         //alert(strExec);
         try {
            self.eval(strExec);
         } catch(e) {
            alert(e + "\nMake sure you have use \\* *\\ style of comment instead of \\\\");
            alert('code was: ' + strExec);
         }
      }
   },
   loadUrl: function(theurl, callback) {
      self = this;
      advAJAX.get({
         url: theurl,
         onInitialization : function() {
            // loading status support
            if (XhrStatus)
               XhrStatus.start('loading-box-active', 50);
         },
         mimeType: 'text/plain',
   //       onFinalization : function() {
   //       },
         onSuccess : function(obj) {
            // loading status support
            if (XhrStatus) XhrStatus.stop("loading-box-active", 100);
            if (callback)
               callback(obj);
            else
               self.defCallback(obj);
         }
      });
   },
   defCallback: function(obj) {
      eval("result = " + obj.responseText + ";");
      if (result['exec']) {
         eval(result['exec'] + ";");
      }
   }
};

/**
 * XhrStatus, shows XHR status
 */
var XhrStatus = {
   taskCount: 0,
   isFadingOut: false,
   isFadingIn: false,
   fadeStage: 0,
   increaseTask: function() {
      this.taskCount += 1;
   },
   decreaseTask: function() {
      this.taskCount -= 1;
   },
   start: function(el_id, delay) {
      this.increaseTask();
      if (this.taskCount == 1)
         this.fadeStage = 0;
      var el = document.getElementById(el_id);
      if (el) {
         // fade out
         if (this.taskCount == 1) { // init loading
            //setTimeout('xhrFadeOut("' + el_id + '", '+ delay +')', delay);
            setTimeout('XhrStatus.fadeOut("' + el_id + '", '+ delay +')', delay);
            this.isFadingOut = true;
         }
      }
   },
   fadeOut: function(el_id, delay) {
	   var rad = 1.5707963267948966;
      if (this.fadeStage>100) {
         this.fadeStage = 0;
         this.isFadingOut = false;
      } else if (this.taskCount > 0) {
         var el = document.getElementById(el_id);
		   el.style.opacity=Math.sin((this.fadeStage/100)*rad);
         el.style.visibility = "visible";
         this.fadeStage+=10;
         //setTimeout('xhrFadeOut("' + el_id + '", '+ delay +')', delay);
         setTimeout('XhrStatus.fadeOut("' + el_id + '", '+ delay +')', delay);
      }
   },
   stop: function(el_id, delay) {
      var self = this;
      this.decreaseTask();
      var el = document.getElementById(el_id);
      if (el) {
         if (this.taskCount == 0) {
            //setTimeout('xhrFadeIn("' + el_id + '", '+ delay +')', delay);
            setTimeout('XhrStatus.fadeIn("' + el_id + '", '+ delay +')', delay);
            this.isFadingIn = true;
         }
      }
   },
   fadeIn: function(el_id, delay) {
      var self = this;
      var el = document.getElementById(el_id);
      if (this.fadeStage > 100) {
         this.fadeStage = 0;
         this.isFadingIn = false;
      } else if (this.taskCount < 1) {
         el.style.opacity= (100 - this.fadeStage) / 100;
         this.fadeStage+=10;
         //setTimeout('xhrFadeIn("' + el_id + '", '+ delay +')', delay);
         setTimeout('XhrStatus.fadeIn("' + el_id + '", '+ delay +')', delay);
      }
   }
}