/*
 * You wouldn't want to create these yourself, unless you can get the json yourself. Chances are you will use GSpreadsheet.load()
 */
var Panoramio = function(json, options) {
  this.options = options || {};
  this.photos = json.photos;
  this.count = json.count;
  this.index = [];

  for (var x = 0; x < this.photos.length; x++) {
    this.index[this.photos[x].photo_id] = x;
  }

  this.each = function(callback) {
    for (var x = 0; x < this.photos.length; x++) {
      callback(this.photos[x]);
    }
  };

  /*
   * Take either a key (e.g. 'firstname') or the row id that you want
   */
  this.select = function(id) {
    if (typeof id == 'string') {
      return this.photos[this.index[id]];
    } else {
      return this.photos[id];
    }
  };

  this.formImgUrl = function(photoId, imgType) {
    return 'http://www.panoramio.com/photos/' + imgType + '/' + photoId + '.jpg';
  }
  this.formPageUrl = function(photoId) {
    return 'http://www.panoramio.com/photo/' + photoId;
  }

  // -- Debugging Helpers
  this.displayAll = function(inlineCSS, options) {
    if (!options['countPerRow']) options['countPerRow'] = 5;
    if (!options['photoSize']) options['photoSize'] = "square";

    if (!inlineCSS) inlineCSS = '';
    var table = "<table cellpadding='5' cellspacing='0' " + inlineCSS + ">";

    var self = this;
    table += "<tr>";
    for (var x = 0; x < this.photos.length; x++) {
      var aStart = "";
      var aEnd = "";
      if (options['linkToPhoto']) {
        aStart = "<a href='" + this.formPageUrl(this.photos[x].photo_id) + "' target='_blank'>";
        aEnd = "</a>";
      }                     
      table += "<td style='border: 1px solid grey;'>" 
               + aStart + "<img src='" + this.formImgUrl(this.photos[x].photo_id, options['photoSize']) + "'/>"
               + aEnd + "</td>";
      if ((x+1) % options['countPerRow'] == 0) {
        table += "</tr><tr>";
      }
    }

    table += "</tr>";
    table += "</table>";
    return table;
  };
  this.displayRow = function(id) {
    var row = this.select(id);
    var keyvalues = [];
    for (var x in row) {
      keyvalues.push(x + ' = ' + row[x]);
    }
    return keyvalues.join(', ');
  }
}

/*
 * This is a static method that loads in spreadsheets and returns GSpreadsheets, passing them into their callback
 */
Panoramio.load = function(callback, userOptions) {
  var options = {
    order: "popularity",
    set: "public",
    from: "0",
    to: "20",
    minx: "-180",
    miny: "-90",
    maxx: "180",
    maxy: "90"
  };

  for (optionName in userOptions) {
    if (userOptions.hasOwnProperty(optionName)) {
      options[optionName] = userOptions[optionName];
    }
  }

  var url = "http://www.panoramio.com/map/get_panoramas.php?";
  var uniqueID = "";

  for (optionName in options) {
    if (options.hasOwnProperty(optionName)) {
      var optionVal = "" + options[optionName] + "";
      url += optionName + "=" + optionVal + "&";
      uniqueID += optionVal.replace(/[^\w]+/g,"");
    }
  }

  var callbackName = "Panoramio.loader" + uniqueID; //ask dion
  eval(callbackName + " = function(json) { var pa = new Panoramio(json, options); callback(pa); }");

  var script = document.createElement('script');
  script.setAttribute('src', url + 'callback=' + callbackName);
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

