// JavaScript Document

var newTitle = "";
var newId = "";
var mapZoom = 11;

function doLoadTodaysPhotos()
{
	load_todaysphotos("/photos.xml", "photo-list");
}

function load_todaysphotos(url, divName)
{
	var httpreq = getHTTPObject();
	
    httpreq.open("GET", url, true);

    httpreq.onreadystatechange = function () {
			if (httpreq.readyState == 4)
			{
				var div = document.getElementById(divName);
		
				//alert(httpreq.responseXML);
				if (httpreq.responseXML)
				{
					last_xml_response = httpreq.responseXML;
					format_todaysphotos(last_xml_response, div);
				}
				else 
				{		
					div.innerHTML = "Data not available."; 
				}
			}
    }
    httpreq.send(null);		
}

function format_todaysphotos(response, div)
{
    var doc = response.documentElement;

/*
	var para = document.createElement("p");
	para.appendChild(document.createTextNode("Today's Photos"));
	div.appendChild(para);
*/
	// get list of entries
	var list = doc.getElementsByTagName('item');
	
	for (i = 0; i < 3; i++)
	{
		// get entry item from array
		var entry = list[i];
		// need the title, description, date taken, link, image src, height, width
		// get link from guid (link has url for todays_photos.html)
		var linkUrl = entry.getElementsByTagName('guid')[0];
		///para = document.createElement("p");
		///para.appendChild(document.createTextNode("Link: " + linkUrl.firstChild.data));
		//div.appendChild(para);
		
		// get date taken from pubDate
		var pubDate = entry.getElementsByTagName('pubDate')[0];
		//para = document.createElement("p");
		//para.appendChild(document.createTextNode("pubDate: " + pubDate.firstChild.data));
		//div.appendChild(para);

		// get media group for the rest
		var tagPrefix = "";
		var mediaGroup = entry.getElementsByTagName('group')[0];	
		if (mediaGroup == null)
		{
			// try to get with full name
			mediaGroup = entry.getElementsByTagName('media:group')[0];
			// set tag name prefix
			tagPrefix = "media:";
		}
		
		// get title
		var title = mediaGroup.getElementsByTagName(tagPrefix + 'title')[0];		
		//para = document.createElement("p");
		//para.appendChild(document.createTextNode("Title: " + title.firstChild.data));
		//div.appendChild(para);

		// get description
		var desc = mediaGroup.getElementsByTagName(tagPrefix + 'description')[0];		
		//para = document.createElement("p");
		//para.appendChild(document.createTextNode("Description: " + desc.firstChild.data));
		//div.appendChild(para);
		
		// get thumbnail, get first one for now
		var thumbnail = mediaGroup.getElementsByTagName(tagPrefix + 'thumbnail')[0];
		// get attribute
		var imgUrl = thumbnail.getAttribute('url');
		var width = thumbnail.getAttribute('width');
		var height = thumbnail.getAttribute('height');
		
		// testing
		/*
		para = document.createElement("p");
		para.appendChild(document.createTextNode("Image: " + imgUrl));
		div.appendChild(para);
		para = document.createElement("p");
		para.appendChild(document.createTextNode("Size: " + width + ", " + height));
		div.appendChild(para);
		*/
		
		// create html
		
		// start with wrapper
		var divWrapper = document.createElement("div");
		setClass(divWrapper, "photo-wrapper");
		// add pic frame
		var divFrame = document.createElement("div");
		setClass(divFrame, "pic-frame");
		// add link and image
		var lnk = document.createElement("a");
		lnk.setAttribute("href", linkUrl.firstChild.data);
		lnk.setAttribute("target", "_blank");
		var img = document.createElement("img");
		img.setAttribute("src", imgUrl);
		img.setAttribute("height", height);
		img.setAttribute("width", width);
		// add img to link
		lnk.appendChild(img);
		// add link to frame
		divFrame.appendChild(lnk);
		// add to wrapper
		divWrapper.appendChild(divFrame);
		
		// add details
		var dl = document.createElement("dl");
		// add title and description first
			var dt = document.createElement("dt");
			setClass(dt, "title");
			dt.appendChild(document.createTextNode(title.firstChild.data));
			// add to list
			dl.appendChild(dt);
			// add description
			var dd = document.createElement("dd");
			setClass(dd, "description");
			dd.appendChild(document.createTextNode(desc.firstChild.data));
			// add to list
			dl.appendChild(dd);
		// add date taken next
			var dt = document.createElement("dt");
			dt.appendChild(document.createTextNode("Taken: "));
			// add to list
			dl.appendChild(dt);
			// add date
			var dd = document.createElement("dd");
			dd.appendChild(document.createTextNode(pubDate.firstChild.data));
			// add to list
			dl.appendChild(dd);
		
		// add list to wrapper
		divWrapper.appendChild(dl);
		
		// add clear to wrapper
		divWrapper.innerHTML += "<div style='clear:both;'></div>";
		// add to list
		div.appendChild(divWrapper);
		
	}	
}

function showPhoto(id, zoom)
{
	if (zoom) mapZoom = zoom;
	findPhoto(id);
	return;
}

