/*
 * FlickrSlideshow is a widget for flickr slideshows.
 * 
 * Required Parameters:
 *
 * (string) poolId: Flickr Pool Id. (ex: graffiti pool: 29815630@N00, urbannature: 27393618@N00)
 *
 * Optional Parameters:
 * 
 * (int) delay: milliseconds to wait before showing the next image.
*/
 
function FlickrSlideshow(slideshowParameters)
{
    this.data = slideshowParameters;
    this.id = this.data.guid;
 
    this.poolId = this.data.poolId;
    this.cacheNumber = 25;
    this.delay = (this.data.delay) ? this.data.delay : 2000;
  
    this.randomPage = this.data.randomPage;
 
    Page.templates[this.id] = this;
}
 
FlickrSlideshow.prototype.init = function()
{
    $(this.id).poolId = this.poolId;
    $(this.id).cacheNumber = this.cacheNumber;
    $(this.id).delay = this.delay;
    $(this.id).randomPage = this.randomPage;
 
    var params = $H(
    {
        remote_service: "http://api.flickr.com/services/rest/",
        templateId:     this.id,
        per_page:       this.cacheNumber,
        method:         "flickr.groups.pools.getPhotos",
        group_id:       this.poolId
    });
 
    var startSlideshow = function(r)
    {
        eval(r.responseText);
 
        var slideshow = $(responseArray['templateId']);
 
        slideshow.currentImage = 0;
        slideshow.currentPage = 1;
 
        slideshow.renderImagesHtmlFromXml = function(flickrXml)
        {
            var photos = $A(XML.getDocumentFromString(flickrXml).getElementsByTagName('photo'));
 
            var photosHtml = '';
 
            for (var i = 0, photo; photo = photos[i]; i++)
            {
                photosHtml += '<img src="http://static.flickr.com/' + photo.getAttribute('server') + '/' + 
                              photo.getAttribute('id') + '_' + photo.getAttribute('secret') + '.jpg">';
            }
            return photosHtml;
        }
 
        slideshow.innerHTML = '<div class="FlickrSlideshow">' + slideshow.renderImagesHtmlFromXml(responseArray['response']) +
                              '</div>';
 
        Element.addClassName(slideshow.getElementsByTagName('img')[0], 'selected');
 
        slideshow.totalPages = XML.getDocumentFromString(responseArray['response']).getElementsByTagName(
                               'photos')[0].getAttribute('pages');
 
        slideshow.advance = eval("function() { if ($('" + slideshow.id + "')) $('" + slideshow.id + "').showNextImage(); }");
 
        slideshow.showNextImage = function() 
        { 
            if (this.currentImage < this.getElementsByTagName('img').length - 1)
            {
                this.getElementsByTagName('img')[this.currentImage].className = '';
                this.getElementsByTagName('img')[++this.currentImage].className = 'selected';
 
                if (Page.templates[slideshow.id].timer)
                    window.clearTimeout(Page.templates[slideshow.id].timer);
 
                Page.templates[slideshow.id].timer = setTimeout(this.advance, this.delay);
            }
 
            if (this.getElementsByTagName('img').length - this.currentImage < 5)
            {
                this.requestMoreImages();
            }
        };
 
        slideshow.requestMoreImages = function()
        {
            if (!Element.hasClassName(this, 'requestingImages') && this.currentPage < this.totalPages)
            {
                Element.addClassName(this, 'requestingImages');
 
                if (this.randomPage) this.currentPage = Math.floor(Math.random()*this.totalPages - 1); 
 
                var params = $H(
                {
                    remote_service: "http://api.flickr.com/services/rest/",
                    templateId:     this.id,
                    per_page:       this.cacheNumber,
                    method:         "flickr.groups.pools.getPhotos",
                    group_id:       this.poolId,
                    page:           ++this.currentPage
                });
 
                new Ajax.Request(
                    location.href,
                    {
                        method: 'get',
                        parameters: params.toQueryString(),
                        onComplete: function(r)
                        {
                            eval(r.responseText);
 
                            $(responseArray['templateId']).getElementsByTagName('div')[0].innerHTML += 
                                slideshow.renderImagesHtmlFromXml(responseArray['response']);
 
                            Element.removeClassName($(responseArray['templateId']), 'requestingImages');
                         }
                    });
            }
        };
 
        slideshow.advance();
    };
 
    new Ajax.Request(
        location.href,
        {
            method: 'get',
            parameters: params.toQueryString(),
            onComplete: startSlideshow
        });
};
 
var XML = {};
 
XML.getDocumentFromString = function(XMLString)
{	
    try
    {
        if (typeof ActiveXObject != "undefined" && typeof GetObject != "undefined")
        {
	    var b = new ActiveXObject("Microsoft.XMLDOM");
 
            b.loadXML( XMLString );
            return b;
        }
        else if (typeof DOMParser != "undefined")
        {  
            return(new DOMParser()).parseFromString(XMLString, "text/xml" )
        } 
        else 
        {
            return document.createElement("div")
        }
     } 
     catch(c)
     {
         return document.createElement("div");
     }
}
edit view source
cancel cancel

Claim this page. ( already registered? login and claim as existing user. )

This page is unclaimed. You can claim it for yourself and block others from editing

cancel - register as new user

Login.

Claim this page

Logout.