function Fotos_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 = Fotos_readGet();

var photo_array = new Array();
var album_name = "";
var my_numpics = "";

var global_album_id = "";
var global_foto_id = "";
var commentName = "";

var picAccount = "fenpruverkes";

function CatchFotoList(j) {
	album_name = j.feed.title.$t;
	my_numpics = j.feed.entry.length;
	 album_name = j.feed.title.$t;
	for(i=0;i<my_numpics;i++){
		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);
		
		photo_array[i] = id_base;
	}
}

function CatchFoto(j) {
	FotoContent = "";
	
	//Read parameters
	picAccount = j.entry.id.$t.substring(j.entry.id.$t.indexOf("user/")+5, j.entry.id.$t.indexOf("/albumid"));
	if(picAccount.substring(0, baseAccount.length) == baseAccount) {
		var postac = picAccount.substring(baseAccount.length);
	} else {
		var postac = picAccount.substring(baseAccount.length)+picAccount.substring(0, baseAccount.length);	
	}
	var album_id = _GET['albumid'];
	var album_base_path = baseuri+"&albumid="+ album_id+"&postac="+ postac;
 	var img_title = j.entry.title.$t;
	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 = photo_link.substring(photo_link.indexOf("#")+1);
 
	//Find prev and next photo
	for(i=0;i<photo_array.length;i++){
		if (photo_array[i]==photo_id)
		{
			var prev_photo_id = 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 next_photo_id = photo_array[i+1]; //ID of the picture one ahead of this one; if null, we're at the end of the album
		}
	}

	//Header
	var current_index_text = "Foto " + current_index + " van " + my_numpics;
	FotoContent += 
	"<div style='text-align:left; margin-left:3px'>"
	+"<a class='standard' href='"+baseuri+"'>Overzicht</a>"
	+" &gt; <a class='standard' href='" + album_base_path + "'>" + album_name + "</a>"
	+" &gt; " + current_index_text
	+"</div>"
	+"<div style='text-align:right; margin-right:3px; margin-top:-14px'><a href='"+photo_link.replace("#", "#slideshow/")+"' target='PICASA'>Bekijk Slideshow</a></div><br>";

	//Navigation (Prev en Next)
	FotoContent += "<table><tr><td style='width:120px;'>";
	if(prev_photo_id) {
		FotoContent += "<div class='pagerbutton' onmouseover=\"this.className='pagerbutton pagerbutton_over'\" onmouseout=\"this.className='pagerbutton'\" onclick=\"Fotos_RefreshFoto('"+album_id+"', '"+prev_photo_id+"');\"><img align='absmiddle' src='pictures/guestbook/pageback.gif' border='0'>&nbsp;&nbsp;Vorige Foto</div>";
	} else {
		FotoContent += "<div class='pagerbutton pagerbutton_disabled'><img align='absmiddle' src='pictures/guestbook/pageback.gif' border='0'>&nbsp;&nbsp;Vorige Foto</div>";
	}
	FotoContent += "</td><td style='width:120px;'>";
	if(next_photo_id) {
		FotoContent += "<div class='pagerbutton' onmouseover=\"this.className='pagerbutton pagerbutton_over'\" onmouseout=\"this.className='pagerbutton'\" onclick=\"Fotos_RefreshFoto('"+album_id+"', '"+next_photo_id+"');\">Volgende Foto&nbsp;&nbsp;<img align='absmiddle' src='pictures/guestbook/pageahead.gif' border='0'></div>";
	} else {
		FotoContent += "<div class='pagerbutton pagerbutton_disabled'>Volgende Foto&nbsp;&nbsp;<img align='absmiddle' src='pictures/guestbook/pageahead.gif' border='0'></div>";
	}
	FotoContent += "</td></tr></table>";
	
	//FotoDisplay
	FotoContent += "<br/><center><a border=0 target=PICASA href='"+photo_link+"'><img id='picture' border='0' width="+photosize+" src='"+img_base+"?imgmax="+photosize+"' class='pwimages' /></a></center>";

	//Comments
	
	if(Modernizr.localstorage) { 
		if (localStorage.commentName)
		{
			commentName = localStorage.commentName;
		}
	}
	
	 FotoContent += "<h3>Reacties:</h3><table border='0' cellspacing='0' cellpadding='0' id='FotoComments' class='normal'>"
	+"<tr><td><input type='text' id='comName' size='10' maxlength='30' autocomplete='on' value='"+commentName+"'/></td><td style='width:3px;'></td>"
	+"<td colspan='2'><input type='text' id='comMes' size='85' autocomplete='off' /></td></tr>"
	+"<tr><td colspan='4' align='right'><input type='button' value='Reageer' onclick='Fotos_Reageer(\""+album_id+"\", \""+album_name+"\", \""+photo_id+"\", \""+img_base+"\");' /></td></tr>"
	+"</table>";
	
	var FotoDiv = document.getElementById("FotoDiv");
	FotoDiv.innerHTML = FotoContent;
	
	httpObjectFCGet = getHTTPObject();
	if (httpObjectFCGet != null) {
		httpObjectFCGet.open("GET", "ajax.php?type=fotocomments&method=GET&photoid="+photo_id, true);
		httpObjectFCGet.send(null);
		httpObjectFCGet.onreadystatechange = CatchFotoComments;
	}
}

