(function(d){d.event.props.push("dataTransfer");var r=function(){d("#wp_rp_edit_related_posts").click(function(){var h={num_articles:30,num_articles_to_insert:window._wp_rp_num_rel_posts,post_id:window._wp_rp_post_id,admin_ajax_url:window._wp_rp_admin_ajax_url,admin_ajax_action:"rp_update_related_posts",plugin_static_url:window._wp_rp_plugin_static_base_url,zemanta_thumbnail_url:"http://i.zemanta.com/{aid}_150_150.jpg",num_default_thumbnails:30,search_support:!!window._wp_rp_erp_search,promoted:!1!==
window._wp_rp_promoted_content,tx:!1!==window._wp_rp_traffic_exchange,num_external_slots:0},a={holder:null,wrapper:null,search_form:null,search_input:null,selected_articles_wrap:null,replace_articles_wrap:null,replace_articles_list:null,article_loader:null,article_list:{},articles_to_insert:null,footer:null,save:null},i={},j=[],k={},r=[],u=function(b){b.preventDefault();a.holder.remove();d("html").css("overflow","visible")},s=function(b){var c=[];d.each(i,function(){});for(var a=0;a<h.num_articles_to_insert;a+=
1){var f=i[a];f?"own_sourcefeed"===f.type?c.push({ID:f.aid,post_url:f.url,thumbnail:f.thumbnail,post_title:f.title,post_excerpt:f.excerpt||"",post_content:"",post_date:f.date||"",comment_count:f.comments||0,picked:!!f.picked,type:f.type,pos:a}):c.push({ID:!1,pos:a,type:f.type}):c.push({ID:!1,pos:a,type:"empty"})}d.post(h.admin_ajax_url,{action:h.admin_ajax_action,post_id:h.post_id,related_posts:JSON.stringify(c),_wpnonce:window._wp_rp_ajax_nonce},b)},v;v=function(b,c,a,f){var c=window._wp_rp_post_tags&&
window._wp_rp_post_tags.join(",")||"",e=window._wp_rp_post_title||"",b=b||!1;if(!c&&!e&&!1===b)a(!1);else{var g={},i=1,j=function(){i-=1;if(0>=i){var b=[],c={};d.each(["external","internal"],function(a,f){g[f]&&("ok"===g[f].status&&g[f].data)&&d.each(g[f].data.results,function(a,f){if(c[f.url])return!0;c[f.url]=!0;b.push(f)})});g.external&&"ok"===g.external.status&&(h.num_external_slots=g.external.data.settings.num_external_slots);b?a&&b&&a(b):f&&f()}},k=function(b){g["internal"===b.source?"internal":
"external"]=b;j()};d.ajax({url:window._wp_rp_wp_ajax_url,dataType:"json",data:{post_id:h.post_id,search:b||"",action:"wp_rp_load_articles",count:h.num_articles},success:function(b){var c=[];d.each(b,function(b,a){c.push({type:"own_sourcefeed",aid:"in_"+a.id,thumbnail:d(a.img).attr("src"),title:a.title,excerpt:a.excerpt,date:a.date,comments:a.comments,url:a.url,target_url:a.url})});k({status:"ok",source:"internal",data:{results:c}})},error:j});h.remote_recommendations&&get_sre_articles(b,c,e,k,j)}};
var n,w=function(b,c,x){a.replace_articles_list.html("");g.render_selector_shadows();j=[];a.article_loader.find(".zem-no-articles").hide();a.article_loader.find(".zem-loader").show();a.article_loader.show();var f=h.num_external_slots;v(c,x,function(c){c&&c.length?(a.article_loader.hide(),j=d.grep(c,function(b){return 0>window.location.href.indexOf(b.url)}),d.each(j,function(b,c){k[c.aid]?(c=k[c.aid],j[b]=c):k[c.aid]=c}),g.article_selector(),f!==h.num_external_slots&&g.articles()):(a.article_loader.find(".zem-no-articles").show(),
a.article_loader.find(".zem-loader").hide());g.render_selector_shadows();b&&b(!0)},function(){a.article_loader.find(".zem-no-articles").show();a.article_loader.find(".zem-loader").hide();g.render_selector_shadows();b&&b(!1)})},p=function(b,c,a){b.picked=!0;b.pos=c;i[c]=b;k[b.aid]=b;g.article_li_selected(b);a&&(s(),g.article_selector())},l=function(b,c){delete i[b.pos];b.picked=!1;b.pos=-1;b.elm&&b.elm.html('<div class="droppable" /><span class="notice">Drag post here</span>').attr("draggable",!1).removeClass("external").data("aid",
!1);c&&(s(),g.article_selector())},g={article_li:function(b,c){b.html('<div class="droppable" />');b.data("aid",c);b.attr("draggable",!0);b.unbind("dragstart").bind("dragstart",function(a){e.drag(a,c,b)});var a=d('<img draggable="false" />');a.error(function(){a.unbind("error");var b=parseInt(c.aid.replace("in_"))||parseInt(Math.random()*h.num_default_thumbnails),b=h.plugin_static_url+"thumbs/"+b%h.num_default_thumbnails+".jpg";c.thumbnail=b;a.attr("src",b)});c.thumbnail=c.thumbnail||c.thumbnail_url;
a.attr("src",c.thumbnail);b.append(a);b.append('<span unselectable="on" class="title">'+c.title+"</span>");var f=d('<a class="open-article" draggable="false" target="_blank" href="'+c.target_url+'">link out</a>');f.bind("click",function(b){b.stopPropagation()});b.append(f)},article_li_selector:function(b,c){c.elm=b;g.article_li(b,c);var a=d('<a draggable="false" class="insert overlay" href="#"><div class="txt">insert</div></a>');a.bind("click",function(b){b.preventDefault();for(b=b=0;b<h.num_articles_to_insert-
1&&i[b];b+=1);i[b]||p(c,b,!0)});b.append(a)},article_li_selected:function(b){var c=a.article_list[b.pos];b.elm=c;g.article_li(c,b);if(!b.external){var e=d('<a draggable="false" class="remove overlay" href="#"><span class="icon"></span><span class="txt">remove</span></a>');e.bind("click",function(a){a.preventDefault();l(b,!0)});c.append(e)}},article_selector:function(){a.replace_articles_list.html("");var b={};d.each(i,function(a,c){b[c.aid]=!0});var c=0;d.each(j,function(e,f){if(!b[f.aid]){var i=
d("<li />");g.article_li_selector(i,f);a.replace_articles_list.append(i);c+=1}if(c>=h.num_articles)return!1});g.render_selector_shadows()},render_selector_shadows:function(){var b=a.replace_articles_list.scrollLeft(),c=a.replace_articles_list[0].scrollWidth-a.replace_articles_list.width();0<b?a.replace_articles_list.addClass("scroll-left"):a.replace_articles_list.removeClass("scroll-left");b<c?a.replace_articles_list.addClass("scroll-right"):a.replace_articles_list.removeClass("scroll-right")},articles:function(){d.each(i,
function(b,a){a&&(a.aid&&a.picked)&&(b<h.num_articles_to_insert?p(a,b,!1):l(a,!1))})},all:function(){g.articles();g.article_selector()}},e={hint_timeout:null,dragged_article:null,ie9_drag_start:function(b){1!==b.which||(b.ctrlKey||b.metaKey)||d(this).get(0).dragDrop&&d(this).get(0).dragDrop()},drag_hint:function(b){if(!(1!==b.which||b.ctrlKey||b.metaKey)){var b=d(this),c=e.dragged_article||b.data("aid");c&&(e.hint_timeout=setTimeout(function(){!c.picked||c.external?a.wrapper.find("ul.selected li:not(.external)").addClass("drop-hint"):
a.wrapper.find("ul.selected li").addClass("drop-hint");c.picked&&!c.external&&a.remove_article_sign.show()},100))}},drag:function(b,c,d){b.dataTransfer.setData("text","wprp_article_"+c.aid);b.dataTransfer.setDragImage?b.dataTransfer.setDragImage(d.get(0),d.outerWidth()/2,d.outerHeight()/2):b.dataTransfer.addElement&&b.dataTransfer.addElement(d.get(0));e.dragged_article=c;setTimeout(function(){a.wrapper.find("li .droppable").css("z-index",2)},1);e.drag_hint(b)},drop_remove:function(b){b.preventDefault();
var a=e.dragged_article;e.dragged_article&&!a.external&&(l(a,!0),e.dragend(b))},drop:function(b){d(this).removeClass("drop");b.preventDefault();var a=e.dragged_article;if(!a)return!1;var g=a.pos,f=1*d(this).data("pos");if(g===f)return!1;var h=i[f];if(!h||!(h.external&&!a.picked||h.external&&a.external)){var j=a.picked;j&&l(a,!1);h&&(l(h,!1),j&&p(h,g,!1));p(a,f,!0);e.dragend(b)}},dragover:function(b){b.preventDefault();var b=e.dragged_article,a=d(this).data("aid");(!a||!(a.external&&!b.picked||a.external&&
b.external))&&d(this).addClass("drop")},dragleave:function(b){b.preventDefault();d(this).removeClass("drop")},dragend:function(){clearTimeout(e.hint_timeout);e.dragged_article=null;a.remove_article_sign.hide();a.wrapper.find("li .droppable").css("z-index",-1);a.wrapper.find("ul.selected li").removeClass("drop-hint")},init:function(){a.selected_articles_wrap.delegate("li","dragover",e.dragover).delegate("li","dragleave",e.dragleave).delegate("li","drop",e.drop);a.replace_articles_wrap.bind("dragover",
e.dragover).bind("dragleave",e.dragleave).bind("drop",e.drop_remove);a.wrapper.delegate("li[draggable=true]","dragstart",e.drag_hint).delegate("li[draggable=true]","dragend",e.dragend).delegate("li[draggable=true]","mousedown",e.drag_hint).delegate("li[draggable=true]","mouseup",e.dragend).delegate("li[draggable=true]","mousemove",e.ie9_drag_start)}};n={update:w,render:g.all,init:function(){e.init();a.search_form.bind("submit",function(b){b.preventDefault();b=a.search_input.val();w(null,b,!0)});a.replace_articles_list.bind("scroll",
g.render_selector_shadows)}};a.holder=d('<div id="wp_rp_zem_related_posts_holder"></div>');a.wrapper=d('<div id="wp_rp_zem_related_posts_wrap"><div class="selected-header"><h4 class="selected-title">Selected posts</h4><a href="#" class="save button">Save and Close</a></div><div class="selected-content"></div></div>');a.holder.append(a.wrapper);a.wrapper.bind("click",function(b){b.stopPropagation()});a.save=a.wrapper.find(".save");a.save.bind("click",function(){s(function(){window.location.reload()});
return!1});a.selected_articles_wrap=a.wrapper.find(".selected-content");for(var q=d('<ul class="selected" />'),m=0;m<h.num_articles_to_insert;m+=1){var t=d('<li><div class="droppable" /><span class="notice">Drag post here</span></li>');t.data("pos",m);a.article_list[m]=t;q.append(t)}a.selected_articles_wrap.append(q);a.replace_articles_wrap=d('<div id="wp_rp_replace_article_wrap"><div class="remove-article-sign">Drop article here to remove it</div><div class="recommendations-header"><h4 class="recommendations-title">Recommended posts</h4>'+
(h.search_support?'<form class="search" action="#"><input placeholder="search" class="search" type="text" /><input class="go button" type="submit" value="go" /></form>':'<div class="search notice">Please upgrade the plugin to use search.</div>')+'</div><div class="content"><ul></ul></div><div class="footer"><a href="http://www.zemanta.com/?ref=edit-rp" target="_blank">zemanta.com</a></div></div>');a.wrapper.append(a.replace_articles_wrap);a.replace_articles_list=a.replace_articles_wrap.find(".content ul");
a.article_loader=d('<div class="zem-loader-wrap"><div class="zem-no-articles">No results.</div><div class="zem-loader"><div class="zem-loader-step zem-loader-step-1"></div><div class="zem-loader-step zem-loader-step-2"></div><div class="zem-loader-step zem-loader-step-3"></div></div></div>');a.replace_articles_wrap.append(a.article_loader);a.remove_article_sign=a.replace_articles_wrap.find(".remove-article-sign");a.search_form=a.replace_articles_wrap.find("form.search");a.search_input=a.replace_articles_wrap.find("input.search");
a.footer=a.replace_articles_wrap.find(".footer");a.holder.bind("click",u);d(document).keydown(function(b){27==b.keyCode&&u(b)});d("html").css("overflow","hidden");n.init();(q=d(".wp_rp:first li:not(.wp_rp_special)"))&&q.each(function(b,a){a=d(a);if("own_sourcefeed"==a.data("post-type")){var e={aid:a.data("poid").split("-")[1],url:a.find("a:first").attr("href"),title:a.find("a.wp_rp_title").text(),excerpt:a.find(".wp_rp_excerpt").text(),comments:parseInt(a.find(".wp_rp_comments_count").text().replace("(",
"").replace(")",""),10),date:a.find(".wp_rp_publish_date").text(),text_preview:"",published_datetime:"",thumbnail:a.find("img").attr("src"),picked:!0,type:a.data("post-type"),pos:a.data("position")};j.push(e);k[e.aid]=e;i[b]=e}else({promoted:!0,network:!0,external:!0})[a.data("post-type")]&&r.push(b)});n.update();n.render();d("body").append(a.holder);a.replace_articles_wrap.css("width",Math.min(a.holder.width()-142,Math.max(680,110*h.num_articles_to_insert+130))+"px");return!1})};(function a(i,j){j||
(j=10,i=0);d("#wp_rp_edit_related_posts").length?r():3E4>i?setTimeout(function(){a(i+j,1.5*j)},j):d(function(){r()})})()})(jQuery);
