// delicious2Blogger Category Scripts
// Home at http://phydeaux3.blogspot.com
// This work is licensed under a Creative Commons Attribution-ShareAlike 2.5  License.
// http://creativecommons.org/licenses/by-sa/2.5/
// Updated May 09 2006


// Global Variables
// Don't Change Anything Past This Point
var catSplit=/\s?,\s?/g;


// Writes out the source for scripts - Using evil document.writes

// Tags
// Check for Comman Tags and Throttle
if(delCommonTag =='false'){ctwrite = '';}
else {ctwrite = '\/'+delCommonTag;}
if(typeof delThrottleTag == 'undefined'){delThrottleTag = false;}
if(!delThrottleTag){tThrottle = '';}
if(delThrottleTag){tThrottle = '&count='+delThrottleTag;}

document.write('<script type=\"text\/javascript\" src=\"http:\/\/del.icio.us\/feeds\/json\/tags\/'+delUserName+ctwrite+'?sort=alpha'+tThrottle+'\"><\/script>');


// Posts
var pageCheck = decodeURIComponent(document.location);
var d2bCurrent = pageCheck.split('?');
if(d2bCurrent[0] == delTagPost){

     enTag = encodeURIComponent(d2bCurrent[1]);
  if(delCommonTag == 'false'){
       document.write('<script type=\"text\/javascript\" src=\"http:\/\/del.icio.us/feeds/json/'+delUserName+'/'+enTag+'?count=100;\"><\/script>');
       }
     else {
       document.write('<script type=\"text\/javascript\" src=\"http:\/\/del.icio.us/feeds/json/'+delUserName+'/'+enTag+'\+'+delCommonTag+'?count=100;\"><\/script>');
      }
  }





// Writes out the Selected Category in the Category Post

function d2bWriteCat() {
         stripTag = d2bCurrent[1].replace(/_/g,' ');
         document.title = d2bPostCatHeading+stripTag
         catTarget = document.getElementById('del-container')
         if(document.getElementById('delpost-title')){
            document.getElementById('delpost-title').innerHTML = d2bPostCatHeading+stripTag;
           }
         if(document.getElementById('delpost-date')){
            document.getElementById('delpost-date').style.display = 'none';
           }
            if(delShowRSS){
               dcatHeading = document.createElement('h3');
               ar = document.createElement('a');
               if(delCommonTag == 'false'){
                  ar.href = 'http://del.icio.us/rss/'+delUserName+'/'+enTag;
                  }
               else {
                  ar.href = 'http://del.icio.us/rss/'+delUserName+'/'+encodeURIComponent(delCommonTag)+'\+'+enTag;
                  }
               ar.title = 'Subscribe to this Feed';
               img = document.createElement('img');
               img.src = delRSSImg;
               ar.appendChild(img);
               dcatHeading.appendChild(ar);
               entryCount = Delicious.posts.length;
               dtxt = document.createTextNode(delCatShowText+entryCount+delCatEntryText);
               dcatHeading.appendChild(dtxt);
               dcatHeading.className = 'catPostHeading';
               catTarget.appendChild(dcatHeading);
               }
         ul = document.createElement('ul');
         ul.className='delicious-posts';
         for (var i=0, post; post = Delicious.posts[i];i++){
              var li = document.createElement('li');
              li.className='delTitle';
              a = document.createElement('a');
              a.setAttribute('href',post.u);
              a.appendChild(document.createTextNode(post.d));
              li.appendChild(a);
              ul.appendChild(li);
              if(delShowNotes){
                  if(post.n){
                     var liNote = document.createElement('li');
                     liNote.className = 'delNote';
                     liNote.innerHTML = post.n;
                     liNote.innerHTML += delNoteAppend;
                     if(delPostMore){
                        am = document.createElement('a');
                        am.setAttribute('href',post.u);
                        am.title = post.d;
                        amt = document.createTextNode(delPostMoreText)
                        am.appendChild(amt);
                        liNote.appendChild(am);
                        }
                     ul.appendChild(liNote);
                     }
                 }
               if(delShowTags){
                       var d2bSkip = false;
                       li2=document.createElement('li');
                       li2.className='small-del-tags';
                       for (p = 0; p < post.t.length; p++){
                        var tags = post.t[p];
                        strip = tags.split(',');

                        for(r = 0; r < strip.length; r++){
                           if(strip[r] == delCommonTag){
                              d2bSkip = true;
                              continue;
                             }
                           if(d2bSkip){
                              if( p > 1){
                                dsep = document.createTextNode(delSep);
                                li2.appendChild(dsep);
                               }
                             }
                           else if( p > 0 ){
                              dsep = document.createTextNode(delSep);
                              li2.appendChild(dsep);
                            }
                            stripper=strip[r].replace(/_/g,' ');
                            a2 = document.createElement('a');
                            a2.className = 'small-del-link';
                            a2.setAttribute('href', delTagPost+'?'+strip[r]);
                            a2.title = delTagLinkText+stripper;
                            a2.appendChild(document.createTextNode(stripper));
                            li2.appendChild(a2);
                           }
                   }
                ul.appendChild(li2);
               }
             }
         catTarget.appendChild(ul);
       }

