//when the document is ready, run the following function
jQuery(function() {

    //remove the accessibility link/proper xhtml forming element
    jQuery('#flickr-widget-link').remove();

    //assign api key value and user ID
    var flickrApiKey = '4ef2fe2affcdd6e13218f5ddd0e2500d';
    var flickrUserID = '37191705@N06';
    var flickrSetID = '72157621860239578';

    var displayImage = function() {

        largeThumb = jQuery(this).attr('larger');
        largeTitle = jQuery(this).attr('alt');
        largeLink = jQuery(this).attr('link');

        document.getElementById('flickr-thumb').style.backgroundImage = 'url(' + largeThumb + ')';
        jQuery('#flickr-thumb-description').text('' + largeTitle + '');
        jQuery('#flickr-thumb-link').attr('href', largeLink);
    }
    
    //send request to flickr
    jQuery.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + flickrApiKey + '&photoset_id=' + flickrSetID +'&format=json&jsoncallback=?',
    function(data) {
        //when data is received, loop through the results
        for (var i in data.photoset.photo) {
            var entry = '<li class="flickr-thumb-li" link="http://www.flickr.com/photos/ethanolpics/' + data.photoset.photo[i].id + '/in/photostream" larger="http://farm' + data.photoset.photo[i].farm + '.static.flickr.com/' + data.photoset.photo[i].server + '/' + data.photoset.photo[i].id + '_' + data.photoset.photo[i].secret + '_m.jpg" alt="' + data.photoset.photo[i].title + '" style="background-image: url(http://farm' + data.photoset.photo[i].farm + '.static.flickr.com/' + data.photoset.photo[i].server + '/' + data.photoset.photo[i].id + '_' + data.photoset.photo[i].secret + '_s.jpg);"><a href=""></a></li>';
            jQuery(entry).appendTo('#flickr-thumb-ul');
        }
        //show the first thumbnail's larger image
        var firstLargImg = jQuery('.flickr-thumb-li:first-child').attr('larger');
        var largeThumb = document.getElementById('flickr-thumb');
        largeThumb.style.backgroundImage = 'url(' + firstLargImg + ')';

        //set the first description
        var largeTitle = jQuery('.flickr-thumb-li:first-child').attr('alt');
        jQuery('#flickr-thumb-description').text('' + largeTitle + '');

        //set the first link
        largeLink = jQuery('.flickr-thumb-li:first-child').attr('link');
        jQuery('#flickr-thumb-link').attr('href', largeLink);

        //load the jcarouselLite script
        jQuery.getScript('/page/-/js/jcarousel-lite.js',
        function() {

            //when loaded, run the following code
            //trigger jcarouselLite
            jQuery('#flickr-thumb-inner-container').jCarouselLite({
                btnNext: "#flickr-next",
                btnPrev: "#flickr-prev"
            });

        });
        
        //wait a second to attach click event listener
        var t=setTimeout(function() {
            jQuery('.flickr-thumb-li').click(displayImage);
        },1000);

    });

});
