Flickr = function(container) {
    this.key = 'bba0d7c177a5a1f50d94e469e57f3d26';
    this.container = container;
    this.no_images = 0;
    this.mode = 'R';
}

// Public
Flickr.prototype.reset_photos = function() {
    var window_width = $(window).width();
    var window_height = $(window).height();
    
    if (this.boxy) {
        this.boxy.center();
        var size = this.boxy.getSize()
        if (size[0] >= window_width || size[1] >= window_height) {
            this.boxy.hide();
        }
    }
    

    var images_per_line = Math.floor(window_width / 89);
    if ($.browser.msie && $.browser.version == '6.0') {
        images_per_line = images_per_line - 1;
        $(this.container).width(images_per_line * 90);
    }
    else {
        $(this.container).width(images_per_line * 89);
    }
    
    var total_images = images_per_line * 2;
    if (this.no_images != total_images) {
        this.no_images = total_images;
        
        $(this.container).empty();
        for (var i = 0; i < this.no_images; i++) {
            $(this.container).append('<div class="flickr_image spinner"></div>');
        }
        if (this.mode == 'I') {
            this.display_interesting();
        }
        else {
            this.display_recent();
        }
    }
}

Flickr.prototype.display_interesting = function() {
    this.mode = 'I';
    this.get_photos('interestingness-desc');
}

Flickr.prototype.display_recent = function() {
    this.mode = 'R';
    this.get_photos('date-posted-desc');
}

// Private
Flickr.prototype.get_photos = function(mode) {
    this.display_spinners();
    
    var url = 'http://api.flickr.com/services/rest/?&method=flickr.photos.search&api_key=' + this.key + '&user_id=43159524@N00&per_page=' + this.no_images + '&extras=url_sq,url_m,url_o,license,date_taken,tags,path_alias&sort=' + mode + '&format=json&jsoncallback=?';
    
    var self = this;
    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'jsonp',
        timeout: 2000,
        success: function(data) {
            self.display_photos(data.photos.photo);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            self.display_error();
        }
    });
}

Flickr.prototype.display_spinners = function() {
    var self = this;
    $('#flickr .flickr_image').each(function(index, element) {
        if (index < self.no_images) {
            $(this).addClass('spinner');
            $(this).empty();
        }
    });
}

Flickr.prototype.display_error = function() {
    // Show sad mac icon
    // alert('flickr error');
}

Flickr.prototype.display_photos = function(photos) {
    var self = this;
    $('#flickr .flickr_image').each(function(index, element) {
        var flickr_image = this;
        if (index < self.no_images && index < photos.length) {
            var photo = photos[index];
            
            var a = $('<a></a>');
            a.attr('href', '#');
            a.click(function() {
                self.display_photo(photo);
                return false;
            });
            $(this).html(a);
            
            var img = $('<img>');
            img.load(function() {
               $(flickr_image).removeClass('spinner');
               a.html(img);
            });
            img.css('opacity', '0.3');
            img.attr('src', photo.url_sq);
            img.attr('alt', photo.title);
            
            
            img.mouseenter(function() {
                $(this).animate({ opacity:1.0 }, 200);
            });
            
            img.mouseleave(function() {
                $(this).animate({ opacity:0.3 }, 1000);
            });
        }
    });
}

Flickr.prototype.display_photo = function(photo) {
    var window_width = $(window).width();
    var window_height = $(window).height();
    
    if (((parseInt(photo.width_o) + 50) < window_width) && ((parseInt(photo.height_o) + 50) < window_height))
    {
        var width = photo.width_o;
        var height = photo.height_o;
        var url = photo.url_o;
    }
    else
    {
        var width = photo.width_m;
        var height = photo.height_m;
        var url = photo.url_m;
    }
    
    var flickr_url = 'http://flickr.com/photos/' + photo.pathalias + '/' + photo.id;
    
    var content = $('<div></div>');
    
    var image_div = $('<div></div>');
    image_div.addClass('boxy-spinner');
    content.append(image_div);
    
    var img = $('<img>');
    img.addClass('flickr-boxy');
    img.attr('width', width);
    img.attr('height', height);
    img.css('visibility', 'hidden');
    img.attr('alt', photo.title);
    
    var a = $('<a></a>')
    a.attr('href', flickr_url);
    a.html(img);
    
    image_div.html(a);
    
    img.load(function() {
        image_div.removeClass('spinner');
        img.hide();
        img.css('visibility', 'visible');
        img.fadeIn();
    });
    img.attr('src', url);
    
    var caption = $('<p class="caption"></p>').text(photo.title);
    caption.append(' &mdash; ');
    caption.append($('<a>view on Flickr</a>').attr('href', flickr_url));
    content.append(caption);

    this.boxy = new Boxy(content, {title: photo.title, modal: true, unloadOnHide: true});
}