/**
 * Picasa Webalbum Integration Library
 * This library was inspired and based on pwa by Dieter Raber (http://www.gnu.org/copyleft/lesser.html)
 * @name pwi-1.0.js
 * @author Jeroen Diderik - http://www.multiprof.nl/
 * @version 1.0
 * @date March 27, 2009
 * @copyright (c) 2008 Jeroen Diderik(www.multiprof.nl)
 * @license Creative Commons Attribution-Share Alike 3.0 Netherlands License - http://creativecommons.org/licenses/by-sa/3.0/nl/
 * @example Visit http://pwi.googlecode.com/ for more informations, duscussions etc about this library
 */

//var filter_location="testlocatie";
var pwi_imagepath="pwi/images/";
var pwi_container_div = "pwiContainer";
var pwi_album_only = 0;
var pwi_album_thumbsize = 64;
var pwi_album_crop = 1;
var pwi_photosize = 800;
var pwi_thumbsize = 64;
var pwi_photo_crop = 1;
var pwi_maxresults = 999;
var pwi_maxtotal = 999;
var pwi_maxalbums = 0;

var pwi_show_albumthumbinfo = true;
var pwi_show_albumdetails = true;
var pwi_show_albumdate = true;
var pwi_show_albumphotocount = true;
var pwi_show_albumlocation = true;
var pwi_show_albumdescription = true;
var pwi_show_backtoalbumslink = true;
var pwi_show_slideshowlink = false;
var pwi_show_photocaption = true;

var pwi_labels = new Array();
pwi_labels["photos"] = "foto's";
pwi_labels["location"] = "locatie";
pwi_labels["backtoalbums"] = "Terug naar albums";
pwi_labels["slideshow"] = "Bekijk slideshow";
pwi_labels["prev"] = "Vorige";
pwi_labels["next"] = "Volgende";

var pwi_currAlbum = "";
var pwi_currPhoto = "";
var pwi_page = "";
var pwi_si = 1;
var pwi_storage_albums;
var pwi_storage_photos;
var pwi_storage_photo;
var pwi_history = "";

//fast string concat function ( var.push(x), var.toString() )
function StringCat(){
	var sp;
	var ep;
	var l = 0;
	this.push = function(what){
		if(typeof(sp) == "undefined"){
			ep = new Array();
			sp = ep;
		}else{
			var oep = ep;
			ep = new Array();
			oep[1] = ep;
		}
		ep[0] = what; ++l;
	};
	this.toString = function(){
		if(l == 0) return;
		while(l > 1){
			var ptr = sp;
			var nsp = new Array();
			var nep = nsp;
			var nl = 0;
			while(typeof(ptr) != "undefined"){
				if(typeof(nep[0]) == "undefined"){
					nep[0] = ptr[0]; ++nl;
				}else{
					if(typeof(ptr[0]) != "undefined") nep[0] += ptr[0];
					nep[1] = new Array();
					nep = nep[1];
				}
				ptr = ptr[1];
			}
			sp = nsp;
			ep = nep;
			l = nl;
		}
		return sp[0];
	};
}

function formatDate(dt){
	var months = new Array(12);
	months[0]="jan"; months[1]="feb"; months[2]="mrt";
	months[3]="apr"; months[4]="mei"; months[5]="jun";
	months[6]="jul"; months[7]="aug"; months[8]="sep";
	months[9]="okt"; months[10]="nov"; months[11]="dec";
	var date = new Date(Number(dt));
	return(date.getDate()+" "+months[date.getMonth()]+" "+date.getFullYear());
}

if(typeof(filter_location) == "undefined") var filter_location = "";
var photoids = new Array();

function albums(j){
	var scAlbums = new StringCat();
	for(var i=0; i<j.feed.entry.length; i++){
		var id_base = j.feed.entry[i].gphoto$name.$t;
		var np = j.feed.entry[i].gphoto$numphotos.$t;
		var album_title = j.feed.entry[i].title.$t;
		var album_date = formatDate(j.feed.entry[i].gphoto$timestamp.$t);
		var album_fotos = pwi_labels["photos"]+": "; if(np > pwi_maxtotal) album_fotos += pwi_maxtotal; else album_fotos += np;
		var album_location = j.feed.entry[i].gphoto$location.$t;
		var thumb = j.feed.entry[i].media$group.media$thumbnail[0].url.replace(new RegExp("/s160-c/","g"),"/");
		if(!filter_location.length || album_location.toLowerCase() == filter_location.toLowerCase()){
			scAlbums.push("<div class='album'>");
			scAlbums.push("<a href='#' onclick='javascript:$.historyLoad(\""+id_base+"/1\");return false;' title='"+album_title+"'>");
			scAlbums.push("<img src='"+thumb+"?imgmax="+pwi_album_thumbsize+"&crop="+pwi_album_crop+"' /></a>");
			pwi_show_albumthumbinfo? scAlbums.push("<a href='#' onclick='javascript:$.historyLoad(\""+id_base+"/1\");return false;' class='title'>"+album_title+"</a>"+(pwi_show_albumdate? album_date+"<br />": "")+(pwi_show_albumphotocount? album_fotos+"<br />": "")+(pwi_show_albumlocation && album_location? pwi_labels["location"]+": "+album_location+"<br />": "")): false;
			scAlbums.push("<br clear='all' /></div>");
		}
	}
	pwi_storage_albums = scAlbums.toString();

	if(pwi_album_only) getAlbum("",1);
	else show(false,pwi_storage_albums);
}

