jQuery.ajaxSetup({
  'beforeSend': function (xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

jQuery.fn.requestProfilePics = function() {
  $(this).find('fb\\:profile-pic:empty').each(function() {
    FB.XFBML.Host.addElement(new FB.XFBML.ProfilePic(this)); 
  });
}

function setDo() {
  $('#dodont_select').attr('class', 'do_on');
  $('#dodont_verb').val('DO');
}

function setDont() {
  $('#dodont_select').attr('class', 'dont_on');
  $('#dodont_verb').val('DONT');
}

function doSearch() {
  var input = $('#TagSearch');
  if (input.attr('class') == 'input-text' && input.val()) {
    window.location = '/dodonts/tag/' + encodeURIComponent(input.val().toLowerCase());
  }
}

function initDodontForm() {
  if ($('#new_dodont').length > 0) {
    $('#new_dodont')[0].reset();
    setDo();
    $('#dodont_content').focus();
  }
}

function permissionPromptCallback(granted) {
  // Right now, we check on the server side, so don't bother with the parameter.
  $.post('/dodonts/permission_prompt_callback');
}

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post($(this).attr('action'), $(this).serialize(), null, "script");
    return false;
  })
}

jQuery.fn.applySupportOppose = function() {
  $(this).find('.support.no.so_icon a').click(function() {
    var url = $(this).attr('href') + '/support'
    $.get(url, null, null, "script");
    return false;
  });

  $(this).find('.support.yes.so_icon a').click(function() {
    var url = $(this).attr('href') + '/unsupport'
    $.get(url, null, null, "script");
    return false;
  });

  $(this).find('.oppose.no.so_icon a').click(function() {
    var url = $(this).attr('href') + '/oppose'
    $.get(url, null, null, "script");
    return false;
  });

  $(this).find('.oppose.yes.so_icon a').click(function() {
    var url = $(this).attr('href') + '/unoppose'
    $.get(url, null, null, "script");
    return false;
  });

  var supportIcons = $(this).find('.so_icon.support');

  supportIcons.each(function() {
    $(this).setSupportIcon();
  });
  supportIcons.hover(function() {
    $(this).find('img').attr("src", "/images/66_black_support.png");
  },
  function() {
    $(this).setSupportIcon();
  });

  var opposeIcons = $(this).find('.so_icon.oppose');
  opposeIcons.each(function() {
    $(this).setOpposeIcon();
  });
  opposeIcons.hover(function() {
    $(this).find('img').attr("src", "/images/66_black_oppose.png");
  },
  function() {
    $(this).setOpposeIcon();
  });
}

jQuery.fn.showPopup = function(){
  $(this).css("left", ($(window).width() / 2) - ($(this).width() / 2));
  $(this).css("top", $(window).scrollTop() + 30);
  $(this).show();
}

jQuery.fn.applySupportOpposePopup = function () {
  $(this).click(function() {
    $.get($(this).attr('href'), null, null, "script");
    return false;
  });
}

jQuery.fn.applyDeleteButton = function() {
  $(this).click(function() {
    if (confirm('Are you sure you want to delete this post?')) {
      $.post($(this).attr('href'), {authenticity_token : $('input:hidden[name=authenticity_token]').val(), _method : "delete"}, null, "script");
    }
    return false;
  });
}

jQuery.fn.activateDodont = function(loadProfilePics) {
  $(this).find('div.so_wrap').applySupportOppose();
  $(this).find('a.so_popup').applySupportOpposePopup();
  $(this).find('a.trashbtn').applyDeleteButton();
  if (loadProfilePics == true) {
    $(this).requestProfilePics();
  }
}

jQuery.fn.setSupportIcon = function() {
  $(this).setSOIcon(["Unsupport?", "/images/66_pink_support.png"], ["Support?", "/images/66_grey_support.png"]);
}

jQuery.fn.setOpposeIcon = function() {
  $(this).setSOIcon(["Unoppose?", "/images/66_orange_oppose.png"], ["Oppose?", "/images/66_grey_oppose.png"]);
}


jQuery.fn.setSOIcon = function(actives, inactives) {
  var useThese = $(this).hasClass('yes') ? actives : inactives;
  var title = useThese[0];
  var src = useThese[1];
  var img = $(this).find('img');
  if (img.length == 0) {
    img = $('<img src="' + src + '">');
    var a = $(this).find('a');
    a.append(img);
    a.attr("title", title);
  }
  else
  {
    img.attr("src", src);
  }
}

$(function() {
  // Search header
  $('#TagSearch').focus(function(){
    $(this).val('').attr('class', 'input-text').focus(null);
  });
  $('#searchsubmit a').click(doSearch);

  // Home
  initDodontForm();
  $('#do a').click(setDo);
  $('#dont a').click(setDont);

  $('#postitsubmit a').click(function(){
    $('#new_dodont').submit();
  });
  $('#new_dodont').submitWithAjax();

  // Paging
  $('#more a').click(function() {
    var pageParam = $('#more input[name="page"]');
    pageParam.val(+pageParam.val() + 1);
    $('#more form').submit();
  });

  $('#more form').submit(function(){
    $.get($(this).attr('action'), $(this).serialize(), null, "script");
    return false;
  });

  // Submit on tag search.
  $('#TagSearch').keypress(function(e){
    if (e.which == 13) {
      doSearch();
      return false;
    }
  });

  $('a#logout_link').click(function() {
    var url = $(this).attr('href');
    FB.Connect.logout(function() {window.location = url;});
    return false;
  });

  $('#PopupClose a').click(function() {
    $('#Popup').hide();
    return false;
  });

  $('.dodont').activateDodont();

  $('select.link_on_change').change(function() {
      window.location = $(this).val();
  });
});