/**
 * В© Conjectrure corporation 2008
 *
 * jQuery rotate top of the questions plugin 
 *
 * created by Ivan Klimenko
 * 
 */

(function($) {
    
      //public part
    $.fn.extend(
    {
    
          //Set topqrotator instance
        topqrotator : function(options) 
        {   
            return this.each(function() {
                    
                      //Variables
                    var _object = this;
                    var _options = options;
                    
                    var _iMaxQuestionsLimit = 500
                    var _iGrabbedQuestionsCount = 30
                    
                    var _iCacheBlocksCount = 30
                    
                    var _iMaxViewBlocks = 7
                    
                    var _iMinTimeInterval = 1 //seconds
                    var _iMaxTimeInterval = 6 //seconds
                    
                    var _iInitTimeInterval = 1000 //milliseconds
                    
                    
                    var _iCurrTopOffset = 0
                    
                    var _iTIntervalId = 0
                    
                    var _iCurrBlock = 0;
                    
                    var _iBlocksLeft = (_iCacheBlocksCount - _iMaxViewBlocks + 1);
                    
                    
                      //Functions
                    var getNBlocksHeight = function(iStartIndex, iBlocksCount) {
                    
                      var iInitHeight = 0;
                      
                      var iCount = 0;
                      
                      if(iStartIndex < 0) {
                        iInitHeight += $(_object).find(".TopQuestionBlock:eq(0)").height();
                        iStartIndex = 0;
                      }
                      
                      $(_object).find(".TopQuestionBlock:gt("+iStartIndex+")").each(
                      
                        function (iIndex) {
                        
                          if(iCount == iBlocksCount) {
                            return false;
                          }     

                          iInitHeight += $(this).height();
                        
                          iCount++;
                        }
                        
                      );
                      
                      return iInitHeight;
                    
                    }//This is a last punched card of getNBlocksHeight function
                    

                    var removeFirstNBlocks = function(iStartIndex) {
                    
                      $(_object).find(".TopQuestionBlock:lt("+iStartIndex+")").remove();
                      
                      return true;
                    
                    }//This is a last punched card of removeFirstNBlocks function
                    
                    var setInitialHeight = function() {
                    
                      //var iHeight = getNBlocksHeight(0, _iMaxViewBlocks); 
                      var iHeight = getNBlocksHeight(-1, _iMaxViewBlocks); 
                    
                      $(_object).css('height', iHeight);
                    
                    }//This is a last punched card of setInitialHeight function
                    
                    
                    var getRandomQuestionsCount = function() {
                    
                      return 1;
                      
                      //return ( Math.floor(Math.random()*_iMaxViewBlocks) + 1 );
                    
                    }//This is a last punched card of getRandomQuestionsCount function
                    

                    var getRandomInterval = function() {
                    
                      return ( ( Math.floor(Math.random()*(_iMaxTimeInterval - 1)) + _iMinTimeInterval ) * 1000 );
                    
                    }//This is a last punched card of getRandomInterval function

                    
                    var getNextQuestions = function() {
                      
                      var iQuestionsToGrab = (_iCacheBlocksCount - _iMaxViewBlocks);
                      
                      if( (_iGrabbedQuestionsCount + iQuestionsToGrab) > _iMaxQuestionsLimit  ) {
                      
                        _iGrabbedQuestionsCount = 0;
                        
                      }
                      
                      var data = {};
                      data[_options.getQuestionsAction] = true;
                      data['count'] = iQuestionsToGrab;
                      data['start'] = _iGrabbedQuestionsCount;

                      _iGrabbedQuestionsCount += iQuestionsToGrab;    
                      
                      $.post('/index.htm', data, onGetNextQuestions, "html"); 
                    
                    }//This is a last punched card of getNextQuestions function
                    
                    
                    var onGetNextQuestions = function(sResult) {
                    
                      if(sResult != '0') {
                      
                        setInitialState();
                        
                        $(_object).find('#TopQuestionsRotator').append(sResult); 
                        
                        startRotation();
                      
                      } else {
                      
                        clearInterval(_iTIntervalId);
                        
                      }  
                    
                    }//This is a last punched card of onGetNextQuestions function
                    
                    
                    var setInitialState = function() {
                    
                      removeFirstNBlocks(_iCurrBlock-1);
                      
                      _iCurrBlock = 0;
                      
                      $(_object).find('#TopQuestionsRotator').css('top', '0px');
                      
                      _iCurrTopOffset = 0;
                      
                      setInitialHeight();
                    
                    }//This is a last punched card of setInitialState function
                    
                    
                    var animateBlock = function(iOffset) {
                    
                     _iCurrTopOffset = _iCurrTopOffset-iOffset;
                    
                     $(_object).find('#TopQuestionsRotator').animate({ top: _iCurrTopOffset }, 500);  
                    
                    }//This is a last punched card of animateBlock function
                    
                    
                    var startRotation = function() {
                    
                      var iRotateQCount = getRandomQuestionsCount();   
                    
                      var iRotationOffset = getNBlocksHeight(_iCurrBlock, iRotateQCount); 
                      
                      _iCurrBlock += iRotateQCount;
                      
                      if( _iCurrBlock < _iBlocksLeft ) {
                      
                        animateBlock(iRotationOffset);
                        
                        var iTimeInterval = getRandomInterval();
                        
                        _iTIntervalId = setTimeout(startRotation, iTimeInterval)
                        
                      } else {
                      
                        getNextQuestions();
                      
                      }
                    
                    }//This is a last punched card of startRotation function
                    
                               
                    setInitialHeight();
                    
                    _iTIntervalId = setTimeout(startRotation, _iInitTimeInterval);
                    
            });
        }
        
    });
   
})(jQuery);