(function ($) {
"use strict";
$(function () {
// Update range slider
$("input[type='range']").change(function() {
var el = $(this);
var size = el.prev();
if(el.val() == 5) {
size.html('Small');
} else if(el.val() == 10) {
size.html('Medium');
} else if(el.val() == 15) {
size.html('Large');
}
});
// Disble clear button at first
$('.clear').attr("disabled", true);
// Clear search
function clearSearch() {
$('#album_info').hide();
$('#cover').empty();
$('#theimgurl').empty();
$('.clear').attr("disabled", true);
$('#query_artist').val('');
$('#query_album').val('');
}
// Click event for the Clear button
$(document).on( 'click', '.clear', function (e) {
clearSearch();
});
// Click event for setting Post Thumbnail (featured image)
$(document).on( 'click', '.setpostthumbnail', function (e) {
// Empty featured image box before appending new image
$('#postimagediv').find('img').remove();
// Variables to send via Ajax
var attachment_id = $(this).parent().parent().find('img').data('acoverfind_fileid'),
post_id = $('#pid').text();
$.post(
AlbumCoverFinderParams.ajax_url, {
action: 'and_action',
the_attachment: attachment_id,
the_post: post_id,
}, function(data) {
// Build image for attaching to DOM
var chosen_image = '';
var chosen_image_with_link = ''+chosen_image+'';
$('#postimagediv .inside').prepend(chosen_image_with_link);
$('#postimagediv .inside p').hide();
});
e.preventDefault();
});
// Click event for inserting image directly from LastFM CDN in the post editor
$('#insertineditor').on('click', function(e) {
var insert_url = $('#theimgurl').text(),
img_tag = '
';
// Switch to HTML-editor
jQuery('#content-html').trigger('click');
// Append to WYSIWYG-text area
jQuery('.wp-editor-area').val($('.wp-editor-area').val()+img_tag);
// Switch back to Tiny MCE
jQuery('#content-tmce').trigger('click');
e.preventDefault();
});
$('#findalbum').on('click', function(e) {
$('#wait').show();
$('#findalbum').val(AlbumCoverFinderParams.searching);
var artist = $('#query_artist').val(),
album = $('#query_album').val(),
size = $("input[type='range']").val();
// Query Last FM
findCover(artist, album, size);
e.preventDefault();
});
$('#setattachment').on('click', function(e) {
$('#wait').show();
var
att_count = $('.countattachments').text().substr(0,1),
img_url = $('#theimgurl').text(),
attachment_id = $(this).data('idnr'),
pid = $('#pid').text();
// Set number of attachments
$('.countattachments').text(parseInt(att_count)+1+' '+AlbumCoverFinderParams.files);
// Ajax request, set post post thumbnail
$.post(
AlbumCoverFinderParams.ajax_url, {
action: 'and_action',
setattachment: img_url,
postid: pid,
}, function(data) {
$('.acoverfind_attachments').prepend('
'+AlbumCoverFinderParams.nofound+'
'; $('#insertineditor').attr("disabled", true); $('#setattachment').attr("disabled", true); $('#findalbum').val(AlbumCoverFinderParams.search); } }, complete: function(){ $('#cover').append(html).delay(200); $('#album_info').fadeIn(200); $('#cover img').eq(2).remove(); $('#findalbum').val(AlbumCoverFinderParams.search); }, error : function(e,d,f){ }, }); } }); }(jQuery));