//requestVars start
/*
 * @author      Geoff Whittle
 * @copyright   2008-01-27
 * @version     1.0
 * @license     http://www.gnu.org/copyleft/lesser.html
*/
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 = 400;}


function randphotos(j){  //returns all photos in a specific album

 var photo_count = j.feed.openSearch$totalResults.$t; // Get the number of photos in the Album
 var albumdesc =  j.feed.title.$t // Get the Album description from the head of the feed

 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();

// Some code to update "the where am I" DIV with relvant info
//jscriptWhereAmI.innerHTML = '<font class="header"><a href=/index.php>Home</a> ~ Photos ~ ' + albumdesc + ' (' + photo_count + ' photos)</font>';

// $("<table class=borderless><tr>");
//randomise the array
fisherYates(j.feed.entry);

 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);
  photoids[i]=id_base; //must be pre-loaded before the URLs are generated. That's why we need to run the loop twice.
 }
 //for(i=0;i<j.feed.entry.length;i++){
for(i=0;i<2;i++){

// Begin code to grab the descprion of the photo. Note if no description the pic file name will be returned. If file name (.jpg) then discard
	var title = j.feed.entry[i].title.$t;
	if(Right(title,4) == '.jpg'||Right(title,4) == '.wmv'||Right(title,4) == '.JPG'||Right(title,4) == '.WMV')
		{
		title = '';
		}	
// End code to grab the descprion of the photo.

  var img_base = j.feed.entry[i].media$group.media$content[0].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);
  photoids[i]=id_base;
     
  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('?');
    var 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('?');
    var next = j.feed.entry[i+1].id.$t.slice(id_begin, id_end);
  }

  // display the thumbnail (in a table) and make sure 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)
  var link_url = picasapagename+"?albumid="+randurl+"&photoid="+id_base+"&galleryname={"+j.feed.title.$t.replace("'","%27")+"}&prev="+prev+"&next="+next+"&photoids="+photoids;
  if (link_url.length > 2048) { link_url = link_url.slice(0, link_url.indexOf('&photoids=')+10)+id_base; }
  //$("<td class=dottedbordertd valign=top>");
  $("<a href='"+link_url+"'><img src='"+img_base+"?imgmax=160&crop=1' class='pwimages' height='100' /></a><font><br><br></font>");
  //$("<br><font>" + title + "</font>");
 // $("</td>");
//  if (i % 4 == 3) {
//    $("</tr><tr>");
//  }
 }
 var album_link_url = picasapagename+"?albumid="+randurl;
$("<font>Click photo to enlarge or click <a href=" + album_link_url + ">here</a> to see more pictures like this</font>");

// $("</tr></table>");
}

function Right(str, n)
{
      if (n <= 0)
          return "";
      else if (n > String(str).length)
          return str;
      else
   {
          var iLen = String(str).length;
          return String(str).substring(iLen, iLen - n);
      }
}
// function to allow us to randomise an array order
function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

// function to get a specific url parameter and return "" if not found rather than the default undefined. credit: http://www.netlobo.com/url_query_string_javascript.html
function gup( name ){  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  var regexS = "[\\?&]"+name+"=([^&#]*)";  var regex = new RegExp( regexS );  var results = regex.exec( window.location.href );  if( results == null )    return "";  else    return results[1];}

 $('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+randurl+'?category=photo&alt=json&callback=randphotos"></scr'+'ipt>');//albums
