(function($, global){

  //setup website package if undefined
  if (mcd.website === undefined) mcd.website = {}
  
  /**
   * Simply provides ajax functionality to load in more posts onto the
   * index page of the MCD news blog
   * @author dgalarza
   */


  mcd.website.morenews = {
    config: {    
      'page'    : 1,
      'perPage'   : 8,
      'uri'    : '/',
      'page_id'  : '39',
      'total_posts' : 0
    },
    
    /**
     * gotMore : checks if there is more post. if so display link
     */
      
    checkMore: function () {
		var onPage = $.doc.find('.post').size();
		if(this.config.total_posts == onPage){
			$.doc.find('#more-trigger').addClass('hide');
		} else {
			$.doc.find('#more-trigger').removeClass('hide');
		}
    },
	
	
    noMore: function(){
	
		var t =	$.doc.find("#no-more-posts").css('opacity', 0)
		.animate({ opacity: 1 }, 1200, 
		function(){
			var e = $(this); 
			setTimeout(	function(){ 
				$(e).animate({ opacity: 0 }, 1200);
			}, 4000);
		});
	},
	
	
    /**
     * Init
     */
    init: function (uConfig) {
      $.extend(this.config, uConfig);        
      
      var that = this;
      // The more posts click handler
      $.doc.find('#more-trigger').bind('click', function (e) {
        // Move on to the next page
		    that.config.page++;
          
        e.preventDefault();      
        
        // Request
        $.get(that.config.uri, {
          'page_id' : that.config.page_id,
          'pageOffset' : that.config.page,
          'perPage' : that.config.perPage                
        }, that.parseResponse);
      });
      this.checkMore();
    },
    
    /**
     * Parses the ajax response and pushes it to the page
     */
    parseResponse: function (response) {		
		//$.doc.find('#more-posts').append(response);
		//mcd.website.morenews.checkMore();	  
		
		$(response).appendTo('#post-list').hide().slideDown(500, function(){
			mcd.website.morenews.checkMore();	  
		});
		
    }
  }
}(jQuery, this));
