$(document).ready(function () {
  moderator();
  
  $('.full-article-link').each(function(){
    var link = $(this).attr('href');
    $(this).attr('rel', 'shadowbox;nofollow;').attr('title', 'FeedTheBull - ' + link );
  });
  
  $('a.view-profile').add('.node-author-info .picture a').each(function(){
    var userlink = 'ux' + $(this).attr('href');
    $(this).attr('rel', 'shadowbox').attr('href', userlink).removeAttr('href').addClass('hand').click(function(){
      $.get(userlink, function(data) {
        Shadowbox.open({
          player: 'html',
          content: data,
          title: $(this).attr('title'),
          width: '800px',
          height: '600px'
        });
      });
    });
  });

  $('a.send-message').each(function(){
    var username = $(this).attr('title');
    var sendMessageLink = 'ux/privatemsg/' + username;
    $(this).attr('rel', 'shadowbox;options={enableKeys:false}').removeAttr('href').addClass('hand').click(function(){
      $.get(sendMessageLink, function(data) {
        Shadowbox.open({
          player: 'html',
          content: data,
          title: 'Send ' + username + ' a message',
          width: '700px',
          height: '350px'
        }); 
      });
    });
  });

  $('.node-author-info .picture').find('img').attr('rel', 'shadowbox').click(function(){
    
  });

  $('.related-articles-link a').click(function(){
    var link = this;
    var container = $(link).parents('.related-wrapper').find('.node-related');
    $.get('ux/similarterms/' + $(this).attr('id'), function(data){
      $(container).html(data);
    });
    $(link).unbind('click').click(function(){$(container).fadeToggle();});
  });

  if ($('.view-content').length > 0) {
    $('#views-pager').html('<div class="results-arrow"><b>Results: </b></div>');
    $('#content').find('.pager').appendTo('#views-pager');
  }

});

jQuery.fn.fadeToggle = function(s, fn){
    return (this.is(":visible"))
        ? this.fadeOut(s, fn)
        : this.fadeIn(s, fn);
};

function tabHover() {
  $('.header-tab').hover(function(){
    $(this).toggleClass('tab-active');
  }, function(){
    $(this).toggleClass('tab-active');
  });
  $('#tab-images .header-tab-links .one').hover(
      function(){
        $(this).parents('.header-tab-links').addClass('sublink-one');    
      }, function() {
          $(this).parents('.header-tab-links').removeClass('sublink-one');
      }
  );
  $('#tab-images .header-tab-links .two').hover(
      function(){
        $(this).parents('.header-tab-links').addClass('sublink-two');    
      }, function() {
          $(this).parents('.header-tab-links').removeClass('sublink-two');
      }
  );
}

function moderator() {
  nodeToggle();
  ajaxComments();
  //ajaxFilters();
  tabHover();
  relatedContent();
}

function applyShadowbox() {
  //$('.full-article-link').attr('rel', 'shadowbox;nofollow;');
  Shadowbox.setup($('.view-content a.full-article-link').add('a.view-profile'));
}

function nodeToggle() {
  var nodeSelection = $('.node-teaser');
  $('.node-teaser .node-body').toggle();
  $(nodeSelection).find('h2.title a').removeAttr('href').addClass('hand');
  $(nodeSelection).find('h2.title a').click(function(){
      $(this).parents('.node-teaser').find('.node-body').fadeToggle();
      $(this).parents('.node-teaser').toggleClass('node-active');
  });
  $('.node-teaser').hover(
    function() { $(this).toggleClass('node-hover'); }, 
    function() { $(this).toggleClass('node-hover'); }
  );
}

function relatedContent() {
  $('.related-articles-link a').removeAttr('href').addClass('hand').click(function(){
    $(this).find('.node-related').fadeToggle();
  });
}

function ajaxComments() {
  $('.node-comments').each(function(){
    var container = $(this);
    $(this).find('a').addClass('hand').removeAttr('href').unbind('click').bind('click', function() {
      var commentLink = $(this);
      var nid = $(this).attr('id');
      $.get('ux/comments/'+nid, function(data) {
        container.append(data).fadeIn();
        container.find('h2.comments').remove();
        container.find('h3 a').removeAttr('href');
        container.find('a').attr('rel', 'shadowbox;nofollow;');
        initVoteWidget();
        //Shadowbox.setup($('a.full-article-link', '.node .content a'));
        $(commentLink).unbind('click').click(function() {
          $(container).children('.node-comments-body').toggle('slow');
        });
      });
    });
  });
}

function ajaxFilters() {
  // disable form submit
  if (!$('#views-filters').length > 0 || !$('.pager').length > 0) {
    return false; 
  }
  $('#views-filters').appendTo('#filters');
  $('#views-filters').submit(function(){return false;});
  
  var viewName = 'articles';
  // replace the exposed filter submit button with a refresh button
  var submitWrapper = $('.button-refresh');
  
  /* -- when you click refresh... -- */
  $(submitWrapper).html('Refresh').addClass('hand').unbind('click').bind('click', function() {
    // get the values from the form
    var uri = filterUri('#views-filters', viewName);
    // run the query and refresh content area with results
    updateFilteredContent(uri, viewName);
  });
  $('#views-filters').keyup(function(e){
    if (e.keyCode == 13) {
      // get the values from the form
      var uri = filterUri('#views-filters', viewName);
      // run the query and refresh content area with results
      updateFilteredContent(uri, viewName);
    }
  });
}

function filterUri(element_id, viewName) {
  // get form values
  var uri = '/ux/view/' + viewName + '/';
  $(element_id).find('input').add('option:selected').each(function(){
    //var value = ($(this).val() == '') ? 'all' : $(this).val();
    var value = this.value == '' || this.value.indexOf('http://') != -1 || this.value == '**ALL**' || this.value == 0 ? 'all' : this.value;
    uri += value + '/';
  });
  return uri;
}

function updateFilteredContent(uri, viewName) {
  var uri = uri;
  $.get(uri, function(data) {
    updateFilteredElements(viewName, data);
    nodeToggle();
    ajaxComments();
    applyShadowbox();
    updateAddressBar(uri);
  });
}

function updateFilteredElements(viewName, data) {
  // format the view name for css
  cssViewName = viewName.replace(/_/, '-');
  var selector = '.view-content-' + cssViewName;
  $(selector).html(data);
}

function updateAddressBar(uri) {
}

function createBookmark() {
  // uri format = #value/value/value/value/value
  // take current url and append uri
  var hash = window.location.hash;
  var bookmarkUrl = hash.replace('#', '/');
  // check if there is a page offset for the view
  // bookmark - i.e. window.addToFavorites(url, pageTitle)
}

function checkUri() {
  var url = window.location;
  // check if url includes values in the format of http://site.com/page/value/value/value/value
  
}
