/* 	Author : Chris Kuzak (http://www.twitter.com/umichkuz)
	Note: this plugin is a derivative work of the project code detailed below. 
	Derivative in this case implies that the code was inspired by (but is not)
	a direct copy. 
	
	jquery.related-posts-v1.js
	
	--- 
    Author : Mike @ moretechtips.net
	Blog : http://www.moretechtips.net
	Project: http://code.google.com/p/blogger-related-posts
	Cdefyright 2009 [Mike@moretechtips.net] 
	
	Licensed under the Apache License, Version 2.0 
	(the "License"); you may not use this file except in compliance with the License. 
	You may obtain a cdefy of the License at http://www.apache.org/licenses/LICENSE-2.0 
	Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
*/
(function(f){function q(a,e){var b=[],c;for(c in a)a.hasOwnProperty(c)&&b.push({key:c,value:a[c]});b.sort(function(a,b){return e(a.value,b.value)});return b}var a={blogURL:"http://blog.kuzakscloset.com",thumbDef:"https://lh3.googleusercontent.com/_KbuqgqVElho/TZPkyOyLMYI/AAAAAAAADiI/YGHxDGTFZ7E/s108/kuzak_s_closet.png",thumbSz:"s108-c",maxPosts:4,maxTags:10,maxPostsPerTag:5,containerSelector:".related-posts-widget",loadingText:"",loadingClass:"rpw",relevantTip:"",relatedTitle:"Related Posts",recentTitle:"Recent Posts",
onLoad:!0,onFeedEntryLoad:null};f.fn.relatedPosts=function(a){if(i[a])return i[a].apply(this,Array.prototype.slice.call(arguments,1));if("object"===typeof a||!a)return i.init.apply(this,arguments);f.error("Method "+a+" does not exist on jQuery.relatedPosts")};var n=0,k=null,l={},m={},j={},o={},i={init:function(h){a=f.extend({},a,h);k=f(this);if(!a.tags)a.tags=[],f("div .post,.hentry").each(function(){var e=f(this),b=f.trim(f(e).children("a:first-child").attr("name")),c=f(".post-title,.entry-title",
e).children("a").attr("href");o[b]=c;l[b]=[];f('.post-footer-line-2 a[rel="tag"]:lt('+a.maxTags+")",e).each(function(){var c=f.trim(f(this).text().replace(/\n/g,""));-1==f.inArray(c,a.tags)&&a.tags.push(c);l[b].push(c)})});if(0!=a.tags.length||a.recentTitle){a.relatedTitle&&f("<h2>"+a.relatedTitle+"</h2>").appendTo(k);a.loadingText&&f('<div id="related-posts-loadingtext">'+a.loadingText+"</div>").appendTo(k);f("<ul "+(a.loadingClass?'class="'+a.loadingClass+'"':"")+"></ul>").appendTo(k);for(h=0;h<
a.tags.length;h++)f.ajax({url:a.blogURL+"/feeds/posts/summary/",data:{category:a.tags[h],"max-results":a.maxPostsPerTag,alt:"json-in-script"},success:r,dataType:"jsonp",cache:!0})}}},r=function(h){n++;if(h.feed.entry)for(var e=0;e<h.feed.entry.length;e++){for(var b=h.feed.entry[e],c="",g="",d=0;d<b.category.length;d++)if(-1<a.tags.indexOf(b.category[d].term)){g=b.category[d].term;break}for(d=0;d<b.link.length;d++)if("alternate"==b.link[d].rel){c=b.link[d].href;break}var d=b.title.$t,b=b.media$thumbnail?
b.media$thumbnail.url:a.thumbDef,i=b.replace(/\/s72-c\//,"/s640/");a.thumbSz&&"s72-c"!=a.thumbSz&&(b=b.replace(/\/s72-c\//,"/"+a.thumbSz+"/"));m[c]||(m[c]={t:d,thumb:b,img:i});location.href.toLowerCase()!=c.toLowerCase()&&(j[g]||(j[g]=[]),-1==j[g].indexOf(c)&&j[g].push(c))}if(n>=a.tags.length){h=0;for(pid in l){h++;e={};for(g=0;g<l[pid].length;g++)if(b=l[pid][g],j[b])for(c=0;c<j[b].length;c++)d=j[b][c],o[pid]!=d&&(e[d]||(e[d]=0),e[d]+=1);e=q(e,s);for(g=0;g<e.length&&g<a.maxPosts;g++){var b=c=e[g].key,
d=m[c].t,c=m[c].thumb,i=f("#p-"+pid+" ul"),p="";c&&(p='<img src="'+c+'" title="'+d+'" border="0">');i.append('<li><a rel="related" href="'+b+'" title="'+d+'">'+p+"<strong>"+d+"</strong></a></li>")}}f("#related-posts-loadingtext",k).remove()}},s=function(a,e){return e-a}})(jQuery);
