if( typeof at == "undefined" )
   at = {};
if( typeof at.treemotion == "undefined" )
   at.treemotion = {};
if( typeof at.treemotion.Picasa == "undefined" )
	   at.treemotion.Picasa = {};

at.treemotion.PicasaFeed = {
	feedurl : '',
	numEntries : 5,
	entries : false,
	feed : false,
	initialize : function(feedUrl){
		this.feedUrl = feedUrl;
		var feed = new google.feeds.Feed(this.feedUrl);
		feed.setNumEntries(this.numEntries);
 		feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
 		var me = this;
     	feed.load(function(result) {
			if (!result.error) {
				me.entries = result.feed.entries;
				showFeed(me.entries[0]);
			}else{
				alert(result.error.message);
			}
      });
	}	
}

at.treemotion.Picasa.Gallery = Class.create({
	// width of thumbnails, image ///////////////////
	// 72, 144, 200, 288, 320, 400, 512, 576, 640, 
	// 720, 800, 912, 1024, 1152, 1280, 1440, 1600	

	// namespaces
	ns_media : "http://search.yahoo.com/mrss/",
	ns_atom   : "http://www.w3.org/2005/Atom",

	// main container
	main_container : 'picasa_gallery',

	// image container
	image_container : 'picasa_gallery_image',

	// spinner container
	spinner_container : 'picasa_spinner',
	
	// imgmax
	imgmax : 320,
	
	// image width,height
	imageWidth : 400,
	imageHeight : 225,

	// album feed url
	feedUrl : '',
	
	// album entries
	entries : false,
	
	// api Variablen
	numEntries : 1000,
		
	// loading ...
	loadingHTML	: 'loading...',

	// position of current image
	pos : 0,
	
	initialize : function(feedUrl){
		// load feed
		this.feedUrl = feedUrl;
		//this.loadingHTML = $(this.spinner_container).innerHTML;
		this.loadFeed();
	},

	// load feed
	loadFeed : function() {
		var feed = new google.feeds.Feed(this.feedUrl);
		feed.setNumEntries(this.numEntries);
 		feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
 		var me = this;
     	feed.load(function(result) {
			if (!result.error) {
				me.entries = result.feed.entries;
				me.loadImage();

			}else{
				$(me.image_container).update(result.error.message);
			}
      });
	},

	// load image
	loadImage : function(){	
		var entry = this.entries[this.pos];
		var url =  this.nodeAttributeNS(entry,'content','url')
		var me = this;
		$(me.image_container).update(me.loadingHTML);
		var image = new Image();
		image.onload = function() {
			$(me.image_container).update(image);
			$(me.main_container).down('div.title').update(entry.title);
			$(me.main_container).down('div.info').update(eval(me.pos + 1) + '/' + me.entries.length);
			if(me.pos > 0){
				$(me.main_container).down('a.prev').show();
			}
		};
		image.src = url + '?imgmax=' + this.imgmax;
	},
	
	// show next image
	nextImage : function(){
		if(this.pos == this.entries.length - 1){
			this.pos = 0;
		}else{
			this.pos++;
		}
		this.loadImage();
	},

	// show prev image
	prevImage : function(){
		if(this.pos == 0){
			this.pos = this.entries.length - 1;
		}else{
			this.pos--;
		}
		this.loadImage();
	},
	
	// helper functions
	nodeAttributeNS : function(entryObj,nodeName,attribute) {
		var node = google.feeds.getElementsByTagNameNS(entryObj.xmlNode,this.ns_media, nodeName)[0];
		return node.getAttribute(attribute);
	},
	
	nodeValue : function(entryObj,nodeName) {
		var node = entryObj.xmlNode.getElementsByTagName(nodeName)[0];
		if(node.firstChild.nodeValue)
			return node.firstChild.nodeValue;
		return "";
	}	
});

