/*
 * @author      Dieter Raber <dieter@dieterraber.net>
 * @copyright   2004-12-27
 * @version     1.0
 * @license     http://www.gnu.org/copyleft/lesser.html
*/

/* updated by Jesse Berman on May 10, 2007       */
/* for use with the Boston Ironsides RFC website */
/* http://www.bostonironsidesrfc.org/gallery     */

/* updated by Kevin Wong on June 29, 2007         */
/* http://photos.talynkevin.com				     */

/***************/
/* CSS Classes */
/***************/
/* .picasa - <ul> container for list of Picasa Web albums    */
/* .pw_album_list - <li> items for each album in the gallery */
/* .pw_photo_list - <img> element for each photo in an album */
/* .pw_image - <img> element for viewing large-sized photo   */

var _GET = readGet();
var username;
if(!username) $("Username not provided");

// General settings
var photosize, page_title, image_folder;
if(!photosize)		photosize		= 800;
if(!page_title)		page_title		= "";
if(!image_folder)	image_folder		= "http://www.metalyourecycling.com/images";

// Slideshow settings
var slideshow_width, slideshow_height, slideshow_bgcolor;
if(!slideshow_width)	slideshow_width		= 850;
if(!slideshow_height)	slideshow_height	= 650;
if(!slideshow_bgcolor)	slideshow_bgcolor	= "FFFFFF";


/**********************/
/* Display album list */
/**********************/
function picasaweb(j){

	$("<h1>" + page_title + "</h1>");	
	if(j.feed.entry!=null) {
		
		$("<ul class='picasa'>");	
		
		/* Display each album */
		for(i=0;i<j.feed.entry.length;i++){
			
			var album_name = j.feed.entry[i].title.$t;
//			var album_name_picasa = getPicasaAlbumName(album_name);
			var album_name_picasa = getAlbumName(j.feed.entry[i].link[1].href);
			var image = j.feed.entry[i].media$group.media$content[0].url;
			var description = j.feed.entry[i].media$group.media$description.$t;
			var published = j.feed.entry[i].published.$t.slice(0,10);			// only get the date, not the time
			
			$("<li class='pw_album_list' style='list-style-image: none; list-style-type: none;'><a href='?album=" + album_name_picasa + "'><img border='0' src='" + image + "?imgmax=160&crop=1' /><br>");
			$("<span class='album_name'>" + album_name + "</span><br>");
			if(description) $("<span class='description'>" + description + "</span><br>");
			$("<span class='published'>" + published + "</span></a></li>");
			
		}
		
		$("</ul>");
		
	} else $("(none)");
	
} // end function picasaweb


/**********************************/
/* Display all photos in an album */
/**********************************/
function albums(j){

	var photoids = new Array();
	
	var album_name = j.feed.title.$t;
	var album_name_url = encodeUrl(album_name);
//	var album_name_picasa = getPicasaAlbumName(album_name);
	var album_name_picasa = getAlbumName(j.feed.link[1].href);
	var description = j.feed.subtitle.$t;
	
	$("<div class='picasa'>");

	$("<h2>" + album_name);
	$(" <a style='text-decoration: none;' href='?action=slideshow&album=" + album_name_picasa + "&album_name=" + album_name_url + "'><span style='font-size: x-small; font-weight: normal;'>[view slideshow <img border='0' src='" + image_folder + "/image.png' alt='' />]</span></a>");
	$("<span style='position: absolute; right: 1em; width: 10em; font-size: small;'><a href='?' accesskey='u'><img border='0' src='" + image_folder + "/back.gif' alt='<<' /> Back to index</a></span></h2>");
//	$("<h3>" + description + "</h3>");
//	$("<h5>" + j.feed.entry[0].published.$t.slice(0,10) + "</h5>");	// not accurate published date (currently, this represents the upload date of the album, not the actual album date

	// Display each photo
	for(i=0;i<j.feed.entry.length;i++){
		var id_begin = j.feed.entry[i].id.$t.indexOf('photoid/')+8;
		var id_end = j.feed.entry[i].id.$t.indexOf('?');
		var photoid = j.feed.entry[i].id.$t.slice(id_begin, id_end);

		var image = j.feed.entry[i].media$group.media$content[0].url;		
		var title = j.feed.entry[i].title.$t;
		
		$("<a href='?album=" + album_name_picasa + "&photoid=" + photoid + "&index=" + i + "'><img border='0' class='pw_photo_list' src='" + image + "?imgmax=160&crop=1' alt='" + title + "' title='" + title + "' /></a>");
		
	}
	
	$("</div>");

} // end function albums