function CatchFotoComments() {
	if(httpObjectFCGet.readyState == 4){
		jsonObject = eval('(' + httpObjectFCGet.responseText + ')');

		var photoId = jsonObject.PhotoId;
		var photoComments = jsonObject.Comments;//.join("\n");

		if(photoComments.length > 0 ) {
			
			for(var i in photoComments)
			{
				var photoComment = photoComments[i];
				Fotos_AddCommentRow(photoComment.Id, photoComment.Name+": ", photoComment.Message, photoComment.Time);
			}
	
		} 
	}
}

function Fotos_AddCommentRow(cId, cName, cMes, cTime) {
	var commentsTbl = document.getElementById('FotoComments');
				
	var row = commentsTbl.insertRow(commentsTbl.rows.length-2);
	row.vAlign = "top";
	// Name			
	var cell1 = row.insertCell(0);
	cell1.style.fontWeight = "bold";
	var nameNode = document.createTextNode(cName);
	cell1.appendChild(nameNode);
	
	// Whitespace
	var cell2 = row.insertCell(1);
	cell2.width = "3";
	
	// Message
	var cell3 = row.insertCell(2);
	var mesNode = document.createTextNode(cMes);
	cell3.appendChild(mesNode);
	
	// Time
	var cell4 = row.insertCell(3);
	cell4.align = "right";
	cell4.style.fontStyle = "italic";
	cell4.style.fontSize = "smaller";
	cell4.style.whiteSpace = "nowrap";
	var timeNode = document.createTextNode(cTime);
	cell4.appendChild(timeNode);
	
	if(Modernizr.sessionstorage) {
	 if(sessionStorage.lastComment == cId) {
		var delImage = new Image(); 
		delImage.src = "img/icons/delete_icon.gif";
		delImage.style.cursor = "pointer";
		delImage.style.verticalAlign = "middle";
		delImage.title = "Verwijder deze reactie";
		delImage.onclick = function () 
		{ 
			httpObjectFCDel = getHTTPObject();
			if (httpObjectFCDel != null) {
				httpObjectFCDel.open("GET", "ajax.php?type=fotocomments&method=DELETE&cId="+cId, true);
				httpObjectFCDel.send(null);
				httpObjectFCDel.onreadystatechange = function()
				{
					if(httpObjectFCDel.readyState == 4){
						Fotos_RefreshFoto();
					}
				}
			}
		};
		cell4.appendChild(delImage);
	 }
	}
}

