//  Autor: Radu Capan
//  Copyright: www.cnet.ro
// Cod demonstrativ fara angajamente


dimth = 160; //32,48,64,160 sunt patrate, cu crop; 144,200,288,400,512 sunt fara crop - dimensiune thumbnail
dimthm = 160; //32,48,64,160 sunt patrate, cu crop; 144,200,288,400,512 sunt fara crop - dimensiune thumbnail mic
marimeim= 576 //poate fi 400, 512, 576, 640, 720, 800 - marimea imaginii mari
inlinie=3; //cate imagini pe o linie ex: 3
catelinii=4; //cate linii de imagini ex: 3
catealbume=0; //cate albume se afiseaza ex: 3
spatiere=1; //spatiu pe langa imagini
culoareb="#ffffff"; //culoare bordura imagini

lunile = ["","ianuarie","februarie","martie","aprilie","mai","iunie","iulie","august","septembrie","octombrie","noiembrie","decembrie"];
offset=0;
offseta=0;
poza=0;
dimsalt = catelinii*inlinie;
faraAlbume=0;

function square(dim){
	return (dim ==32) || (dim ==48) || (dim ==64) || (dim ==160);
}

function extragePoza(root) {
	var feed = root.feed;
	var text = "";
	text += "<h3>"+feed.subtitle.$t+"</h3>"
	if(feed.gphoto$location.$t!="")
		text += ", "+feed.gphoto$location.$t;

	//afisez sistemul de navigatie
	text += "<p class=center>";
	pagini=0;
	for(i = 0; i<feed.entry.length;i++)
		if(i*dimsalt>poza){
			pagini=i-1;
			break;
		}
	if(poza)
		text += "<a href='#' onClick='javascript:listaPoza(\""+feed.gphoto$user.$t+"\",\""+feed.gphoto$name.$t+"\","+(poza-1)+");'><img class=zero src=img/back.png hspace=5 vspace=1 border=0></a> ";
	else
		text += "<img class=zero src=img/back_.png hspace=5 vspace=1 border=0> ";
	text += "<a href='#' onClick='javascript:listaPoze(\""+feed.gphoto$user.$t+"\",\""+feed.gphoto$name.$t+"\","+pagini+");'><img class=zero src=img/up.png hspace=5 vspace=1 border=0></a> ";
	if(feed.entry.length > (poza+1))
		text += "<a href='#' onClick='javascript:listaPoza(\""+feed.gphoto$user.$t+"\",\""+feed.gphoto$name.$t+"\","+(poza+1)+");'><img class=zero src=img/forward.png hspace=5 vspace=1 border=0></a>";
	else
		text += "<img class=zero src=img/forward_.png hspace=5 vspace=1 border=0>";
	//afisez poza
	text += "<table width=100% align=center cellpadding=3 cellspacing=0 border=0>";
	entry = feed.entry[poza];
	if(entry.summary.$t!="")
		text += "<tr><td><p align=center>"+entry.summary.$t+"</p></td></tr>";
	sursa = entry.media$group.media$thumbnail[0].url;
	sursa = sursa.replace("/s72/","/s"+marimeim+"/");
    text += "<tr><td valign=center align=center><a href=# onClick='javascript:listaZero(\""+feed.gphoto$user.$t+"\",\""+feed.gphoto$name.$t+"\","+poza+");'><img src="+sursa+" style='border:2px solid "+culoareb+"' hspace="+spatiere+" vspace="+spatiere+" alt='"+descr+"' title='"+descr+"'></a></td></tr>";
	text += "</table>";
	document.getElementById("spatiupicasa").innerHTML = text;
  }

function listaPoza(user,album,idpoza){
	document.getElementById("spatiupicasa").innerHTML = "<img src=img/animatie.gif align=absmiddle hspace=20>Se incarca...";
	poza= idpoza;
	var script = document.createElement('script');
	script.setAttribute('src','http://picasaweb.google.com/data/feed/api/user/'+user+'/album/'+album+'?kind=photo&alt=json-in-script&callback=extragePoza');
	script.setAttribute('id', 'jsonScript');
	script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(script);
}

