//requestVars start

/*
 * @author      Jesse Berman
 * @copyright   2008-01-31
 * @version     1.0
 * @license     http://www.gnu.org/copyleft/lesser.html
*/


/*
 * Portions by Dieter Raber <dieter@dieterraber.net>
 * copyright   2004-12-27
*/


/* pwa.js: a drop-in JavaScript utility that displays galleries from picasaweb.google.com in your website */

/* This JavaScript file, when called from a webpage, will load all the thumbnail images of all the galleries
   in a user's Picasa Web Albums account into an HTML table that's 4 rows wide.  Clicking on any of the
   galleries will display thumbnails of all the photos in that gallery, and clicking on any of those thumbnails
   will display the photo.  

   To call this file from your own webpage, use the following syntax:

       <script type="text/javascript">username='YourPicasawebUsername'; photosize='800'; columns='4';</script>
       <script type="text/javascript" src="http://www.yoursite.com/pwa.js"></script>

   Make sure you change YourPicasawebUsername to your actual Picasaweb username.  For more information about
   Picasa, check out picasaweb.google.com.  Also, www.yoursite.com should point to your actual site name, and
   the location of the pwa.js file.  The script looks for the images back.jpg, next.jpg, and home.jpg, in the
   same directory as pwa.js, to create the navigation arrows.  Please make sure those exist!  I'm providing
   samples in the SourceForce repository, but feel free to substitute your own.

   Note: "Photosize" is the size of the image to be displayed when viewing single images.  I like 800.  :-)
   Note: "columns" is the number of columns of photos to be displayed on your site in the gallery and album views.
   You may omit either of these values; if you do, the default settings are 800 for photosize and 4 for columns.

*/

// $("<link href='template.css' rel='stylesheet' type='text/css' />");


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();
//requestVars end


function $(a){
    document.write(a);
}
var photosize;
if(!photosize){
    photosize = 800;
}

var columns;
if(!columns || isNaN(columns) || columns < 1) {
    columns = 4;
}


//Global variables
var photolist = new Array(); //this is used globally to store the entire list of photos in a given album, rather than pass the list around in a URL (which was getting rediculously long as a result)
var album_name = ""; //this is used globally to store the album name, so we don't have to pass it around in the URL anymore either.
var my_numpics = ""; //this is used globally to store the number of items in a particular album, so we don't have to pass it around in the URL anymore either.
var prev = ""; //used in the navigation arrows when viewing a single item
var next = "";//used in the navigation arrows when viewing a single item



function picasaweb(j){ //returns the list of all albums for the user

    $("<div id='container'>");
 
    $("<div id='title'>Sammlungen</div>");
 
    //  $("<div id='title'>Gallery Home <a target=PICASA href='http://picasaweb.google.com/"+username+"/'>on picasa web</a></div>");
 
 
 
    $("<div id='gallerycontainer'>");

 
    for(i=0;i<j.feed.entry.length;i++){

        // for each of the albums in the feed, grab its album cover thumbnail and the link to that album,
        // then display them in a table with 4 columns (along with the album title)

        //This was the old way of grabbing the photos; since Google updated the feed, the media entry is better. :-)
        //
        //var img_begin = j.feed.entry[i].summary.$t.indexOf('src="')+5;
        //var img_end = j.feed.entry[i].summary.$t.indexOf('?imgmax');
        //var img_base = j.feed.entry[i].summary.$t.slice(img_begin, img_end);

        var img_base = j.feed.entry[i].media$group.media$content[0].url;

        var id_begin = j.feed.entry[i].id.$t.indexOf('albumid/')+8;
        var id_end = j.feed.entry[i].id.$t.indexOf('?');
        var id_base = j.feed.entry[i].id.$t.slice(id_begin, id_end);

        $("<div class='thumbnail' ><a href='?albumid="+id_base+"'><img width='96' src='"+img_base+"?imgmax=160&crop=1' /></a>");
        $("<br />"+ j.feed.entry[i].title.$t +"</div>");

    }

    $("</div id='gallerycontainer'>");
    $("</div id='container'>");

}





