$(document).ready(function() {
    $(".jqmWindow").jqm({ modal: false, toTop: true });

    $('a').each(function(n, el) {
        el = $(el);
        var link = el.attr('href');
        var server = document.location.hostname; // for IE
        if (link && link.search(/^http:\/\//) != -1 && link.indexOf(server) == -1) {
            el.attr('class', 'external');
            el.attr('target', '_blank');
        }
        if (link && link.search(/\.pdf$/) != -1) {
            el.attr('class', 'external');
            el.attr('target', '_blank');
        }
    });
});


/* checkboxes */

function select_all(p) {
    p.find(".selector:checkbox").attr("checked", "checked");
    return false;
}

function unselect_all(p) {
    p.find(".selector:checkbox").removeAttr("checked");
    return false;
}


/* like */

function likeIt(content_type_id, object_id, url) {
    var id = content_type_id + '_' + object_id;
    $('#id_like_it_' + id).hide();
    $('#id_like_it_wait_' + id).show();
    $.post(url, {type: 'like', content_type: content_type_id, object_id: object_id},
          function (data) {
              $('#id_like_it_wait_' + id).hide();
              if (data != "error") {
                  $('#id_unlike_it_' + id).show();
                  $('#id_users_who_liked_it_' + id).html(data);
              } else {
                  $('#id_like_it_' + id).show();
              }
          });
    return false;
}

function unlikeIt(content_type_id, object_id, url) {
    var id = content_type_id + '_' + object_id;
    $('#id_unlike_it_' + id).hide();
    $('#id_like_it_wait_' + id).show();
    $.post(url, {type: 'dislike', content_type: content_type_id, object_id: object_id},
          function (data) {
              $('#id_like_it_wait_' + id).hide();
              if (data != "error") {
                  $('#id_like_it_' + id).show();
                  $('#id_users_who_liked_it_' + id).html(data);
              } else {
                  $('#id_unlike_it_' + id).show();
              }
          });
     return false;
}

function showUsersWhoLikedIt(id) {
    $("#id_users_who_liked_it_window_content").html($("#id_users_who_liked_it_content_" + id).html());
    $("#id_users_who_liked_it_window").jqmShow();
    return false;
}


/* follow */

function followUser(user_id, url) {
    $('.id_follow_' + user_id).hide();
    $('.id_follow_wait_' + user_id).show();
    $.post(url,
          {action: "follow",
           user_id: user_id},
          function (data) {
              $('.id_follow_wait_' + user_id).hide();
              if (data == "ok") {
                  $('.id_unfollow_' + user_id).show();
              } else {
                  $('.id_follow_' + user_id).show();
              }
          });
    return false;
}

function unfollowUser(user_id, url) {
    $('.id_unfollow_' + user_id).hide();
    $('.id_follow_wait_' + user_id).show();
    $.post(url,
          {action: "unfollow",
           user_id: user_id},
          function (data) {
              $('.id_follow_wait_' + user_id).hide();
              if (data == "ok") {
                  $('.id_follow_' + user_id).show();
              } else {
                  $('.id_unfollow_' + user_id).show();
              }
          });
     return false;
}

var signup_and_follow_url;

/* block follower */

function blockUser(user_id, url) {
    if (!confirm('Are you sure you want to block this user? They will not be able to follow you.'))
        return;
    $('.id_block_' + user_id).hide();
    $('.id_follow_wait_' + user_id).show();
    $.post(url,
          {action: "block",
           user_id: user_id},
          function (data) {
              $('.id_follow_wait_' + user_id).hide();
              if (data == "ok") {
                  $('.id_unblock_' + user_id).show();
              } else {
                  $('.id_block_' + user_id).show();
              }
          });
    return false;
}

function unblockUser(user_id, url) {
    $('.id_unblock_' + user_id).hide();
    $('.id_follow_wait_' + user_id).show();
    $.post(url,
          {action: "unblock",
           user_id: user_id},
          function (data) {
              $('.id_follow_wait_' + user_id).hide();
              if (data == "ok") {
                  $('.id_block_' + user_id).show();
              } else {
                  $('.id_unblock_' + user_id).show();
              }
          });
     return false;
}

/* report */

function reportObject(e, url, content_type, object_id, type) {
    if (confirm("Are you sure you want to report this? You can report inappropriate, offensive, erotic content or content which violates Heylog's Terms of Service."))
    {
        $(e).remove();
        $.post(url, {content_type: content_type, 
                     object_id: object_id, 
                     type: type},
               function(data) {
                    alert('Thank you for reporting this! We will investigate it shortly.');
               });
    }
    return false;
}
