﻿/* * jQuery Flickr - jQuery plug-in * Version 1.0, Released 2008.04.17 * * Copyright (c) 2008 Daniel MacDonald (www.projectatomic.com) * Dual licensed GPL http://www.gnu.org/licenses/gpl.html  * and MIT http://www.opensource.org/licenses/mit-license.php */
(function($) {
    $.fn.flickr = function(o) {
        var s = {
            api_key: null,              // [string]    required, see http://www.flickr.com/services/api/misc.api_keys.html
            type: null,                 // [string]    allowed values: 'photoset', 'search', default: 'flickr.photos.getRecent'
            photoset_id: null,          // [string]    required, for type=='photoset'  
            text: null, 		            // [string]    for type=='search' free text search
            user_id: null,              // [string]    for type=='search' search by user id
            group_id: null,             // [string]    for type=='search' search by group id
            tags: null,                 // [string]    for type=='search' comma separated list
            tag_mode: 'any',            // [string]    for type=='search' allowed values: 'any' (OR), 'all' (AND)
            sort: 'relevance',    // [string]    for type=='search' allowed values: 'date-posted-asc', 'date-posted-desc', 'date-taken-asc', 'date-taken-desc', 'interestingness-desc', 'interestingness-asc', 'relevance'
            thumb_size: 's',            // [string]    allowed values: 's' (75x75), 't' (100x?), 'm' (240x?)
            size: null,                 // [string]    allowed values: 'm' (240x?), 'b' (1024x?), 'o' (original), default: (500x?)
            per_page: 100,              // [integer]   allowed values: max of 500
            page: 1,     	              // [integer]   see paging notes
            attr: '',                   // [string]    optional, attributes applied to thumbnail <a> tag
            api_url: null,              // [string]    optional, custom url that returns flickr JSON or JSON-P 'photos' or 'photoset'
            params: '',                 // [string]    optional, custom arguments, see http://www.flickr.com/services/api/flickr.photos.search.html
            api_callback: '?',          // [string]    optional, custom callback in flickr JSON-P response
            callback: null              // [function]  optional, callback function applied to entire <ul>
            // PAGING NOTES: jQuery Flickr plug-in does not provide paging functionality, but does provide hooks for a custom paging routine
            // within the <ul> created by the plug-in, there are two hidden <input> tags, 
            // input:eq(0): current page, input:eq(1): total number of pages, input:eq(2): images per page, input:eq(3): total number of images
            // SEARCH NOTES: when setting type to 'search' at least one search parameter  must also be passed text, user_id, group_id, or tags
            // SIZE NOTES: photos must allow viewing original size for size 'o' to function, if not, default size is shown
        };
        if (o) $.extend(s, o);
        return this.each(function() {
            // create unordered list to contain flickr images
            var list = $('<ul>').appendTo(this);
            var url = $.flickr.format(s);
            $.getJSON(url, function(r) {
                if (r.stat != "ok") {
                    for (i in r) {
                        $('<li>').text(i + ': ' + r[i]).appendTo(list);
                    };
                } else {
                    if (s.type == 'photoset') r.photos = r.photoset;
                    // add hooks to access paging data
                    list.append('<input type="hidden" value="' + r.photos.page + '" />');
                    list.append('<input type="hidden" value="' + r.photos.pages + '" />');
                    list.append('<input type="hidden" value="' + r.photos.perpage + '" />');
                    list.append('<input type="hidden" value="' + r.photos.total + '" />');
                    for (var i = 0; i < r.photos.photo.length; i++) {
                        var photo = r.photos.photo[i];
                        // format thumbnail url
                        var t = 'http://farm' + photo['farm'] + '.static.flickr.com/' + photo['server'] + '/' + photo['id'] + '_' + photo['secret'] + '_' + s.thumb_size + '.jpg';
                        //format image url
                        var h = 'http://farm' + photo['farm'] + '.static.flickr.com/' + photo['server'] + '/' + photo['id'] + '_';
                        switch (s.size) {
                            case 'm':
                                h += photo['secret'] + '_m.jpg';
                                break;
                            case 'b':
                                h += photo['secret'] + '_b.jpg';
                                break;
                            case 'o':
                                if (photo['originalsecret'] && photo['originalformat']) {
                                    h += photo['originalsecret'] + '_o.' + photo['originalformat'];
                                } else {
                                    h += photo['secret'] + '_b.jpg';
                                };
                                break;
                            default:
                                h += photo['secret'] + '.jpg';
                        };
                        list.append('<li><a href="' + h + '" ' + s.attr + ' title="' + photo['title'] + '"><img src="' + t + '" alt="' + photo['title'] + '" /></a></li>');
                    };
                    if (s.callback) s.callback(list);
                };
            });
        });
    };
    // static function to format the flickr API url according to the plug-in settings 
    $.flickr = {
        format: function(s) {
            if (s.url) return s.url;
            var url = 'http://api.flickr.com/services/rest/?format=json&jsoncallback=' + s.api_callback + '&api_key=' + s.api_key;
            switch (s.type) {
                case 'photoset':
                    url += '&method=flickr.photosets.getPhotos&photoset_id=' + s.photoset_id;
                    break;
                case 'search':
                    url += '&method=flickr.photos.search&sort=' + s.sort;
                    if (s.user_id) url += '&user_id=' + s.user_id;
                    if (s.group_id) url += '&group_id=' + s.group_id;
                    if (s.tags) url += '&tags=' + s.tags;
                    if (s.tag_mode) url += '&tag_mode=' + s.tag_mode;
                    if (s.text) url += '&text=' + s.text;
                    break;
                default:
                    url += '&method=flickr.photos.getRecent';
            };
            if (s.size == 'o') url += '&extras=original_format';
            url += '&per_page=' + s.per_page + '&page=' + s.page + s.params;
            return url;
        }
    };
})(jQuery);

