function init() {
  maxTags();
  /* Inits the twitter bootstrap topbar */
  $('.topbar').dropdown();
  $('.selectSpecial').dropkick({
    change: function (value, label) {
      var valueArray = value.split('-');
      var type = valueArray[0]
      var category = valueArray[1]
      if (type == 'recent') {
        $('.' + category +'-row .content.controversial').fadeOut(400).remove();
        $('.' + category +'-row .content').fadeIn(400);
      } else if (type == 'controversial') {
        GetControversialPosts(category);
      }
    }
  });
  /* $('.gallery-icon a').fancybox(); */
  $('.more.btn').bind('click', GetPostsByCategory);
  $('img').removeAttr('width').removeAttr('height');
}


function tvinit() {
  channelInfo();
   
}
function tvinitextended() {
	channelInfoExtended();
}
/* This is a helper function to deal with the leaderboard */
  function fixedTop() {
    if ($('#leaderboard').length) {
      $(window).scroll(function () {
        if (93 <= $(window).scrollTop()) {
              $('.topbar').css('position','fixed');
              $('body').css('padding-top','40px');
          } else {
              $('.topbar').css('position','relative');
              $('body').css('padding-top','0px');
          }
      });
    } else {
      $('.topbar').css('position','fixed');
      $('body').css('padding-top','40px');
    }
  }

/* Helper function to capitalize */

function capitaliseFirstLetter(string)
{
  return string.charAt(0).toUpperCase() + string.slice(1);
}

/* This is a helper function to round floating points */
function round(number) {
  return Math.round(  
    Number(number)    
  ); 
}

/* This is the main function to hide the tag overflow on the left */
function maxTags() {
  var mainHeight = $('#main').height();
  var floatingCount = (mainHeight - 100) / 25;
  var intCount = round(floatingCount);
  /* Iterates through the last 6, to gradually fade them out */
  for (i=0;i<=intCount;i++) {
    if (i == intCount - 5) {
      $('.tags a:eq('+i+')').css('opacity', '0.9');
    } else if (i == intCount - 4) {
      $('.tags a:eq('+i+')').css('opacity', '0.85');
    } else if (i == intCount - 3) {
      $('.tags a:eq('+i+')').css('opacity', '0.75');
    } else if (i == intCount - 2) {
      $('.tags a:eq('+i+')').css('opacity', '0.6');
    } else if (i == intCount - 1) {
      $('.tags a:eq('+i+')').css('opacity', '0.4');
    } else if (i == intCount) {
      $('.tags a:eq('+i+')').css('opacity', '0.15');
    }
  }
  /* Hide everything that's greater than the intCount - cleans up the remaining ones */
  $('.tags a:gt('+intCount+')').hide();
  $('.tags').delay(1000).fadeIn(800);

}

/* This is a helper method that launches a spinner. It gets passed a category slug which it uses to manipulate the dom */
function animateMore(category) {
  var height = 0;
  $('.' + category + '-row .ajax-row').each(function(index) {
    if ($(this).height() > height) {
      height = $(this).height();
    }
  });
  /* Slide down...
  $('.' + category + '-row .ajax-row:hidden').css('height','0px').show().animate({
      height: height
    }, 400, function(){
      // Animation complete
    }); */
  $('.' + category + '-row .ajax-row:hidden').height(height).fadeIn(300);
}


/*-----------------------------------------------------------------------------------*/
/* More Button and User Controls */
/*-----------------------------------------------------------------------------------*/


