
function $a(a){document.write(a);}


//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
currentAlbum=0;
albumCount=0;


function picasaweb(j){ //returns the list of all albums for the user
var myAlbums="";

 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);
  
  myAlbums+="<div class=thumbnailalbum id='album"+i+"' style=\"background-image:url('"+img_base+"?imgmax=288')\" onClick='loadAlbum(\""+id_base+"\",\""+i+"\")'><span class=albumtitle style='float:left'>"+j.feed.entry[i].title.$t+"</span></div>";
  if(i==0)
   {
     	var savEval="setTimeout(\"loadAlbum('"+id_base+"','0');\",1000)";
     	
   }
 }
 albumCount=i;
 document.getElementById("profilealbums").innerHTML=myAlbums;
 
 eval(savEval);
 savEval="";
}

function loadAlbum(j,i){
 
 currentAlbum=i;
 //prompt("",'http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+j+'?category=photo&alt=json&callback=getphotolist');
 loadjsfile('http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+j+'?category=photo&alt=json&callback=albums',"js");
 loadjsfile('http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+j+'?category=photo&alt=json&callback=getphotolist',"js");
 
}


function albums(j){  //returns all photos in a specific album
setTimeout('loadSplash("albums")', 1500);

 //get the number of photos in the album
 var np = j.feed.openSearch$totalResults.$t;

 for (var i=0; i<albumCount; i++){
 	thisname="album"+i;
 	document.getElementById(thisname).style.opacity=".60";
 	document.getElementById(thisname).style.filter="alpha(opacity=60)";
 }
 albumFirst=new Image();
 document.getElementById("album"+currentAlbum+"").style.opacity="1";
 document.getElementById("album"+currentAlbum+"").style.filter="alpha(opacity=100)";
  currentAlbum
 var myAlbum="";
 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();

 for(i=0;i<j.feed.entry.length;i++){

  //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('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 savEval="loadImage('"+img_base+"','"+i+"','"+np+"');";
 }
  myAlbum+=("<div class=thumbnail id='picture"+i+"' style=\"background-image:url('"+img_base+"?imgmax=200');\" onClick=\"loadImage('"+img_base+"','"+i+"','"+np+"')\"><span class=title style='float:left'>"+eval(i+"+1")+"/"+j.feed.entry.length+"</span></div>");  
 }
 document.getElementById("albumthumbs").innerHTML=myAlbum;
 document.getElementById('albumthumbs').scrollTop="0"
 eval(savEval);
 savEval="";
 return true;
}

function nextPhoto(){
	for(i=0;i<photo_array.length;i++){
	 if (photo_array[i]==photo_id)
	 {
		 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
	 }
	}
}
function lastPhoto(){
	for(i=0;i<photo_array.length;i++){
	 if (photo_array[i]==photo_id)
	 {
		 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
	 }
	}
}

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);
  }

 }
 document.getElementById("currentalbum").innerHTML=album_name+" ("+j.feed.entry.length+")";
}




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;


 // is this a video? If so, we will display that in the breadcrumbs below.
 var is_video = 0;
 if (j.entry.media$group.media$content.length > 1)
 {
   //$a('This is a '+j.entry.media$group.media$content[1].medium+'.<br>');
   if (j.entry.media$group.media$content[1].medium == "video")
   {
	   is_video = 1;
   }
 }

 
 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'];

 //album name is now taken from the global variable instead. 7/18/2007
 //
 //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 = next;
 var my_prev = prev;
 var photo_array = photolist;
 //var my_numpics = _GET['np'];
 //instead, we now get this through the global variable my_numpics. 7/18/2007

 //$a("photo ids: "+_GET['photoids']+".<br><br>");
 //$a("photolist: "+photo_array+".<br><br>");

 //var my_galleryname = _GET['galleryname'];
 //var my_fixed_galleryname = my_galleryname.slice(1, my_galleryname.length-1);
 var my_galleryname = album_name;
 var my_fixed_galleryname = album_name;
 var album_base_path = window.location.protocol + "//" + window.location.hostname+window.location.pathname +"?albumid="+ _GET['albumid'];

 // 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","\'");