function album(j){
	var scPhotos = new StringCat();
	var len = j.feed.entry.length; if(len > pwi_maxtotal) len = pwi_maxtotal;
	var np = j.feed.openSearch$totalResults.$t; if(np > pwi_maxtotal) np = pwi_maxtotal;
	var album_title = j.feed.title.$t;
	var album_date = formatDate(j.feed.gphoto$timestamp.$t);
	var album_fotos = pwi_labels["photos"]+": "+np;
	var album_location = j.feed.gphoto$location.$t;
	var album_description = j.feed.subtitle.$t;
	
	if(!pwi_album_only){
		scPhotos.push("<p>"+(pwi_show_backtoalbumslink? "<a href='#' onclick='$.historyLoad(\"\");return false;'>"+pwi_labels["backtoalbums"]+"</a>": "")+(pwi_show_slideshowlink? " &nbsp;|&nbsp; <a href='http://picasaweb.google.com/"+pwi_username+"/"+j.feed.gphoto$name.$t+"/photo#s"+j.feed.entry[0].gphoto$id.$t+"' rel='gb_page_fs[]' target='_blank'>"+pwi_labels["slideshow"]+"</a>": "")+"</p>");
		scPhotos.push("<p><span class='title'>"+album_title+"</span>");
		if(pwi_show_albumdetails) scPhotos.push((pwi_show_albumdate? album_date+"<br />": "")+(pwi_show_albumphotocount? album_fotos+"<br />": "")+(pwi_show_albumlocation && album_location? pwi_labels["location"]+": "+album_location+"<br />": "")+"</p>"+(pwi_show_albumdescription && album_description? "<p>"+album_description: ""));
		scPhotos.push("</p>");
	}
	
	if(np > pwi_maxresults){
		pageCount = (np/pwi_maxresults);
		var ppage="",npage="";
		if(pwi_page > 1) ppage = "<a href='#' onclick='$.historyLoad(\""+pwi_currAlbum+"/"+(parseInt(pwi_page)-1)+"\");return false;'>"+pwi_labels["prev"]+"</a>";
		else ppage = pwi_labels["prev"];
		if(pwi_page < pageCount) npage = "<a href='#' onclick='$.historyLoad(\""+pwi_currAlbum+"/"+(parseInt(pwi_page)+1)+"\");return false;'>"+pwi_labels["next"]+"</a>";
		else npage = pwi_labels["next"];
		var navRow = new StringCat();
		navRow.push("<div class='pager'>"+ppage+" &nbsp;|&nbsp; ");
		for(var i=1; i<pageCount+1; i++){
			if(i == pwi_page) navRow.push("<strong>"+i+"</strong> ");
			else navRow.push("<a href='#' onclick='$.historyLoad(\""+pwi_currAlbum+"/"+i+"\");return false;'>"+i+"</a> ");
		}
		navRow.push(" &nbsp;|&nbsp; "+npage+"</div>");
		//scPhotos.push(navRow.toString());
	}
	
	for(var i=0; i<len; i++){
		var img_base = j.feed.entry[i].content.src;
		var id_base = j.feed.entry[i].gphoto$id.$t;
		var photo_caption = j.feed.entry[i].summary.$t;
		scPhotos.push("<div class='photo'>");
		scPhotos.push("<a href='"+img_base+"?imgmax="+pwi_photosize+"' class='thickbox2'"+(np > 1? "rel='"+j.feed.title.$t+"'": "")+(pwi_show_photocaption && photo_caption? " title='"+photo_caption+"'": "")+">");
		scPhotos.push("<img src='"+img_base+"?imgmax="+pwi_thumbsize+"&crop="+pwi_photo_crop+"' /></a>");
		scPhotos.push("</div>");
	}
	
	scPhotos.push("<br clear='all' />");
	scPhotos.push(navRow);
	pwi_storage_photos = scPhotos.toString();
	show(false,pwi_storage_photos);
}

function getAlbums(){
	if(pwi_storage_albums){
		show(false,pwi_storage_albums);
	}else{
		show(true,"");
		var url = "http://picasaweb.google.com/data/feed/api/user/"+pwi_username+"?category=album"+(pwi_maxalbums > 0 ? "&max-results="+pwi_maxalbums: "")+"&access=public&alt=json";
		$.getJSON(url,"callback=?",albums);
	}
}

function getAlbum(albumid,newPage){
	if(albumid != pwi_currAlbum || pwi_page != newPage){
		pwi_page = newPage;
		pwi_currAlbum = albumid;
		pwi_si = ((pwi_page-1)*pwi_maxresults)+1;
		var url = "http://picasaweb.google.com/data/feed/api/user/"+pwi_username+"/album/"+albumid+"?category=photo&max-results="+pwi_maxresults+"&start-index="+pwi_si+"&alt=json";
		show(true,"");
		$.getJSON(url,"callback=?",album);
	}else{
		show(false,pwi_storage_photos);
	}
}

function fromHistory(hash){
	if(hash){
		var a,p;
		if(hash.indexOf("/") > 0){
			a = hash.split("/")[0];
			p = hash.split("/")[1];
			getAlbum(a,p);
		}
	}else{
		getAlbums();
	}
}

function show(loading,data) {
	if(loading){
		$("#"+pwi_container_div).html("<img src='"+pwi_imagepath+"loading.gif' alt='Loading&hellip;' />");
	}else{
		$("#"+pwi_container_div).html(data);
		tb_init('a.thickbox2');
	}
}

$(document).ready(function(){
	$.ajaxSetup({cache: true});
	$("#"+pwi_container_div).addClass("pwi_container");
	$.historyInit(fromHistory);
});