/* This is the generic function for returning more posts */
function GetPostsByCategory() {
  var category = $(this).data('category');
  var exclusionsList = $('#featured-exclusions').data('exclusions');
  var exclusions = exclusionsList.split(',');
  var count = $('.' + category + '-row .story').length;
  var page = (round(count/3)) + 1;
  var returncount = 4;
  var order_by = 'date';
  var excluding = null;

  var requestStr = '/api/taxonomy/get_taxonomy_posts/?taxonomy=section&slug=' + category + '&page=' + page + '&count=' + returncount + '&order_by=' + order_by + '&date_format=j%20F,%20Y';
  
  var container = '<div class=\"row content ' + category + '-row-ajaxed\" style=\"margin-left: -20px;\"></div>';
  if ($('.' + category + '-row-ajaxed').length < 1 ) {$('.' + category + '-row').append(container);}
  $.getJSON(requestStr, function(data) {
    var results = data.count;
    var content = '';
    console.log(data);
    for (i=0;i<results;i++) {
  
      var block = '';
      var title = data.posts[i].title;
      var excerpt = data.posts[i].excerpt;
      var date = data.posts[i].date;
      var commentcount = data.posts[i].comment_count;
      var id = data.posts[i].id;
      var stringifyId = id.toString();
      var url = data.posts[i].url;
      // We check to see if the post id is a member of the exclusions array. We check for -1 because that's what .inArray returns if there's no match.
      if ($.inArray(stringifyId, exclusions) == -1) {
        block = '<div class=\"ajax-row\" style=\"display:none\"><div class="span4 smallStory story" data-id="' + id + '"><a href="' + url + '"><h5>' + title + '</h5></a><p>' + excerpt + '</p><div class="othersmall-meta"><span>' + date +'</span><br /><a class="othersmall-comments" href="' + url + '#comments"><img src="http://thevarsity.ca/wp-content/themes/varsity/images/comment-icon.png" />  ' + commentcount +' Comments</a></div></div></div>';
        content += block;
        
      } else {
        block = '<div class=\"ajax-row\" style=\"display:none\"><div class="span4 smallStory story" style=\"display:none;\" data-id="' + id + '"><a href="' + url + '"><h5>' + title + '</h5></a><p>' + excerpt + '</p><div class="othersmall-meta"><span>' + date + '</span><br /><a class="othersmall-comments" href="' + url + '#comments"><img src="http://thevarsity.ca/wp-content/themes/varsity/images/comment-icon.png" />  ' + commentcount +' Comments</a></div></div></div>';
        content += block;
      
      }
    }
      $('.' + category + '-row-ajaxed').append(content);
      animateMore(category);

    });
   
  if (page >= 3) {
    var categoryCap = capitaliseFirstLetter(category);
    $(category + '-row .more.btn').unbind('click', GetPostsByCategory);
    $('.' + category + '-row .btn').replaceWith('<a href=\"/section/' + category + '\" class=\"more btn\" data-category="news">Go to Section</a>');
  }
};

function GetControversialPosts(category) {

  $('.' + category + '-row .content').fadeOut(400);
  $('.' + category + '-row .more.btn').addClass('disabled');
  var content;
  var requestStr = '/api/taxonomy/get_taxonomy_posts/?taxonomy=section&slug=' + category + '&count=8&orderby=comment_count&date_format=j%20F,%20Y';

  $.getJSON(requestStr, function(data) {

      var results = data.count;
      var content = '';
      for (i=0;i<results;i++) {
        if (i==0) {
          content += '<div class="row content controversial" style="display:hidden;"><div class=\"ajax-row\">'
        }
        var title = data.posts[i].title;
        var excerpt = data.posts[i].excerpt;
        var date = data.posts[i].date;
        var commentcount = data.posts[i].comment_count;
        var id = data.posts[i].id;
        var url = data.posts[i].url;
        content += '<div class="span4 smallStory story" data-id="' + id + '"><a href="' + url + '"><h5>' + title + '</h5></a><p>' + excerpt + '</p><div class="othersmall-meta"><span>' + date + '</span><br /><a class="othersmall-comments" href="#"><img src="http://varsity.kmsm.ca/wp-content/themes/varsity/images/comment-icon.png" />  ' + commentcount + ' Comments</a></div></div>';
        if (i==3) {
          content += '</div><div class=\"ajax-row\">'
        } else if (i==7) {
          content += '</div></div>'
        }

      }
      $('.' + category + '-row .content-title').after(content).fadeIn(400);
  });
  
 
}

/*-----------------------------------------------------------------------------------*/
/* Varsity TV */
/*-----------------------------------------------------------------------------------*/