function Fotos_Reageer(album_id, album_name, photo_id, imgurl) {
	var newName = document.getElementById('comName');
	var newMes = document.getElementById('comMes');
	
	if(!newName.value || newName.value == "") {
		newName.value = "Anoniempje";
	} else if(newMes.value && newMes.value != "") {
		httpObjectFCSave = getHTTPObject();
		if (httpObjectFCSave != null) {
			httpObjectFCSave.open("GET", "ajax.php?type=fotocomments&method=INSERT&accountid="+picAccount+"&albumid="+album_id+"&albumname="+album_name+"&photoid="+photo_id+"&imgurl="+imgurl+"&photo_krabbelaar="+newName.value+"&photo_krabbel="+newMes.value, true);
			httpObjectFCSave.send(null);
			httpObjectFCSave.onreadystatechange = function()
			{
				if(httpObjectFCSave.readyState == 4){
					if(Modernizr.sessionstorage) {
						sessionStorage.lastComment = httpObjectFCSave.responseText;
					}
					Fotos_RefreshFoto(album_id, photo_id);
				}
			}
		}
		if(Modernizr.localstorage) { 
			localStorage.commentName = newName.value;
		}
		newMes.value = "";
	}
}

function Fotos_RefreshFoto(albumid, photoid) {
	if(!albumid && !photoid) {
		var albumid = global_album_id;
		var photoid = global_photo_id;
	}
	
	var url = 'http://picasaweb.google.com/data/entry/base/user/'+picAccount+'/albumid/'+albumid+'/photoid/'+photoid+'?alt=json&callback=CatchFoto';
	var fileref=document.createElement('script');
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", url);
	document.getElementsByTagName("head")[0].appendChild(fileref);
	
	global_album_id = albumid;
	global_photo_id = photoid;
}

function CatchFotoAlbum(j) {
	picAccount = j.feed.id.$t.substring(j.feed.id.$t.indexOf("user/")+5, j.feed.id.$t.indexOf("/albumid"));
	if(picAccount.substring(0, baseAccount.length) == baseAccount) {
		var postac = picAccount.substring(baseAccount.length);
	} else {
		var postac = picAccount.substring(baseAccount.length)+picAccount.substring(0, baseAccount.length);	
	}
	//get the number of photos in the album
	var np = j.feed.openSearch$totalResults.$t;
	var item_plural = "'s";
	if (np == "1") { item_plural = ""; }

	var album_id = _GET['albumid'];
	var album_begin = j.feed.entry[0].summary.$t.indexOf('href="')+6;
	var album_end = j.feed.entry[0].summary.$t.indexOf('#');
	var album_link = j.feed.entry[0].summary.$t.slice(album_begin, album_end);
	album_name = j.feed.title.$t;

	$("<div style='text-align:left; margin-left:3px'><a class='standard' href='"+baseuri+"'>Overzicht</a> &gt; "+ album_name +"&nbsp;&nbsp;["+np+" foto"+item_plural+"]</div>");
	$("<div style='text-align:right; margin-right:5px; margin-top:-14px'><a href='"+album_link+"#slideshow' target='PICASA'>Bekijk Slideshow</a></div><br>");
	
	for(i=0;i<j.feed.entry.length;i++){
		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);
		var link_url = baseuri+"&albumid="+album_id+"&photoid="+id_base+"&postac="+postac;
		
		$("<div style='width:170px; height:180px; float:left;'>");
		$("<div id='photo"+id_base+"' class='nrc' title='opmerkingen gemaakt bij deze foto'></div>");
		$("<a href='"+link_url+"'><img border='0' src='"+img_base+"?imgmax=160&crop=1' /></a>");
		$("</div>");
		
	//	trysave("fenpruverkes", _GET['albumid'], album_name, id_base, img_base);
	}
		httpObjectAlbumComments = getHTTPObject();
		if (httpObjectAlbumComments != null) {
			httpObjectAlbumComments.open("GET", "ajax.php?type=fotocomments&method=ALBUMGET&albumid="+album_id, true);
			httpObjectAlbumComments.send(null);
			httpObjectAlbumComments.onreadystatechange = function()
			{
				if(httpObjectAlbumComments.readyState == 4){
					jsonObject = eval('(' + httpObjectAlbumComments.responseText + ')');

					var commentedPhotos = jsonObject.CommentedPhotos;//.join("\n");
					for (i in commentedPhotos) {
						var commentedPhoto = commentedPhotos[i];
						var photoDiv = document.getElementById(commentedPhoto.Id);
						photoDiv.innerHTML = commentedPhoto.Count;
						photoDiv.title = commentedPhoto.Count + " " + photoDiv.title;
					}
				}
			}
		}
}

