/*  -----------------------------------------------------------------------------------------------------------	*/
/*  Class: FlickrFeed																							*/
/*	Description: This class is used to process a Flickr API feed.   				*/
/*	Author: Mike Biang (mbiang@cramerdev.com)																	*/
/*	Copyright: 2007 Cramer Development Inc.																		*/
/*  ----------------------------------------------------------------------------------------------------------- */

var FlickrFeed = Class.create();
FlickrFeed.prototype = {
	imageClass: '',
	lightboxAlbum: '',
	linkClass: '',
	useLightbox: true,
	photos: [],
	containers: [],
	initialize: function (){

	},
	load: function (flick_api_feed){
		for(var i=0;i<flick_api_feed.photos.photo.length;i++){
			this.photos[i] = new FlickrPhoto(flick_api_feed.photos.photo[i]);
		}
	},
	setLocation: function (element, quantity) {
		if(quantity == undefined){
			quantity = this.photos.length;	
		}
		this.containers[this.containers.length] = {id: element, quantity: quantity};
	},
	write: function(){
		var total_written = 0;
		for(var i=0;i<this.containers.length;i++){
			total_written = 0;
			element = $(this.containers[i].id);
			quantity = this.containers[i].quantity;
			while(total_written < quantity){
				var index = total_written % this.photos.length;
				var photo = this.photos[index];
				var media = photo.getURL('s');
				photo_html = '<a ';
				if(this.useLightbox){
					photo_html += 'rel="lightbox['+ this.lightboxAlbum + ']" ';
				}
				photo_html += 'href="' + photo.getURL('') + '" title="' + photo.getExtendedTitle() + '" class="' + this.linkClass + '"><img class="' + this.imageClass + '" src="' + photo.getURL('s') + '" alt="' + photo.title + '" /></a>';
				new Insertion.Bottom(element,photo_html);
				total_written++;
			}
		}
	}
};


/*  -----------------------------------------------------------------------------------------------------------	*/
/*  Class: FlickrPhoto																							*/
/*	Description: This class is used to represent a Flickr photo.  FlickrFeed utilizes this class. 				*/
/*	Author: Mike Biang (mbiang@cramerdev.com)																	*/
/*	Copyright: 2007 Cramer Development Inc.																		*/
/*  ----------------------------------------------------------------------------------------------------------- */

var FlickrPhoto = Class.create();
FlickrPhoto.prototype = {
	id: '',
	title: '',
	date: '',
	author: '',
	server: '',
	secret: '',
	farm: '',
	initialize: function (feed_item){
		this.id = feed_item.id;
		this.title = feed_item.title;
		this.date = feed_item.datetaken;
		this.author = feed_item.ownername;
		this.farm = feed_item.farm;
		this.secret = feed_item.secret;
		this.server = feed_item.server;
	},
	getURL: function (size){
		if(size != ''){
			size = '_' + size;
		}
		return 'http://farm' + this.farm + '.static.flickr.com/' + this.server + '/' + this.id + '_' + this.secret + size + '.jpg';
	},
	formatDate: function (){
		var calendar_date = this.date.split(' ');
		var date_parts = calendar_date[0].split('-');
		return date_parts[1] + '.' + date_parts[2] + '.' + date_parts[0];	
	},
	getExtendedTitle: function (){
		var extended_title = this.title + ', taken on ' + this.formatDate();
		if(this.author != ''){
			extended_title += ' by ' + this.author;
		}
		return extended_title;
	}
};