// FreshTags v0.6
// 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/

Delicious={};delFeed='http://feeds.delicious.com/v2/json/';loadHTML='<center><img src="http://ghill.customer.netspace.net.au/snake_transparent.gif" /><br>Please wait<br/>loading <a href="http://ghill.customer.netspace.net.au/FreshTags/">FreshTags<a> ...</center>';anchor=anchor.replace(/\s+/g,'+');defs=defs.replace(/\s+/g,'+');if(typeof(maxposts)=='undefined'||!maxposts)maxposts=20;if(typeof(sort)=='undefined'||!sort)sort='freq';if(typeof(cloudmax)=='undefined'||!cloudmax)cloudmax=200;if(typeof(cloudmin)=='undefined'||!cloudmin)cloudmin=80;if(typeof(peeksize)=='undefined'||!peeksize)peeksize=100;curr_tags=fetchTags();if(defs&&!curr_tags)curr_tags=defs;if(typeof(tagsDiv)=='undefined'||!tagsDiv)tagsDiv='FT-tags';if(typeof(postDiv)=='undefined'||!postDiv)tagsDiv='FT-post';if(typeof(archDiv)=='undefined'||!archDiv)tagsDiv='FT-arch';if(del_user)fetchFeed(delFeed+'tags/'+del_user+'/'+anchor+'?sort='+sort+'&count=100','initTags');function initTags(tagObj){Delicious.all_tags=tagObj;return;}function fetchFeed(targ, cb){var s=document.createElement('script');s.type='text/javascript';if(cb)s.src=targ+"&callback="+cb;else s.src=targ;document.getElementsByTagName("head")[0].appendChild(s);return;}function doFreshTags(){curr_tags=curr_tags.replace(/\s+/ig,'+');if(del_user){var d=document.getElementById(tagsDiv);if(d)d.innerHTML=loadHTML;fetchFeed(delFeed+'tags/'+del_user+'/'+anchor+'+'+curr_tags+'?sort='+sort+'&count=100', 'makeTags');}d=document.getElementById(archDiv);if(d&&d.innerHTML.length==0&&archive){d.innerHTML=loadHTML;fetchFeed(delFeed+del_user+'/'+archive+'?count=100','makeArch');}rewriteURLs();if(FT_tags.type.search('nest')>=0)return;if(!curr_tags)return;d=document.getElementById(postDiv);if(d)d.innerHTML=loadHTML;if(del_user)fetchFeed(delFeed+del_user+'/'+anchor+'+'+curr_tags+'?count='+maxposts, 'makePost');else fetchFeed('http://xoxotools.ning.com/outlineconvert.php?output=json&classes=post&url='+encodeURIComponent('http://del.icio.us/rss/tag/'+anchor+'+'+curr_tags+'?count=100'),'makeGeneric');return;}function makeTags(tagsObj){Delicious.tags=tagsObj;var h=listTags(FT_tags.type, FT_tags.rows, FT_tags.prompt, FT_tags.cust);var d=document.getElementById(tagsDiv);d.innerHTML=h;if(FT_tags.type.search('nest')>=0)if(del_user)fetchFeed(delFeed+del_user+'/'+anchor+'+'+curr_tags+'?count='+maxposts,'makePost');else fetchFeed('http://xoxotools.ning.com/outlineconvert.php?output=json&classes=post&url='+delFeed+anchor+'+'+curr_tags+'?sort='+sort+'&count=100','makeGeneric');return;}function makePost(postObj){Delicious.posts=postObj;var h=listTitles(FT_post.type,FT_post.rows,FT_post.prompt,'posts',FT_post.cust);var d=document.getElementById(postDiv);d.innerHTML=h;return;}function makeArch(archObj){Delicious.arch_posts=archObj;var h=listTitles(FT_arch.type,FT_arch.rows,FT_arch.prompt,'archive',FT_arch.cust);var d=document.getElementById(archDiv);d.innerHTML=h;if(typeof(archURL)!='undefined')doNav();return;}function makeGeneric(genericObj){if(!genericObj.post){makePost({});return;}var q,p=genericObj.post.toSource();p=p.replace(/link:/ig,'\"u\":');p=p.replace(/title:/ig,'\"d\":');p=p.replace(/description:/ig,'\"n\":');eval('q='+p);makePost(q);return;}function listTags(type,rows,prompt,cust_html){var outputHTML;if(!del_user)return '';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":if (cust_html.length==4) raw_html=cust_html; else alert('Bad custom type in ListTags:'+cust_html);break;case "flat":raw_html=['','<a class="showtag" href="!url!">!tag! (!count!)</a> ','  '+del_plug,'<a class="showtag-select" href="!murl!">!tag! (!count!)</a> '];break;case "flat-cloud":raw_html=['<p align="justify">','<a class="showtag" href="!url!" style="font-size:!fs!%;" title="!count!">!tag!</a> ','</p>'+del_plug,'<a class="showtag-select" href="!murl!" style="font-size:!fs!%;" title="!count!">!tag!</a> '];break;case "flat-multi":raw_html=['','<a class="showtag" href="!murl!">!tag! (!count!)</a> ','  '+del_plug,'<a class="showtag-select" href="!murl!">!tag! (!count!)</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-select"><a href="!murl!">!tag! (!count!)</a></li>'];break;case "list-cloud":raw_html=['<ul class="showtag">','<li class="showtag"><a class="showtag" href="!url!"  style="font-size:!fs!%;" title="!count!">!tag!</a></li>',del_plug+'</ul>','<li class="showtag-select"><a href="!murl!" style="font-size:!fs!%;" title="!count!">!tag!</a></li>'];break;case "list-nest":raw_html=['<ul class="showtag">','<li class="showtag"><a class="showtag" href="!url!">!tag! (!count!)</a></li>',del_plug+'</ul>','<li class="showtag-select"><a href="!murl!">!tag! (!count!)</a><div id="'+postDiv+'"></div></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-select">[<a href="!murl!">&ndash;</a>]&nbsp;<a class="showtag-select" href="!url!">!tag! (!count!)</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-select" selected value="!tag!">!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-select" selected value="!tag!">!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('+');var cMax=0,cMin=10000;for(var i in Delicious.all_tags){var a=Delicious.all_tags[i];if(a>cMax)cMax=a;if(a<cMin)cMin=a;}var scale=(cloudmax-cloudmin)/(cMax-cMin);var offset=cloudmax -(scale*cMax);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);var fs=scale*a + offset;template=template.replace(/!fs!/gi,Math.round(fs));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":if(cust_html.length==3)raw_html=cust_html;else alert('Bad custom type in ListTitles:'+cust_html);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;case "peek":raw_html=['<ul class='+css_label+'>','<li class='+css_label+'><a class='+css_label+' href="!url!">!title!</a><div class="notes-'+css_label+'">!notes!</div></li>','</ul>'];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+')';notes=entry[i].n;if(!notes)notes='';if(peeksize>0){notes=notes.substr(0,peeksize);var p=notes.lastIndexOf('\s');notes=notes.substr(0,p);}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);template=template.replace(/!notes!/i,notes);outputHTML+=template;if(i>=rows-1&&rows!=0)break;}outputHTML+=raw_html[2];return outputHTML;}function tidy(descr){if(!descr)return descr;pos=descr.indexOf(':');if(pos==-1)return descr;return descr.substr(pos+1);}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 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 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){var cmd='javascript:curr_tags=%22'+tags+'%22;doFreshTags%28%29;';return cmd;}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);url=url.replace('?&','?');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(/\s+/g,'|')+')\\b','ig');black=black.replace(rexp,'');stop=new RegExp('\\b('+black.replace(/\s+/g,'|')+')\\b','ig');tags=tags.replace(stop,'').replace(/\s+/g,'+');return tags.replace(/^\+|\+$/g,'');}function rewriteURLs(tags){if(!tags)tags=curr_tags;var l=document.links;for(var i=0;i<l.length;i++)if(l[i].href.search(/^http:\/\/.+?.*tags=/ig)>=0)l[i].href=makeURL(tags,l[i].href);l=document.getElementsByTagName('option');for(var i=0;i<l.length;i++)if(l[i].value.search(/^http:\/\/.+?.*tags=/ig)>=0)l[i].value=makeURL(tags,l[i].value);return;}
