var tumblrURL = 'http://tumblr.littlecommabig.com';

// IE still doesn't support indexOf for arrays :P 
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(searchElement /*, fromIndex */)
  {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (len === 0)
      return -1;

    var n = 0;
    if (arguments.length > 0)
    {
      n = Number(arguments[1]);
      if (n !== n) // shortcut for verifying if it's NaN
        n = 0;
      else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
    }

    if (n >= len)
      return -1;

    var k = n >= 0
          ? n
          : Math.max(len - Math.abs(n), 0);

    for (; k < len; k++)
    {
      if (k in t && t[k] === searchElement)
        return k;
    }
    return -1;
  };
}


function tumblr_callback (tumblr_data) {
	tumblr_tag(tumblr_data, 'photos');
	tumblr_tag(tumblr_data, 'comics');
	tumblr_tag(tumblr_data, 'videos');
	tumblr_tag(tumblr_data, 'news');
}

function tumblr_tag (tumblr_data, tag) {
   var loading_tag = 'loading_' + tag;
   var tag_list = tag + '_list';

   document.write(
      '<div class="tag_section" id="' + tag + '">' +
      '<div id="' + loading_tag + '">' +
      '<a href="#"><img src="images/ajax-loader.gif"> </a>' +
      '</div>' +
      '<ul id="' + tag_list + '" class="tumblr_list"></ul>' +
      '</div>'
   );

   $(tumblr_data.posts).each(function(i, post) {
      if (post["tags"]) {
         if (post["tags"].indexOf(tag) != -1) {
            switch (post["type"]) {
               case "photo":
                  link = '<em>' + post["date"] + '</em>' + post["photo-caption"] + '<a href="' + post["url"] + '"  title="' +
                  post["slug"] + '"><img src="' + post["photo-url-400"] + '"></a>';
                  break;
               case "audio":
                  link = '<em>' + post["date"] + '</em>' + post["audio-caption"] + post["id3-title"] + ':<br>' + post["audio-player"];
                  break;
               case "video":
                  link = '<em>' + post["date"] + '</em>' + post["video-caption"] + post["video-player"];
                  break;
               case "link":
                  link = '<em>' + post["date"] + '</em>' + '<p><b><a  href="' + post["link-url"] + '"  title="' +
                  post["link-text"] + '">' + post["link-text"] + '</a></b>' + post["link-description"];
                  break;
               case "quote":
                  link = '<em>' + post["date"] + '</em>' + '<p><b>&quot;<a href="' + post["url"] + '" title="' + post["regular-title"]
                     + '">' + post["quote-text"] + '</a>&quot;</b>' + post["quote-source"];
                  break;
               default:
                  if (typeof(post["regular-title"]) == 'string') {
                     title = post["regular-title"];
                     link = '<em>' + post["date"] + '</em>' + '<p><b><a href="' + post["url"] + '" title="' + post["regular-title"]
                     + '">' + post["regular-title"] + '</a></b>' + post["regular-body"];
                  }
            }
            output = '<li>' + link + ' </li>';
            $("#" + tag_list).append(output);
            $("#" + loading_tag).html('');
         }
      }
   });
   output = '<li>&gt;&gt;&gt; More <a target="_blank" href="' + tumblrURL + '/tagged/' + tag + '"><span style="text-transform:capitalize;">' + tag + '</span></a> &gt;&gt;&gt;</li>';
   $("#" + tag_list).append(output);
}




