// Base viewController class
var ViewController = Class.extend({
   init: function(){
      this.buffers = new Array();
      return;
   },

   //**********************
   // Getters and setters
   //**********************
   getBuffer: function(strBufferID) {
      return this.buffers[strBufferID];
   },
   
   setBuffer: function(strBufferID,newBuffer) {
      this.buffers[strBufferID] = newBuffer;
   },

   //**********************
   // Functions
   //**********************
   loadContentCallback: function(strContentID,htmlContent,arrAdditionalBuffers,strEffect) {      
      $('.'+strContentID).append(htmlContent);

      // Loop through the additional buffers array and set these buffers to display
      if(arrAdditionalBuffers!=undefined) {
         for (var i in arrAdditionalBuffers) {
            $('.'+arrAdditionalBuffers[i]).css('display','block');
         }
      }
      
      // Buffer ID is always content id + buffer
      var strBufferID = strContentID + "Buffer";
      
      var newContentBuffer = this.getBuffer(strBufferID);
      var oldContentBuffer = 'A';
      
      if(newContentBuffer == 'A') {
         oldContentBuffer = 'B';
      }
      
      if($('.'+strBufferID+oldContentBuffer).css('display')==undefined) {
         this.loadContentEffectCallback(strBufferID,strEffect);
      }
      else {
         switch(strEffect) {
            case 'dropUp':
               $('.'+strBufferID+oldContentBuffer).hide('drop',{direction : 'up'},500,function() { myPageViewController.loadContentEffectCallback(strBufferID,strEffect); } );
               break;
            case 'dropDown':
               $('.'+strBufferID+oldContentBuffer).hide('drop',{direction : 'down'},500,function() { myPageViewController.loadContentEffectCallback(strBufferID,strEffect); } );
               break;
            case 'dropLeft':
               $('.'+strBufferID+oldContentBuffer).hide('drop',{direction : 'left'},500,function() { myPageViewController.loadContentEffectCallback(strBufferID,strEffect); } );
               break;
            case 'dropRight':
               $('.'+strBufferID+oldContentBuffer).hide('drop',{direction : 'right'},500,function() { myPageViewController.loadContentEffectCallback(strBufferID,strEffect); } );
               break;
            case 'fade':
            default:
               $('.'+strBufferID+oldContentBuffer).fadeOut(400,function() { myPageViewController.loadContentEffectCallback(strBufferID,strEffect); } );
               break;
         }
      }
   },

   loadContentEffectCallback: function(strBufferID,strEffect,callback) {
      var newContentBuffer = this.getBuffer(strBufferID);
      var oldContentBuffer = 'A';
      
      if(newContentBuffer == 'A') {
         oldContentBuffer = 'B';
      }
      
      // Remove the old content
      $('.'+strBufferID+oldContentBuffer).remove();

      switch(strEffect) {
         case "dropUp":
            $('.'+strBufferID+newContentBuffer).show('drop',{direction :'down'},500);
            break;
         case "dropDown":
            $('.'+strBufferID+newContentBuffer).show('drop',{direction :'up'},500);
            break;
         case "dropLeft":
            $('.'+strBufferID+newContentBuffer).show('drop',{direction :'right'},500);
            break;
         case "dropRight":
            $('.'+strBufferID+newContentBuffer).show('drop',{direction :'left'},500);
            break;
         case "fade":
         default:
            $('.'+strBufferID+newContentBuffer).fadeIn(400);
            break;
      }
      
      this.setBuffer(strBufferID,oldContentBuffer);

      if(callback!=undefined) {
         eval(callback);
      }
   },
   
   reloadPage: function() {
      location.reload(true);
   },
   
   toggleCSS: function() {
      xajax_xToggleCSS();
   }
});