function trysave(accountid, albumid, albumname, photoid, imgurl) {
	httpObjecttempsave = getHTTPObject();
	if (httpObjecttempsave != null) {
		httpObjecttempsave.open("GET", "ajax.php?type=fotocomments&method=INSERTTEMP&accountid="+accountid+"&albumid="+albumid+"&albumname="+albumname+"&photoid="+photoid+"&imgurl="+imgurl, true);
		httpObjecttempsave.send(null);
	}
}

function CatchFotoAlbumList(j) {
	picAccount = j.feed.id.$t.substring(j.feed.id.$t.lastIndexOf("/")+1);

	if(picAccount.substring(0, baseAccount.length) == baseAccount) {
		var postac = picAccount.substring(baseAccount.length);
	} else {
		var postac = picAccount.substring(baseAccount.length)+picAccount.substring(0, baseAccount.length);	
	}
	var curYear = 0 ;
	for(i=0; i<j.feed.entry.length; i++){
		var img_base = j.feed.entry[i].media$group.media$thumbnail[0].url;
		var img_width = j.feed.entry[i].media$group.media$thumbnail[0].width;
		var img_height = j.feed.entry[i].media$group.media$thumbnail[0].height;
		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);
  
		publishYear = parseInt(j.feed.entry[i].published.$t.substring(0, 4), 10);
		if(curYear != publishYear) {
			$("<div style='float:left; width:100%;'><hr/><h2>"+publishYear+"</h2><br/></div>");
			curYear = publishYear;
		}
		
		var albumurl = baseuri+"&albumid="+id_base+"&postac="+postac;

		$("<div style='width:"+(img_width+20)+"px; height:"+(img_height+60)+"px; float:left;'>");
		$("<div style='width:"+(img_width+4)+"px; height:"+(img_height+4)+"px; background-image:url(img/dropshadow.png); text-align:left; margin-left: auto; margin-right:auto; '>");
		$("<a class='standard' href='"+albumurl+"'><img src='"+img_base+"' border='0'/></a>");
		$("</div>");
		$("<center><a class='standard' href='"+albumurl+"'>"+ j.feed.entry[i].title.$t +"</a></center>");
		$("</div>");
	}
}

function FotoPage() {
  if(_GET['postac']) {
	var postac = _GET['postac'];
	if(postac.length > 4) {
		var picAccount = postac.substring(postac.length - baseAccount.length) + postac.substring(0, postac.length - baseAccount.length);	
	} else {
		var picAccount = baseAccount + postac;
	}
  } else {
  		var picAccount = baseAccount;
  }
	
	if(_GET['photoid']&&_GET['albumid']){
		$('<div id="FotoDiv"></div>');
		$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+picAccount+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=CatchFotoList"></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
		$('<script type="text/javascript" src="http://picasaweb.google.com/data/entry/base/user/'+picAccount+'/albumid/'+_GET['albumid']+'/photoid/'+_GET['photoid']+'?alt=json&callback=CatchFoto"></script>');//photo
	}else if(_GET['albumid']&&!_GET['photoid']) {
		$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/'+picAccount+'/albumid/'+_GET['albumid']+'?category=photo&alt=json&callback=CatchFotoAlbum"></script>');//albums
	} else {
		$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/112819192874944256323?category=album&alt=json&callback=CatchFotoAlbumList&access=public&thumbsize=144c"></script>');//picasaweb
		$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/fenpruverkes2011?category=album&alt=json&callback=CatchFotoAlbumList&access=public&thumbsize=144c"></script>');//picasaweb
		$('<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/fenpruverkes?category=album&alt=json&callback=CatchFotoAlbumList&access=public&thumbsize=144c"></script>');//picasaweb
	}
}