function fetch(apiKey, userID, perPage, rootElement, gv) {
       
    if (gv == null) {
        gv = new globalValues('1', perPage, null, null, rootElement, apiKey, userID, null);
        $(rootElement).find(".switch").click(function() {
            $(gv.rootElement).find(".listWrapper").css("display", "block");
            $(gv.rootElement).find(".prevWrapper").css("display", "none");
        });
    }

    // fetch items
    $(gv.rootElement).find(".photoList").flickr({
        api_key: gv.apiKey,
        user_id: gv.userID,
        thumb_size: 's',
        per_page: gv.imagesPerPage,
        type: 'search',
        page: gv.currentPage,
        callback:
        function(el) {
            customcallback(el, gv);
        }
    });
}
function customcallback(el, gv) {
    // save paging information returned in hidden fields
    var currentPage = $(el).children().eq(0)[0].value;
    var imagesPerPage = $(el).children().eq(2)[0].value;
    var currentStartIndex = ((currentPage - 1) * imagesPerPage);
    var totalImages = $(el).children().eq(3)[0].value;
    var anchors = $(el).find("a");

    // remove previous ul
    if ($(gv.rootElement).find(".photoList ul").length == 2) {
        $(gv.rootElement).find(".photoList ul:first").remove();
    }

    if (gv.photoArray == null) {
        var arrPhotos = new Array();
    } else {
        var arrPhotos = gv.photoArray;
    }

    // insert photoitems into array
    anchors.each(function(i) {
        arrPhotos[currentStartIndex + i] = new photoItem($(this).attr("href"), $(this).attr("title"));
    });

    var newGV = new globalValues(currentPage, imagesPerPage, currentStartIndex, totalImages, gv.rootElement, gv.apiKey, gv.userID, arrPhotos);

    setPaging($(el).children().eq(0)[0].value, $(el).children().eq(1)[0].value, newGV);

    // bind image clicks
    anchors.each(function(i) {
        $(this).bind("click", function() {
            showImage(arrPhotos[currentStartIndex + i], newGV);
            setImageNavigation(currentStartIndex + i, newGV);
            $(gv.rootElement).find(".listWrapper").css("display", "none");
            $(gv.rootElement).find(".prevWrapper").css("display", "block");
            return false;
        });
    });
}

