﻿var erbc = {};

$(document).ready(function()	{

  $.ajaxSetup ({
    // Disable caching of AJAX responses
    cache: false
  });

  erbc.pagination.featureBoxLarge();
  erbc.series.select();
  $('li:first.item h3 a').click();
  
});

erbc.series = {

  select: function() {
    $('li.item p.image a img').live('click',function(){
      var link = $(this).parent().attr('href');
      $('div#sermons').load(link,function(){
        //load complete
      });
      $('li.item p.image a img').removeClass('selected');
      $(this).addClass('selected');
      return false;
    });
    $('li.item h3 a').live('click',function(){
      var link = $(this).attr('href');
      $('div#sermons').load(link,function(){
        //load complete
      });
      $('li.item p.image a img').removeClass('selected');
      $('li.item p.image a[href='+link+'] img').addClass('selected');
      return false;
    });
    $('#controls select#sort').change(function(){
      var link = $('form.sort').attr('action');
      switch ($(this).val()) {
        case 'date':
          $('div#carousel').load(link+"?sort=date",function(){
            $('li:first.item h3 a').click();
            // draw the pagination
            erbc.pagination.featureBoxLarge();
          });
          break;
        case 'title':
          $('div#carousel').load(link+"?sort=title",function(){
            $('li:first.item h3 a').click();
            // draw the pagination
            erbc.pagination.featureBoxLarge();
          });
          break;
      }
      return false;
    });
  }

};

erbc.pagination = { //functions for small paginated panels

  
  featureBoxLarge: function() { // init
    $('ul.featureSix').each(function() {
      var itemsPerPage = 6;
      var list = jQuery(this);
      var items = list.children('li').length;
      var currentPage = 1;
      var pages = Math.ceil(items/itemsPerPage);
      var container = list.parents('div.content');
      container.css({ overflow:"hidden" });
      if (pages > 1) {
        // hide all but first 'page'
        list.children('li').each(function(i){
          if ( i >= itemsPerPage) {
            var li = jQuery(this);
            li.hide();
          }
        });
        // draw the pagination
        erbc.pagination.drawLinks(pages,container,currentPage,itemsPerPage); 
      }
    });
  },
  
  drawLinks: function(end,container,currentPage,itemsPerPage)  { // draw pagination links
    container.siblings('.pagination').remove();
    var nextPage = parseInt(currentPage) + 1;
    container.before('<ol class="pagination"></ol>');
    // Add previous link
    if (currentPage == 1) {
      container.siblings('.pagination').append('<li><img src="fileadmin/images/button_prev_sm.png"></li>');
    } else {
      container.siblings('.pagination').append('<li><a href="1" class="prev"><img src="fileadmin/images/button_prev_sm.png"></a></li>');
    }
    // Create page numbers
    for (i=1; i<=end; i++) {
      if (i == currentPage) {
        container.siblings('.pagination').append('<li>' + i + '</li>');
      } else {
        container.siblings('.pagination').append('<li><a href="' + i + '">' + i + '</a></li>');
      }
    }
    //Add next link
    if (currentPage == end) {
      container.siblings('.pagination').append('<li><img src="fileadmin/images/button_next_sm.png"></li>');
    } else {
      container.siblings('.pagination').append('<li><a href="' + nextPage + '" class="next"><img src="fileadmin/images/button_next_sm.png"></a></li>');
    }
    //add click events 
    erbc.pagination.buttons(end,container,currentPage,itemsPerPage);
  },
  
  buttons: function(end,container,prevPage,itemsPerPage) { // change pages
    var pagination = container.siblings('.pagination');
    //number links
    pagination.find('a').bind('click', function() {
      if ( $(this).hasClass('prev') ) {
        var currentPage = parseInt(prevPage) - 1;
      } else if ( $(this).hasClass('next')) {
        var currentPage = parseInt(prevPage) + 1;
      } else {
        var currentPage = $(this).text();
      }
      erbc.pagination.drawLinks(end,container,currentPage,itemsPerPage);
      erbc.pagination.changePage(currentPage,container,itemsPerPage);
      container.find('.heightAuto').removeClass('heightAuto');
      container.find('.description').addClass('limitHeight');
      container.find('.more').show();
      return false;
    });
  },
  
  changePage: function(currentPage,container,itemsPerPage) {
    var firstItem = (currentPage-1) * itemsPerPage;
    var lastItem = (firstItem -1) + itemsPerPage;
    var list = container.find('ul.featureSix');
    list.children('li').each(function(i){
      var li = jQuery(this);
      if ( i < firstItem || i > lastItem) {
        li.fadeOut();
      } else {
        li.fadeIn();
      }
    });
  }
};


