// Thanks to http://googlified.com/2007embedding-picasa-web-albums-on-your-site/ and www.jdee.nl for the script


//User variables to set
var photosize = 720; //return maximum size picture (see for supported format:  http://code.google.com/apis/picasaweb/reference.html#Parameters)
var thumbsize = 160; //size thumb /cropped(see for supported format:  http://code.google.com/apis/picasaweb/reference.html#Parameters)
var albumcols = 3; // distribute thumbs on main page over x collums
var cols = 3; // distribute thumbs on albumphotos page over x collums
var maxresults = 300; //maximum of pictures on albumphotos page
var uhrzeitkorrektur = 2; //wird von der Uhrzeit abgezogen
var normalzeilen = 1; //Anzahl der Zeilen mit geordneten Alben
var alben = 1; //Anzahl der Zeilen mit Zufallsalben

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();

if(!photosize){photosize = 512;}
var si = _GET['si'];
if(!si){si = 1}else{si=Number(si)};

function $(a){document.write(a);}

function GetRandom( min, max ) {
  if( min > max ) {
  return( -1 );
  }
  if( min == max ) {
  return( min );
  }
  var r = parseInt( Math.random() * ( max+1 ) );
  return( r + min <= max ? r + min : r );
  }


function formatDate(dt){
   var months = new Array(12);months[0]  = "Januar";months[1]  = "Februar";months[2]  = "März";months[3]  = "April";months[4]  = "Mai";months[5]  = "Juni";months[6]  = "Juli";months[7]  = "August";months[8]  = "September";months[9]  = "Oktober";months[10] = "November";months[11] = "Dezember";
	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 };
        tag = today.getDate();
        stunde = today.getHours() - uhrzeitkorrektur;
        if (stunde < 0) {stunde = stunde + 24;tag = tag -1;}
	return (tag + "." + (today.getMonth()+1) + "." + year + " " + stunde + ":" + (today.getMinutes()<10 ? "0"+today.getMinutes() : today.getMinutes()) );
}

function picasaweb(j){ //returns the list of all albums for the user

   if (vonbis == 'ja') 
    {
  		var from = von - 1;
  		var to = bis;
  		if (bis == 0) {var to = j.feed.entry.length;}
  	}    
   else 
    {
  		var from = 0;
      var to = j.feed.entry.length;
    }
  	
	$("<table width='100%'>");
 
    var i=from;
		var id_base = j.feed.entry[i].gphoto$id.$t;
		var album_date = formatDate(j.feed.entry[i].gphoto$timestamp.$t);
	
	for(i=from;i<to;i++){  //alle alben 
    var alben = j.feed.entry.length - 1;
 		var ik=GetRandom(1,alben);	

		var id_base = j.feed.entry[ik].gphoto$id.$t;
		var album_date = formatDate(j.feed.entry[ik].gphoto$timestamp.$t);
		var k = j.feed.entry.length - i;

		$("<td bgcolor='black' valign=bottom align=center width='33%'><a class='standard' href='?page="+_GET['page']+"&albumid="+id_base+"' title='Klicken, um Album &quot;"+ j.feed.entry[ik].title.$t +"&quot; zu öffnen'><small>"+ j.feed.entry[ik].title.$t +"</small><small><small><small><small><small><small><br><br></small></small></small></small></small></small><img src='"+j.feed.entry[ik].media$group.media$thumbnail[0].url+"?imgmax="+thumbsize+"&crop=1' class='pwimages' />");
		$("<br/></a></td>");
		if (i % albumcols == (albumcols-1)) {$("</tr><tr><td colspan='"+albumcols+"'><hr  style='border-width:0px 0px 1px 0px;border-bottom:1px dotted #c0c0c0;height:1px;'></td></tr><tr>");}
	}
	
	
	$("</tr></table>"+tom+"</center>");
}

