
//  AJAX gallery browser display javascript.
//  Peter J Lawrence (December 2008)
//  Tested with picasa, coppermine, photobucket and flickr RSS feeds

var thumbsize = 72; //size thumb /cropped(see for supported format:  http://code.google.com/apis/picasaweb/reference.html#Parameters)
var cols = 4; // distribute thumbs on albumphotos page over x collums
var maxresults = 12; //maximum of pictures on albumphotos page
var photosize = 512; //return maximum size picture (see for supported format:  http://code.google.com/apis/picasaweb/reference.html#Parameters)
var g_aRSSRequestObject = new Array();
var scriptfolder="/modules/RSSBrowser/";
var g_TableHeaderCol = '#8CA6B5';
var g_TableBackGroundCol = '#F2F5F7';

function DisplayAlbum(htmltag,albumpage)
{
	// Parsing RSS
	var node = g_aRSSRequestObject[htmltag].responseXML.documentElement; 
	if (node==null || node.getElementsByTagName('channel').item(0)==null)
	{
			content='<b>No Data Found</b>';
			document.getElementById(htmltag).innerHTML = content;
			return;
	}
	// Get Channel information
	var channel = node.getElementsByTagName('channel').item(0);
	var title = channel.getElementsByTagName('title').item(0).firstChild.data;
	var link = channel.getElementsByTagName('link').item(0).firstChild.data;
	var album_date = channel.getElementsByTagName('pubDate').item(0).firstChild.data;
	var loc = "";
	var desc= "";
	if (channel.getElementsByTagName('description').item(0))
	{
		desc=channel.getElementsByTagName('description').item(0).firstChild;
	}
	var id = "";
	var username ="";
	var gphotoname="";
	var items = channel.getElementsByTagName('item');
	var pageCount = parseInt((items.length-1)/maxresults);
	var item_plural = "s";
	if (items.length == "1") { item_plural = ""; }
    var page_plural = "s";
	if (pageCount == "0") { page_plural = ""; }
 
    content = "<table align='center' width='100%' cellspacing='1' style='clear: both; table-layout: fixed;'>";
	content+= "<tr><td colspan='"+cols+"'>";
	content+= "<div style='margin-left:3px'><b><i>Album:</i></b> <a href='"+link+"'>"+ title +"</a></div>";
	content+= "</td></tr>";
	
    //create paging navigation
	var navRow = "<tr><td bgcolor='"+g_TableHeaderCol+"' colspan='"+cols+"'>";
    navRow+="<table width='100%'><tr><td>";
    navRow+="<div style='margin-left:3px'><i>"+ items.length +" photo"+item_plural+" on "+(pageCount+1)+" page"+page_plural+"</i></div></td>";
    navRow+="<td align='right'><div style='margin-right:3px'>";
	var StartItem=albumpage*maxresults;
	if (StartItem>items.length)
	{
		albumpage=0;
		StartItem=0;
	}
	var i;
	for(i=0;i<=pageCount;i++)
	{
		if(albumpage == i)
		{
			navRow += "<b>["+(i+1)+"]</b> ";
		}
		else
		{
		    navRow += "<a href='javascript:DisplayAlbum(\""+htmltag+"\","+i+")'>"+(i+1)+"</a> ";
		};
	};
    navRow +="</div></td></tr></table>"
	navRow += "</td></tr>"
	content += navRow; 
	
	var EndItem =(albumpage+1)*maxresults;
	if (EndItem>items.length) EndItem=items.length;
	var PhotoCount=0;
	for(i=StartItem;i<EndItem;i++)
	{
		var img_base, id_base, img_title, photoDate;
	    var TheItem=items[i].getElementsByTagName('media:thumbnail');
		if (TheItem && TheItem.item(0))
		{
			if (TheItem.item(1))
			{
				img_base = TheItem.item(1).getAttribute('url');
			}
			else
			{
				img_base = TheItem.item(0).getAttribute('url');
			}
		}
		else
		{
			// google chrome does not like :'s so try this
			TheItem=items[i].getElementsByTagName('thumbnail');
			if (TheItem && TheItem.item(0))
			{
				if (TheItem.item(1))
				{
					img_base = TheItem.item(1).getAttribute('url');
				}
				else
				{
					img_base = TheItem.item(0).getAttribute('url');
				}
			}
		}
        TheItem=items[i].getElementsByTagName('media:content');
		if (TheItem && TheItem.item(0))
		{
			id_base = TheItem.item(0).getAttribute('url');
		}
		else
		{
			// google chrome does not like :'s so try this
			TheItem=items[i].getElementsByTagName('content');
			if (TheItem && TheItem.item(0))
			{
				id_base = TheItem.item(0).getAttribute('url');
			}
		}
		TheItem=items[i].getElementsByTagName('pubDate');
		if (TheItem)
		{
			photoDate = TheItem.item(0).firstChild.data;
		}
		TheItem=items[i].getElementsByTagName('media:description');
		var img_title="";
		if (TheItem && TheItem.item(0) && TheItem.item(0).firstChild)
		{
			img_title =TheItem.item(0).firstChild.data;
		}
		else
		{
			TheItem=items[i].getElementsByTagName('title');
			if (TheItem && TheItem.item(0) && TheItem.item(0).firstChild)
			{
				img_title = TheItem.item(0).firstChild.data;
			}
		}
        content += "<td bgcolor=\""+g_TableBackGroundCol+"\" valign=\"middle\" align=\"center\">";	
        var matchPos1 = link.indexOf("picasa");
        if(matchPos1 != -1)
        {
            content += "<a href='"+img_title+"' id='"+img_title+"' class='image' target='_blank' onclick='return false;'>"
            content += "<img src='"+img_base+"?imgmax="+photosize+"' class='set1' onclick='enlarge(this);' alt='"+img_title+"' id='"+i+htmltag+"'  longdesc='"+id_base+"?imgmax="+photosize+"' title='"+img_title+"' />";
        }
        else
        {
            content += "<a href='"+img_title+"' id='"+img_title+"' class='image' target='_blank' onclick='return false;'>"
            content += "<img src='"+img_base+"' class='set1' onclick='enlarge(this);' alt='"+img_title+"' id='"+i+htmltag+"'  longdesc='"+id_base+"' title='"+img_title+"' />";
        }
		content += "</a>";
        content +="<br/>";
		//content += "<small>"+photoDate+"</small>";
		content += "<small>"+img_title+"</small>";
        content += "</td>";
		if (PhotoCount % cols == (cols-1))
		{
			content += "</tr><tr>";
		}
		PhotoCount++;
	}
    // add any empty cells
    PhotoCount = cols - ((PhotoCount-1) % cols) -1;
    for (i=0;i<PhotoCount;i++)
    {
        content += "<td bgcolor=\""+g_TableBackGroundCol+"\" ></td>";
    }
	content += "</tr>";
	content += navRow; 
    
    var GeneratedBy = null;
    var GeneratedByURI = '';
    
    TheItem=channel.getElementsByTagName('generator');
    if (TheItem && TheItem.item(0) && TheItem.item(0).firstChild)
    {
        GeneratedBy =TheItem.item(0).firstChild.data;
    }
    if (GeneratedBy)
    {
        content+= "<tr><td colspan='"+cols+"'>";
        content+="<table width='100%'><tr>";
        content+= "<td><small><i><a href='"+link+"'>View Album: "+title+"</a></i></small></td>";
        content+= "<td align=\"right\"><small><i>"+GeneratedBy+"</i></small></td>";
        content+= "</tr></table>";
        content+= "</td></tr>";
    }
	content += "</table>";
	
	// Display the result
	document.getElementById(htmltag).innerHTML = content;
}

