// Attach the flickr libraries
document.write('<script type="text/javascript" src="js/jsr_class.js"></script>');
document.write('<script type="text/javascript" src="js/parsetext.js"></script>');
//Wait for libraries to load
setTimeout(function() {

    MyFlickr = Class.create();

    MyFlickr.prototype = {
        initialize: function(container){
            this.container = container;
            this.api_key = '5e381272088f1d247ef56a05fec98871';
            this.user_id = '29432483@N00';
            this.fav_tag = 'vip';
            this.index = 1;
            this.photos = [];
            this.findPhotos();
        },
        // create holders to put the images in
        buildPage: function(){
            photoHolder = document.createElement('div');
            photoHolder.setAttribute('id', 'photoHolder')
            this.container.appendChild(photoHolder);
            imageHolder = document.createElement('div');
            imageHolder.setAttribute('id', 'imageHolder');
            this.container.appendChild(imageHolder);
            this.findPhotos();
        },
        // Find all photos with the fav_tag
        findPhotos: function(){
            var req = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&jsoncallback=' +
                      'FlickrBuilder.getPhotos&api_key='+this.api_key+'&user_id='+this.user_id+'&tags='+this.fav_tag+'&format=json';
            bObj = new JSONscriptRequest(req);
            bObj.buildScriptTag();
            bObj.addScriptTag();
        },
        // Callback function for findPhotos
        getPhotos: function(jsonData) {
            if(jsonData.stat.indexOf('ok') != -1) {
                this.photos = jsonData.photos.photo;
                if(this.photos.length) 
                {
                    FlickrBuilder.album.startup();
                }
                else 
                {
                    // alert('No Images Found....');
                }
            }
            else {
                if(jsonData.message.indexOf('Invalid API Key') != -1) 
                {
                    alert('ERROR: Please check your text document and verify you entered the correct API Key.');
                }
                else if(jsonData.message.indexOf('Unknown user') != -1) 
                {
                    alert('ERROR: Please check your text document and verify you entered the correct User ID.');
                }
                else 
                {
                    alert('ERROR: Please check your text document and verify your information is in the correct format.');
                }
            }
            bObj.removeScriptTag(); 
        },
        // loop through all photos and build thumbnails for the photoHolder
        buildPhotoHolder: function(photo, start) {
            this.findOriginal(photo.id, photo.secret);
        },
        // Retrieve the original image from the server
        findOriginal: function(id, secret){
            var req  = 'http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&jsoncallback=FlickrBuilder.displayOriginal' +
                       '&api_key='+this.api_key+'&photo_id='+id+'&secret='+secret+'&format=json';
            rObj = new JSONscriptRequest(req);
            rObj.buildScriptTag();
            rObj.addScriptTag();
        },
        // Define the callback function
        displayOriginal: function(jsonData) {
            var photo = jsonData.photo;
            var src = 'http://farm'+photo.farm+'.static.flickr.com/'+photo.server+'/'+photo.id+'_'+photo.secret+'.jpg';
            var originalImage = document.createElement('img');
            originalImage.src = src;
            //originalImage.title = photo.title._content;
            //originalImage.alt = photo.title._content;
            $('imageHolder').innerHTML = '';
            $('imageHolder').appendChild(originalImage);
            new Effect.Appear('imageHolder', {duration: 1.5, queue:'end'});
            
            rObj.removeScriptTag(); 
        }
    };
    
    // initialize flickrbuilder
    var container = $('content');
    if(container) {
        FlickrBuilder = new MyFlickr(container);
    }
    
    FlickrBuilder.album = { 
        startup: function() { 
            new PeriodicalExecuter(FlickrBuilder.album.cycle, 5) // change image every 5 seconds 
        },  
        cycle: function() {
            new Effect.Fade('imageHolder', { duration: 1,  afterFinish: function() {
                    if(FlickrBuilder.index == FlickrBuilder.photos.length) { FlickrBuilder.index = 0; }
                    var photo = FlickrBuilder.photos[FlickrBuilder.index];
                    FlickrBuilder.index = FlickrBuilder.index + 1;
                    FlickrBuilder.buildPhotoHolder(photo, false);
                }
            });
        } 
    };
    
},2000);