// helper object, holds information about the 
function globalValues(currentPage, imagesPerPage, currentStartIndex, totalImages, rootElement, apiKey, userID, photoArray) {
    this.currentPage = currentPage;
    this.imagesPerPage = imagesPerPage;
    this.currentStartIndex = currentStartIndex;
    this.totalImages = totalImages;
    this.rootElement = rootElement;
    this.apiKey = apiKey;
    this.userID = userID;
    this.photoArray = photoArray;
}

// helper object
function photoItem(href, title) {
    this.href = href;
    this.title = title;
}

// show image, set text, calculate width
function showImage(photo, gv) {
    var image = $(gv.rootElement).find(".flickrImage");

    if (photo != null) {
        $(gv.rootElement).find(".anchorImg").attr("href", photo.href);
        image.attr("src", photo.href);
        if ($(image).width() == 0 || $(image).width() > $(gv.rootElement).width() - 40) {
            image.width($(gv.rootElement).width() - 40);
        } else {
            $(gv.rootElement).find(".flickrPreview").width($(gv.rootElement).width() - 40);
        }
        //image.hide("slow");
        //image.show("slow");
        $(gv.rootElement).find(".flickrText").html('"' + photo.title + '"');
    }
}
// set next/previous image click
function setImageNavigation(index, gv) {
    var prevImage = $(gv.rootElement).find(".prevImage");
    var nextImage = $(gv.rootElement).find(".nextImage");

    // unbind all existing events
    $(prevImage).unbind();
    $(nextImage).unbind();

    if (index > 0) {
        $(prevImage).removeAttr("disabled");
        $(prevImage).click(function() {
            showImage(gv.photoArray[index - 1], gv);
            setImageNavigation(index - 1, gv);
        });
        $(prevImage).hover(function() {
            $(this).css('background-color', '#646464');
        },
        function() {
            $(this).css('background-color', '');
        });
    } else {
        $(prevImage).attr("disabled", "true");
    }

    if (index < gv.totalImages - 1) {
        $(nextImage).removeAttr("disabled");
        if (gv.photoArray[index + 1] != null) {
            $(nextImage).click(function() {
                showImage(gv.photoArray[index + 1], gv);
                setImageNavigation(index + 1, gv);
            });
        } else {
            // populate list, if we haven´t the values            
            gv.currentPage = ++gv.currentPage;
            fetch(null, null, null, null, gv);
            $(nextImage).click(function() {
                showImage(gv.photoArray[index + 1], gv);
                setImageNavigation(index + 1, gv);
            });
        }

        $(nextImage).hover(function() {
            $(this).css('background-color', '#646464');
        },
        function() {
            $(this).css('background-color', '');
        });

    } else {
        $(nextImage).attr("disabled", "true");
    }

}
// main paging function
function setPaging(currentPage, maxPage, gv) {
    var next = $(gv.rootElement).find(".nextPage");
    var prev = $(gv.rootElement).find(".prevPage");

    $(next).unbind();
    $(prev).unbind();

    if (gv.currentPage == 1) {
        $(prev).attr("disabled", "true");
    } else {
        $(prev).removeAttr("disabled");
        $(prev).click(function() {
            gv.currentPage = --gv.currentPage;
            fetch(null, null, null, null, gv);
            return false;
        });
    }

    if (gv.currentPage < maxPage) {
        $(next).removeAttr("disabled");
        $(next).click(function() {
            gv.currentPage = ++gv.currentPage;
            fetch(null, null, null, null, gv);
            return false;
        });
    } else {
        $(next).attr("disabled", "true");
    }
}
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();