function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object 
document.write("<dt>");
   for (var i in obj) {
document.write("<dl>");
      // if a parent (2nd parameter) was passed in, then use that to 
      // build the message. Message includes i (the object's property name) 
      // then the object's property value on a new line 
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they 
      // click "CANCEL" then quit this level of recursion 
//       if (!confirm(msg)) { return; }
document.write(msg);

      // If this property (i) is an object, then recursively process the object 
      if (typeof obj[i] == "object") { 
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
document.write("</dl>");

   }
document.write("</dt>");

}


/* picasa */
var imgsizes = new Array(32, 48, 64, 72, 144, 160, 200, 288, 320, 400, 512, 576, 640);//, 720, 800, 912, 1024, 1152, 1280, 1440, 1600);
var imgs = 0;
var imgmax;

function output(data) {

// alert("descricao_"+data.feed.gphoto$name.$t);
	document.write("<p id='descricao_"+data.feed.gphoto$name.$t+"'></p>");

	while( imgsizes[imgs] < document.getElementById("descricao_"+data.feed.gphoto$name.$t).parentNode.offsetWidth) {
		imgmax = imgsizes[imgs];
		imgs++;
	}

	document.write("<div class='galeria ct' style='width:"+imgmax+"px;'><img src='"+data.feed.entry[0].content.src+"' id='imagem_"+data.feed.gphoto$name.$t+"' name='"+data.feed.gphoto$name.$t+"'>");
	document.write("<div class='galeria' id='imagens_"+data.feed.gphoto$name.$t+"' onmouseover='moveImagens(this, event);' onmouseout='moveParar();'></div></div>");

// feed.gphoto$name.$t
// dumpProps(data);

	document.getElementById('descricao_'+data.feed.gphoto$name.$t).innerHTML = data.feed.subtitle.$t;
	for (var i = 0; i < data.feed.entry.length; i++) {
		var item = data.feed.entry[i];
		var title = item.title.$t;// the filename
		var imgsrc = item.content.src;//useful for downloading image
		
		var height = parseInt(item.gphoto$height.$t);
		var width = parseInt(item.gphoto$width.$t);

		if (width > imgmax) {
			height = height * imgmax / width;
			width = imgmax;
		}
// 

// alert(width);
		var orientation =(height>width ? "vert" : "hor");
		var description = item.media$group.media$description.$t;// Picasa Web photo caption

		var liNode = document.createElement('span');

		var aNode = document.createElement('a');
// 		aNode.setAttribute('href', imgsrc + '?imgmax=800#'+i);
		aNode.setAttribute('href', '#'+data.feed.entry[i].gphoto$id.$t);
		aNode.setAttribute('class', orientation);

		var imgNode = document.createElement('img');
		imgNode.setAttribute('src', imgsrc + '?imgmax=72');
		imgNode.setAttribute('onclick', "carregaImagem(this, document.getElementById('imagem_"+data.feed.gphoto$name.$t+"'), "+width+", "+height+"); location.href='#"+data.feed.entry[i].gphoto$id.$t+"'; return false;");
		imgNode.setAttribute('alt', title);
		imgNode.setAttribute('title', title);

		aNode.appendChild(imgNode);
		liNode.appendChild(aNode);
		document.getElementById('imagens_'+data.feed.gphoto$name.$t).appendChild(liNode);

		if ('#'+data.feed.entry[i].gphoto$id.$t == location.hash || (/*location.hash == '' && */i == 0) ) carregaImagem(imgNode, document.getElementById('imagem_'+data.feed.gphoto$name.$t), width, height);
	}
}

function carregaImagem(objfrom,objto,width,height) {
// alert(objfrom);
	objto.setAttribute('width', width);
	objto.setAttribute('height', height);
	objto.setAttribute('src', objfrom.getAttribute('src'));

	var img = document.createElement("img");
// 	objfrom.focus();

	img.onerror = function(e) {
		alert('Erro ao carregar a imagem. Tente novamente.');
	}
	img.onload = function(e) {
		objto.setAttribute('src', img.getAttribute('src'));
// 		objto.removeAttribute('width');
// 		objto.removeAttribute('height');
	}
	img.setAttribute('src', objfrom.getAttribute('src').replace('=72', '='+imgmax));
// alert(img.getAttribute('src'));
}

/* galeria mover */
var velocidade = 2.3;
var mouseX;
var mouseY;
var lActive;
var rActive;
var divW;
var divH;
var movendo;

function moveImagens(obj, event) {
	function moveEsquerda() {
// 		alert("esquerda "+obj.id);
		vel = Math.exp( (lActive - mouseX) * velocidade / lActive );
		obj.scrollLeft = obj.scrollLeft - vel;
	}

	function moveDireita() {
// 		alert("direita "+obj.id);
		vel = Math.exp( (mouseX - rActive) * velocidade / (divW - rActive) );
		obj.scrollLeft = obj.scrollLeft + vel;
// 		obj.scrollLeft = obj.scrollWidth;
	}

/*	mouseX = window.event.x-obj.offsetLeft;
	mouseY = window.event.y-obj.offsetTop;*/
// 	mouseX = obj.offsetLeft;
// 	mouseY = obj.offsetTop;
/*	mouseX = (window.event) ? window.offsetLeft : event.layerX;
	mouseY = (window.event) ? window.offsetTop : event.layerY;*/
// 	mouseX = (window.Event) ? evt.layerX : evt.x;
// 	mouseY = (window.Event) ? evt.layerY : evt.y;

	mouseX = (window.event) ? window.event.x - obj.offsetLeft : event.pageX - obj.offsetLeft;
	mouseY = (window.event) ? window.event.y - obj.offsetTop : event.pageY - obj.offsetTop;

// 		alert("X: " + mouseX + "\nY: " + mouseY + "\nW: " + obj.offsetWidth + "\nH: " + obj.offsetHeight + "\nl: " + lActive + "\nr: " + rActive);

	divW = obj.offsetWidth;
	divH = obj.offsetHeight;

	lActive = divW * 0.2;
	rActive = divW * 0.8;

	if (mouseX < lActive) {
// 		alert("X: " + mouseX + "\nY: " + mouseY + "\nW: " + obj.offsetWidth + "\nH: " + obj.offsetHeight + "\nl: " + lActive + "\nr: " + rActive);
		if (!movendo) {
			movendo = setInterval(moveEsquerda,20);
		}
	} else if (mouseX > rActive) {
// 		alert("X: " + mouseX + "\nY: " + mouseY + "\nW: " + obj.offsetWidth + "\nH: " + obj.offsetHeight + "\nl: " + lActive + "\nr: " + rActive);
		if (!movendo) {
			movendo = setInterval(moveDireita,20);
		}
	} else {
		moveParar();
	}
}


function moveParar() {
	clearInterval(movendo);
	movendo = '';
}


