


//Global user variables to set
var ellgallery_username = "cusbari";
var ellgallery_photosize = 640; //return maximum size picture (see for supported format:  http://code.google.com/apis/picasaweb/reference.html#Parameters)
var ellgallery_thumbsize = 160; //size thumb /cropped(see for supported format:  http://code.google.com/apis/picasaweb/reference.html#Parameters)
var ellgallery_albumcols = 3; // distribute thumbs on main page over x collums
var ellgallery_cols = 3; // distribute thumbs on albumphotos page over x collums
var ellgallery_maxresults = 42; //maximum of pictures on albumphotos page
var ellgallery_maxalbums = 42; //maximum of pictures on albumphotos page
var ellgallery_container_div = "#photogallery-container";


// ****  DONT CHANGE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING ****
//preset globals
var ellgallery_currAlbum = "";
var ellgallery_currAlbumTitle = "";
var ellgallery_currPhoto = "";
var ellgallery_page = "";
var ellgallery_si = 1;
var ellgallery_storage_albums;
var ellgallery_storage_photos;
var ellgallery_storage_photo;
var ellgallery_history = "";

// various functions
//get querytring (for later use to make deeplinks)
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;
};
var _GET = readGet();

//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] = "Gennaio";
	months[1] = "Febbraio";
	months[2] = "Marzo";
	months[3] = "Aprile";
	months[4] = "Maggio";
	months[5] = "Giugno";
	months[6] = "Luglio";
	months[7] = "Agosto";
	months[8] = "Settembre";
	months[9] = "Ottobre";
	months[10] = "Novembre";
	months[11] = "Dicembre";
	var today = new Date(Number(dt));
	var year = today.getYear();
	if (year < 1000) {
		year += 1900;
	};
	return (today.getDate() + " " + months[(today.getMonth())] + " " + year);
}

function formatDateTime(dt) {
	var today = new Date(Number(dt));
	var year = today.getYear();
	if (year < 1000) {
		year += 1900
	};
	return (today.getDate() + "-" + (today.getMonth() + 1) + "-" + year + " " + today.getHours() + ":" + (today.getMinutes() < 10 ? "0" + today.getMinutes() : today.getMinutes()));
}

var photoids = new Array();


