/*
* Main AJAX RSS reader request stuff
*/

function AllocateXMLHttpObject()
{
	var RSSRequestObject = false;
	// Provide the XMLHttpRequest class for IE 5.x-6.x:
	if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
		  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
		  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
		  try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
		  try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
		  throw new Error( "This browser does not support XMLHttpRequest." )
	};

	RSSRequestObject = new XMLHttpRequest();
	return(RSSRequestObject);
}

function RSSRequest(aURLfeed,htmltag)
{
    if (g_aRSSRequestObject[htmltag]==false);
	{
		g_aRSSRequestObject[htmltag] = AllocateXMLHttpObject();
		if (g_aRSSRequestObject[htmltag]==false)
		{
			document.getElementById(htmltag).innerHTML = "This browser does not support XMLHttpRequest.";
			return;
		}
	}
	document.getElementById(htmltag).innerHTML = "Loading photos ... <img id='loader' src='"+scriptfolder+"loader.white.gif'/>";
	
	// Prepare the request
	g_aRSSRequestObject[htmltag].open("GET", aURLfeed , true);
	// Set the onreadystatechange function
	g_aRSSRequestObject[htmltag].onreadystatechange = function ReqChange()
		{
			// If data received correctly
			if (g_aRSSRequestObject[htmltag].readyState==4)
		    {	
				// if data is valid
				if (g_aRSSRequestObject[htmltag].responseText.indexOf('invalid') == -1) 
				{ 	
					DisplayAlbum(htmltag,0);
				}
				else
				{
					// Tell the reader that there was error requesting data
					document.getElementById(htmltag).innerHTML = "<div class=error>Error requesting data.<div>";
				}
			}
		}
;
	// Send
	g_aRSSRequestObject[htmltag].send(null); 
}
