/**
 * @class Page behavior for Iowa City Land Scaping Markup Factory Site.
 */
var ICLandscaping = Class.create({
  /**
   * Initialize function.
   */
  initialize : function () {
    this.externalLinks(); // Make links open in new windows
    this.slideShowLinks(); // Create slide show
  },

  /**
   * Make links with a rel attribute set open in a new window.
   */
  externalLinks : function (rel) {
    var defaultRel = "external"; // Defualt value for rel attribute
    rel = rel || defaultRel;
    // Find every link and set the target to blank if the rel attribute is 
    // set correctly.
    $$('a').each(function(a) {
      if (a.getAttribute("rel") === rel) { a.target = "_blank"; } 
    });
  },

  /**
   * Observer slide show links and update flickr iframe with items
   */
  slideShowLinks : function () {
    var iFrame = "slideShow"; // Show frame
    var title = "slideShowTitle"; // Show heading 
    var selector = "ul.slideShowList li a"; // Selection list 
    var iFrameUrl = "http://www.flickr.com/slideShow/index.gne?user_id=21316186@N06&tags="
    if ($(iFrame)) {
      // Observe each link on the list
      $$(selector).each(function(a) {
        a.observe('click', function () {
          // Add the class name as a tag and change the frame source
          $(iFrame).src = iFrameUrl + a.className; 
          if ($(title)) {
            // Change the title to the contents of the item
            $(title).update(a.innerHTML);
          }
        });
      });
    }
  }
});

/* This is done below in the jsonFlickrFeed function */
//document.observe('dom:loaded', function () { new ICLandscaping() });

/**
* This function acts as a callback for the JSON feed from flickr defined
* in another script tag. This is for the tiny pics at the bottom of the page.
*/
jsonFlickrFeed = function(o) {
  var quantity = 6;
  var photos = '';
  var div = "flickrphotos";
  Event.observe(window, 'load', function() {
    // Photos at bottom
    if ($(div)) {
      if (o.items.length < quantity) { quantity = o.items.length; }
      for (var i=0; i < quantity; i += 1) {
        var imgURL = o.items[i].media.m.gsub("_m.", "_s.");
        var content = { title : o.items[i].title, 
                    link : o.items[i].link, 
                    img : imgURL };
        photos += ('<a rel="external" href="#{link}">' +
          '<img src="#{img}" alt="#{title}" title="#{title}" /></a>')
          .interpolate(content);
          
      }
      $(div).update(photos); 
    }
    // Load the site class here to get access to it's methods
    var ic = new ICLandscaping();
    ic.externalLinks(); // Set links to open in new window.
  });
}