// Normal Tag List in the Sidebar

function d2bWriteList(sortMode){
   var ul = document.createElement('ul');
   ul.className = 'linklist';
   if(sortMode == 'alpha'){
      for (p in Delicious.tags){
          tagURL = encodeURIComponent(p);
          var strip = p.replace(/_/g,' ');
          if(strip == delCommonTag){
             continue;
            }
          li = document.createElement('li');
	  a = document.createElement('a');
          a.href=delTagPost+'?'+tagURL;
          a.title=delTagLinkText+strip;
          atxt = document.createTextNode(strip);
          a.appendChild(atxt);
          if(showCountList){
             span = document.createElement('span');
             span.className = 'delicious-tag-count';
             stxt = document.createTextNode('('+Delicious.tags[p]+')');
             span.appendChild(stxt);
             a.appendChild(span);
            }
          li.appendChild(a);
          ul.appendChild(li);
         }
       document.getElementById('d2bList').appendChild(ul);
      }
   if(sortMode == 'freq'){
       delSort = new Array();
       for(i in Delicious.tags){
           delSort[delSort.length] = [i,Delicious.tags[i]];
           }
       delSort.sort(mysortfn);
       for(i = delSort.length-1; i >=0; i--){
           tagURL =encodeURIComponent(delSort[i][0]);
           strip = delSort[i][0].replace(/_/g,' ');
           if(strip == delCommonTag){
              continue;
             }
           li = document.createElement('li');
           a = document.createElement('a');
           a.href=delTagPost+'?'+tagURL;
           a.title=delTagLinkText+strip;
           atxt = document.createTextNode(strip);
           a.appendChild(atxt);
           if(showCountList){
              span = document.createElement('span');
              span.className = 'delicious-tag-count';
              stxt = document.createTextNode('('+delSort[i][1]+')');
              span.appendChild(stxt);
              a.appendChild(span);
             }
           li.appendChild(a);
           ul.appendChild(li);
          }
        document.getElementById('d2bList').appendChild(ul);
      }
  }




// Creates the Tag Cloud

// The following function taken directly from the guts of the Delicious Cloud
function s(a,b,i,x){
      if(a>b){
          var m=(a-b)/Math.log(x),v=a-Math.floor(Math.log(i)*m)
             }
      else{
          var m=(b-a)/Math.log(x),v=Math.floor(Math.log(i)*m+a)
          }
      return v
   }