/*******************/
/* Display a photo */
/*******************/
function photo(j){
	
	var title = j.entry.media$group.media$title.$t;
	var description = j.entry.media$group.media$description.$t;
	
	var image = j.entry.content.src;
	var album_name_url = encodeUrl(album_name);
//	var album_name_picasa = getPicasaAlbumName(album_name);
	var album_name_picasa = _GET['album'];
	var current_photo = index + 1;
	
	var album_base_path = window.location.protocol + "//" + window.location.hostname+window.location.pathname +"?album="+ album_name_picasa;

	var is_video = 0, video_link;
	if (j.entry.media$group.media$content.length > 1 && j.entry.media$group.media$content[1].medium == "video") {
		is_video = 1;
		var begin = j.entry.summary.$t.indexOf('href="')+6;
		var end = j.entry.summary.$t.indexOf('"><img');
		video_link = j.entry.summary.$t.slice(begin, end);
	}
	
	$("<div class='picasa'>");
		
	/* Title */
	$("<h2>" + album_name);
	$(" : " + title);
	$(" <a style='text-decoration: none;' href='?action=slideshow&album=" + album_name_picasa + "&album_name=" + album_name_url + "'><span style='font-size: x-small; font-weight: normal;'>[view slideshow <img border='0' src='" + image_folder + "/image.png' alt='' />]</span></a>");
	$("<span style='position: absolute; right: 1em; width: 10em; font-size: small;'><a href='" + album_base_path + "' accesskey='u'><img border='0' src='" + image_folder + "/back.gif' alt='<<' /> Back to album</a></span></h2>");
	
	
	/* Navigation */
	var first_link = album_base_path + "&photoid=" + first + "&index=" + 0;
	var prev_link = album_base_path;
	if(index>0) prev_link += "&photoid=" + prev + "&index=" + (index-1);
	var next_link = album_base_path;
	if(index<(album_total-1)) next_link += "&photoid=" + next + "&index=" + (index+1);
	var last_link = album_base_path + "&photoid=" + last + "&index=" + (album_total-1);
	
	$("<div style='text-align: center;'>");
	$("<a href='" + first_link + "'><img border='0' src='" + image_folder + "/first.png' alt='First' title='First picture'></a> ");
	$("<a href='" + prev_link + "'><img border='0' src='" + image_folder + "/previous.png' alt='Previous' title='Previous picture'></a> ");
	$("<span style='font-weight: bold;'>"+current_photo+" of "+album_total+"</span> ");
	$("<a href='" + next_link + "'><img border='0' src='" + image_folder + "/next.png' alt='Next' title='Next picture'></a> ");
	$("<a href='" + last_link + "'><img border='0' src='" + image_folder + "/last.png' alt='Last' title='Last picture'></a>");
	$("</div>");
	
	
	/* Display image and caption */
	if(is_video) $("<a href='" + video_link + "'>");
	$("<div style='text-align: center;'><img border='0' id='picture' src='" + image + "?imgmax=" + photosize + "' class='pw_image'");
	if(is_video) $(" title='View this video at Picasa Web Albums'");
	$("/></div>");
	if(is_video) $("</a>");
	$("<h3 style='text-align: center;'>" + description + "</h3>");
	
	
	/* EXIF info */
//	if(j.entry.exif$tags){
//		var photo_exif;
//		photo_exif = "<a style='float: left;' onclick='toggle(\"exif_info\");'><img src='" + image_folder + "/zoom.png' alt='' /> Details</a>";
//		photo_exif += "<table id='exif_info' style='display: none; margin-left: 30px;'>";
//		photo_exif += "<caption>Exchangeable Image File Data (Exif)</caption>";
//		for(var exif in j.entry.exif$tags){
//			photo_exif += "<tr><td width='120'><b>"+exif.replace("exif$","")+":</b> </td><td>"+j.entry.exif$tags[exif].$t+"</td></tr>";
//		}
//		photo_exif += "</table>";
//		$(photo_exif);
//	}
	
	
	/* Save photo */
//	$("<a title='' style='float: right; vertical-align: top;' href='" + image + "?imgmax=800&imgdl=1'><img src='" + image_folder + "/page_save.png' alt='' /> Save photo</a>");		// NOTE: max 800 pixel size for non *.blogspot.com & *.google.com referrers
		// (according to http://groups.google.com/group/Google-Picasa-Data-API/browse_thread/thread/1daaa1bb807a25e8/ec58f08cc8b6f75c)
	
	
	/* Keyboard shortcuts */
	$("<script language='javascript'>");
	$("function testKeyCode( evt, intKeyCode ) {");
	$("if ( window.createPopup ) return evt.keyCode == intKeyCode; else return evt.which == intKeyCode; }");
	$("document.onkeydown = function ( evt ) {");
	$("if ( evt == null ) evt = event;");
	$("if ( testKeyCode( evt, 37 ) ) { window.location='" + prev_link + "'; return false; }");
	$("if ( testKeyCode( evt, 39 ) ) { window.location='" + next_link + "'; return false; }");
	$("}");
	$("</script>");
	
	$("</div>");
	
} // end function photo