function album(j) { //returns all photos in a specific album
	var scPhotos = new StringCat();
	 //get the number of photos in the album
	var np = j.feed.openSearch$totalResults.$t;
	var loc = j.feed.gphoto$location.$t;
	var desc = j.feed.subtitle.$t;
	var album_date = formatDate(j.feed.gphoto$timestamp.$t);

	// var item_plural = "s";
	var item_plural = "";

	if (np == "1") {
		item_plural = "";
	}
	var len = j.feed.entry.length;
	ellgallery_currAlbumTitle = j.feed.title.$t;

	scPhotos.push("<center><table border=0><tr>");
	scPhotos.push("<tr><td colspan='" + ellgallery_cols + "'><a class='standard' href='javascript:void(0)' onclick='$.historyLoad(\"\");'>Photogallery Home</a> &gt; " + j.feed.title.$t + "<br/>");

	// scPhotos.push("<blockquote><div style='margin-left:3px'><h4>" + j.feed.title.$t + "</h4></div>");
	scPhotos.push("<div style='margin-left:3px'><h4>" + j.feed.title.$t + "</h4></div>");

	// scPhotos.push("<div style='margin-left:3px'><i>" + np + " foto" + item_plural + ", " + album_date + " &nbsp; &nbsp; " + loc + "</i></div>");
	scPhotos.push("<div style='margin-left:3px'><i>" + np + " foto" + item_plural + "</i></div>");

	// scPhotos.push("<div style='margin-left:3px'><b>" + desc + "</b></div>");
	// scPhotos.push("<div style='margin-left:3px'><a href='http://picasaweb.google.com/" + ellgallery_username + "/" + j.feed.gphoto$name.$t + "/photo#s" + j.feed.entry[0].gphoto$id.$t + "'  rel='lyteframe' rev='width: 760px; height: 600px; scrolling: no;' title='Photogallery CUS Bari'>Slideshow</a></div>");

	// scPhotos.push("</blockquote><br/>");
	scPhotos.push("<br/>");

	scPhotos.push("</td></tr><tr>");

	 //create paging navigation
	pageCount = (np / ellgallery_maxresults);
	var ppage = "&lt; | ",
	npage = "| &gt;";
	if (ellgallery_page > 1) {
		ppage = "<a href='javascript:void(0)' onclick='$.historyLoad(\"" + ellgallery_currAlbum + "/" + (parseInt(ellgallery_page) - 1) + "\")'>&lt;</a> | "
	};
	if (ellgallery_page < pageCount) {
		npage = "| <a href='javascript:void(0)' onclick='$.historyLoad(\"" + ellgallery_currAlbum + "/" + (parseInt(ellgallery_page) + 1) + "\")'>&gt;</a>"
	};

	var navRow = new StringCat();
	navRow.push("<tr><td style='height:40px;' colspan='" + ellgallery_cols + "'>" + ppage + "Pagina: ");
	for (var i = 1; i < pageCount + 1; i++) {
		if (i == ellgallery_page) {
			navRow.push("<b>[" + (i) + "]</b> ");
		} else {
			navRow.push("<a href='javascript:void(0)' onclick='$.historyLoad(\"" + ellgallery_currAlbum + "/" + i + "\")'>" + (i) + "</a> ");
		};
	};
	navRow.push(npage + "</td></tr>");
	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 desc = j.feed.entry[i].summary.$t;
		var photoDate = j.feed.entry[i].exif$tags.exif$time ? formatDateTime(j.feed.entry[i].exif$tags.exif$time.$t) : j.feed.entry[i].published.$t;
		
		if (typeof(desc) == 'undefined') {
			title = '';
		} else {
			title = desc;
		}

		// desc = photoDate + " - " + desc;
		scPhotos.push("<td valign=top><center><a href='" + img_base + "?imgmax=" + ellgallery_photosize + "&crop=0' class='lightbox' title='" + desc + "'><img src='" + img_base + "?imgmax=" + ellgallery_thumbsize + "&crop=1' class='ellgallerymages' style='border:4px solid #aaa;' /></a><br/>");
		// scPhotos.push("<small>" + photoDate + "</small></center></td>");
		scPhotos.push("<textarea readonly style='background:transparent; overflow:hidden; cursor:default; font-family:Arial; font-size:10px; border:0; width:164px; height:28px; text-align:center;'>" + title + "</textarea></center></td>");

		if (i % ellgallery_cols == (ellgallery_cols - 1)) {
			scPhotos.push("</tr><tr><td colspan=" + ellgallery_cols + "><hr size='1'/></td></tr><tr>");
		}
	}
	scPhotos.push("</tr>"+navRow+"</table></center>");
	ellgallery_storage_photos = scPhotos.toString();
	show(false, ellgallery_storage_photos);
}

function getAlbums() {
	if (ellgallery_storage_albums) {
		show(false, ellgallery_storage_albums);
	} else {
		show(true, '');
		var url = 'http://picasaweb.google.com/data/feed/api/user/' + ellgallery_username + '?category=album&max-results=' + ellgallery_maxalbums + '&access=public&alt=json';
		$.getJSON(url, 'callback=?', albums);
	}
}

function getAlbum(albumid, newPage) {
	if (albumid != ellgallery_currAlbum || ellgallery_page != newPage) {
		ellgallery_page = newPage;
		ellgallery_currAlbum = albumid;
		ellgallery_si = ((ellgallery_page - 1) * ellgallery_maxresults) + 1;
		var url = 'http://picasaweb.google.com/data/feed/api/user/' + ellgallery_username + '/albumid/' + albumid + '?category=photo&max-results=' + ellgallery_maxresults + '&start-index=' + ellgallery_si + '&alt=json';
		show(true, '');
		$.getJSON(url, 'callback=?', album);
	} else {
		show(false, ellgallery_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) {
		$(ellgallery_container_div).fadeOut('slow');
		$("#maincontent").block("<img src=\"sources/3rd_party/modxed/jquery/plugins/jquery.lightbox/images/lightbox-ico-loading.gif\"> Caricamento in corso...", {
			border: '3px solid #a00'
		});
	} else {
		$("#maincontent").unblock();
		$(ellgallery_container_div).html(data).fadeIn('fast');
		$('a.lightbox').lightBox();
	}
}

$(document).ready(function() {
	if(ellgallery_username == "your_picasa_username"){
		alert('Open the ellgallery-0.2.js and change content \nof the variable ellgallery_username in the top of the file\nto your Picasa Webalbum name');
	}else{
		$.extend($.blockUI.defaults.overlayCSS, {
			backgroundColor: '#000'
		});
		$.ajaxSetup({
			cache: true
		});
		$.historyInit(fromHistory);
	}
});


