/* Simple Javascript - JSON Cross-Domain Example */

index_url = 'http://picasaweb.google.com/data/feed/api/user/margit.hartnagel?kind=album&alt=json&callback={callback}'
album_url = 'http://picasaweb.google.com/data/feed/api/user/margit.hartnagel/albumid/'

json = { callbacks: {} }

function showGalleryIndex(json_obj) {
	var feed = json_obj.feed
	var content = document.getElementById('gallery')
	
	for (var i in feed.entry) {
		var entry = feed.entry[i]
		var title = document.createElement('h4')
		content.appendChild(title)
		var title_txt = document.createTextNode(entry.title.$t)
		title.appendChild(title_txt)
		
		var summary = document.createElement('div')
		content.appendChild(summary)
		var summary_txt = document.createTextNode(entry.media$group.media$description.$t)
		summary.appendChild(summary_txt)
		summary.style.position = "absolute"
		summary.style.left = "12%"
		summary.style.marginTop = "1em"
		
		var albumid = entry.gphoto$id.$t
		album_link = document.createElement('a')
		album_link.id = albumid
		album_link.setAttribute('href', entry['id']['$t'])
		album_link.onclick= function() {
			feed_url = album_url + this.id + '?kind=photo&alt=json&callback={callback}'
			load_json(feed_url, 'showGalleryAlbum')
			return false
		}
		content.appendChild(album_link)

		var img = document.createElement('img')
		img.setAttribute('src', entry.media$group.media$thumbnail[0].url)
		img.setAttribute('width', '72')
		img.setAttribute('height', '72')
		album_link.appendChild(img)
	}
}

function showGalleryAlbum(json_obj) {
	var feed = json_obj.feed
	var content = document.getElementById('gallery')
	var h3 = document.getElementsByTagName('h3')[0].firstChild
	h3.nodeValue = 'arbeiten ' + json_obj.feed.title.$t
	clearContent()
	for (i in feed.entry) {
		var entry = feed.entry[i]
		
		var title = entry.media$group.media$description.$t
		var albumid = entry.gphoto$id.$t
		var image_url = entry.media$group.media$content[0].url+'?imgmax=800'

		var album_link = document.createElement('a')
		album_link.className = 'thickbox'
		album_link.setAttribute('href', image_url)
		album_link.setAttribute('title', title)
		album_link.setAttribute('rel', 'album')
		
		album_link.onclick= function() {
			myview = open('', this.title, 'width=800,height=600,left=100,top=20,status=no,scrollbars=no')
			var myview_body = myview.document.getElementsByTagName('body')[0]
			myview_body.style.background = 'url('+this.href+')'
			return false
		}
		content.appendChild(album_link)

		var img = document.createElement('img')
		img.setAttribute('src', entry.media$group.media$thumbnail[0].url)
		img.setAttribute('width', entry.media$group.media$thumbnail[0].width)
		img.setAttribute('height', entry.media$group.media$thumbnail[0].height)
		album_link.appendChild(img)
	}
	return false
}

function load_json(url, callback) {
	var script = document.getElementById('jsondata')
	var headID = document.getElementsByTagName("head")[0]
	if (script) {
		headID.removeChild(script)
	}
	var script = document.createElement('script')
	script.id = 'jsondata'
	script.type = 'text/javascript'
	script.src = url.replace(/{callback}/, callback)
	headID.appendChild(script)
}

function clearContent() {
	var content = document.getElementById('gallery')
	while (content.hasChildNodes()) {
		content.removeChild(content.firstChild)
	}
}

function init_gallery() {
	gallery = document.getElementById('gallery')
	load_json(index_url, 'showGalleryIndex')
}

window.onload = init_gallery
