/*****************************************
 * Flickr REST API (in jQuery)
 * version: 1.0 (02/23/2010)
 * written for jQuery 1.4.2
 * by Hina, Cain Chen (http://blog.hinablue.me)
 *****************************************/
;
(function ($) {
    $.fn.queryflickr = function (e, f) {
        var g = $.extend({}, $.fn.queryflickr.defaults, e);
        var h = "http://api.flickr.com/services/rest/?format=json&jsoncallback=?&api_key=" + g.api_key;

        function getURL() {
            var d;

            switch(g.type) {
            case "photos.getAllContexts":
            case "photos.getContext":
            case "photos.getSizes":
            case "photos.geo.getLocation":
            case "photos.people.getList":
            case "tags.getListPhoto":
                d = h + "&method=flickr." + g.type + "&photo_id=" + g.photo_id;
                break;
            case "photos.getContactsPublicPhotos":
                var d = (g.single_photo === 1) ? h + "&method=flickr." + g.type + "&just_friends=" + g.just_friends + "&single_photo=1&include_self=" + g.include_self + "&extras=" + g.extras : h + "&method=flickr." + g.type + "&count=" + g.count + "&just_friends=" + g.just_friends + "&include_self=" + g.include_self + "&extras=" + g.extras;
                if (g.user_id !== null) d += "&user_id=" + encodeURIComponent(g.user_id);
                break;
            case "photos.getExif":
            case "photos.getInfo":
                d = h + "&method=flickr." + g.type + "&photo_id=" + g.photo_id + "&secret=" + g.secret;
                break;
            case "photos.getFavorites":
            case "galleries.getListForPhoto":
                d = h + "&method=flickr." + g.type + "&photo_id=" + g.photo_id + "&page=" + g.page + "&per_page=" + g.per_page;
                break;
            case "photos.search":
                d = h + "&method=flickr." + g.type + "&page=" + g.page + "&per_page=" + g.per_page + "&extras=" + g.extras + "&tag_mode=" + g.tag_mode + "&sort=" + g.sort;
                if (g.user_id !== null) d += "&user_id=" + encodeURIComponent(g.user_id);
                if (g.tags !== null) d += "&tag=" + encodeURIComponent(g.tags);
                if (g.text !== null) d += "&text=" + encodeURIComponent(g.text);
                if (g.min_upload_date > 0 && typeof g.min_upload_date === 'number') d += "&min_upload_date=" + g.min_upload_date;
                if (g.max_upload_date > 0 && typeof g.max_upload_date === 'number') d += "&max_upload_date=" + g.max_upload_date;
                if (g.min_taken_date > 0 && typeof g.min_taken_date === 'number') d += "&min_taken_date=" + g.min_taken_date;
                if (g.max_taken_date > 0 && typeof g.max_taken_date === 'number') d += "&max_taken_date=" + g.max_taken_date;
                if (g.license !== null) d += "&license=" + g.license;
                if (g.privacy_filter !== null) d += "&privacy_filter=" + g.privacy_filter;
                if (g.bbox !== null) d += "&bbox=" + g.bbox;
                if (g.accuracy > 0) d += "&accuracy=" + g.accuracy;
                if (g.safe_search > 0) d += "&safe_search=" + g.safe_search;
                if (g.content_type > 0) d += "&content_type=" + g.content_type;
                if (g.machine_tags !== null) d += "&machine_tags=" + g.machine_tags;
                if (g.machine_tag_mode !== null) d += "&machine_tag_mode=" + g.machine_tag_mode;
                if (g.contacts !== null) d += "&contacts=" + g.contacts;
                if (g.woe_id !== null) d += "&woe_id=" + g.woe_id;
                if (g.place_id !== null) d += "&place_id=" + g.place_id;
                if (g.media !== null) d += "&media=" + g.media;
                if (g.has_geo !== null) d += "&has_geo=" + g.has_geo;
                if (g.geo_context !== null) d += "&geo_context=" + g.geo_context;
                if (g.lat !== null && typeof lat === 'number') d += "&lat=" + g.lat;
                if (g.lon !== null && typeof lon === 'number') d += "&lon=" + g.lon;
                if (g.radius !== null) d += "&radius=" + g.radius;
                if (g.radius_units !== null) d += "&radius_units=" + g.radius_units;
                if (g.is_commons !== false) d += "&is_commons=" + g.is_commons;
                if (g.is_gallery !== false) d += "&is_gallery=" + g.is_gallery;
                if (g.is_getty !== false) d += "&is_getty=" + g.is_getty;
                break;
            case "photos.comments.getList":
                d = h + "&method=flickr." + g.type + "&photo_id=" + g.photo_id;
                if (g.min_comment_date > 0 && typeof g.min_comment_date === 'number') d += "&min_comment_date=" + g.min_comment_date;
                if (g.max_comment_date > 0 && typeof g.max_comment_date === 'number') d += "&max_comment_date=" + g.max_comment_date;
                break;
            case "photos.licenses.getInfo":
            case "panda.getList":
            case "people.getUploadStatus":
            case "places.getPlaceTypes":
            case "reflection.getMethods":
                d = h + "&method=flickr." + g.type;
                break;
            case "photosets.getInfo":
            case "photosets.comments.getList":
                d = h + "&method=flickr." + g.type + "&photoset_id=" + g.photoset_id;
                break;
            case "photosets.getContext":
                d = h + "&method=flickr." + g.type + "&photo_id=" + g.photo_id + "&photoset_id=" + g.photoset_id;
                break;
            case "photosets.getList":
            case "people.getInfo":
            case "people.getPublicGroups":
                d = h + "&method=flickr." + g.type + "&user_id=" + g.user_id;
                break;
            case "photosets.getPhotos":
                d = h + "&method=flickr." + g.type + "&photoset_id=" + g.photoset_id + "&extras=" + g.extras;
                if (g.privacy_filter !== null) d += "&privacy_filter=" + g.privacy_filter;
                if (g.media !== null) d += "&media=" + g.media;
                break;
            case "collections.getTree":
                d = h + "&method=flickr." + g.type;
                if (g.user_id !== null) d += "&user_id=" + encodeURIComponent(g.user_id);
                if (g.collection_id !== null) d += "&collection_id=" + g.collection_id;
                break;
            case "contacts.getPublicList":
            case "galleries.getList":
                d = h + "&method=flickr." + g.type + "&user_id=" + g.user_id + "&page=" + g.page + "&per_page=" + g.per_page;
                break;
            case "galleries.getInfo":
                d = h + "&method=flickr." + g.type + "&gallery_id=" + g.gallery_id;
                break;
            case "galleries.getPhotos":
                d = h + "&method=flickr." + g.type + "&gallery_id=" + g.gallery_id + "&extras=" + g.extras + "&page=" + g.page + "&per_page=" + g.per_page;
                break;
            case "groups.getInfo":
                d = h + "&method=flickr." + g.type + "&group_id=" + g.group_id + "&lang=" + g.lang;
                break;
            case "groups.search":
                d = h + "&method=flickr." + g.type + "&text=" + encodeURIComponent(g.text) + "&page=" + g.page + "&per_page=" + g.per_page;
                break;
            case "interestingness.getList":
                d = h + "&method=flickr." + g.type + "&extras=" + g.extras + "&page=" + g.page + "&per_page=" + g.per_page;
                if (g.date > 0) d += "&date=" + g.date;
                break;
            case "machinetags.getNamespaces":
                d = h + "&method=flickr." + g.type + "&page=" + g.page + "&per_page=" + g.per_page;
                if (g.predicate !== null) d += "&predicate=" + encodeURIComponent(g.predicate);
                break;
            case "machinetags.getPairs":
            case "machinetags.getValues":
                d = h + "&method=flickr." + g.type + "&page=" + g.page + "&per_page=" + g.per_page;
                if (g.predicate !== null) d += "&predicate=" + encodeURIComponent(g.predicate);
                if (g.namespace !== null) d += "&namespace=" + encodeURIComponent(g.namespace);
                break;
            case "machinetags.getPredicates":
                d = h + "&method=flickr." + g.type + "&page=" + g.page + "&per_page=" + g.per_page;
                if (g.namespace !== null) d += "&namespace=" + encodeURIComponent(g.namespace);
                break;
            case "machinetags.getRecentValues":
                d = h + "&method=flickr." + g.type;
                if (g.namespace !== null) d += "&namespace=" + encodeURIComponent(g.namespace);
                if (g.predicate !== null) d += "&predicate=" + encodeURIComponent(g.predicate);
                if (g.added_since > 0) d += "&added_since=" + g.added_since;
                break;
            case "panda.getPhotos":
                d = h + "&method=flickr." + g.type + "&panda_name=" + g.panda_name + "&extras=" + g.extras + "&page=" + g.page + "&per_page=" + g.per_page;
                break;
            case "people.findByEmail":
                d = h + "&method=flickr." + g.type + "&find_email=" + g.find_email;
                break;
            case "people.findByUsername":
                d = h + "&method=flickr." + g.type + "&username=" + g.username;
                break;
            case "people.getPhotos":
                d = h + "&method=flickr." + g.type + "&user_id=" + g.user_id + "&extras=" + g.extras + "&page=" + g.page + "&per_page=" + g.per_page;
                if (g.safe_search > 0) d += "&safe_search=" + g.safe_search;
                if (g.content_type > 0) d += "&content_type=" + g.content_type;
                if (g.min_upload_date > 0 && typeof g.min_upload_date === 'number') d += "&min_upload_date=" + g.min_upload_date;
                if (g.max_upload_date > 0 && typeof g.max_upload_date === 'number') d += "&max_upload_date=" + g.max_upload_date;
                if (g.min_taken_date > 0 && typeof g.min_taken_date === 'number') d += "&min_taken_date=" + g.min_taken_date;
                if (g.max_taken_date > 0 && typeof g.max_taken_date === 'number') d += "&max_taken_date=" + g.max_taken_date;
                if (g.privacy_filter !== null) d += "&privacy_filter=" + g.privacy_filter;
                break;
            case "people.getPhotosOf":
                d = h + "&method=flickr." + g.type + "&user_id=" + g.user_id + "&extras=" + g.extras + "&page=" + g.page + "&per_page=" + g.per_page;
                break;
            case "people.getPublicPhotos":
                d = h + "&method=flickr." + g.type + "&user_id=" + g.user_id + "&extras=" + g.extras + "&page=" + g.page + "&per_page=" + g.per_page;
                if (g.safe_search > 0) d += "&safe_search=" + g.safe_search;
                break;
            case "places.find":
                d = h + "&method=flickr." + g.type + "&query=" + g.query;
                break;
            case "places.findByLatLon":
                d = h + "&method=flickr." + g.type + "&lat=" + g.lat + "&lon=" + g.lon;
                if (g.accuracy > 0) d += "&accuracy=" + g.accuracy;
                break;
            case "places.getChildrenWithPhotosPublic":
            case "places.getInfo":
            case "places.getShapeHistory":
                d = h + "&method=flickr." + g.type;
                if (g.woe_id !== null) d += "&woe_id=" + g.woe_id;
                if (g.place_id !== null) d += "&place_id=" + g.place_id;
                break;
            case "places.getInfoByUrl":
                d = h + "&method=flickr." + g.type + "&url=" + g.url;
                break;
            case "places.getTopPlacesList":
                d = h + "&method=flickr." + g.type + "&place_type_id=" + g.place_type_id;
                if (g.woe_id !== null) d += "&woe_id=" + g.woe_id;
                if (g.place_id !== null) d += "&place_id=" + g.place_id;
                if (g.date > 0) d += "&date=" + g.date;
                break;
            case "places.placesForBoundingBox":
                d = h + "&method=flickr." + g.type + "&bbox=" + g.bbox;
                if (g.place_type !== null) d += "&place_type=" + g.place_type;
                if (g.place_type_id !== null) d += "&place_type_id=" + g.place_type_id;
                break;
            case "places.resolvePlaceId":
                d = h + "&method=flickr." + g.type + "&place_id=" + g.place_id;
                break;
            case "places.resolvePlaceId":
                d = h + "&method=flickr." + g.type + "&url=" + g.url;
                break;
            case "places.tagsForPlace":
                d = h + "&method=flickr." + g.type;
                if (g.woe_id !== null) d += "&woe_id=" + g.woe_id;
                if (g.place_id !== null) d += "&place_id=" + g.place_id;
                if (g.min_upload_date > 0 && typeof g.min_upload_date === 'number') d += "&min_upload_date=" + g.min_upload_date;
                if (g.max_upload_date > 0 && typeof g.max_upload_date === 'number') d += "&max_upload_date=" + g.max_upload_date;
                if (g.min_taken_date > 0 && typeof g.min_taken_date === 'number') d += "&min_taken_date=" + g.min_taken_date;
                if (g.max_taken_date > 0 && typeof g.max_taken_date === 'number') d += "&max_taken_date=" + g.max_taken_date;
                break;
            case "flickr.reflection.getMethodInfo":
                d = h + "&method=flickr." + g.type + "&method_name=" + g.method_name;
                break;
            case "tags.getClusterPhotos":
                d = h + "&method=flickr." + g.type + "&tag=" + g.tag + "&cluster_id=" + g.cluster_id;
                break;
            case "tags.getClusters":
                d = h + "&method=flickr." + g.type + "&tag=" + g.tag;
                break;
            case "tags.getHotList":
                d = h + "&method=flickr." + g.type + "&count=" + g.count + "&period=" + g.period;
                break;
            case "tags.getListUserPopular":
                d = h + "&method=flickr." + g.type + "&count=" + g.count;
                if (g.user_id !== null) d += "&user_id=" + g.user_id;
                break;
            case "tags.getListUserRaw":
            case "tags.getRelated":
                d = h + "&method=flickr." + g.type;
                if (g.tag !== null) d += "&tag=" + g.tag;
                break;
            case "urls.getGroup":
                d = h + "&method=flickr." + g.type + "&group_id=" + g.group_id;
                break;
            case "urls.getUserPhotos":
            case "urls.getUserProfile":
                d = h + "&method=flickr." + g.type;
                if (g.user_id !== null) d += "&user_id=" + g.user_id;
                break;
            case "urls.lookupGallery":
            case "urls.lookupGroup":
            case "urls.lookupUser":
                d = h + "&method=flickr." + g.type + "&url=" + g.url;
                break;
            case "photos.getRecent":
            default:
                d = h + "&method=flickr." + g.type + "&page=" + g.page + "&per_page=" + g.per_page + "&extras=" + g.extras;
            }

            return d;
        }

        return $(this).each(function () {
            var self = $(this);

            $.ajax({
                type: "GET",
                url: getURL(),
                dataType: "json",
                success: function (a, b, c) {
                    if (a.stat === "ok") {
                        if ($.isFunction(f)) f.call(this, a, self);
                    } else {
                        alert("flickr API Error: Code:" + a.code + "\n" + a.message);
                    }
                    return self;
                }
            });
        });
    };

    $.fn.queryflickr.defaults = {
        api_key: "d1038f051000214af2bf694014ca8f98",
        type: null,
        photoset_id: null,
        user_id: null,
        thumb_size: 's',
        size: null,
        per_page: 10,
        page: 1,
        attr: '',
        api_url: null,
        params: '',
        api_callback: '?',
        callback: null,
        photo_id: null,
        count: 10,
        just_friends: 1,
        single_photo: 0,
        include_self: 1,
        extras: 'last_update',
        secret: 'sharing',
        text: null,
        group_id: null,
        tags: null,
        tag_mode: 'any',
        sort: 'date-posted-desc',
        min_upload_date: 0,
        max_upload_date: 0,
        min_taken_date: 0,
        max_taken_date: 0,
        license: null,
        privacy_filter: 1,
        bbox: null,
        accuracy: 16,
        safe_search: 1,
        content_type: 7,
        machine_tags: null,
        machine_tag_mode: 'any',
        contacts: null,
        woe_id: null,
        place_id: null,
        media: 'all',
        has_geo: null,
        geo_context: null,
        lat: null,
        lon: null,
        radius: 5,
        radius_units: 'km',
        is_commons: false,
        is_gallery: false,
        is_getty: false,
        min_comment_date: 0,
        max_comment_date: 0,
        collection_id: null,
        gallery_id: null,
        lang: "en-us",
        date: 0,
        predicate: null,
        namespace: null,
        added_since: 0,
        panda_name: null,
        find_email: null,
        username: null,
        query: null,
        url: null,
        place_type_id: 12,
        place_type: 'country',
        method_name: null,
        tag: null,
        cluster_id: null,
        period: 'day'
    };
})(jQuery);

