/* 
	Picasaweb integration

	Copyright 2007 Scott Penrose <scottp@dd.com.au>
*/

var picasaweb = {
	/* withid - object/hash of generated Functions for callbacks */
	withid: {},

	/* nextid - Sequence for generating IDs */
	nextid: 0,

	/* href - the URL for the call - This allows you to change this without the rest
	*/
	href: function(q, callback, max) {
		return 'http://picasaweb.google.com/data/feed/api/all?q=' + q + '&alt=json-in-script&callback=' + callback + '&access=public&start-index=1&max-results=' + max;
	},

	/* search - search picasaweb
		q = Search string free form
		max = Maximum number of images to return, default = 5 
		id = element ID to add results to, default is in current place (automatic)
	*/
	search: function(q, max, id) { with (document) {
		// Default 5 entries
		if (!max || (max == 0)) { max = 5; }

		// No id - assume current location and make an id
		if (!id) {
			picasaweb.nextid = picasaweb.nextid + 1;
			id = 'PicasaGallery' + picasaweb.nextid;
			document.write('<div class="previewset" id="' + id + '"></div>');
		}

		var href = picasaweb.href(q, 'picasaweb.withid.' + id, max);
		var span = createElement('SPAN');
		span.style.display = 'none';
		body.insertBefore(span, body.lastChild);

		// Generate the stand alone Callback (each id has to have one)
		picasaweb.withid[id] = new Function("data", "picasaweb.renderer(data, '" + id + "');");
		
		span.innerHTML = 'Text for IE.' + '<s'+'cript></' + 'script>';
		setTimeout(function() {
			var s = span.getElementsByTagName('script')[0];
			s.language = 'JavaScript';
			if (s.setAttribute) s.setAttribute('src', href); else s.src = href;
		}, 10);
	}},

	/* renderer - output the results - this is the callback from Google
		data - JSON returned from Google (via the generated Function above)
		id - the html element to add it to
	*/
	renderer: function(data, id) {
		if (data && data.feed && data.feed.entry && data.feed.entry.length && data.feed.entry.length > 0) {
			for (var i = 0; i < data.feed.entry.length; i++) {
				picasaweb.item(data.feed.entry[i], id);
			}
		} else {
			var newDiv = document.createElement("DIV");
			newDiv.className = "imageElement";
			newDiv.innerHTML = '<b>No images found</b>';
			document.getElementById(id).appendChild(newDiv);
		}
	},


	/* item - add a new photo entry
		item - a JSON item to add
		divid - the HTML element to add it to
	*/
	item: function(item, divid) {
		var link = item.link;
		var href = "";
		for (var i = 0; i < link.length; i++) {
			if (link[i].rel == 'alternate' && link[i].type == 'text/html') {
				href= link[i].href;
			}
		}

		picasaweb.add(	
			divid, 
			href, 
			item.content.src,			// Image - thumbnail size
			item.title.$t,				// the filename
			item.media$group.media$description.$t	// Picasa Web photo caption
		);
	},

	/* add - the image presentation part (this one outputs Zaltana compatible)
		id = Element id to add to
		href = Where to link to
		src = full image src  (add ?imgmax=72 for thumbnail)
		title = the name, file or title
		description = the description
	*/
	add: function(id, href, src, title, description) {
		// XXX Why mixed innerHTML vs document model !
		var newDiv = document.createElement("DIV");
		newDiv.className = "preview";
		// TODO Consider adding info re: Picasaweb !
		newDiv.innerHTML = ''
				+ '<p>'
				// + '<b>' + '<a href="' + href + '">' + title + '</a>' + '</b>'
				+ '<br />'
				// TODO How do we make sure this is all entified? - do we need to?
				+ '<a class="preview" href="' + src + '?imgmax=640" title="' + title + ' - ' + description  + '" '
				+ 'rel="lyteshow[' + id + ']"'
				+ '>'
				+ '<img src="' + src + '?imgmax=72" />'
				+ '</a>'
				+ '<br />'
				+ description
				+ ' <a target="_new" href="' + href + '">more...</a>'
				+ '</p>'
				;

		document.getElementById(id).appendChild(newDiv);
	}

};