function findPhoto(id)
{	
	// check if there is already a response for this one
	/*if (responses[i] != null)
	{
		// found response, show this one
		showPhotos(responses[i]);
	}
	else
	{*/
		//var pt = map_points[i];	
		var url = "m=flickr.photos.getInfo&id=" + id;

		// no response yet, get as new
		var httpreq = getHTTPObject();
		//alert("/fp.php?" + url);
		//return;
		httpreq.open("GET", "/fp.php?" + url, true);
	
		httpreq.onreadystatechange = function () {
				if (httpreq.readyState == 4)
				{
					if (httpreq.responseText)
					{
						last_text_response = httpreq.responseText;
						var func = new Function("return " + last_text_response);
						var rsp  = func();
						// save response for later calls 
						//responses[i] = rsp;
						displayPhoto(rsp);
					}
					else 
					{		
						//alert("Data not available."); 
					}
				}
		}
	
		httpreq.send(null);		
	//}
}


function displayPhoto(rsp)
{
	if (rsp == null)
	{
		// not loaded yet
		return;
	}
	
	// clear out existing photo list
	var div = document.getElementById("photo-preview");

	//alert(rsp.photo.id);
	var photo = rsp.photo;
//<photo id="2956369460" owner="7230385@N04" secret="9c2945cd8f" server="3002" farm="4" title="At the Hawks Game... early" ispublic="1" isfriend="0" isfamily="0"/>
		
	var	url = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_m.jpg";
	var title = photo.title._content;
	newTitle = title;
	newId = photo.id;
	
	// preload image
	var img = document.getElementById("previewId");
	
	var oldonload = img.onload;
	if (oldonload == null) addLoadEvent(img, setTitle);	
	//alert(img.onload);

	img.setAttribute("src", url);


	var lnk = document.getElementById("previewLink");
	lnk.setAttribute("href", "/photo/" + photo.id + ".html");
	lnk.setAttribute("title", "View Full Photo");
	
	
	// look for map from location node
	var mapUrl = "";
	var placeName = "";
	var showPlaceLink = false;
	
	if (photo.location != null)
	{
		// get location values
		var loc = photo.location;
		var lat = loc.latitude;
		var lng = loc.longitude;
		var placeId = loc.woeid;
		
		if (lat != "" && lng != "")
			mapUrl = "http://maps.google.com/staticmap?center=" + lat + "," + lng + "&markers=" + lat + "," + lng + ",smallblue&zoom=" + mapZoom + "&size=260x200&key=" + map_key;
		
		if (loc.locality._content != null)
		{
			placeName = loc.locality._content;
			
			if (placeId != null) 
			{
				//placeName = "<a href='/place/" + placeId + "/page1.html' title='View Photos from " + placeName + "'>" + placeName + "</a>";			
				showPlaceLink = true;
			}
		}
	}
	
	// clear map and place values if needed
	if (mapUrl == "") mapUrl = "http://maps.google.com/staticmap?center=20.789498,-156.340942&zoom=9&size=260x200&key=" + map_key;
	if (placeName == "") placeName = "Location not specified";
	
	var place = document.getElementById("placeId");
	place.innerHTML = "";
	if (showPlaceLink == true)
	{
		place.appendChild(document.createTextNode("Taken in "));
		var plnk = document.createElement("a");
		plnk.setAttribute("href", "/place/" + placeId + "/page1.html");
		plnk.setAttribute("title", "View Photos from " + placeName);
		plnk.appendChild(document.createTextNode(placeName));
		place.appendChild(plnk);
	}
	else
	{
		place.appendChild(document.createTextNode("Taken in " + placeName));
	}
	
	var mapImg = document.getElementById("mapId");
	if (mapUrl != "") mapImg.setAttribute("src", mapUrl);
	//alert("location: " + photo.location);
/*
$location = $photo->getElementsByPath('location', 1);
						if ($location)
						{
							$lat = $location->getAttribute("latitude");
							$lng = $location->getAttribute("longitude");
							$mapUrl = "http://maps.google.com/staticmap?center=$lat,$lng&markers=$lat,$lng,smallblue&zoom=13&size=260x200&key=$map_key";
							$locality = $location->getElementsByPath('locality', 1);
							$locality = $locality->getText();
						}
*/
	/*var p = document.getElementById("previewTitle");
	p.innerHTML = "";
	p.appendChild(document.createTextNode(title));
	*/
	return;
}

function setTitle()
{
	//alert("Set Title");
	var p = document.getElementById("previewTitle");
	p.innerHTML = "";
	p.appendChild(document.createTextNode(newTitle));	
	
	// find photo width from attributes
	var img = document.getElementById("previewId");
	var width = img.width;
	var height = img.height;
	
	if (width)
	{
		// add 18 for border
		width += 18;
		height += 18;
		// adjust width on preview link to 
		var lnk = document.getElementById("previewLink");
		//lnk.setAttribute("style", "width: " + width + "px");
		
		setWidthHeight(lnk, width + "px", height + "px");
	}
}


function addLoadEvent(obj, func) {
  var oldonload = obj.onload;
  if (typeof obj.onload != 'function') {
    obj.onload = func;
  } else {
    obj.onload = function() {
      oldonload();
      func();
    }
  }
}
