var theCurrentPage = 1;
var theStartingIndex = 1;
var pageCount;
var theAlbum;

//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 album(j)
{
	theAlbum = j;
	theCurrentPage = 1;
	if (theAlbum.feed.entry.length > 0)
	{
			pageCount = Math.floor(1 + ((theAlbum.feed.entry.length-1)/12));
			showpage();
	}
}

function preppage(newPageNo)
{
	theCurrentPage = parseInt(newPageNo);
	$('#container').fadeOut('fast',showpage);
}

function showpage() {

	var thePhotos = new StringCat();
	var theNavigation = new StringCat();

	theStartingIndex = ((theCurrentPage - 1) * 12) + 1;	

	if (theCurrentPage > 1) 
	{
		theNavigation.push("<a href='javascript:void(0)' onclick='preppage(\"" + (theCurrentPage - 1) + "\")'><img src='images/left_arrow.png' class='navicon'></a>");
	}
	else
	{
		theNavigation.push("<img src='images/left_arrow.png' class='inactive'>");
	}
		
	for (var i = 1; i <= pageCount; i++)
	{
		if (i == theCurrentPage)
		{
			theNavigation.push("<img src='images/redcircle.png' class='navicon'>");
		}
		else
		{
			theNavigation.push("<a href='javascript:void(0)' onclick='preppage(\"" + i + "\")'><img src='images/circle.png' class='navicon'></a>")
		};
	};
	
	if (theCurrentPage < pageCount)
	{
		theNavigation.push("<a href='javascript:void(0)' onclick='preppage(\"" + (theCurrentPage + 1) + "\")'><img src='images/right_arrow.png' class='navicon'></a>");
	}
	else
	{
		theNavigation.push("<img src='images/right_arrow.png' class='inactive'>");
	};
			
	thePhotos.push("<center><table border=0><tr>");

	var len = theAlbum.feed.entry.length;
	var LastPhotoIndex = theStartingIndex + 11;
	if (LastPhotoIndex >= len) LastPhotoIndex = len - 1;
	for (var i = theStartingIndex - 1; i < LastPhotoIndex; i++) {
		var img_base = theAlbum.feed.entry[i].content.src;
		var title = theAlbum.feed.entry[i].title.$t;
		thePhotos.push("<td valign=top><center><a href='" + img_base + "?imgmax=800' class='lightbox' title='" + title + "'><img src='" + img_base + "?imgmax=144&crop=0' class='photo'/></a><br/>");
		if (i % 4 == (3)) {
			thePhotos.push("</tr><tr>");
		}
	}
	thePhotos.push("</tr></table>");
	
	$('#picnav').html(theNavigation.toString());
					   
	show(false, thePhotos.toString());
	
	theBody = thePhotos.toString();
}

function show(loading, data) {
	$('#container').html(data).fadeIn('fast');
	$('a.lightbox').attr('rel','lightbox[thispage]');
	$('a.lightbox').lightbox();
}

$(document).ready(function() {
	
		$.ajaxSetup({cache: true});
		$.getJSON('http://picasaweb.google.com/data/feed/base/user/fcaceres62/albumid/5317823064419236209?alt=json&kind=photo&authkey=Gv1sRgCPLnv5uh9fDNUQ&hl=en_US','callback=?',album);
});