var entryArray = [];

var ID_COL 					= 0;
var DATE_COL 				= 1;
var PARAS_LEN_COL 		= 2;
var PARAS_COL 				= 3;
var PICTURES_LEN_COL 	= 4;
var PICTURES_COL 			= 5;

var ENTRIES_COL_NUM 		= 6;


// classes

function Entry(id,date,title)
{
	this.id = id;
	this.date = date;
	this.title = title;
	this.paragraphs = new Array();
	this.pictures = new Array();
}

Entry.prototype.addParagraph = function(text) { this.paragraphs[this.paragraphs.length] = text;}
Entry.prototype.addPicture = function(picture) { this.pictures[this.pictures.length] = picture;}


function Picture(title,petLocation,justify,filename,finalID)
{
	this.title = title;
	this.petLocation = petLocation;
	this.justify = justify;
	this.cats = new Array();
	this.filename = filename;
	this.finalID = finalID;
}

Picture.prototype.addCat = function(name) { this.cats[this.cats.length] = name;}

// page functions

function load()
{
	/*
	var newlink = document.createElement("a");
	newlink.title = 'new link';
	newlink.className = 'subnavlink';
	newlink.href= 'javascript:showSignature(xyz)';
	newlink.appendChild(document.createTextNode('Test Link'));

	var navLinks = document.getElementById("navlinks");
	navLinks.appendChild(newlink);								// add the div to the thumb bar
	*/
	loadAllEntries();

	var id = readCookie("JOURNAL_ENTRY_COOKIE");

	if(id != null)
	{
		//alert("id = " + id);
		createNavBar(id);
		showEntry(id);

	}
	else
	{
		createCookie("JOURNAL_ENTRY_COOKIE",0,30);
		createNavBar(0);
		showEntry(0);

	}
	//window.location = "http://www.yahoo.com";	

}

function handleJournalLink(entryIndex)
{
	createCookie("JOURNAL_ENTRY_COOKIE",entryIndex,30);
	createNavBar(entryIndex);
	showEntry(entryIndex);
}

function createNavBar(currentEntry)
{

	/*
	<a class="navlink"      href="index.html">Home</a>
	<a class="navlink"      href="journal.html">Journal</a>
	<a class="navlink"      href="media.html">Media</a>
	<a class="navlink"      href="projects.html">Projects</a>
	<a class="navlink"      href="essay.html">Essay Sketch</a>
	<a class="navlink"      href="final.html">Final Project</a>                        
	*/

	var navLinks = document.getElementById("navlinks");
	navLinks.innerHTML = "";
	
	// Home
	var newlink = document.createElement("a");
	newlink.className = 'navlink';
	newlink.href= 'index.html';
	newlink.appendChild(document.createTextNode('Home'));
	navLinks.appendChild(newlink);								// add the div to the thumb bar
	
	// journal
	var newlink = document.createElement("a");
	newlink.className = 'navlink';
	newlink.href= 'journal.html';
	newlink.appendChild(document.createTextNode('Journal'));
	navLinks.appendChild(newlink);								// add the div to the thumb bar


	for (var i = 0; i<entryArray.length;i++)
	{
		if(i == currentEntry)
		{
			var newlink = document.createElement("a");
			newlink.className = 'subnavlinkSelected';
			newlink.appendChild(document.createTextNode(entryArray[i].date));
			navLinks.appendChild(newlink);								// add the div to the thumb bar

		}
		else
		{
			var newlink = document.createElement("a");
			newlink.className = 'subnavlink';
			newlink.href= 'javascript:handleJournalLink('+i+')';
			newlink.appendChild(document.createTextNode(entryArray[i].date));
			navLinks.appendChild(newlink);								// add the div to the thumb bar

		}
	}
  
	// media
	var newlink = document.createElement("a");
	newlink.className = 'navlink';
	newlink.href= 'media.html';
	newlink.appendChild(document.createTextNode('Media'));
	navLinks.appendChild(newlink);								// add the div to the thumb bar
	
	// projects
	var newlink = document.createElement("a");
	newlink.className = 'navlink';
	newlink.href= 'projects.html';
	newlink.appendChild(document.createTextNode('Projects'));
	navLinks.appendChild(newlink);								// add the div to the thumb bar
	
	// essay
	var newlink = document.createElement("a");
	newlink.className = 'navlink';
	newlink.href= 'essay.html';
	newlink.appendChild(document.createTextNode('Essay Sketch'));
	navLinks.appendChild(newlink);								// add the div to the thumb bar
	
	// final
	var newlink = document.createElement("a");
	newlink.className = 'navlink';
	newlink.href= 'final.html';
	newlink.appendChild(document.createTextNode('Final Project'));	
	navLinks.appendChild(newlink);								// add the div to the thumb bar

}