function d2bWriteCloud(sortMode){
// ta is for Number of Items Displayed - First Slider on Delicious Tag Setup Page
// Keep it at 0 to show all tags
var ta=0
var c=[];
// Make some arrays to get the number of matching hits ( the spread )
// Spread goes to tz
var delCount = new Array();
for (p in Delicious.tags){
     if (!delCount[Delicious.tags[p]]){
           delCount[Delicious.tags[p]] = new Array(Delicious.tags[p])
           }
      }
tz = delCount.length-1;
del2 = document.getElementById('d2bCloud');
ul = document.createElement('ul');
ul.className = "delicious-cloud";
if(sortMode == 'alpha'){
for(var t in Delicious.tags){
    for (var i=0;i<3;i++) {
             c[i]=s(minColor[i],maxColor[i],Delicious.tags[t]-ta,tz)
              }
// Strip out the underscores for the Displayed Text         
         var strip = t.replace(/_/g,' ');
         if(strip == delCommonTag){
           continue;
           }
         var fs = s(minFontSize,maxFontSize,Delicious.tags[t]-ta,tz);
         li = document.createElement('li');
         li.style.fontSize = fs+'px';
         li.style.lineHeight = "1";
         a = document.createElement('a');
         a.title = delTagLinkText+strip;
         a.style.color = 'rgb('+c[0]+','+c[1]+','+c[2]+')';
// Point the links at the Category Post
         a.href = delTagPost+'?'+encodeURIComponent(t);
         if (showCountCloud){
             span = document.createElement('span');
             span.innerHTML = '('+Delicious.tags[t]+') ';
             span.className = 'delicious-tag-count';
             a.appendChild(document.createTextNode(strip));
             li.appendChild(a);
             li.appendChild(span);
             }
         else {
             a.appendChild(document.createTextNode(strip));
             li.appendChild(a);
             }
       
         ul.appendChild(li);
         abnk = document.createTextNode(' ');
         ul.appendChild(abnk);
    }
  del2.appendChild(ul);
  }
if(sortMode == 'freq'){
   var delSort = new Array();
    for(p in Delicious.tags){
       delSort[delSort.length] = [p,Delicious.tags[p]];
       }
    delSort.sort(mysortfn);
    for (t = delSort.length-1; t >=0; t--){
         for (var i=0;i<3;i++) {
             c[i]=s(minColor[i],maxColor[i],delSort[t][1]-ta,tz)
              }
// Strip out the underscores for the Displayed Text         
         strip = delSort[t][0].replace(/_/g,' ');
         if(strip == delCommonTag){
           continue;
           }
         fs = s(minFontSize,maxFontSize,delSort[t][1]-ta,tz);
         li = document.createElement('li');
         li.style.fontSize = fs+'px';
         li.style.lineHeight = "1";
         a = document.createElement('a');
         a.title = delTagLinkText+strip;
         a.style.color = 'rgb('+c[0]+','+c[1]+','+c[2]+')';
// Point the links at the Category Post
         a.href = delTagPost+'?'+encodeURIComponent(delSort[t][0]);
         if (showCountCloud){
             span = document.createElement('span');
             span.innerHTML = '('+delSort[t][1]+') ';
             span.className = 'delicious-tag-count';
             a.appendChild(document.createTextNode(strip));
             li.appendChild(a);
             li.appendChild(span);
             }
         else {
             a.appendChild(document.createTextNode(strip));
             li.appendChild(a);
             }
         ul.appendChild(li);
         abnk = document.createTextNode(' ');
         ul.appendChild(abnk);
    }
  del2.appendChild(ul);
  }

}

// Makes a Drop-down menu for the Categories

  function d2bWriteSelect(sortMode){
   var sel = document.createElement('select');
      sel.style.width = "95%";
      sel.onchange = function(){location.href=this.options[this.selectedIndex].value;};
      sel.options[0] = new Option(d2bSelectHeading,'/');
      q = 1;
      if(sortMode == 'alpha'){
         for (p in Delicious.tags){
             tagURL =encodeURIComponent(p);
             strip = p.replace(/_/g,' ');
             if(strip == delCommonTag){
                continue;
               }
             otext = strip +' ('+Delicious.tags[p]+')';
             ourl = delTagPost+'?'+tagURL;
             sel.options[q] = new Option(otext,ourl);
             q++;
            } 
        }
      if(sortMode == 'freq'){
         delSort = new Array();
         for(i in Delicious.tags){
             delSort[delSort.length] = [i,Delicious.tags[i]];
             }
         delSort.sort(mysortfn);
         for(i = delSort.length-1; i >=0; i--){
             tagURL =encodeURIComponent(delSort[i][0]);
             strip = delSort[i][0].replace(/_/g,' ');
             if(strip == delCommonTag){
                continue;
                }
             otext = strip +' ('+delSort[i][1]+')';
             ourl = delTagPost+'?'+tagURL;
             sel.options[q] = new Option(otext,ourl);
             q++;
            } 
         }              
    document.getElementById('d2bSelect').appendChild(sel);
  }

  

