//requestVars start
/*
 * @author      Dieter Raber <dieter@dieterraber.net>
 * @copyright   2004-12-27
 * @version     1.0
 * @license     http://www.gnu.org/copyleft/lesser.html
*/
/* updated by Jesse Berman on May 10, 2007       */
/* for use with the Boston Ironsides RFC website */
/* http://www.bostonironsidesrfc.org/gallery     */
/* updated again 6-27-2007 to work with new Picasa XML format */

/* updated by Geoff Whittle January 10, 2008 - February 29, 2008 */
/* for use with the Ships Down Under website      				 */
/* http://www.ShipsDownUnder.com/                                */

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);}

function picasaweb(j){ //returns the list of all albums for the user

//checks that their is a DIV with this name in the source document. If there is then paste this
try 		{
		//Calculate paramters for current page and total number of pages
		var requestedpage = gup('page'); // find the page number
		if(requestedpage == "") { requestedpage = 1;} // If page number not set, set page to page 1
		var album_count = j.feed.entry.length; //determine the number of albums for user
		var totpages = Math.ceil(album_count / maxresults) + 1; // determine the total number of pages needed note: maxersults = max number of pics per page. Set in base page
		var pages = ""; // pages variables creates the html for the page number list ie page 1 2 3 4 5 etc
		if(totpages >2) // only show the number of pages link if there are more than one page
			{
			var pages = "| Page ";
			for(c=1;c<totpages;c++)
				{
				if(c == requestedpage)
					{
					//no url to page as current page
					var pages = pages + ' </font><font class="header" style="background-color: white">' + c + '</font><font class="header">';
					}
					else
					{
					//url to page
					var pages = pages + " <a href=?page="+ c + ">"+ c + "</a>";
					}
				}
			}
		//build the final text for the DIV that display some naviagtion. if set to "INLINE" then put above pics rather than in a specific DIV
		var divname_data = '<table class="borderless"><tr><td class="borderlesstd" width="60%"><font class="header"><a href=/index.php>Home</a> ~ Photos</font></td><td class="borderlesstd"align="right"><font class="header">' + pages + ' | '+ album_count +' Albums |</td></td></table>';
		if(divname == "INLINE")
			{
			$(divname_data);
			}
		else
			{	
			document.getElementById(divname).innerHTML = divname_data;
			}		
		var ok1 = "TRUE"; // set success variable for use later
				}
	catch(err)
	{
	var ok1 = "FALSE";  // set failutre variable for use later
	}		
		
	// caculate the start and end variable depending on the page number and the number of pics per page 
	var start_album = (( requestedpage - 1) * maxresults);
	var end_album = requestedpage * maxresults;

	$("<table class=borderless><tr>");
 	//for(i=0;i<j.feed.entry.length;i++){
 	for(i=start_album;i<end_album;i++){
 	// for each of the albums in the feed, grab its album cover thumbnail and the link to that album,
 	var img_base = j.feed.entry[i].media$group.media$content[0].url; //thumbnail 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); //albumid

  var publishe = j.feed.entry[i].published.$t.slice(0,10);			// only get the date, not the time
  var publishe_month = publishe.slice(5,7)							// Convert Month into 3 letter text

  if(publishe_month == '01')
  	{
	publishe_month = "Jan";
	}
	else if(publishe_month == '02')
	{
	publishe_month = "Feb";
	}
	else if(publishe_month == '03')
	{
	publishe_month = "Mar";
	}	
	else if(publishe_month == '04')
	{
	publishe_month = "Apr";
	}	
	else if(publishe_month == '05')
	{
	publishe_month = "May";
	}	
	else if(publishe_month == '06')
	{
	publishe_month = "Jun";
	}
	else if(publishe_month == '07')
	{
	publishe_month = "Jul";
	}	
	else if(publishe_month == '08')
	{
	publishe_month = "Aug";
	}	
	else if(publishe_month == '09')
	{
	publishe_month = "Sep";
	}	
	else if(publishe_month == '10')
	{
	publishe_month = "Oct";
	}	
	else if(publishe_month == '11')
	{
	publishe_month = "Nov";
	}	
	else if(publishe_month == '12')
	{
	publishe_month = "Dec";
	}
	
	publishe2 = publishe.slice(8,10) + ' ' + publishe_month + ' ' + publishe.slice(0,4); // reconstruct the date onto required "dd mmm yyyy" format
	//paste it all to the page
	$("<td class=borderlesstd valign=top><center><a href='?albumid="+id_base+"'><img src='"+img_base+"?imgmax=160&crop=1' class='pwimages' /></a>");
  	$("<br><a class='standard' href='?albumid="+id_base+"'><font>"+ j.feed.entry[i].title.$t + '<br>' + publishe2 +"</font></a></center></td>");

  	if (i % columns == (columns - 1)) {
    	$("</tr><tr>");
  		}
 	}
 	$("</tr></table>");
 
	//if(document.getElementById("divname")) // paste the footer with the page numbers etc only if there is a header as well
	if(ok1 == "TRUE")
		{ 
		$('<table class="borderless"><tr><td class="borderlesstd"></td><td class="borderlesstd"align="right"><font class="header">' + pages + ' | '+ album_count +' Albums |</td></td></table>'); 
		}

	}