function showEntry(entryIndex)
{
	var journalHeader = document.getElementById("journal_header");
	journalHeader.innerHTML = entryArray[entryIndex].title;

	var journalRow = document.getElementById("journal_row");
	journalRow.innerHTML = "";
	var theHTML = "";
	//journalRow.innerHTML = entryArray[entryIndex].date;

	// do paragraphs

	for (var i = 0; i<entryArray[entryIndex].paragraphs.length;i++)
	{

		var journalRowDiv = document.createElement("div");
		journalRowDiv.className = 'journal_paragraph';
		
		//journalRowDiv.appendChild(document.createTextNode(entryArray[entryIndex].paragraphs[i]));
		//journalRow.appendChild(journalRowDiv);								// add the div to the thumb bar



		//theHTML += entryArray[entryIndex].paragraphs[i];

		if(entryArray[entryIndex].pictures.length > i)
		{

			//alert(entryArray[entryIndex].pictures[i].justify);

			var journalRowSpan = document.createElement("span");
			if(entryArray[entryIndex].pictures[i].justify == "Left")
			{
				journalRowSpan.className = 'image_left';
			}
			else
			{
				journalRowSpan.className = 'image_right';
			}
			
			var journalRowSpanDiv = document.createElement("div");
			journalRowSpanDiv.className = 'image_thumb';
			journalRowSpanDiv.innerHTML = "<A HREF='javascript:navigateToFinal("+entryArray[entryIndex].pictures[i].finalID+")'><IMG class = 'cats' SRC = '"+entryArray[entryIndex].pictures[i].filename+"' BORDER = 0></A>";			
			journalRowSpan.appendChild(journalRowSpanDiv);								// add the div to the thumb bar

			journalRowSpanDiv = document.createElement("div");
			journalRowSpanDiv.className = 'image_thumb_title';
			journalRowSpanDiv.innerHTML = "<CENTER>"+entryArray[entryIndex].pictures[i].title+"</CENTER>";			
			journalRowSpan.appendChild(journalRowSpanDiv);								// add the div to the thumb bar



			journalRow.appendChild(journalRowSpan);								// add the div to the thumb bar
			
		}

		journalRowDiv.appendChild(document.createTextNode(entryArray[entryIndex].paragraphs[i]));
		journalRow.appendChild(journalRowDiv);								// add the div to the thumb bar

	}
	var extraBR = document.createElement("br");
	journalRow.appendChild(extraBR);								// add the div to the thumb bar
	journalRow.appendChild(extraBR);								// add the div to the thumb bar
	journalRow.appendChild(extraBR);								// add the div to the thumb bar

	// do pictures

}

function navigateToFinal(imageIndex)
{
	createCookie("SHOW_HAPPY_COOKIE",0,30);
	createCookie("SHOW_OSCAR_COOKIE",0,30);
	createCookie("SHOW_ANGEL_COOKIE",0,30);
	createCookie("SHOW_SUGAR_COOKIE",0,30);
	createCookie("SHOW_KATCHINA_COOKIE",0,30);
	createCookie("SHOW_JAMBI_COOKIE",0,30);
	createCookie("SHOW_PEPPY_COOKIE",0,30);

	// pet filter cookies

	createCookie("SHOW_BATHROOM_COOKIE",0,30);
	createCookie("SHOW_STAIRS_COOKIE",0,30);
	createCookie("SHOW_HALLWAY_COOKIE",0,30);
	createCookie("SHOW_KITCHEN_COOKIE",0,30);
	createCookie("SHOW_OFFICE_COOKIE",0,30);
	createCookie("SHOW_LEDGE_COOKIE",0,30);
	createCookie("SHOW_OUTSIDE_COOKIE",0,30);

	createCookie("IMAGE_INDEX_COOKIE",imageIndex,30);
	createCookie("THUMB_INDEX_COOKIE",imageIndex,30);

	window.location = "final/index.html";	
}



