/*
Functions to create and navigate a pictures album
*/

//arrays to keep track of the photo albums
var album_photo = new Array();
var album_text = new Array();
var album_orienta = new Array();
var album_counter = 0;

/*makes a pictures album given the list of pictures and the div to contain it*/
function make_album(pictures_list, father_div)
{	
	/*create the structure*/
	var album_left = add_elem({elem_type:"div", elem_dad:father_div, elem_class:"album_left"});
	var album_center = add_elem({elem_type:"div", elem_dad:father_div, elem_class:"album_center"});
	var album_bottom = add_elem({elem_type:"div", elem_dad:father_div, elem_class:"album_bottom"});
	
	/*make sure the father_div has enough space for the album*/
	father_div.style.padding="0%";
	father_div.style.width="100%";
	father_div.style.height="96%";

	/*add the pictures info*/
	for(var i=0; i<pictures_list.length; i++)
	{
		var foto_orienta = pictures_list.item(i).attributes.getNamedItem("orienta").nodeValue;
		var foto_src_sm = pictures_list.item(i).attributes.getNamedItem("src_sm").nodeValue;
		var foto_src_bg = pictures_list.item(i).attributes.getNamedItem("src_bg").nodeValue;
		var foto_text = pictures_list.item(i).firstChild.nodeValue;
		
		var album_id = father_div.attributes.getNamedItem("id").nodeValue;

		//put the info into the array
		album_photo[album_counter] = foto_src_bg;
		album_text[album_counter] = foto_text;
		album_orienta[album_counter] = foto_orienta;
	
		var foto_onclick = "change_foto('"+album_id+"',"+album_counter+")";
		add_elem({elem_type:"img", elem_dad:album_left, elem_src:foto_src_sm, elem_class:"album_"+foto_orienta+"_small", elem_onclick: foto_onclick});

		//the first one is shown	
		if(i==0)
		{
			var foto_center = add_elem({elem_type:"img", elem_dad:album_center, elem_src:foto_src_bg, elem_class:"album_"+foto_orienta+"_big"});
			var text_bottom = add_elem({elem_type:"p", elem_dad:album_bottom, elem_text:foto_text});
		}

		album_counter++;
	}
}

/*changes vision to the picture chosen in the chosen picture gallery*/
function change_foto(album_id, foto_order)
{
	//find the desired lists
	var pictures_list;
	var comments_list;
	var album_elems = document.getElementsByTagName("div");
	for(var i=0; i<album_elems.length; i++)
	{
		var dad_id = null; 
		if(album_elems.item(i).parentNode.attributes.getNamedItem("id"))
		{
			dad_id = album_elems.item(i).parentNode.attributes.getNamedItem("id").nodeValue;
			if(dad_id == album_id)
			{	
				var div_class = album_elems.item(i).attributes.getNamedItem("class").nodeValue;
				if(div_class == "album_center")
				{
					var desired_img = album_elems.item(i).getElementsByTagName("img").item(0);
					desired_img.attributes.getNamedItem("src").nodeValue = album_photo[foto_order];
					desired_img.attributes.getNamedItem("class").nodeValue = "album_"+album_orienta[foto_order]+"_big";
					//pictures_list = album_elems.item(i).getElementsByTagName("img");
				}
				if(div_class == "album_bottom")
				{
					album_elems.item(i).getElementsByTagName("p").item(0).firstChild.nodeValue = album_text[foto_order];
					//comments_list = album_elems.item(i).getElementsByTagName("p");
				}
			}
		}
	}
}