function albums(j){  //returns all photos in a specific album

	//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";
	if (np == "1") { item_plural = ""; }
	var photoids = new Array();
	var len = j.feed.entry.length;

	$("<table border=0 width='100%'><tr>");
	$("<tr><td bgcolor='black' colspan='"+cols+"'><a class='standard' href='" + window.location.protocol + "//" + window.location.hostname+window.location.pathname+"?page="+_GET['page']+"' title='Zurück zur Albumübersicht'>&#8226;&#8226; Alle Alben</a> <a href='http://picasaweb.google.com/"+username+"/"+j.feed.gphoto$name.$t+"/photo#s"+j.feed.entry[0].gphoto$id.$t+"' title='Automatische Bilderschau' rel='gb_page_fs[]' target='_new'>&#8226; "+ j.feed.title.$t +"</a><br/>");

	$("<blockquote padding-left: 20px;'>");
	$("<div style='margin-left:3px'>"+desc+"</div></blockquote>");
	$("</td></tr><tr>");

	for(i=0;i<len;i++){
		// get the list of all photos referenced in the album and display;
		// also stored in an array (photoids) for navigation in the photo view (passed via the URL)
		var id_base = j.feed.entry[i].gphoto$id.$t;
		photoids[i]=id_base; //must be pre-loaded before the URLs are generated. That's why we need to run the loop twice.
	}
 
	//create paging navigation
	pageCount = (np/maxresults);
	var ppage = "", npage = "";
	if(si>1){
		ppage = "<a href='?page="+_GET['page']+"&albumid="+_GET['albumid']+"&si="+(si-maxresults)+"'>letzte</a> | "
	};
	if((si+maxresults)<np){
		npage = " | <a href='?page="+_GET['page']+"&albumid="+_GET['albumid']+"&si="+(si+maxresults)+"'>nächste</a>"
	};

	var navRow = "<tr><td colspan='"+cols+"'><small>" +ppage;

        
	for(i=0;i<=pageCount;i++){
		if(si == ((i*maxresults)+1)){
			navRow += "<b>["+(i+1)+"]</b> ";
		}else{
			navRow += "<a href='?page="+_GET['page']+"&albumid="+_GET['albumid']+"&si="+((i*maxresults)+1)+"'>"+(i+1)+"</a> ";
		};
	};

	strich = "<tr><td colspan="+cols+"><hr  style='border-width:0px 0px 1px 0px;border-bottom:1px dotted #c0c0c0;height:1px;'/></td></tr>";
        navRow += npage + "</small></td></tr>"

	if((np/maxresults) <= 1){
		navRow = "";
	};

	$(navRow); 
	$(strich); 
	for(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 photoDate = j.feed.entry[i].exif$tags.exif$time ? formatDateTime(j.feed.entry[i].exif$tags.exif$time.$t) : "";
		photoids[i]=id_base;
		if (i>0){ var prev = j.feed.entry[i-1].gphoto$id.$t; }
		if (i<len-1){ var next = j.feed.entry[i+1].gphoto$id.$t; }

		//photo page
		$("<td valign=top><center><a href='?page="+_GET['page']+"&albumid="+_GET['albumid']+"&photoid="+id_base+"&galleryname={"+j.feed.title.$t+"}&np="+np+"&prev="+prev+"&next="+next+"&photoids="+photoids+"&si="+si+"' title='Klicken, um "+(i+1)+". Bild zu öffnen'><img src='"+img_base+"?imgmax="+thumbsize+"&crop=1' class='pwimages'/>");
		$("</a></center></td>");
  
	if (i % cols == (cols-1)) {

                      if (i != len-1) $("</tr><tr><td colspan="+cols+"><hr  style='border-width:0px 0px 1px 0px;border-bottom:1px dotted #c0c0c0;height:1px;'/></td></tr><tr>");
        }
	}
        
	$("</tr>");
	$(navRow); 
	$("</table></center>");
}

