// FreshTags v0.5
// Tag-Driven Blog Navigation
// www.greg-hill.id.au 2006
// This work is licensed under a Creative Commons Attribution-ShareAlike 2.1 Australia License.
// URL: http://ghill.customer.netspace.net.au/freshtags/

if(typeof(Delicious) == 'undefined') 	Delicious = {};Delicious.all_tags = Delicious.tags;if (archive)Delicious.arch_posts = Delicious.posts;Delicious.posts=false;anchor = anchor.replace(/ +/g,'+');defs = defs.replace(/ +/g,'+');if (!maxposts)	maxposts = 100;curr_tags = fetchTags();if(defs && !curr_tags)curr_tags = defs;if (curr_tags) {if (del_user){writeScript('http://del.icio.us/feeds/json/'+del_user+'/'+anchor+'+'+curr_tags+'?count='+maxposts);writeScript('http://del.icio.us/feeds/json/tags/'+del_user+'/'+anchor+'+'+curr_tags+'?count=100&sort=freq');}else writeScript('http://del.icio.us/feeds/json/tag/'+anchor+'+'+curr_tags+'?count='+maxposts);}function listTags(type, rows, prompt, cust_html){var outputHTML; if (!type) type="drop";  if (!rows) rows=0;if (type=="custom" && !cust_html) type="drop";del_plug = '<a class="del_plug" href="http://del.icio.us/'+del_user+'/'+anchor+'+'+curr_tags+'"><img src="http://del.icio.us/static/img/delicious.small.gif" alt="delicious"></a><br/>'; delete Delicious.tags[anchor];delete Delicious.all_tags[anchor];switch(type){case "custom": raw_html=cust_html.split('@');break;case "flat": raw_html=['', '<a class="showtag" href="!url!">!tag! (!count!)</a> ', '  '+del_plug, '<a class="showtag" href="!murl!"><strong>!tag! (!count!)</strong></a> '];break;case "flat-multi": raw_html=['', '<a class="showtag" href="!murl!">!tag! (!count!)</a> ', '  '+del_plug, '<a class="showtag" href="!murl!"><strong>!tag! (!count!)</strong></a> '];break;case "list": raw_html=['<ul class="showtag">', '<li class="showtag"><a class="showtag" href="!url!">!tag! (!count!)</a></li>',del_plug+'</ul>', '<li class="showtag"><a href="!murl!"><strong>!tag! (!count!)</strong></a></li>'];break;case "list-multi": raw_html=['<ul class="showtag">', '<li class="showtag">[<a class="showtag" href="!murl!">+</a>]&nbsp;<a class="showtag" href="!url!">!tag! (!count!)</a></li>',del_plug+'</ul>', '<li class="showtag">[<a class="showtag" href="!murl!">--</a>]&nbsp;<a href="!url!"><strong>!tag! (!count!)</strong></a></li>'];break;case "scroll": raw_html=['<select class="showtag" multiple name="multimenu">', '<option class="showtag" value="!tag!">!tag! (!count!)</option>', '</select><br/><input class="showtag" type="button" value="Go" onClick="clickScroll();">&nbsp;&nbsp;'+del_plug, '<option class="showtag" selected value="!tag!">&gt;&gt;!tag! (!count!)</option>'];break;case "drop": raw_html=['<select class="showtag" name="dropmenu" size="1" onChange="document.location.href=makeURL(this.options[this.selectedIndex].value);">', '<option class="showtag" value="!tag!">!tag! (!count!)</option>', '</select>&nbsp;'+del_plug, '<option class="showtag" selected value="!tag!">&gt;&gt;!tag! (!count!)</option>'];break;case "sub": raw_html=['[ ', '!tag! (!count!) ', ']', '<strong>!tag! !count!</strong> '];break;default: alert("Bad type in listTags: "+type);}outputHTML = raw_html[0];if (prompt && type=="drop")outputHTML += '<option selected value="">'+prompt+'</option>';var loop=0;var taglist=curr_tags.split('+');for(var i in Delicious.all_tags){loop++;template = raw_html[1];for(var j=0; j<taglist.length; j++)if (taglist[j].toLowerCase()==i.toLowerCase()){template = raw_html[3]; break;}template=template.replace(/!tag!/gi, i);template=template.replace(/!url!/gi, makeURL(i) );template=template.replace(/!murl!/gi, makeURL(taggle(i)) );c=Delicious.tags[i];a=Delicious.all_tags[i];if (!a || a==0){if (!c || c==0 || !curr_tags || c==a)cLabel = "";else cLabel = c;}else{if (!c || c==0 || !curr_tags || c==a)cLabel = a;else cLabel = c+'/'+a;}template=template.replace(/!count!/gi, cLabel);template=template.replace(/\(\)/gi,'');outputHTML+=template;if(loop>=rows && rows!=0)break;}outputHTML+=raw_html[2];return outputHTML;}function listTitles(type, rows, prompt, source, cust_html){var outputHTML=""; if (typeof(navbar = document.forms["b-search"]) !='undefined')navbar.as_q.value=curr_tags;var css_label='\"showtitle\"';if (!type) type="list";if (!rows) rows=0;if (type=="custom" && !cust_html) type="list";if (!source) source="posts";switch(source){case "posts": entry = Delicious.posts; break;case "archive": css_label='\"showarch\"'; if (archive) entry = Delicious.arch_posts; else { alert("No archive"); return ''; }break;default: alert("Bad source in listTitles: "+source);}if (!entry || entry.length==0) return '';if (rows==0) rows=entry.length;rows=Math.min(rows, entry.length);switch(type){case "custom": raw_html=cust_html.split('@');break;case "flat": raw_html=['', '<a class='+css_label+' href="!url!">!title!</a> ', ''];break;case "list": raw_html=['<ul class='+css_label+'>', '<li class='+css_label+'><a class='+css_label+' href="!url!">!title!</a></li>','</ul>'];break;case "scroll": raw_html=['<select class='+css_label+' name="multilist" size="'+rows+'" onChange="document.location.href=this.options[this.selectedIndex].value">', '<option class='+css_label+' value="!url!">!title!</option>', '</select>'];break;case "drop": raw_html=['<select class='+css_label+' name="droplist" size="1" onchange="document.location.href=this.options[this.selectedIndex].value">', '<option class='+css_label+' value="!url!">!title!</option>', '</select>'];break;case "sub": raw_html=['[ ', '!title!, ', ']'];break;default: alert("Bad type in listTitles: "+type);}outputHTML+=raw_html[0];if (prompt && type=="drop")outputHTML+='<option selected value="">'+prompt+'</option>';for(var i=0; i<entry.length; i++){descr = tidy(entry[i].d);if (source=="archive") descr +=' ('+entry[i].n+')'; url = entry[i].u;if (curr_tags)url=makeURL(curr_tags, url);template = raw_html[1];template=template.replace(/!title!/i, descr);template=template.replace(/!url!/i, url);outputHTML+=template;if(i>=rows-1 && rows!=0)break;}outputHTML+=raw_html[2];return outputHTML;}function tidy(descr){pos = descr.indexOf(':');if(pos==-1) return descr;return descr.substr(pos+1);}function list_post_tags(post_tags){var selected, c, tags=post_tags.split(' ');document.write('<td align="center"><small>[&nbsp;');for(var i=0;i<tags.length;i++){rexp = new RegExp( tags[i], 'i' );selected = (curr_tags.search(rexp)>=0);if (selected)document.write('<strong>');document.write('&nbsp;&nbsp;'+tags[i]);if (del_user) c = Delicious.all_tags[tags[i]];if (c && c>0)document.write('('+c+')');if (selected)document.write('</strong>');}document.write('&nbsp;&nbsp;&nbsp;]&nbsp;&nbsp;');var keep_curr_tags = curr_tags;curr_tags=post_tags.replace(/ +/gi,'+');document.write('<a href="'+makeURL(curr_tags)+'">&gt;&gt;</a></small></td>');curr_tags = keep_curr_tags;return;}function fetchTags( source, names ){var ref=document.referrer.indexOf('?');var ref_path=document.referrer;var ref_query='';if (ref>0){ref_path=document.referrer.substring(0,ref+1);ref_query=unescape(document.referrer.substr(ref));}if (!source && names)return '';if (!source)return fetchTags( location.search, ['tags', 'tag', 'cat', 'label'] ) || fetchTags( ref_query, ['tags', 'q', 'p', 'tag', 'cat', 'query', 'search', 'topics', 'topic', 'label']) || fetchTags( ref_path, ['tag', 'tags', 'cat', 'category', 'wiki', 'search', 'topics', 'topic'] );var peeker, i, tag;for( i=0; i<names.length; i++ ){if (source.indexOf('http:')==0)peeker = new RegExp( '[/]'+ names[i] +'[/]([^&/?]*)', 'i' );else peeker = new RegExp( '[?&]'+ names[i] +'[=]([^&]*)', 'i' );if( (tag = peeker.exec( source )) )return filterTags(unescape( tag[1] ) );}return '';}function archive_count(arch_URL){for(var i=0; i<Delicious.arch_posts.length; i++){post = Delicious.arch_posts[i];url= post.u;note = post.n;if (url==arch_URL)return note;}return;}function show_count(count){if (count>0)document.write(' ('+count+')');return;}function writeScript( src ){  document.write( '<script type="text/javascript" src="'+ src +'"><\/script>' );}function taggle(new_tag){var present=false, tagstring='', old_tags = curr_tags.split('+');for(var i=0; i<old_tags.length; i++)if(old_tags[i].toLowerCase()!= new_tag.toLowerCase() )tagstring+=old_tags[i]+'+';else present=true;if (!present) tagstring+=new_tag;tagstring=tagstring.replace(/\++/gi, '+');tagstring=tagstring.replace(/^\+/gi, '');tagstring=tagstring.replace(/\+$/gi, '');return tagstring;}function makeURL(tags, url){var pos;if(!url) url = document.location.href;if (url.indexOf('?')==-1) url+="?";pos = url.search(/tags=/i);if (pos==-1) {url+="&";pos=url.length;}endpos=url.indexOf('&',pos);if (endpos==-1) endpos=url.length;url=url.substr(0,pos)+"tags="+tags+url.substr(endpos);return url;}function clickScroll(){var menus=document.getElementsByName('multimenu');var len = menus.length;var tagstring="";for (var i=0; i<len; i++)for (var j=0; j<menus[i].options.length; j++)if (menus[i].options[j].selected)tagstring+=menus[i].options[j].value+"+";tagstring=tagstring.replace(/\+$/gi, '');document.location.href=makeURL(tagstring);return;}function filterTags( tags ){tags=tags.replace(/[+]/g, ' ');var black = 'also another could every find from have here into I just many more most much next only really same should show still such that their them then there these they thing this those very well were what when where which while will with without would'; var white = 'art bbc css diy irc job fun law log mac map net osx pda php rdf rss tag tax tv web win xml';var match = [], i, rexp, stop;tags = tags.replace(/[!?\"#]/g,'');if (del_user){for(i in Delicious.all_tags )if( (new RegExp('\\b('+ i +')\\b', 'i')).test(tags) )match.push(i);return match.join('+');}rexp = new RegExp('\\b([a-z]{1,3})\\b','g'); if (tags.match(rexp))black += ' '+tags.match(rexp).join(' ');rexp = new RegExp('\\b('+white.replace(/ +/g,'|')+')\\b','ig');black = black.replace(rexp,'');stop = new RegExp('\\b('+black.replace(/ +/g,'|')+')\\b', 'ig');tags = tags.replace(stop, '').replace(/ +/g, '+');return tags.replace(/^\+|\+$/g, '');}