//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.
//
//NOTE: as of 7/16/2007, the photo array is taken from global photolist (loaded in the getphotolist function) rather than from the URL.
//This has eliminated the need for really long URLs, which were hitting up against the URL length limit in some extreme cases.
for(i=0;i<photo_array.length;i++){
 if (photo_array[i]==photo_id)
 {
	 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
 }
}
//these will be passed along if we move to the next or previous photo - removed the gallery name 7/18/2007
//var prev = album_base_path + "&photoid=" + p1 + "&np=" + my_numpics + "&galleryname=" + my_galleryname.replace("'","%27") + "&prev="+p2+ "&next="+photo_id; //+"&photoids="+photo_array;
//var next = album_base_path + "&photoid=" + n1 + "&np=" + my_numpics + "&galleryname=" + my_galleryname.replace("'","%27") + "&prev="+photo_id+ "&next="+n2; //+"&photoids="+photo_array;
var prev = album_base_path + "&photoid=" + p1; //+"&photoids="+photo_array;
var next = album_base_path + "&photoid=" + n1; //+"&photoids="+photo_array;


//Display the breadcrumbs
var my_item_plural = "";
if (my_numpics != 1)
{
	my_item_plural = "s";
}
var item_label = "picture";
var item_label_caps = "Picture";
if (is_video == 1) //if it's a video, don't say it's a picture, say it's an "item" instead
{
	item_label = "item";
	item_label_caps = "Item";
}
//if (photo_array.length == 1) { var current_index_text = "Total of " + my_numpics + " " + item_label + my_item_plural; } else { var current_index_text = item_label_caps + " " + current_index + " of " + my_numpics; }
var current_index_text = item_label_caps + " " + current_index + " of " + my_numpics;
if (is_video == 1) { current_index_text = current_index_text + "&nbsp;&nbsp;[VIDEO]"; }  //show in the breadcrumbs that the item is a video
$a("<div style='margin-left:3px'><a class='standard' href='"+ window.location.protocol + "//" + window.location.hostname+window.location.pathname+"'>Gallery Home</a> &gt; <a class='standard' href='" + album_base_path + "'>" + my_fixed_galleryname + "</a> &gt; <!--" + img_filename + "-->" + current_index_text + "</div><div style='text-align:right; margin-right:3px; margin-top:-14px'><a target=PICASA class='standard' href='"+photo_link+"'>View this image in Picasa</a></div><br>");


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.
 $a("<center><table border=0><tr valign=top>");
 if (photo_array.length > 1) { $a("<td><a class='standard' href='"+prev+"'><img border=0 alt='Previous item' src='prev.jpg'></a> </td><td></td>"); }
 $a("<td> <a class='standard' href='"+album_base_path+"'><img border=0 alt='Back to album index' src='home.jpg'></a> </td>");
 if (photo_array.length > 1) { $a("<td></td><td> <a class='standard' href='"+next+"'><img border=0 alt='Next item' src='next.jpg'></a></td>"); }
 $a("</tr></table></center><br>");

 var max_width = 658; //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
 $a("<center><a border=0 target=PICASA href='"+photo_link+"'><img id='picture' width="+display_width+" src='"+img_base+"?imgmax="+photosize+"' class='pwimages' /></a></center>");
 $a("<br><center><div style='margin-left:2px'>"+j.entry.media$group.media$description.$t+"</div></center></p>");


 //now we will trap left and right arrow keys so we can scroll through the photos with a single keypress ;-) JMB 7/5/2007
 $a('<script language="Javascript"> function testKeyCode( evt, intKeyCode ) { if ( window.createPopup ) return evt.keyCode == intKeyCode; else return evt.which == intKeyCode; } document.onkeydown = function ( evt ) { if ( evt == null ) evt = event; if ( testKeyCode( evt, 37 ) ) { window.location = "' + prev + '"; return false; } if ( testKeyCode( evt, 39 ) ) { window.location = "' + next + '"; return false; } } </script>');


 // an attempt at resampling the photo, rather than relying on the browser's internal resize function. doesn't work, unfortunately.
 //
 //$a("<?php $filename='"+img_base+"?imgmax="+photosize+"'; $width = 658; $height = 1600; list($width_orig, $height_orig) = getimagesize($filename); ");
 //$a("$ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } ");
 //$a("$image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); ");
 //$a("imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); imagejpeg($image_p, null, 100); ?>");

}