function photo(j){//returns exactly one photo

	var img_title = j.entry.title.$t;

	//get the dimensions of the photo we're grabbing; if it's smaller than our max width, there's no need to scale it up.
	var img_width = j.entry.media$group.media$content[0].width;
	var img_height = j.entry.media$group.media$content[0].height;
	var img_base = j.entry.media$group.media$content[0].url;
	
	var photo_id = _GET['photoid'];
	var album_id = _GET['albumid'];
	var my_next = _GET['next'];
	var my_prev = _GET['prev'];
	var my_photoids = _GET['photoids'];
	var my_numpics = _GET['np'];
	var my_galleryname = _GET['galleryname'];
	var my_fixed_galleryname = my_galleryname.slice(1, my_galleryname.length-1);
	var album_base_path = window.location.protocol + "//" + window.location.hostname+window.location.pathname +"?page="+_GET['page']+"&si="+si+"&albumid="+ _GET['albumid'];

	// Get the filename for display in the breadcrumbs
	var LastSlash = 0;
	var img_filename = img_title;
	var photo_array = my_photoids.split(",");

	//find preceding two and following two pictures in the array; used for the navigation arrows.
	//the arrows are already linked to the previous and next pics, which were passed in with the URL.
	//however, we need the ones that are two behind and two ahead so that we can pass that info along when we link to another photo.
	//"pretty sneaky, sis."
	for(i=0;i<photo_array.length;i++){
		if (photo_array[i]==photo_id){
			var p2 = photo_array[i-2]; //ID of the picture two behind this one
			var p1 = photo_array[i-1]; //ID of the picture one behind this one; if null, we're at the beginning of the album
			var current_index = i + 1; //this is the count of the current photo
			var n1 = photo_array[i+1]; //ID of the picture one ahead of this one; if null, we're at the end of the album
			var n2 = photo_array[i+2]; //ID of the picture two ahead of this one
		}
	}
	//these will be passed along if we move to the next or previous photo
	var prev = album_base_path + "&photoid=" + p1 + "&np=" + my_numpics + "&galleryname=" + my_galleryname + "&next="+photo_id+ "&prev="+p2+"&photoids="+my_photoids;
	var next = album_base_path + "&photoid=" + n1 + "&np=" + my_numpics + "&galleryname=" + my_galleryname + "&prev="+photo_id+ "&next="+n2+"&photoids="+my_photoids;

if (p1 == null){ var prev = album_base_path } //we're at the first picture in the album; going back takes us to the album index
	if (n1 == null){ var next = album_base_path } //we're at the last picture in the album; going forward takes us to the album index

	//Display the breadcrumbs
	$("<table border='0' width='" + photosize + "'><tr><td bgcolor='black' valign='top' align='left'><a class='standard' href='"+ window.location.protocol + "//" + window.location.hostname+window.location.pathname+"?page="+_GET['page']+"' title='Zurück zur Albumübersicht'>&#8226;&#8226;&#8226; Alle Alben</a> <a class='standard' href='" + album_base_path + "' title='Zurück zum Album'>&#8226;&#8226; " + my_fixed_galleryname + "</a> <a class='standard' href='"+next+"' title='Nächstes Bild'>&#8226; " + (current_index+si-1) + ".  [" + my_numpics + "]</a><br>");


	$("<br><div style='margin-left:2px'>"+j.entry.media$group.media$description.$t+"</div>");
	
	if (img_width < photosize){ display_width = img_width; } //don't scale up photos that are narrower than our max width; disable this to set all photos to max width

	//at long last, displayed the image and its description. photos larger than max_width are scaled down; smaller ones are left alone
	$("<center><br><a class='standard' href='"+next+"' title='Klicken, für nächstes Bild'><img id='picture' src='"+img_base+"?imgmax="+photosize+"' class='pwimages' /></a></center>");
	$("</td></tr></table>");
}

if(_GET['photoid']&&_GET['albumid']){
	$('<script type="text/javascript" src="http://picasaweb.google.com/data/entry/api/user/'+username+'/albumid/'+_GET['albumid']+'/photoid/'+_GET['photoid']+'?alt=json&callback=photo"></script>');//photo
}else if(_GET['albumid']&&!_GET['photoid']){
	$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/api/user/'+username+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=albums&max-results='+maxresults+'&start-index='+si+'"></script>');//albums
}else{
	$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/api/user/'+username+'?category=album&alt=json&callback=picasaweb&access=public"></script>');//picasaweb
}
//$Update: May 10, 2007$
//$Update: July 31, 2007, Jeroen Diderik$