var flickrImageSet = Class.create({

	initialize : function (id) {
		this.id = null;
		this.targetElement = null;

		this.title = null;
		this.description = null;
		
		this.photos = [];
		this.photosLoaded = false;

		if (id) { this.id = id; }
	},
	
	loadInfo : function(transport) {
		if (!transport) {
	
			var url = 'xml.php?url=' + encodeURIComponent("http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&api_key=" + api_key + "&photoset_id=" + this.id);
				
			new Ajax.Request(url, {
				method : 'get',
				onSuccess : this.loadInfo.bind(this)
			});
		}
		else {
			this.title = (Prototype.Browser.IE) ? transport.responseXML.getElementsByTagName("title").item(0).text:transport.responseXML.getElementsByTagName("title").item(0).textContent;
			this.description = (Prototype.Browser.IE) ? transport.responseXML.getElementsByTagName("description").item(0).text:transport.responseXML.getElementsByTagName("description").item(0).textContent;
			this.loadPhotos();
		}
	},
	
	loadPhotos : function(transport) {
	
		if (!transport) {
			var url = 'xml.php?url=' + encodeURIComponent("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=" + api_key + "&photoset_id=" + this.id);
			
			new Ajax.Request(url, {
				method : 'get',
				onSuccess : this.loadPhotos.bind(this)
			});
		}
		else {
			inPhotos = $A(transport.responseXML.getElementsByTagName("photo"));
			inPhotoCount = inPhotos.length;
			
			for (i=0; i < inPhotoCount; i++) {

				photo = new flickrImage(inPhotos[i].getAttribute("id"));
				photo.title = inPhotos[i].getAttribute("title");
				photo.loadInfo();
				
				this.photos.push(photo);			
			}
			
			this.photosLoaded = true;
			this.display();
		}	
	},

	display : function() {
		if (this.targetElement) {
		
			title = new Element("h1", { "style": "clear: left" }).update(this.title);			
			this.targetElement.appendChild(title);

			if (this.description) {
				description = new Element("p").update(this.description);	
				this.targetElement.appendChild(description);
			}

			this.targetElement.appendChild(new Element("hr"));
			
			photoCount = this.photos.length;
			
			for (i=0; i < photoCount; i++) {
				link = new Element("a", { 'class': 'thumbnail', 'href': "#" });
				image = new Element("img", { "width": "75", "height": "75", "border": "0", 'id': ("flickrImage-" + this.photos[i].id), "src": "wp-content/themes/ickaboo/images/zoom/zoom-spin-1.png", "alt": this.photos[i].title })
				
				link.update(image);
				
				this.targetElement.appendChild(link);
			}		
		}
	}
});

var flickrImage = Class.create({
	initialize : function (id) {
		this.id = null;
		
		this.sizes = null;
		
		if (id) { this.id = id; }
	},
	
	loadInfo : function(transport) {
		if (!transport) {
			var url = 'xml.php?url=' + encodeURIComponent("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=" + api_key + "&photo_id=" + this.id);
			
			new Ajax.Request(url, {
				method : 'get',
				onSuccess : this.loadInfo.bind(this)
			});
		}
		else {
			this.sizes = transport.responseXML.getElementsByTagName("size");
			this.display();
		}
	},
	
	imageSourceForSize : function(size) {
		if (this.sizes.length > 0) {
			return this.sizes[size].getAttribute("source");
		}
	},
	
	biggestImage : function() {
		if (this.sizes.length > 0) {
			biggestSize = this.sizes[this.sizes.length-1];
												
			if (biggestSize.getAttribute("width") > 1024 || biggestSize.getAttribute("height") > 1024) {
				biggestSize = this.sizes[this.sizes.length-2];
			}
			
			return biggestSize.getAttribute("source");
		}
	},
	
	display : function() {
		$("flickrImage-" + this.id).width = 75;
		$("flickrImage-" + this.id).height = 75;
		$("flickrImage-" + this.id).src = this.imageSourceForSize(0);
		$("flickrImage-" + this.id).parentNode.setAttribute("href",this.biggestImage());
		setupZoom();
	}
});