var userID = "72448649@N00";

// Classes --------------------------------------------------------------------

function FlickrCollection(attributes) {
  attrs = $H(attributes);

  // Set the properties for this object
  id = attrs.unset("id").split("-");
  this.id = id[1];
  this.title = attrs.get("title");
  this.smallIcon = attrs.get("iconsmall");
  this.largeIcon = attrs.get("iconlarge");
  this.description = attrs.get("description");
  this.url = "http://www.flickr.com/photos/" + userID + "/collections/" + this.id;

  // Build an array of FlickrCollection objects
  if (Object.isArray(attrs.get("collection"))) {
    var cols = [];
    attrs.get("collection").each(function(item) {
      cols.push(new FlickrCollection(item));
    })
    this.collections = cols;
  } else {
    this.collections = [];
  }

  // Build an array of FlickrSet objects
  if (Object.isArray(attrs.get("set"))) {
    var sets = [];
    attrs.get("set").each(function(item) {
      sets.push(new FlickrSet(item));
    })
    this.photosets = sets;
  } else {
    this.photosets = [];
  }

}

function FlickrSet(attributes) {
  attrs = $H(attributes);

  //Set the properties for this object
  this.id = attrs.get("id");
  this.title = attrs.get("title");
  this.description = attrs.get("description");
  this.url = "http://www.flickr.com/photos/" + userID + "/sets/" + this.id;
}


// API methods-----------------------------------------------------------------


function subitemsTitle(flickrCollection) {
  var title = "";
  if (flickrCollection.collections.size() > 0) {
    title = flickrCollection.collections.size() + " collections";
  }
  if (flickrCollection.photosets.size() > 0) {
    title = flickrCollection.photosets.size() + " photosets";
  }
  return title;
}


function displayFlickrPhotoset(photoset) {
  var setDiv = document.createElement('div');
  setDiv.className = 'flickrPhotoset';

  // var imageDiv = document.createElement('div');
  // imageDiv.className = 'flickrImageSquare';

  var detailsDiv = document.createElement('div');
  detailsDiv.className = 'flickrPhotosetDetails';

  // Add Photoset Title Link
  var photosetLink = $(document.createElement('a'));
  photosetLink.setAttribute('href', photoset.url);
  photosetLink.setAttribute('target', '_blank');
  photosetLink.update(photoset.title);

  var setTitle = document.createElement('p');
  setTitle.setAttribute('class', 'title');

  // Add Photoset Description
  var setDescription = $(document.createElement('p'));
  setDescription.setAttribute('class', 'description');
  setDescription.update(photoset.description);

  // Put all the pieces together
  setTitle.appendChild(photosetLink);
  detailsDiv.appendChild(setTitle);
  detailsDiv.appendChild(setDescription);
  setDiv.appendChild(detailsDiv);
  return setDiv;
}


function displayFlickrCollection(flickrCollection) {
  var collectionDiv = document.createElement('div');
  collectionDiv.className = 'flickrCollection';

  var imageDiv = document.createElement('div');
  imageDiv.className = 'flickrImage';

  var detailsDiv = document.createElement('div');
  detailsDiv.className = 'flickrDetails';

  var itemsDiv = document.createElement('div');
  itemsDiv.setAttribute('style', 'display:none');
  itemsDiv.setAttribute('id', flickrCollection.id + "Items");
  itemsDiv.className = 'flickrItems';

  // Add subitems - collections
  flickrCollection.collections.each(function(collection) {
    itemsDiv.appendChild(displayFlickrCollection(collection));
  })

  // Add subitems - photosets
  flickrCollection.photosets.each(function(photoset) {
    itemsDiv.appendChild(displayFlickrPhotoset(photoset));
  })

  // Add Collection Title
  var collectionTitle = $(document.createElement('h2'));
  collectionTitle.update(flickrCollection.title + "<br/><span>(" + subitemsTitle(flickrCollection) + ")</span>");

  // Add Collection Description
  var collectionDescription = $(document.createElement('p'));
  collectionDescription.update(flickrCollection.description);

  // Add 'Photosets...' link
  var collectionPhotosetsLinkParagraph = $(document.createElement('p'));
  var collectionPhotosetsLink = $(document.createElement('a'));
  collectionPhotosetsLink.setAttribute('href', '#');
  collectionPhotosetsLink.setAttribute('onclick', "Effect.toggle('" + flickrCollection.id + "Items', 'blind', {duration:0.5});return false;");
  collectionPhotosetsLink.update('View Photosets...');
  collectionPhotosetsLinkParagraph.appendChild(collectionPhotosetsLink);

  imageDiv.appendChild(flickrImageLink(flickrCollection.largeIcon, flickrCollection.url, flickrCollection.title));
  detailsDiv.appendChild(collectionTitle);
  detailsDiv.appendChild(collectionDescription);
  detailsDiv.appendChild(collectionPhotosetsLinkParagraph);
  collectionDiv.appendChild(imageDiv);
  collectionDiv.appendChild(detailsDiv);
  collectionDiv.appendChild(itemsDiv);
  return collectionDiv;
}


function flickrImageLink(aSrc, aUrl, aTitle) {
    var listitem = document.createElement('li');
    listitem.className = 'flickritem';

    var anchor = document.createElement('a');
    anchor.setAttribute('href', aUrl);
    anchor.setAttribute('target', '_blank');
    anchor.className = 'flickra';

    var image = document.createElement('img');
    image.setAttribute('src', aSrc);
    image.setAttribute('title', aTitle);
    image.setAttribute('alt', 'Photograph of ' + aTitle);
    // image.setAttribute('width', 75);
    // image.setAttribute('height', 75);
    image.className = 'flickrphoto';

    anchor.appendChild(image);
    return anchor;
}


//
// grab flickr images (via JSON) and display them
//

function jsonFlickrApi(obj) {
    // make sure required DOM API is available
    if (!document.createElement)
  return false;
    if (!document.getElementById)
  return false;
    if (!document.getElementsByTagName)
  return false;

  var collections_wrapper = $H(obj.collections);
  collections_wrapper.each(function(pair) {
    if (pair.key == 'collection') {
      collections = pair.value;
      collections.each(function(collectionValues) {
        flickrCollection = new FlickrCollection(collectionValues);
        $('flickrphotos').appendChild(displayFlickrCollection(flickrCollection));
      })
    }
  })
}



function encodePhotoID(photoID)
{
  var base58alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

  if (typeof photoID !== "number")
    photoID = parseInt(photoID);

  var enc = "";
  var div = photoID;
  while (photoID >= 58)
  {
    div = photoID / 58;
    var mod = (photoID - (58 * Math.floor(div)));
    enc = "" + base58alphabet.substr(mod, 1) + enc;
    photoID = Math.floor(div);
  }
  return (div ? ("" + base58alphabet.substr(div, 1) + enc) : enc);
}