function albums(j){  //returns all photos in a specific album
// test crap
 //$('feed: ' + j.feed.entry.georss$where.gml$Point.gml$pos.$t); //base
  //$('feed: ' + j.feed.georss$where.gml$Point.gml$pos.$t); //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();


	album_date_raw = formatDate(j.feed.gphoto$timestamp.$t);
	if(album_date_raw.length == 11)
		{
		album_date = album_date_raw.slice(0,7) + ' ' + album_date_raw.slice(9,11);
		}
		else
		{
		album_date = '0' + album_date_raw.slice(0,6) + ' ' + album_date_raw.slice(8,10);
		}

//Calculate current page and total number of pages
	var requestedpage = gup('page');
	if(requestedpage == "") { requestedpage = 1;}
	
	//var startindex = (maxresults * (requestedpage - 1)) + 1;
	var albumid = _GET['albumid'];
	var totpages = Math.ceil(photo_count / maxresults) + 1;
	var pages = "";
	if(totpages >2)
		{
		var pages = "| Page ";
		for(c=1;c<totpages;c++)
			{
			if(c == requestedpage)
				{
				//no url to page as current page
				var pages = pages + ' </font><font class="header" style="background-color: white">' + c + '</font><font class="header">';
				}
				else
				{
				//url to page
				var pages = pages + " <a href=?albumid="+albumid+"&page="+ c + ">"+ c + "</a>";
				}
			}
		}
// Some code to update "the where am I" DIV with relvant info
//if(document.getElementById("divname"))
try	{
	var meta_data = pages + ' | ' + album_date + ' | ' + photo_count + ' photos |';
	var divname_data = '<table class="borderless"><tr><td class="borderlesstd" width="60%"><font class="header"><a href=/index.php>Home</a> ~ Photos ~ ' + albumdesc + '</font></td><td class="borderlesstd" align="right" valign="top"><font class="header">' + meta_data + '</font></td></tr></table>';
		if(divname == "INLINE")
			{
			$(divname_data);
			}
		else
			{	
			document.getElementById(divname).innerHTML = divname_data;
			}		

	var ok1 = "TRUE";
	}
	catch(err)
	{
	var ok1 = "FALSE"; //Handle errors here
	}	
	
 $("<table class=borderless><tr>");

 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 phototitle = j.feed.entry[i].id.title.$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; //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++){

	var description = ""; //*****NEED TO FIX THIS ****j.feed.entry[i].media$group.media$description.$t // 
	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
  var link_url = "?albumid="+_GET['albumid']+"&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=borderlesstd valign=top><center><a href='"+link_url+"'><img src='"+img_base+"?imgmax=160&crop=1' class='pwimages' /></a><br><font>" + description + "</font></center></td>");
//class=dottedbordertd


  if (i % columns == (columns - 1)) {
    $("</tr><tr>");
  }
 }
 $("</tr></table>");