function channelInfo() {
  
  var requestStrVideos = 'http://vimeo.com/api/v2/channel/261723/videos.json?callback=vimeoResults';

  $.ajax({
    url: requestStrVideos,
    dataType: 'jsonp',
    jsonp: 'callback',
    success: function(vimeoResults) {
      var videocount = $(vimeoResults).length;
      var content = '';
      var mastercount = null;
      for (i=0;i<videocount;i++) {
        var id = vimeoResults[i].id;
        var title = vimeoResults[i].title;
        var description = vimeoResults[i].description;
        var url = vimeoResults[i].url;
        var upload_date = vimeoResults[i].upload_date;
        var date = upload_date;
        var thumb_medium = vimeoResults[i].thumbnail_medium;
        var thumb_large = vimeoResults[i].thumbnail_large;
        var playcount = parseInt(vimeoResults[i].stats_number_of_plays);
        mastercount += playcount;
        if (i == 0) {
          content += '<div class=\"video-row\">';
        } 

        content += '<div class=\"span4\"><a href=\"http://player.vimeo.com/video/' + id + '\" target=\"vimeoframe\" data-id=\"' + id + '\" data-title=\"' + title + '\" data-date=\"' + date + '\"><img class=\"thumbnail\" src=\"' + thumb_medium + '\" width=\"218\"/></a><a href=\"http://player.vimeo.com/video/' + id + '\" target=\"vimeoframe\" data-id=\"' + id + '\" data-title=\"' + title + '\" data-date=\"' + date + '\"><h4>' + title + '</h4></a><div class="otherbig-meta"></div></div>'

        if ((i + 1) % 4 == 0) {
          content += '</div><div class=\"video-row\">';
        } 

      }
      content += '</div>'
      $('.playcounts').text(mastercount + ' plays!');

      $('#video-playlist').append(content);
      var title = $('.video-row a:first').data('title');
      var date = $('.video-row a:first').data('date');
      var id = $('.video-row a:first').data('id');

      $('#vimeoiframe').attr('src','http://player.vimeo.com/video/' + id)

      $('#featured-content h2.title').text(title);
      $('#featured-video-date').text(date);

      $('.video-row a').bind('click', loadThatBitchUp);
      
    }
  });

}

function channelInfoExtended() {
  
  var requestStrVideos = 'http://vimeo.com/api/v2/channel/261723/videos.json?callback=vimeoResults';

  $.ajax({
    url: requestStrVideos,
    dataType: 'jsonp',
    jsonp: 'callback',
    success: function(vimeoResults) {
      var videocount = $(vimeoResults).length;
      var content = '';
      var mastercount = null;
      for (i=0;i<2;i++) {
        var id = vimeoResults[i].id;
        var title = vimeoResults[i].title;
        var description = vimeoResults[i].description;
        var url = vimeoResults[i].url;
        var upload_date = vimeoResults[i].upload_date;
        var date = upload_date;
        var thumb_medium = vimeoResults[i].thumbnail_medium;
        var thumb_large = vimeoResults[i].thumbnail_large;
        var playcount = parseInt(vimeoResults[i].stats_number_of_plays);
        mastercount += playcount;
        if (i == 0) {
          content += '<div class=\"video-row\">';
        } 
			content += '<div class=\"span8\"><iframe src=\"http://player.vimeo.com/video/' + id + '?byline=0&amp;portrait=0&amp;color=00adef\" width=\"460\" height=\"259\" frameborder=\"0\" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div>'
      }
      content += '</div>'
      
      $('#video-playlist').append(content);
      var title = $('.video-row a:first').data('title');
      var date = $('.video-row a:first').data('date');
      var id = $('.video-row a:first').data('id');

      $('#vimeoiframe').attr('src','http://player.vimeo.com/video/' + id)

      $('#featured-content h2.title').text(title);
      $('#featured-video-date').text(date);

      $('.video-row a').bind('click', loadThatBitchUp);
      
    }
  });

}

function loadThatBitchUp() {
  var title = $(this).data('title');
  var date = $(this).data('date');

  $('#featured-content h2.title').text(title);
  $('#featured-video-date').text(date);
}


