// Global vars
var photo_arr;             // Array of photos in the picture viewer

function load()
{
	showStats();
}

//
//	Ajax function for open async calls
//
function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null
		
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("This example doesn't work in Opera") 
		return 
	}

	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 	
		catch(e)
		{ 
		 	alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
}

function showDD(id)
{
   var x = document.getElementById(id); 

   if(x.style.display != "block")
   {
      // Clicked on dt has a dd that is not visible.  Make it visible
      x.style.display = "block";
   }
   else
   {
      // Clicked on dt has a dd that is already visible.  Hide it
      x.style.display = "none";
   }
}

function show_working(message)
{
	var x = document.getElementById("main");
	x.innerHTML = "<h1>"+message+"</h1>";
}

//
//	Display the stats page in the main area
//
function showStats()
{
	asProcess('stats.php','','Fetching baby stats page...');
}

//
//	Display my firsts in the main window
//
function showFirsts(args)
{
	asProcess('show_firsts.php', args, 'Fetching my firsts page...', handleFirstXML);
}

//
//	Display the pictures page in the main area
//
function showPictures()
{
	asProcess('pictures.php', '', 'Fetching baby pictures page...', handleASProcess);
}

function joinMailList()
{
	asProcess('join_list.php', '', 'Fetching email signup page...', handleASProcess);
}

function showPictures2()
{
	asProcess('http://www.georgia-martin.com/pictures-test.php', '', 'Fetching baby pictures page...', handlePhotoXML);
}

function editPictures()
{
	asProcess('http://www.georgia-martin.com/pictures-test.php', '', 'Fetching baby pictures page...', handlePhotoXMLEdit);
}

//
//	Generic asynchronous process call displays waiting message and then results in the main div
//
function asProcess(url, args, msg, func)
{
	var rand = parseInt(Math.random()*99999999);  // cache buster

	if(args)
		args = args + "&";
	args = args + "rand=" + rand;

	xmlHttp = GetXmlHttpObject(func);
   if(msg)
   	show_working(msg);
	xmlHttp.open("GET", url+"?"+args, true);
	xmlHttp.send(null);
}

function handleASProcess()
{
	if(xmlHttp.readyState == 4)
	{
		var x = document.getElementById("main");
		x.innerHTML = xmlHttp.responseText;
   }
	else
		show_working("Fetching page...");
}

function handleEditResponse()
{
	if(xmlHttp.readyState == 4)
	{
		var x = document.getElementById("status");
		x.innerHTML = xmlHttp.responseText;
   }
}

function handleFirstXML()
{
	if(xmlHttp.readyState == 4)
	{
      var the_html = "";
		var xmlDoc = xmlHttp.responseXML;
 //     document.write(xmlHttp.responseText);

		var r = xmlDoc.getElementsByTagName("firsts");
		var count = r[0].getAttribute("count");

      // Now process the firsts
      var firsts = xmlDoc.getElementsByTagName("first");

      // read each first
      for (var a = 0; a < firsts.length; a++)
      {
         the_html += "<dt onclick=\"showDD('first" + firsts[a].getAttribute("id") + "')\">" + firsts[a].getAttribute("date") + ", My First ... " + firsts[a].getAttribute("name") + "</dt>\n";
         the_html += "<dd id=\"first" + firsts[a].getAttribute("id") + "\">" + firsts[a].getAttribute("description") + "</dd>\n";
		}

		x = document.getElementById("main");
		x.innerHTML = "<dl>\n" + the_html + "</dl>";
   }
	else
		show_working("Fetching page...");
}

function getPreloadPics(xmlDoc)
{
   var r = xmlDoc.getElementsByTagName("photos");
	var count = r[0].getAttribute("count");

   // Now process the photos
   var photos = xmlDoc.getElementsByTagName("photo");
   var preload = "";                                  // Set the preload section to empty
   photo_arr = new Array();                           // Empty the photo_arr before we fill it again

   // read each photo
   for (var a = 0; a < photos.length; a++)
   {
      photo_arr[a] = new Array(photos[a].getAttribute("filename"), photos[a].getAttribute("taken"), photos[a].getAttribute("description"));
      preload += '<img src="http://www.georgia-martin.com/photos/' + photo_arr[a][0] + '" />\n';
	}

   return preload;
}

function handlePhotoXML()
{
	if(xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML;
      var preload = getPreloadPics(xmlDoc);

		var x = document.getElementById("preload");
	   x.innerHTML = preload;

		x = document.getElementById("main");
		x.innerHTML = '<p id="photo_taken"></p><br>';
		x.innerHTML += '<p id="photo_description"></p><br>';
		x.innerHTML += '<p id="photo_buttons"><input type="button" onclick="changePic(\'1\',\'0\')" value="&gt; next"></p><br>';
		x.innerHTML += '<div id="photo_photo"></div><br>';
      changePic(0,0);
   }
	else
		show_working("Fetching page...");
}

