/**
 * marketing.js
 */

$(function() {
      $('a.frame').click(function() {
          if($(this).hasClass('expand')) {
              if($(this).hasClass('video')) {
                  var url = $(this).attr('href');
                  var video = '<iframe src="' + url + '" width="600" height="600" frameborder="0"></iframe>';
                  $.facebox(video);
              } else {
                  var img = $(this).css('backgroundImage');
                  // remove the image path from url()
                  if (img.indexOf('png') != -1) {
                      img = img.slice(img.indexOf('http'), img.indexOf('png')+3);
                  } else {
                      img = img.slice(img.indexOf('http'), img.indexOf('jpg')+3);
                  }
                  $.facebox({ image: img });
              }
          }

          if (!$(this).hasClass('clickthrough')) {
              return false;
          }

      });

      $('.more-link .hide').css('display', 'none');
      $('.more-container').each(function() {
           $(this).data('height', $(this).height());
      });
      $('.more-container').css('height', 0);
      $('.more-link').click(function(e) {
          var more_container = $(this).siblings('.more-container');
          var active = $(this).data('active');
          if (!active) { active = false; }
          if (active) {
              $(more_container).animate({
                      height: 0
                  }, 700);
              active = false;
              $(this).find('.hide').css('display', 'none');
              $(this).find('.show').css('display', 'inline');
          } else {
              $(more_container).animate({
                      height: $(more_container).data('height')+"px"
                  }, 700);
              active = true;
              $(this).find('.show').css('display', 'none');
              $(this).find('.hide').css('display', 'inline');
          }
          $(this).data('active', active);
          e.preventDefault();
      });

      /**
       * Pull in trending topics
       */
      if ($('#trending-topics').length) {
          DISQUS.sexyapi.trends.listThreads({
              data: {
                  limit: 4,
                  related: 'forum',
                  api_key: context.apiPublicKey
              },
              success: function(data) {
                  $.each(data, function(index, trend) {
                      if (!trend.link) {
                          return;
                      }

                      // template in base_marketing.html
                      $('#trending-topics-post-tmpl').tmpl({
                          trend: trend,
                          thread: trend.thread,
                          context: context
                      }).appendTo('#trending-topics');
                  });
              }
          });
      }
      /**
       * Active conversations
       */
      if ($('#active-conversations').length) {
          var button = $('#active-conversations-more').show();
          button.attr('data-offset', $('#active-conversations > li').length);
          button.click(function() {
              button.disable();

              // TODO: move this into jquery template
              $.ajax({
                  url: '',
                  data: {
                      offset: button.attr('data-offset')
                  },
                  dataType: 'json',
                  success: function(data) {
                      $.each(data.conversations, function(index, el) {
                          $('#active-conversations').append(el);
                      });
                      button.attr('data-offset', data.offset);
                      button.enable();

                      if (!data.conversations.length || !data.has_more) {
                          button.hide();
                      }
                  }
              });
              return false;
          });
      }
});