//if(document.getElementById("divname"))
if(ok1 =="TRUE")
	{
	$('<table class="borderless"><tr><td class="borderlesstd"></td><td class="borderlesstd" align="right"><font class="header">' + meta_data + '</td></tr></table>');
	}	
	
}
function photo(j){//returns exactly one photo

 var album_begin = j.entry.summary.$t.indexOf('href="')+6;
 var album_end = j.entry.summary.$t.indexOf('/photo#');
 var album_link = j.entry.summary.$t.slice(album_begin, album_end);

 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_begin = j.entry.summary.$t.indexOf('href="')+6;
 var photo_end = j.entry.summary.$t.indexOf('"><img');
 var photo_link = j.entry.summary.$t.slice(photo_begin, photo_end);
 var photo_id = _GET['photoid'];
 
 // Do things different if it is a video.
	var is_video = 0, video_link;
	if (j.entry.media$group.media$content.length > 1 && j.entry.media$group.media$content[1].medium == "video") {
		is_video = 1;
		var begin = j.entry.summary.$t.indexOf('href="')+6;
		var end = j.entry.summary.$t.indexOf('"><img');
		video_link = j.entry.summary.$t.slice(begin, end);
	}

 var album_name_begin = j.entry.summary.$t.indexOf(username)+username.length+1;
 var album_name_end = j.entry.summary.$t.indexOf('/photo#');
 var album_name = j.entry.summary.$t.slice(album_name_begin, album_name_end);

 var album_id = _GET['albumid'];
 //var my_next = _GET['next'];
 var my_next = gup('next');
 //var my_prev = _GET['prev'];
 var my_prev = gup('prev');
 //var my_photoids = _GET['photoids'];
 var my_photoids = gup('photoids');
 
 //var my_galleryname = _GET['galleryname'];
 var my_galleryname = gup('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 +"?albumid="+ _GET['albumid'] +"&galleryname="+ _GET['galleryname'];

 // Get the filename for display in the breadcrumbs
 var LastSlash = 0;
 var img_filename = img_title;
 for(i=0;i<img_base.length-1;i++){
  if (img_base.charAt(i)=="/")
  {
	  LastSlash = i;
  }
 }
 if (LastSlash != 0)
 {
	 img_filename = img_base.slice(LastSlash+1, img_base.length);
 }
 // replace some commonly-used URL characters like %20
 img_filename = img_filename.replace("%20"," ");
 img_filename = img_filename.replace("%22","\"");
 img_filename = img_filename.replace("%27","\'");
 
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.
 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 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 + "&galleryname=" + my_galleryname.replace("'","%27") + "&next="+photo_id+ "&prev="+p2+"&photoids="+my_photoids;
 var next = album_base_path + "&photoid=" + n1 + "&galleryname=" + my_galleryname.replace("'","%27") + "&prev="+photo_id+ "&next="+n2+"&photoids="+my_photoids;

if (p1 == null) //we're at the first picture in the album; going back takes us to the album index
  { var prev = album_base_path }

if (n1 == null) //we're at the last picture in the album; going forward takes us to the album index
  { var next = album_base_path }

 //the navigation panel: back, home, and next.
 $("<center><table class=borderless><tr valign=top><td class=borderlesstd ><center><font>");
 if (photo_array.length > 1) { $("<a class='standard' href='"+prev+"'>Previous</a>&nbsp;"); }
 $("<a class='standard' href='"+album_base_path+"'>Album index</a>&nbsp;");
 if (photo_array.length > 1) { $("<a class='standard' href='"+next+"'>Next</a>&nbsp;"); }
 $("</font></center></td></tr></table></center><br>");



 var max_width = 700; //max width for our photos
 var display_width = max_width;
 if (img_width < display_width)
   { 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, display the image and its description. photos larger than max_width are scaled down; smaller ones are left alone
 if(is_video == 0) {
 
 $("<center><!--<a border=0 target=PICASA href='"+photo_link+"'>--><img id='picture' width="+display_width+" src='"+img_base+"?imgmax="+photosize+"' class='pwimages' style='border:10px solid white;'/><!--</a>--></center>");
 $("<br><center><div style='margin-left:2px'><font></font></div></center></p>");
}
else {
 $("<center><a border=0 target=PICASA href='"+video_link+"'><img id='picture' width="+display_width+" src='"+img_base+"?imgmax="+photosize+"' class='pwimages' /></a></center>");
 $("<br><center><div style='margin-left:2px'><font><br><br></font><font class=header>This is a video. Click the picture to begin playback</font></div></center></p>");
//j.entry.media$group.media$description.$t
}

}
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 formatDate(dt) {
	var months = new Array(12);
	months[0] = "Jan";
	months[1] = "Feb";
	months[2] = "Mar";
	months[3] = "Apr";
	months[4] = "May";
	months[5] = "Jun";
	months[6] = "Jul";
	months[7] = "Aug";
	months[8] = "Sep";
	months[9] = "Oct";
	months[10] = "Nov";
	months[11] = "Dec";
	var today = new Date(Number(dt));
	var year = today.getYear();
	if (year < 1000) {
		year += 1900;
	};
	return (today.getDate() + ' ' + months[(today.getMonth())] + ' ' + year);
}


// 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];}

if(_GET['photoid']&&_GET['albumid']){
 //$('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/entry/base/user/'+username+'/albumid/'+_GET['albumid']+'/photoid/'+_GET['photoid']+'?alt=json&callback=photo&galleryname='+_GET['galleryname']+'"></scr'+'ipt>');//photo
$('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/entry/base/user/'+username+'/albumid/'+_GET['albumid']+'/photoid/'+_GET['photoid']+'?alt=json&callback=photo"></scr'+'ipt>');//photo
}else if(_GET['albumid']&&!_GET['photoid']){
 			var requestedpage = gup('page');
			if(requestedpage == "") { requestedpage = 1;}
			var startindex = (maxresults * (requestedpage - 1)) + 1;
 $('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/feed/api/user/'+username+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=albums&start-index='+startindex+'&max-results='+maxresults+'"></scr'+'ipt>');//albums with photo offset
//url for all pics in album - no pages
// $('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=albums"></scr'+'ipt>');//albums

}else{
var requestedpage = gup('page');
			if(requestedpage == "") { requestedpage = 1;}
			var startindex = (maxresults * (requestedpage - 1)) + 1;
 $('<scr'+'ipt type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'?category=album&alt=json&callback=picasaweb&access=public"></scr'+'ipt>');//picasaweb
}

//$Update: May 10, 2007$