function handlePhotoXMLEdit()
{
	if(xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML;
      var preload = getPreloadPics(xmlDoc);

		var x = document.getElementById("preload");
	   x.innerHTML = preload;

		x = document.getElementById("main");
		x.innerHTML = '<label for="photo_taken">Photo Taken:</label><input type="text" id="photo_taken" value="" /><br>';
		x.innerHTML += '<label for="photo_description">Description:</label><textarea rows="3" cols="70" id="photo_description"></textarea><br>';
		x.innerHTML += '<p id="photo_buttons"><input type="button" onclick="changePic(\'1\',\'1\')" value="&gt; next"></p><br>';
		x.innerHTML += '<p id="photo_edit_button"><input type="button" onclick="doEditPic()" value="Save Changes"></p><br>';
		x.innerHTML += '<div id="photo_photo"></div><br>';
      changePic(0,1);
   }
	else
		show_working("Fetching page...");
}

function doEditPic()
{
   var taken = document.getElementById("photo_taken").value;
   var description = document.getElementById("photo_description").value;
   var name = document.getElementById("current_photo").name;

	asProcess('edit_photos.php', 'taken='+taken+'&name='+name+'&description='+description, '', handleEditResponse);
}

function changePic(photo_id, edit)
{
   // Adjust photo_id so that it is in the legal range
	if(photo_id < 0)
   	photo_id = photo_arr.length - 1;
	else if(photo_id >= photo_arr.length)
   	photo_id = 0;

   var photo = photo_arr[photo_id];

	var x = document.getElementById("photo_taken");
   // If we are in admin and editing photos, need to change the value and not innerhtml
   if(edit)
   	x.value = photo[1];
   else
   	x.innerHTML = "Photo taken on " + photo[1];

	x = document.getElementById("photo_description");
   // If we are in admin and editing photos, need to change the value and not innerhtml
   if(edit)
   	x.value = photo[2];
   else
   	x.innerHTML = photo[2];

	x = document.getElementById("photo_photo");
	x.innerHTML = '<img id="current_photo" name="' + photo[0] + '" src="http://www.georgia-martin.com/photos/' + photo[0] + ' />';

   var photo_id_next = photo_id + 1;
   var photo_id_prev = photo_id - 1;

	x = document.getElementById("photo_buttons");
	x.innerHTML = '<input type="button" onclick="changePic(' + photo_id_prev + ',' + edit + ')" value="&lt; prev"><input type="button" onclick="changePic(' + photo_id_next + ',' + edit + ')" value="next &gt;">';
}

//
//	Display the pictures page in the main area
//
function doVote()
{
	var id = document.getElementById("id").value;
	var action = document.getElementById("action").value;
	var myRandom = parseInt(Math.random()*99999999);  // cache buster
	var args = "action="+action+"&id="+id+"&rand=" + myRandom;

	show_working("Recording vote...");
	xmlHttp = GetXmlHttpObject(handleDoVote);

	xmlHttp.open("GET", "name.php?"+args, true);
	xmlHttp.send(null);

}

function handleDoVote()
{
	if(xmlHttp.readyState == 4)
	{
		var x = document.getElementById("main");
		x.innerHTML = xmlHttp.responseText;
   }
	else
		show_working("Recording vote...");
}

function showGiftReg(a)
{
	if(a == "target")
	{
		var x = document.getElementById("main");
		x.innerHTML = "<iframe id=\"if\" width=\"100%\" src =\"http://www.target.com/gp/registry/registry.html/ref=gp_re_registry/601-1405935-7738564?ie=UTF8&sort=taxonomy&type=babyreg&id=1H6WFRFHIU2O7&msgid=&jsebd=1&message=&pageNum=1&view=owner\"></iframe>";
	}
	else if(a == "bru")
	{
		var x = document.getElementById("main");
		x.innerHTML = "<iframe id=\"if\" width=\"100%\" src =\"http://www.toysrus.com/ControllerServlet?lastName=martin&firstName=tracie&state=CO&x=38&y=4&target=search&userType=giftGiver&searchForPerson=primReg&whereTo=viewRegistry\"></iframe>";
	}
	else
	{
		var x = document.getElementById("main");
		x.innerHTML = "<iframe id=\"if\" width=\"100%\" src =\"http://www.babycenter.com/fetal-development-images-"+a+"-weeks\"></iframe>";
	}

	window.onresize();
}