function getphotolist(j){

    // This function is called just before displaying an item; it returns info about the item's current state within its parent
    // album, such as the name of the album it's in, the index of the photo in that album, and the IDs of the previous and next
    // photos in that album (so we can link to them using navigation arrows).  This way we don't have to pass state information
    // around in the URL, which was resulting in hellishly long URLs (sometimes causing "URI too long" errors on some servers).

    // Get the number of pictures in the album.  Added 7/18/2007.
    my_numpics = j.feed.openSearch$totalResults.$t;

    // Also get the name of the album, so we don't have to pass that around either.  Added 7/18/2007.
    album_name = j.feed.title.$t;

    for(i=0;i<j.feed.entry.length;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_begin = j.feed.entry[i].id.$t.indexOf('photoid/')+8;
        var id_end = j.feed.entry[i].id.$t.indexOf('?');
        var id_base = j.feed.entry[i].id.$t.slice(id_begin, id_end);
        photolist[i]=id_base;

        // now get previous and next photos relative to the photo we're currently viewing
        if (i>0)
        {
            var prev_begin = j.feed.entry[i-1].id.$t.indexOf('photoid/')+8;
            var prev_end = j.feed.entry[i-1].id.$t.indexOf('?');
            prev = j.feed.entry[i-1].id.$t.slice(id_begin, id_end);
        }
        if (i<j.feed.entry.length-1)
        {
            var next_begin = j.feed.entry[i+1].id.$t.indexOf('photoid/')+8;
            var next_end = j.feed.entry[i+1].id.$t.indexOf('?');
            next = j.feed.entry[i+1].id.$t.slice(id_begin, id_end);
        }

    }
}




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 item_plural = "s";
    if (np == "1") {
        item_plural = "";
    }

    var album_begin = j.feed.entry[0].summary.$t.indexOf('href="')+6;
    var album_end = j.feed.entry[0].summary.$t.indexOf('/photo#');
    var album_link = j.feed.entry[0].summary.$t.slice(album_begin, album_end);
    var photoids = new Array();


    $("<div id='container'>");
    $("<div id='title'><a  href='" + window.location.protocol + "//" + window.location.hostname+window.location.pathname+"'>Sammlungen</a> &gt; "+ j.feed.title.$t +"&nbsp;&nbsp;("+np+" Foto"+item_plural+") </div>");

 
    $("<div id='gallerycontainer'>");


    for(i=0;i<j.feed.entry.length;i++){

        var img_base = j.feed.entry[i].media$group.media$content[0].url;
        var img_cmt = j.feed.entry[i].media$group.media$description.$t;
        //  j.entry.media$group.media$description.$t

        var id_begin = j.feed.entry[i].id.$t.indexOf('photoid/')+8;
        var id_end = j.feed.entry[i].id.$t.indexOf('?');
        var id_base = j.feed.entry[i].id.$t.slice(id_begin, id_end);
        photoids[i]=id_base;
  

        // display the thumbnail and make the link to the photo page, including the gallery name so it can be displayed.
        // (apparently the gallery name isn't in the photo feed from the Picasa API, so we need to pass it as an argument in the URL) - removed the gallery name 7/18/2007
        var link_url = "?albumid="+_GET['albumid']+"&photoid="+id_base; //+"&photoids="+photoids;
        // disable the navigation entirely for really long URLs so we don't hit against the URL length limit.
        // note: this is probably not necessary now that we're no longer passing the photoarray inside the URL. 7/17/2007
        // Not a bad idea to leave it in, though, in case something goes seriously wrong and we need to revert to that method.
  
        $("<div class='thumbnail'><a href='"+img_base+"' rel='lightbox[imgs]' title='"+img_cmt+"'><img width='96' src='"+img_base+"?imgmax=160&crop=1'  /></a> </div>");
    }
    $("</div id='gallerycontainer'>");
    $("</div id='container'>");

}
if(_GET['albumid']&&!_GET['photoid']){
    $('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=albums"></script>');//albums
}else{
    $('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'?category=album&alt=json&callback=picasaweb&access=public"></script>');//picasaweb
}


//$Update: January 31, 2008$