/*if(_GET['photoid']&&_GET['albumid']){

 $a('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=getphotolist"></script>');//get the list of photos in the album and put it in the global "photolist" array so we can properly display the navigation arrows; this eliminates the need for really long URLs :-) 7/16/2007
 $a('<script type="text/javascript" src="http://picasaweb.google.com/data/entry/base/user/'+username+'/albumid/'+_GET['albumid']+'/photoid/'+_GET['photoid']+'?alt=json&callback=photo"></script>');//photo

}else if(_GET['albumid']&&!_GET['photoid']){
 $a('<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{
 $a('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+username+'?category=album&alt=json&callback=picasaweb&access=public"></script>');//picasaweb
}*/


function loadImage(image,number,np){

	document.getElementById("lightbox").src=loadingPicture.src;
	document.getElementById("lightbox").width=loadingPicture.width;
	document.getElementById("lightbox").height=loadingPicture.height;
	currentPicture=image;
	for (var j=0; j<np; j++){
		thisname="picture"+j+"";
		document.getElementById(thisname).style.opacity=".60";
		document.getElementById(thisname).style.filter="alpha(opacity=60)";
	}
	document.getElementById("picture"+number+"").style.opacity="1";
	document.getElementById("picture"+number+"").style.filter="alpha(opacity=100)";;
	myNewImage=new Image();
	myNewImage.onload = function(){
	if((myNewImage.height/maxHeight) > (myNewImage.width/maxWidth)){
			myNewImage.width=Math.floor(myNewImage.width/(myNewImage.height/maxHeight));
			myNewImage.height=maxHeight;
		}else{
			myNewImage.height=Math.floor(myNewImage.height/(myNewImage.width/maxWidth));
			myNewImage.width=maxWidth;
		}
		var myEffect = new Fx.Morph('lightbox', {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		document.getElementById("lightbox").src="blank.gif";
		if(document.getElementById("lightbox").width!=myNewImage.width || document.getElementById("lightbox").height!=myNewImage.height){
			setTimeout("fadeInPhoto('lightbox',myNewImage)", 350)
		}else{
			fadeInPhoto('lightbox',myNewImage);
		}
		var myEffect = new Fx.Morph('lightbox', {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		 
		myEffect.start({
		    'height': myNewImage.height, 
		    'width': myNewImage.width   
		});
		var myEffect = new Fx.Morph('photoframe', {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
				 
		myEffect.start({
		    'height': myNewImage.height, 
		    'width': myNewImage.width   
		});
		//document.getElementById("lightbox").width=myNewImage.width;
		//document.getElementById("lightbox").height=myNewImage.height-2;
		//alert ("hi?");
	}
	//prompt("url",image+"?imgmax=800");
	myImageLocation=unescape(image+"?imgmax=800");
	myNewImage.src=myImageLocation;
	return true;
}

function fadeInPhoto(lightbox, image){
	$(lightbox).setStyles({
		'filter':'alpha(opacity=0)',
		'-moz-opacity':0,
		'-khtml-opacity': 0,
		'opacity': 0
	});
	document.getElementById("lightbox").src=unescape(image.src)
	var myEffect = new Fx.Morph(lightbox, {duration: 'normal', transition: Fx.Transitions.Sine.easeIn});
	myEffect.start({
		'filter':'alpha(opacity=1)',
		'-moz-opacity':1,
		'-khtml-opacity': 1,
		'opacity': 1
	});
}

function loadSplash(slide){
	if(slide == "albums"){
		document.getElementById("splashscreen").style.visibility="hidden";
		document.getElementById('albumcontainer').style.visibility="visible";
	}else{
		document.getElementById("splashscreen").style.visibility="visible";
		document.getElementById("splashscreen").innerHTML=document.getElementById(slide).innerHTML;
		document.getElementById("splashscreen").scrollTop=0;
		//document.getElementById('albumcontainer').style.visibility="hidden";
	}
}

function loadjsfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
if (typeof fileref!="undefined"){
	document.getElementsByTagName("head")[0].appendChild(fileref)
	return true;
}
}
