﻿google.load("feeds", "1");

var photos = [];
var photonames = [];
var currentindex;

function loadAlbums(result) {
	var container = document.getElementById("feed");
    container.innerHTML = ""
    for (var i = 0; i < result.feed.entry.length; i++) {
        var entry = result.feed.entry[i];
        var div = document.createElement('div');
        var a = document.createElement("a");
		
        var albumFeed = entry.id.$t.replace('/entry/','/feed/') + "&callback=manyPhotosCallback&thumbsize=160"
        a.onclick = new Function("return loadPhotos('" + albumFeed + "');");
        a.setAttribute("href","#");
        var img = document.createElement("img");
        img.setAttribute("src",entry.media$group.media$thumbnail[0].url);
        
        a.appendChild(img);
        div.appendChild(a);
		div.className = "singlePictureDiv";
        
        var infoDiv = document.createElement("div");
        infoDiv.innerHTML = entry.title.$t + "<br />" + entry.gphoto$numphotos.$t + " Photos";
        div.appendChild(infoDiv);
        container.appendChild(div);
    }
}
//This function takes the feed for each album and displays each photo from that feed
function loadPhotos(feedurl) {
    var container = document.getElementById("feed");
    document.getElementById("hiddenAlbumLink").href = feedurl;
    
    removeChildrenFromNode(container);
    removeChildrenFromNode(document.getElementById("links"));
    removeChildrenFromNode(document.getElementById('goleft'));
	removeChildrenFromNode(document.getElementById('goright'));
    
    var a = document.createElement('a');
    a.href = "#";
    a.onclick = new Function("return reloadAlbums();");
    a.appendChild(document.createTextNode('Return to all Albums'));
    document.getElementById("links").appendChild(a);
    
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = feedurl;
    container.appendChild(script);
}

function removeChildrenFromNode(node) {
   if(node == undefined || node == null)
   {
      return;
   }
   var len = node.childNodes.length;
   while (node.hasChildNodes())
   {
      node.removeChild(node.firstChild);
   }
}


//This function renders each photo from the photo based feed
function manyPhotosCallback(result) {
    removeChildrenFromNode(document.getElementById('goleft'));
	removeChildrenFromNode(document.getElementById('goright'));

    var container = document.getElementById("feed");
    photos = [];
    for (var i = 0; i < result.feed.entry.length; i++) {
        var entry = result.feed.entry[i];
        //Create a div to put each photo in
        var div = document.createElement('div');
        //Create an image to display each thumbnail
        var img = document.createElement("img")
        img.src = result.feed.entry[i].media$group.media$thumbnail[0].url;
        var a = document.createElement('a');
        a.href = '#';
            
		var photoname = result.feed.entry[i].title.$t;
		var photolink = result.feed.entry[i].media$group.media$content[0].url;
		var j = photolink.length;
		var currChar = "";
		while(currChar != "/" || j < 0){
		    j = j - 1;
		    currChar = photolink.charAt(j);
		}

		var tempaddress = photolink.substring(0,j) + "/s800/" + photolink.substring(j+1);
        a.onclick = new Function("return displaySinglePhoto('" + tempaddress + "','" + photoname + "');");
        a.appendChild(img);
        photos[i] = tempaddress;
        photonames[i] = photoname;
        div.appendChild(a);
		div.className = "singlePictureDiv";
		var infoDiv = document.createElement("div");
        infoDiv.appendChild(document.createTextNode(entry.title.$t.replace(".jpg","").replace(".JPG","").replace(".Jpg","")));
        div.appendChild(infoDiv);
						
        container.appendChild(div);
    }
}
function reloadAlbums(){
	var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = document.getElementById("initialScript").src;
    document.getElementById("feed").appendChild(script);
    removeChildrenFromNode(document.getElementById('links'));
    removeChildrenFromNode(document.getElementById('photo'));
    removeChildrenFromNode(document.getElementById('goleft'));
	removeChildrenFromNode(document.getElementById('goright'));
	document.getElementById('feed').style.height = '600px';
}
		
function displaySinglePhoto(photoUrl,photoname) {
    var container = document.getElementById("photo");
    var titleDiv = document.createElement('div');
	titleDiv.appendChild(document.createTextNode(photoname.replace(".jpg","")));
	titleDiv.className = "photoTitle";
	titleDiv.id = "photoName";
	
	var img = document.createElement('img');
	img.id = "currentPhoto";
    img.src = photoUrl;
    img.style.width = "600px";
    var feedContainer = document.getElementById("feed");
    removeChildrenFromNode(feedContainer);
				
    feedContainer.style.height = '0px';
				
	container.appendChild(titleDiv);
    container.appendChild(img);
    
    for (var i = 0; i < photos.length; i++) {
		if (photos[i] == photoUrl){
			currentindex = i;
		}
	}
	    
    var golefta = document.createElement('a')
    golefta.href = "#";
    golefta.onclick = new Function("return navigate('left');");
    var leftimg = document.createElement('img');
    leftimg.src = 'images/left_16x16.gif'
    golefta.appendChild(leftimg);
    golefta.id = 'goleftlink';
    
    var gorighta = document.createElement('a')
    gorighta.href = "#";
    gorighta.onclick = new Function("return navigate('right');");
    var rightimg = document.createElement('img');
    rightimg.src = 'images/right_16x16.gif'
    gorighta.appendChild(rightimg);
    gorighta.id = 'gorightlink';
    
    removeChildrenFromNode(document.getElementById('goleft'));
	removeChildrenFromNode(document.getElementById('goright'));
    document.getElementById("goleft").appendChild(golefta);
    document.getElementById("goright").appendChild(gorighta);
    
    var a = document.createElement('a');
    a.href = "#";
    a.onclick = new Function("return reloadPhotos();");
    a.appendChild(document.createTextNode('Return to this Album'));
    a.id = "singleAlbum";
    document.getElementById("links").appendChild(document.createTextNode(' | '));
    document.getElementById("links").appendChild(a);
}

function navigate(direction){
	if (direction == 'right')
	{
		currentindex = currentindex + 1;
		if (currentindex == photos.length){
			currentindex = 0;
		}
	}
	else
	{
		currentindex = currentindex - 1;
		if (currentindex == -1){
			currentindex = photos.length - 1;
		}
	}
	document.getElementById("currentPhoto").src = photos[currentindex];
	removeChildrenFromNode(document.getElementById("photoName"));
	document.getElementById("photoName").appendChild(document.createTextNode(photonames[currentindex]));
}

function reloadPhotos(){
	var container = document.getElementById('links');
    container.removeChild(container.childNodes[1]);
    container.removeChild(container.childNodes[1]);
    removeChildrenFromNode(document.getElementById('photo'));
    loadPhotos(document.getElementById('hiddenAlbumLink').getAttribute("href"));
	document.getElementById('feed').style.height = '600px';
}


