/**
 * embreach.net photo gallery script.
 * Whipped up by Jari, works for us (tm).
 */

var preferences = {
  width: 800,
  height: 500
};

function createFlickrGalleries() {
  $('div.photo-set div.photos-flickr').each(function(i) {
    var parts = this.id.split(/:/);
    var userId = parts[0];
    var setId = parts[1];
    $(this).html('<object width="' + preferences.width + '" height="' + preferences.height + '"> <param name="flashvars" value="&offsite=true&lang=en-us&page_show_url=%2Fphotos%2F' + userId + '%2Fsets%2F' + setId + '%2Fshow%2F&page_show_back_url=%2Fphotos%2F' + userId + '%2Fsets%2F' + setId + '%2F&set_id=' + setId + '&jump_to="></param> <param name="movie" value="http://www.flickr.com/apps/slideshow/show.swf?v=69832"></param> <param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/slideshow/show.swf?v=69832" allowFullScreen="true" flashvars="&offsite=true&lang=en-us&page_show_url=%2Fphotos%2F' + userId + '%2Fsets%2F' + setId + '%2Fshow%2F&page_show_back_url=%2Fphotos%2F' + userId + '%2Fsets%2F' + setId + '%2F&set_id=' + setId + '&jump_to=" width="' + preferences.width + '" height="' + preferences.height + '"></embed></object>');
  });
}

function createPhotoSetLinks() {
  $('div.photo-set').each(function(i) {
    var id = 'photoSet' + i;
    this.id = id;
    var title = $(this).children(".title").text();
    var link = $(document.createElement('a'))
      .attr('href', 'javascript:void(0)')
      .text(title)
      .bind("click", function() {
        showSet(id);
      });
    var linkContainer = $(document.createElement('div'))
      .attr('id', 'linkFor' + id)
      .addClass('photo-set-link')
      .append(link);
    $('#photo-set-links').append(linkContainer);
  });
}

function showSet(id) {
  var set = $('#' + id);
  set.siblings().hide();
  set.show();
  var link = $('#linkFor' + id);
  link.siblings().removeClass('active-set-link');
  link.addClass('active-set-link');
}

function showDefaultSet() {
  showSet($('.default-set').attr('id'));
}

function createGallery() {
  $(document).ready(function() {
    $('#sidebar').hide();
    $('#photo-sets').height(preferences.height + 100);
    createFlickrGalleries();
    createPhotoSetLinks();
    showDefaultSet();
  });
}
