// noded.js 

function getXMLHttpRequest()
{
	var request = false;
		
	try
	{
		request = new XMLHttpRequest();
	}
	catch (err1)
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (err2)
		{
			try 
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err3)
			{
				request = false;
			}
		}
	}
	return request;
}

var xmlRequest = getXMLHttpRequest();
var photo = "img/palm.jpg";
var caption = "Palm (Evening)";

function loadNoded()
{
	var exif = document.getElementById("exif");	
	removeAllChildren(exif);
	var mainPhoto = document.getElementById("mainphoto");
	removeAllChildren(mainPhoto);
	loadPhoto(photo);
	getExif(photo);
    	
}

function loadPhoto(img)
{
	var mp = document.getElementById("mainphoto");
	var mi = document.createElement("img");
	mi.src = img;
	mi.alt = img;
	mp.appendChild(mi);
	
	
}

function removeAllChildren(node)
{
	if (node.hasChildNodes())
	{
		while (node.childNodes.length >= 1)
		 node.removeChild(node.firstChild);
	}
}


function getExif(img)
{
	var url = "get_photo.php?photo=" + img;
	if (xmlRequest)
	{
		xmlRequest.open("GET",url,true);
		xmlRequest.onreadystatechange = exifResponse;
		xmlRequest.send(null);
	}
	else
		Alert("no xmlrequest");
}
function exifResponse()
{
	if(xmlRequest.readyState == 4 )
	{
		if (xmlRequest.status == 200 )
		{
			var exifObj = xmlRequest.responseText;
			printExifResponse(exifObj);
		}
	}
	
}
function printExifResponse(exif)
{
	
 try
    {
	    var exifObj = exif.parseJSON();
		var ul = document.getElementById("exif");
		ul=createListItem(caption,ul);
	 	ul=createListItem("Camera:" + exifObj.Model,ul);
		ul=createListItem("ISO:" + exifObj.ISOSpeedRatings,ul);
		ul=createListItem("Shutter:" + exifObj.ExposureTime,ul,"");
		ul=createListItem("Aperture:" + exifObj.COMPUTED.ApertureFNumber,ul);
		ul=createListItem("Copyright:" + exifObj.Copyright,ul);
		ul=createListItem("Date:" + exifObj.DateTimeOriginal,ul);
	}
	catch (e)
	{
		alert(e.message);
	}
	
}

function createListItem(text,ul,cls)
{
	var li = document.createElement("li");
	var txt = document.createTextNode(text);
	li.appendChild(txt);
	ul.appendChild(li)
	return ul;
}
String.parseJSON  = (function (s) {

  var m = {
    '\b': '\\b',
    '\t': '\\t',
    '\n': '\\n',
    '\f': '\\f',
    '\r': '\\r',
    '"' : '\\"',
    '\\': '\\\\'
  };

  s.parseJSON = function (filter) {
     /*
           Reason: Why this function is useful?
     */
    // Parsing happens in three stages. In the first stage, we run the text against
    // a regular expression which looks for non-JSON characters. We are especially
    // concerned with '()' and 'new' because they can cause invocation, and '='
    // because it can cause mutation. But just to be safe, we will reject all
    // unexpected characters.

    try {
	    if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)) {

          // In the second stage we use the eval function to compile the text into a
          // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
          // in JavaScript: it can begin a block or an object literal. We wrap the text
          // in parens to eliminate the ambiguity.

          var j = eval('(' + this + ')');

          // In the optional third stage, we recursively walk the new structure, passing
          // each name/value pair to a filter function for possible transformation.

          if (typeof filter === 'function') {

            function walk(k, v) {
              if (v && typeof v === 'object') {
                for (var i in v) {
                  if (v.hasOwnProperty(i)) {
                    v[i] = walk(i, v[i]);
                  }
                }
              }
              return filter(k, v);
            }

            j = walk('', j);
          }
          return j;
       }
      } catch (e) {

  // Fall through if the regexp test fails.

      }
      throw new SyntaxError("parseJSON: filter failed " + e);
    };
  }
) (String.prototype);
// End public domain parseJSON block

function SyntaxError(e)
{
 alert(e);
}
