var tConfigs = new Object();  // configurations for TagCloudMaker

// jsr_class.js
//
// JSONscriptRequest -- a simple class for making HTTP requests
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page
// to access data from any server in the web, which is really useful.
// Unfortunately, the data is returned in the form of a script. That script
// can deliver the data, but it runs with the same authority as scripts on
// the base page, so it is able to steal cookies or misuse the authorization
// of the user with the server. A rogue script can do destructive things to
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.
//
//
// Sample Usage:
//
// <script type="text/javascript" src="jsr_class.js"></script>
//
// function callbackfunc(jsonData) {
//      alert('Latitude = ' + jsonData.ResultSet.Result[0].Latitude +
//            '  Longitude = ' + jsonData.ResultSet.Result[0].Longitude);
//      aObj.removeScriptTag();
// }
//
// request = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&
//            output=json&callback=callbackfunc&location=78704';
// aObj = new JSONscriptRequest(request);
// aObj.buildScriptTag();
// aObj.addScriptTag();
//
//

// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl;
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");

    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
};

// removeScriptTag method
//
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);
};

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
};


// callback
function thumbnailHundler(data) {
  var ul = document.createElement('ul');
  var t = data['thumbnails'];
  for (var i in t) {
    if(!t[i]['thumbnail']) {
      continue;
    }
    var li = document.createElement('li');
    li.setAttribute('class', 'thumbnail_'+i);
    var a = document.createElement('a');
    if(tConfigs.link == 'lightbox') {
      a.setAttribute('href', t[i]['image']);
      a.setAttribute('rel', 'lightbox');
    } else {
      a.setAttribute('href', t[i]['permalink']);
      a.setAttribute('target', '_blank');
    }
    a.setAttribute('target', '_blank');
    var img = document.createElement('img');
    img.setAttribute('src', t[i]['thumbnail']);
    a.appendChild(img);
    li.appendChild(a);
    ul.appendChild(li);
  }
  document.getElementById('thumbnails_'+tConfigs.id).appendChild(ul);
  var clear = document.createElement('div');
  clear.style.clear = 'both';
  document.getElementById('thumbnails_'+tConfigs.id).appendChild(clear);
  aObj.removeScriptTag();
}


// fire
(function (){
  var params;
  var type;
// get arguments
  var scripts = document.getElementsByTagName("script");
  for (var i = 0; i < scripts.length; i++) {
    var s = scripts[i];
    if (s.src && s.src.match(/thumbnails\.js(\?.*)?/)) {
      params = s.src.replace(/.*\?/, '');
      type = 'thumbnails';
    } else if(s.src && s.src.match(/thumnail\.js(\?.*)?/)) {
      params = s.src.replace(/.*\?/, '');
      type = 'thumnail';
    }
  }
  params = params.split("&");
  for(var i = 0; i < params.length; i++) {
    var tmp = params[i].split("=");
    tConfigs[tmp[0]] = unescape(tmp[1]);
  }
  if(typeof(tConfigs.id) == 'undefined' || !tConfigs.id.match(/^[_0-9a-zA-Z]+$/)) tConfigs.id = 'id';
  if(typeof(tConfigs.num) == 'undefined') tConfigs.num = 10;
  if(typeof(tConfigs.link) == 'undefined') tConfigs.link = 'permalink';

// tag cloud frame
  document.write('<div id="thumbnails_'+tConfigs.id+'" class="'+type+'"></div>');

// api->jsonp
  if(typeof(tConfigs.user) != 'undefined') {
    request = 'http://th.umbls.com/getthumbnails?user='+tConfigs.user
             +'&num='+tConfigs.num;
    if(typeof(tConfigs.date) != 'undefined') {
      request = request+'&date='+tConfigs.date;
    }
    if(typeof(tConfigs.tag) != 'undefined') {
      request = request+'&tag='+tConfigs.tag;
    }
    request = request+'&type=json&callback=thumbnailHundler';
    aObj = new JSONscriptRequest(request);
    aObj.buildScriptTag();
    aObj.addScriptTag();
  }
})();