function homepagelist(j) {
	var newfotoDiv = document.getElementById('newfotoDiv');

	var baseuri='index.php?page=Fotos';
	var baseAccount = 'fenpruverkes';
	
	picAccount = j.feed.id.$t.substring(j.feed.id.$t.lastIndexOf("/")+1);
	if(picAccount.substring(0, baseAccount.length) == baseAccount) {
		var postac = picAccount.substring(baseAccount.length);
	} else {
		var postac = picAccount.substring(baseAccount.length)+picAccount.substring(0, baseAccount.length);	
	}

	my_numpics = j.feed.entry.length;
	album_name = j.feed.title.$t;
	for(i=0;i<j.feed.entry.length && i<4 ;i++){
		var publishDate = getDateObj(j.feed.entry[i].published.$t);
		var updateDate = getDateObj(j.feed.entry[i].updated.$t);
		
		var today = new Date();
		publishDate.setDate(publishDate.getDate()+30);
		updateDate.setMinutes(updateDate.getMinutes()+65);
		//alert(updateDate+","+today);
		if (publishDate >= today && updateDate < today)
		{
			newfotoDiv.style.visibility = "visible";

			var img_base = j.feed.entry[i].media$group.media$thumbnail[0].url;
			var img_width = j.feed.entry[i].media$group.media$thumbnail[0].width;
			var img_height = j.feed.entry[i].media$group.media$thumbnail[0].height;
			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);

			var albumurl = baseuri+"&albumid="+id_base+"&postac="+postac;
			
			newfotoDiv.innerHTML += 
			"<div style='width:"+(img_width+20)+"px; height:"+(img_height+60)+"px;'>"
			+"<div style='width:"+(img_width+4)+"px; height:"+(img_height+4)+"px; background-image:url(img/dropshadow.png); text-align:left; margin-left: auto; margin-right:auto; '>"
			+"<a class='standard' href='"+albumurl+"'><img src='"+img_base+"' border='0'/></a>"
			+"</div>"
			+"<center><a class='standard' href='"+albumurl+"'>"+ j.feed.entry[i].title.$t +"</a></center>"
			+"</div>"
		}
	}
}

function getDateObj(gaDate, endDate) {
  var date = new Date(
	 parseInt(gaDate.substring(0, 4), 10)
	,parseInt(gaDate.substring(5, 7), 10) - 1
	,(gaDate.length > 10 || !endDate) ? parseInt(gaDate.substring(8, 10), 10) : parseInt(gaDate.substring(8, 10), 10)-1 
  	,(gaDate.length > 10) ? parseInt(gaDate.substring(11, 13), 10) : endDate ? 23 : 0
	,(gaDate.length > 10) ? parseInt(gaDate.substring(14, 16), 10) : endDate ? 59 : 0
	,(gaDate.length > 10) ? parseInt(gaDate.substring(17, 19), 10) : endDate ? 59 : 0
 );
 // var year = parseInt(gaDate.substring(0, 4), 10);
 // var month = parseInt(gaDate.substring(5, 7), 10) - 1;
 // var day = parseInt(gaDate.substring(8, 10), 10);
 // date.setFullYear(year);
 // date.setMonth(month, day);
  return date;
}