// Writes out link to category in post footer or header

   function d2bWritePostCat(bIN, bTgs){
          cat = document.getElementById('cat-'+bIN);
          itemTags = bTgs;
          if(itemTags){
             cat.innerHTML = d2bPostCatHeading;
             linkTags = itemTags.split(catSplit);
             for (i = 0; i < linkTags.length; i++){
                 if (i > 0){
                    cSep = document.createTextNode(delSep);
                    cat.appendChild(cSep);
                    }
                 strip = linkTags[i].replace(/ /g,'_');
                 encodeTags=encodeURIComponent(strip);
                 a = document.createElement('a');
                 a.href = delTagPost+'?'+encodeTags;
                 a.className = 'small-del-link';
                 a.title = delTagLinkText+linkTags[i];
                 tx = document.createTextNode(linkTags[i]);
                 a.appendChild(tx);
                 cat.appendChild(a);
                 }
               }
          else {
               cat.style.display = 'none';
               }
        }
// Writes Out Post to Delicious Admin Link

function d2bAdminWrite(adN,adTgs,adTitle,adURL){
    d2bAdmin = document.getElementById('d2bAdmin-'+adN);
    var itemTags = adTgs;
    var urlTags = '';
    var linkTags = itemTags.split(catSplit);
    for (i = 0; i < linkTags.length; i++){
        var strip = linkTags[i].replace(/ /g,'_');
        urlTags +=strip+' ';
        }
    if(delCommonTag != 'false'){
        urlTags += delCommonTag;
        }
    encoTags = encodeURIComponent(urlTags);
    var postTitle = adTitle;
    urlTitle = encodeURIComponent(postTitle);
    a = document.createElement('a');
    tx = document.createTextNode('Delicious');
    a.appendChild(tx);
    a.href = "http://del.icio.us/"+delUserName+"?url="+adURL+"&amp;title="+urlTitle+"&amp;tags="+encoTags;
    a.title = "Post to Delicious";
    d2bAdmin.appendChild(a);
  }

// Cloak Recent Posts UL
function d2bCloakRecentUL(){
      rec = document.getElementById('recently').getElementsByTagName('a');
      for(i=0; i < rec.length; i++){
          if(rec[i].href == delTagPost){
             rec[i].parentNode.style.display = 'none';
             }
          }
       }

// Cloak Recent Posts Select
function d2bCloakRecentSelect(){
      rec = document.getElementById('posthide');
      for(i=0; i < rec.options.length; i++){
            value = rec.options[i].value;
            if(value == delTagPost){
                  rec.options[i] = null;
               }
           }
        }

// Cloak Archive UL
function d2bCloakArchiveUL(){
          arc = document.getElementById('archive-list').getElementsByTagName('a');
          for(i=0; arc.length > i; i++){
              if(arc[i].href == delArchivePost){
                 arc[i].parentNode.style.display = 'none';
                }
             }
          }

// Cloak Archive Select 
function d2bCloakArchiveSelect(){
      rec = document.getElementById('archivehide');
      for(i=0; i < rec.options.length; i++){
            value = rec.options[i].value;
            if(value == delArchivePost){
                  rec.options[i] = null;
               }
             }
          }

// Cloak the Post Footer
function d2bCloakPostFooter(){
     if(d2bCurrent[0] == delTagPost){
        document.getElementById('delpost-footer').style.display = 'none';
       }
     }

// Sort Function

function mysortfn(a,b) {
  if (a[1]<b[1]) return -1;
  if (a[1]>b[1]) return 1;
  return 0;
 }