/************************************/
/* Get the photo list from an album */
/************************************/
var album_total, album_name, index;
var first, prev, next, last;
function getPhotoList(j){

	album_total = j.feed.openSearch$totalResults.$t;
	album_name = j.feed.title.$t;
	index = parseInt(_GET['index']);

	/* Get photoids for navigation */	
	first = getPhotoId(j.feed.entry[0].id.$t);
	if(index>0) prev = getPhotoId(j.feed.entry[index-1].id.$t);
	if(index<(album_total-1)) next = getPhotoId(j.feed.entry[index+1].id.$t);
	last = getPhotoId(j.feed.entry[album_total-1].id.$t);
	
} // end function getPhotoList



/************************************************/
/* Return the photo id from the entry id object */
/************************************************/
function getPhotoId(entry_id) {
	var id_begin = entry_id.indexOf('photoid/')+8;
	var id_end = entry_id.indexOf('?');
	return entry_id.slice(id_begin, id_end);
}



/***********************/
/* Display a slideshow */
/***********************/
function slideshow(album, album_name) {
	album_name = decodeUrl(album_name);
	$("<h2>" + album_name + "<span style='position: absolute; right: 1em;	width: 10em; font-size: small;'><a href='?album=" + album + "' accesskey='u'><img border='0' src='" + image_folder + "/back.gif' alt='<<' /> Back to album</a></span></h2>");
	$("<div style='text-align: center;'><embed type='application/x-shockwave-flash' src='http://picasaweb.google.com/s/c/bin/slideshow.swf' width='" + slideshow_width + "' height='" + slideshow_height + "' flashvars='host=picasaweb.google.com&captions=1&RGB=0x" + slideshow_bgcolor + "&feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2F" + username + "%2Falbum%2F" + album + "%3Fkind%3Dphoto%26alt%3Drss' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></div>");	
}


/*************************/
/* Get feeds from Picasa */
/*************************/
if(_GET['photoid']&&_GET['album']){
	$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'/album/'+_GET['album']+'?category=photo&alt=json&callback=getPhotoList"></script>'); //get photo list for navigation
	$('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/entry/api/user/'+username+'/album/'+_GET['album']+'/photoid/'+_GET['photoid']+'?alt=json&callback=photo"></scr'+'ipt>');//photo by album name
} else if(_GET['photoid']&&_GET['albumid']){
	$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=getphotolist"></script>'); //get photo list for navigation
	$('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/entry/base/user/'+username+'/albumid/'+_GET['albumid']+'/photoid/'+_GET['photoid']+'?alt=json&callback=photo"></scr'+'ipt>');//photo by albumid
} else if(_GET['album']&&_GET['action']=="slideshow"){
	slideshow(_GET['album'],_GET['album_name']);//slideshow
} else if(_GET['album']&&!_GET['photoid']){
	$('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'/album/'+_GET['album']+'?category=photo&alt=json&callback=albums"></scr'+'ipt>');//albums by album name
} else if(_GET['albumid']&&!_GET['photoid']){
	$('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=albums"></scr'+'ipt>');//albums by albumid
} else{
	$('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'?category=album&alt=json&callback=picasaweb&access=public"></scr'+'ipt>');//picasaweb
}


/******************************/
/* Show/Hide an element by id */
/******************************/
function toggle(id) {
	var obj = document.getElementById(id);
	obj.style.display = obj.style.display == 'none' ? 'inline' : 'none';
}


/**************************************/
/* Replace special characters for url */
/**************************************/
function encodeUrl(string) {
	string = encodeURIComponent(string);
	string = string.replace(/'/g,"%27");
	return string;
}


/***************************************/
/* Replace special characters from url */
/***************************************/
function decodeUrl(string) {
	string = decodeURIComponent(string);
	string = string.replace(/%27/g,"'");
	return string;
}

/*********************************************/
/* Remove special characters from album name */
/*********************************************/
function getPicasaAlbumName(album) {
	var deletableCharacters = / |\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\-|\[|\]|\{|\}|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\./g;
	return album.replace(deletableCharacters, "");
}

/*************************************/
/* Return album name from Picasa url */
/*************************************/
function getAlbumName(url){
	var urlSections = url.split("/");
	return urlSections.pop();
}

/*********************/
/* Retrieve GET vars */
/*********************/
function readGet(){
	var _GET = new Array();
	var uriStr  = window.location.href.replace(/&amp;/g, '&');
	var paraArr, paraSplit;
	if(uriStr.indexOf('?') > -1) {
		var uriArr  = uriStr.split('?');
		var paraStr = uriArr[1];
	} else {
		return _GET;
	}
	if(paraStr.indexOf('&') > -1) {
		paraArr = paraStr.split('&');
	} else {
		paraArr = new Array(paraStr);
	}
	for(var i = 0; i < paraArr.length; i++) {
		paraArr[i] = paraArr[i].indexOf('=') > -1 ? paraArr[i] : paraArr[i] + '=';
		paraSplit  = paraArr[i].split('=');
		_GET[paraSplit[0]] = decodeURI(paraSplit[1].replace(/\+/g, ' '));
	}
	return _GET;
}

function $(a){document.writeln(a);}