function extragePoze(root) {
    var feed = root.feed; 
	var text = "";
	text += "<h3>"+feed.subtitle.$t+"</h3>"

	//afisez sistemul de navigatie
	if(feed.entry.length % dimsalt)
		pagini = Math.ceil(feed.entry.length / dimsalt)
	else
		pagini = Math.ceil(feed.entry.length / dimsalt)-1
	if(pagini>1){
  	text += "<p class=center>";
  	if(offset)
  		text += "<a href='#' onClick='javascript:listaPoze(\""+feed.gphoto$user.$t+"\",\""+feed.gphoto$name.$t+"\","+(offset-1)+");'><img class=zero src=img/back.png hspace=5 vspace=1 border=0></a> ";
  	else
  		text += "<img class=zero src=img/back_.png hspace=5 vspace=1 border=0> ";
	if(!faraAlbume)
		text += "<a href='#' onClick='javascript:listaAlbume(\""+feed.gphoto$user.$t+"\","+offseta+");'><img class=zero src=img/up.png hspace=5 vspace=1 border=0></a> ";
  	if(feed.entry.length > (offset+1)*dimsalt)
  		text += "<a href='#' onClick='javascript:listaPoze(\""+feed.gphoto$user.$t+"\",\""+feed.gphoto$name.$t+"\","+(offset+1)+");'><img class=zero src=img/forward.png hspace=5 vspace=1 border=0></a>";
  	else
  		text += "<img class=zero src=img/forward_.png hspace=5 vspace=1 border=0>";
	}
	else
		if(!faraAlbume)
			text += "<p align=center><a href='#' onClick='javascript:listaAlbume(\""+feed.gphoto$user.$t+"\","+offseta+");'><img src=img/up.png hspace=5 vspace=1 border=0></a></p>";
	//afisez pozele pe linii si coloane
	text += "<table width=100% align=center cellpadding=3 cellspacing=0 border=0>";
	for (var i = 0; (i < feed.entry.length) && (i<offset*dimsalt); ++i) {
	}
	incheiat=0;
    for (; i < feed.entry.length; ++i) {
		var entry = feed.entry[i];
		if(i%inlinie == 0) {
			text += "<tr>";
			incheiat = 0;
		}
		descr = entry.summary.$t;
		poza = entry.media$group.media$thumbnail[0].url;
		poza = poza.replace("/s72/","/s"+dimth+(square(dimth)?"-c":"")+"/");
		text += "<td height="+dimth+" width="+dimth+" valign=center align=center><p><a href=# onClick='javascript:listaPoza(\""+feed.gphoto$user.$t+"\",\""+feed.gphoto$name.$t+"\","+i+");'><img src="+poza+" style='border:2px solid "+culoareb+"' hspace="+spatiere+" vspace="+spatiere+" alt='"+descr+"' title='"+descr+"'></a></td>";
		if(i%inlinie == (inlinie-1)){
			text += "</tr>";
			incheiat = 1;
		}
		if(i > ((dimsalt-2)+offset*dimsalt))
			break;
    }
	if(!incheiat){
		i--;
		while((i%inlinie)!=(inlinie-1)){
			text += "<td><img src=img/gol.gif height="+dimth+" width="+dimth+" hspace="+spatiere+" vspace="+spatiere+" border=0></td>";
			i++
		}
		text += "</tr>";
	}
	text += "</table>";
	document.getElementById("spatiupicasa").innerHTML = text;
}

function listaPoze(user,album,salt){
	document.getElementById("spatiupicasa").innerHTML = "<img src=img/animatie.gif align=absmiddle hspace=20>Se incarca...";
	offset=salt;
	var script = document.createElement('script');
	script.setAttribute('src','http://picasaweb.google.com/data/feed/api/user/'+user+'/album/'+album+'?kind=photo&alt=json-in-script&callback=extragePoze');
	script.setAttribute('id', 'jsonScript');
	script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(script);
}

function extrageAlbume(root) {
    var feed = root.feed;
	var text = "";
	//afisez sistemul de navigatie
	if(feed.entry.length % catealbume)
		pagini = Math.ceil(feed.entry.length / catealbume)
	else
		pagini = Math.ceil(feed.entry.length / catealbume)-1
	if(pagini>1){
  	text += "<p align=center>";
  	if(offseta)
  		text += "<a href='#' onClick='javascript:listaAlbume(\""+feed.gphoto$user.$t+"\","+(offseta-1)+");'><img class=zero src=img/back.png hspace=5 vspace=1 border=0></a> ";
  	else
  		text += "<img class=zero src=img/back_.png hspace=5 vspace=1 border=0> ";
  	if(feed.entry.length > (offseta+1)*catealbume)
  		text += "<a href='#' onClick='javascript:listaAlbume(\""+feed.gphoto$user.$t+"\","+(offseta+1)+");'><img class=zero src=img/forward.png hspace=5 vspace=1 border=0></a>";
  	else
  		text += "<img class=zero src=img/forward_.png hspace=5 vspace=1 border=0>";
  	text += "<br>Pagini: ";
  	for(i=0;i < pagini; i++)
  		if(i==offseta)
  			text += ""+(i+1)+" ";
  		else
  			text += "<a href='#' onClick='javascript:listaAlbume(\""+feed.gphoto$user.$t+"\","+(i)+");'>"+(i+1)+"</a> ";
		text +="</p>";
	}
	//afisez pozele pe linii si coloane
	text += "<table width=100% align=center cellpadding=3 cellspacing=0 border=0>";
	for (var i = 0; (i < feed.entry.length) && (i<offseta*catealbume); ++i) {
		}
    for (; i < feed.entry.length; ++i) {
		var entry = feed.entry[i];
		text += "<tr><td valign=top><a href=# onClick='javascript:listaPoze(\""+entry.gphoto$user.$t+"\",\""+entry.gphoto$name.$t+"\",0);'><img src="+entry.media$group.media$thumbnail[0].url+" style='border:2px solid "+culoareb+"'></a></td><td valign=top width=90%>";
		text += "<strong>"+entry.title.$t+"</strong><br><br>"
		text += "Fotografii: "+entry.gphoto$numphotos.$t+"<br>";
		cand = entry.published.$t;
		cand = cand.substr(0,10);
		cand = (cand.substr(8,2)-0)+" " +lunile[cand.substr(5,2)-0]+" "+cand.substr(0,4);
		text += "Data: "+cand+"<br>";
		if(entry.gphoto$location.$t!="")
			text += "Locatie: "+entry.gphoto$location.$t+"<br>";
		if(entry.summary.$t!="")
			text += "Descriere: "+entry.summary.$t+"<br>";
		text += "</td></tr>";
		if(i > ((catealbume-2)+offseta*catealbume))
			break;
    }
	text +="</table>";
	document.getElementById("spatiupicasa").innerHTML = text;
  }

function listaAlbume(user,salt){
	document.getElementById("spatiupicasa").innerHTML = "<img src=img/animatie.gif align=absmiddle hspace=20>Se incarca...";
	offseta= salt;
	script = document.createElement('script');
	script.setAttribute('src','http://picasaweb.google.com/data/feed/api/user/'+user+'?kind=album&alt=json-in-script&callback=extrageAlbume');
  	script.setAttribute('id', 'jsonScript');
	script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(script);
}