function showEntry_old(entryIndex)
{
	var journalHeader = document.getElementById("journal_header");
	journalHeader.innerHTML = entryArray[entryIndex].title;

	var journalRow = document.getElementById("journal_row");
	var theHTML = "";
	//journalRow.innerHTML = entryArray[entryIndex].date;

	// do paragraphs

	for (var i = 0; i<entryArray[entryIndex].paragraphs.length;i++)
	{
		theHTML += entryArray[entryIndex].paragraphs[i];

		if(entryArray[entryIndex].pictures.length >= i)
		{
			var htmlText = "<SPAN CLASS = 'image_left'>";
			htmlText +=		"<DIV CLASS = 'image_thumb'>";
			htmlText +=		"<A HREF='photos/inkadu/inkadu_walking.jpg' target='_blank'><IMG class = 'cats' SRC = 'photos/inkadu/inkadu_walking_t.jpg' BORDER = 0></A>";
			htmlText +=		"</DIV>";
			htmlText +=		"<DIV CLASS 'image_thumb_title'>";
			htmlText +=		"<CENTER>Inkadu</CENTER>";
			htmlText +=		"<DIV CLASS = 'image_thumb'>";
			htmlText +=		"</DIV>";
			htmlText +=		"</SPAN>";
			/*
			theHTML += "
				<SPAN CLASS = 'image_left'>
				<DIV CLASS = 'image_thumb'>
				<A HREF='photos/inkadu/inkadu_walking.jpg' target='_blank'><IMG class = 'cats' SRC = 'photos/inkadu/inkadu_walking_t.jpg' BORDER = 0></A> 
				</DIV>
				<DIV CLASS 'image_thumb_title'>
				<CENTER>
				Inkadu
				</CENTER>
				</DIV>
				</SPAN>
				";
				*/
			theHTML += htmlText;
		}
	}

	// do pictures

/*
	<SPAN CLASS = "image_left">
	<DIV CLASS = "image_thumb">
	<A HREF='photos/inkadu/inkadu_walking.jpg' target='_blank'><IMG class = "cats" SRC = "photos/inkadu/inkadu_walking_t.jpg" BORDER = 0></A> 
	</DIV>
	<DIV CLASS "image_thumb_title">
	<CENTER>
	Inkadu
	</CENTER>
	</DIV>
	</SPAN>
*/

	journalRow.innerHTML = theHTML;
}

function loadAllEntries()
{
	var xmlDoc = loadXML("journal.xml");
	var entries = xmlDoc.getElementsByTagName("entry");

	var id;
	var date;
	var title;
	var para;
	var picture;
	var title;
	var petLocation;
	var justify;
	var filename;

	// load the entry array
	for (var i = 0; i<entries.length;i++)
	{
		// get the entry info

		id = entries[i].attributes[0].nodeValue;
		date = entries[i].attributes[1].nodeValue;
		title = entries[i].attributes[2].nodeValue;

		// assign entry values to array

		entryArray[i] = new Entry(id,date,title);
				
		// get the paragraphs for the entry

		var paras = entries[i].getElementsByTagName("paragraph");

		for (var j = 0; j<paras.length;j++)
		{
			para = paras[j].textContent; 
			entryArray[i].addParagraph(para);
		}

		// get the pictures for the entries

		var pictures = entries[i].getElementsByTagName("picture");

		for (var k = 0; k<pictures.length;k++)
		{  		
			title = pictures[k].childNodes[1].textContent; 
			petLocation = pictures[k].childNodes[5].textContent; 
			justify = pictures[k].childNodes[7].textContent; 
			filename = pictures[k].attributes[0].nodeValue;
			var finalID = pictures[k].attributes[1].nodeValue;

			var myPicture = new Picture(title,petLocation,justify,filename,finalID);

			// get the cats
			
			var catNum = pictures[k].childNodes[3].attributes.length;

			for(var curCat = 0; curCat < catNum ; curCat++)
			{
				var catName = pictures[k].childNodes[3].attributes[curCat].nodeName;
				myPicture.addCat(catName);
			}
			entryArray[i].addPicture(myPicture);
		}
	}


	//alert("done");
}


// clearElement(id)
//
// parameters:
//		name:		the name of the element to clear
//
// clears the html of the element indicated by name

function clearElement(id)
{
	var element = document.getElementById(id);					// get the element to clear
	element.innerHTML = "";												// set the html of this element to blank
}


//	loadXML(xmlURL)

function loadXML(xmlURL)
{
   var xmlDoc;
   
   if (document.implementation && document.implementation.createDocument)
   {
      xmlDoc = document.implementation.createDocument("", "", null);
   }
   else
   {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
   }
   xmlDoc.async = false;
   xmlDoc.load(xmlURL);

   return xmlDoc;
} 

