/*
 * Dynamic squarespace photo gallery
 *
 * Copyright (c) 2008 Wirepoint Media, LLC (wirepointmedia.com)
 *
 * @Author: Chris Cox <chris.cox@wirepointmedia.com> 
 * @Date: 12-16-2008
 * @version: 1.1 rev 12-24-08
 */


/*
 * This function will dynamically load a squarespace picture gallery.  Gallery folders must have same name as
 * the page that it will be displayed on.  Also, main gallery page must be named 'imagelibrary'.
 */


$(function(){

  //Determine current page name
  var pageTitle = document.title.replace(Squarespace.Constants.WEBSITE_TITLE + " - ", ""); 
  pageTitle = $.trim(pageTitle);

  //Create description container and make ajax call
  $("#content").append("<div id='desc' style='display:none'></div>");
  $("#desc").load("/imagelibrary/" + pageTitle + "/ #content .description", function(data) {});

  //Create thumbnail container to load images into and make ajax call
  $("#content").append("<div id='thumbs'></div>");
  $("#thumbs").load("/imagelibrary/" + pageTitle + "/ #content img", function(data) {

    //Filter out gallery thumbnail images    
    $("#thumbs img").each(function(i){
      
       //Get attributes
       var desc = $("#content .description:eq(" + i + ") p").text();
       var filename = $(this).attr("title");
       var id = $(this).attr("id").replace("picture", "").replace("-img", "");     
       var imgUrl = "/picture/" + filename + "?pictureId=" + id; 
       var thumbUrl = imgUrl + "&asThumbnail=true";   
       var rel= "shadowbox[" + pageTitle + "]";

       //Assign links to images
       $(this).attr("src", thumbUrl).removeAttr("class");
       $(this).wrap("<a></a>").parent().attr("href", imgUrl).attr("rel", rel).attr("title", desc);
     });

     // set up all anchor elements to work with Shadowbox
     Shadowbox.setup($("#thumbs a"), {
     });

  });
});