var gPhotoCollection;
var gSelectedImageIndex;

function Menu(selectedIndex)
{
	// Attributes
	this.count = 0;
	this.selectedIndex = 0;
	this.menuItems = new Array();

	// Methods
	this.addMenuItem = function(menuItem)
	{
		menuItem.index = this.count;
		this.menuItems[this.count] = menuItem;
		this.count++;
	}
 }

 function MenuItem(caption, link, hasSubMenu)
 {
	// Attributes
	this.caption = caption;
	this.link = link;
	this.index = 0;
	this.hasSubMenu = hasSubMenu;
	this.subMenu = null;

	// Methods
	this.addSubMenu = function(subMenu)
	{
		this.subMenu = subMenu;
	}
 }

 function CreateHeaderSection()
 {
 	document.write('<div id="headerSection">\n');
 	document.write('<h2 id="missionStatement">Ordinary lives, Extraordinary moments</h2>\n');
 	document.write('<h1 id="siteTitle"><span class = "author">Shuva Rahim</span> | PHOTOGRAPHER</h1>');	    
 	document.write('</div>\n');
 }
 
 function CreateFooterSection()
 {
 	document.write('<div id="footerSection">\n');
	document.write('<span id="copyright">All Images &copy; Shuva Rahim</span><br />\n');
	document.write('<span id="rights">ALL RIGHTS RESERVED</span>\n');
  	document.write('</div>\n');
  	
  	
  	ProtectMainDisplayedImage();
 }
 
 function ProtectMainDisplayedImage()
 {
       var element;
       element = document.getElementById('imgDisplay')
       if(element)
       {
	       element.onmousedown = CheckForRightClick;	

	       document.oncontextmenu = function()
	       {      
		  return false;
	       }
	}	 
 }

 function CreateMenu(menu)
 {
	// Declaring variables
	var divMenuTag;
	var divMenuItemTag;
	var anchorTag;
	var menuItem;
	var textNode;
	var i;

	divMenuTag = document.getElementById("mainMenu");
	for(i=0; i<menu.count; i++)
	{
		menuItem = menu.menuItems[i];

		// Set up the anchor tag
		textNode = document.createTextNode(menuItem.caption);
		anchorTag = document.createElement("a");
		anchorTag.href = menuItem.link;
		anchorTag.appendChild(textNode);

		// Set up the menu item div
		divMenuItemTag = document.createElement("div");
		divMenuItemTag.appendChild(anchorTag);
		if(i == menu.selectedIndex)
		{
			divMenuItemTag.className = "menuItemSelected";
		}
		else
		{
			divMenuItemTag.className = "menuItem";
		}
		divMenuItemTag.hasSubMenu = menuItem.hasSubMenu;
		CreateSubMenu(divMenuItemTag, menuItem, (i == menu.selectedIndex));


		divMenuTag.appendChild(divMenuItemTag);
	}

 }

 function CreateSubMenu(divMenuItemTag, menuItem, bDisplaySubMenu)
 {
	// Declaring variables
	var divSubMenuTag;
	var divSubMenuItemTag;
	var anchorTag;
	var subMenuItem;
	var textNode;
	var i;

	if(menuItem.hasSubMenu)
	{
		divSubMenuTag = document.createElement("div");
		divSubMenuTag.className = "subMenu";
		for(i=0; i<menuItem.subMenu.count; i++)
		{
			subMenuItem = menuItem.subMenu.menuItems[i];

			// Set up the anchor tag
			textNode = document.createTextNode(subMenuItem.caption);
			anchorTag = document.createElement("a");
			anchorTag.href = subMenuItem.link;
			anchorTag.appendChild(textNode);
			// Set up the menu item div
			divSubMenuItemTag = document.createElement("div");
			divSubMenuItemTag.appendChild(anchorTag);
			if(i == menuItem.subMenu.selectedIndex)
			{
				divSubMenuItemTag.className = "subMenuItemSelected";				
			}
			else
			{
				divSubMenuItemTag.className = "subMenuItem";				
			}
			
			if(bDisplaySubMenu)
			{
				divSubMenuTag.style.display = "block";
			}
			else
			{
				divSubMenuTag.style.display = "none";
			}

			
			divSubMenuTag.appendChild(divSubMenuItemTag);
		}



		divMenuItemTag.appendChild(divSubMenuTag);
	}
 }
 
 
 function CreateMainMenu(selectedIndex, subMenuSelectedIndex)
 {
	// Declaring variables
	var menu, subMenu;
	var menuItem, subMenuItem;

	menu = new Menu();
	
	
	// Add the menu items
	{
		// Home
		menuItem = new MenuItem("Home", "index.html", false);
		menu.addMenuItem(menuItem);

		// Documentary
		menuItem = new MenuItem("Documentary", "documentary.html", true);

		subMenu = new Menu();
		subMenuItem = new MenuItem("Harvesting a Dream", "documentary.html", false);
		subMenu.addMenuItem(subMenuItem);
		menuItem.addSubMenu(subMenu);

		menu.addMenuItem(menuItem);

		// Photojournalism
		menuItem = new MenuItem("Photojournalism", "singles.html", false);
		menu.addMenuItem(menuItem);

		// International
		menuItem = new MenuItem("International", "international.html", true);
		
		subMenu = new Menu();
		subMenuItem = new MenuItem("Suriname", "international.html", false);
		subMenu.addMenuItem(subMenuItem);
		menuItem.addSubMenu(subMenu);
		
		menu.addMenuItem(menuItem);

		// About
		menuItem = new MenuItem("About", "about.html", false);
		menu.addMenuItem(menuItem);
	}
	
	// Set the selected index, and the submenu selected index (if applicable)
	menu.selectedIndex = selectedIndex;
	if(menu.menuItems[selectedIndex].hasSubMenu)
	{
	   menu.menuItems[selectedIndex].subMenu.selectedIndex = subMenuSelectedIndex;
	}
	
	// Create the menu
	CreateMenu(menu);
	
 }




function PhotoInfo(fileName, altText, caption)
{
	// Attributes
	this.fileName = fileName;
	this.altText = altText;
	this.caption = caption;
	this.index = 0;
}


function PhotoCollection(filePath)
{
	// Attributes
	this.filePath = filePath;
	this.count = 0;
	this.photos = new Array();

	// Methods
	this.addPhoto = function(p)
	{
		this.photos[this.count] = p;
		p.index = this.count;
		this.count++;
	}

 }

function CreateThumbnails(photoCollection, width, height, numThumbNailsPerRow)
{
	var i;

	for (i=0;i<photoCollection.count; i++)
	{

		// Declaring variables
		var photo;
		var divTag;
		var imageTag;
		var tableTag;
		var tableRowTag;
		var tableDataTag;
		var anchorTag;

		photo = photoCollection.photos[i];
		
		// Create the image tag
		imageTag = document.createElement("img");
		imageTag.src = photoCollection.filePath + "/thumbs/" + photo.fileName;
		imageTag.alt = photo.altText;
		imageTag.width = width;
		imageTag.height = height;
		
		// Create the anchor tag
		anchorTag = document.createElement("a");
		anchorTag.href = photoCollection.filePath + "/thumbs/" + photo.fileName;
		anchorTag.index = photo.index;
		anchorTag.style.height = imageTag.height + "px";
		anchorTag.style.width = imageTag.width + "px";
		anchorTag.onclick = function()
		{
			gSelectedImageIndex = this.index;
			SetSelectedImage();
			return false;
		}

		// Create the table data tag		
		tableDataTag = document.createElement("td");
		
		// Create the table row tag if required
		if((i % numThumbNailsPerRow) == 0)
		{
			tableRowTag = document.createElement("tr");
		}
		
		anchorTag.appendChild(imageTag);
		tableDataTag.appendChild(anchorTag);
		tableRowTag.appendChild(tableDataTag);
		if(i % numThumbNailsPerRow == 0)
		{
			document.getElementById("thumbSection").appendChild(tableRowTag);
		}
	}
}


function CreateImageNumberArea()
{
	// Declaring variables
	var divTag, spanTag, anchorTag;
	var textNode;
	var imageIndex;

	// Create the number of images
	divTag = document.getElementById("divImageNumber");

	// Add the prev image span
	textNode = document.createTextNode("<");
	anchorTag = document.createElement("a");
	if(gSelectedImageIndex > 0)
	{
		imageIndex = gSelectedImageIndex - 1;
	}
	else
	{
		imageIndex = gPhotoCollection.count - 1;
	}
	anchorTag.href = gPhotoCollection.filePath + "/thumbs/" + gPhotoCollection.photos[imageIndex].fileName;
	anchorTag.id = "anchorPrevImage";
	anchorTag.onclick = function()
	{
		SelectPreviousImage();
		SetSelectedImage();
		return false;
	}
	spanTag = document.createElement("span");
	spanTag.id = "spanPrevImage";	
	anchorTag.appendChild(textNode);
	spanTag.appendChild(anchorTag);
	divTag.appendChild(spanTag);

	// Add the current image span
	spanTag = document.createElement("span");
	spanTag.id = "spanCurrentImage";
	textNode = document.createTextNode("");
	spanTag.appendChild(textNode);
	divTag.appendChild(spanTag);

	textNode = document.createTextNode(">");
	anchorTag = document.createElement("a");
	if(gSelectedImageIndex < (gPhotoCollection.count - 1))
	{
		imageIndex = gSelectedImageIndex + 1;
	}
	else
	{
		imageIndex = 0;
	}
	anchorTag.href = gPhotoCollection.filePath + "/thumbs/" + gPhotoCollection.photos[imageIndex].fileName;
	anchorTag.id = "anchorNextImage";
	anchorTag.onclick = function()
	{
		SelectNextImage();
		SetSelectedImage();
		return false;
	}
	spanTag = document.createElement("span");
	spanTag.id = "spanNextImage";	
	anchorTag.appendChild(textNode);
	spanTag.appendChild(anchorTag);
	divTag.appendChild(spanTag);
}


function SetSelectedImage()
{
	var spanTag;
	var anchorTagPrev, anchorTagNext;
	var imageIndex;
	
	document.getElementById("imgDisplay").src = gPhotoCollection.filePath + "/main/" + gPhotoCollection.photos[gSelectedImageIndex].fileName;
	document.getElementById("imgDisplay").alt = gPhotoCollection.photos[gSelectedImageIndex].altText;
	document.getElementById("imgDisplay").title = gPhotoCollection.photos[gSelectedImageIndex].altText;
	document.getElementById("photoCaption").style.display = "block";
	document.getElementById("photoText").firstChild.nodeValue = gPhotoCollection.photos[gSelectedImageIndex].caption;
	
	spanTag = document.getElementById("spanCurrentImage");
	if(spanTag != null)
	{
		spanTag.firstChild.nodeValue = " " + (gSelectedImageIndex + 1) + " of " + gPhotoCollection.count + " ";
		
		anchorTagPrev = document.getElementById("anchorPrevImage");
		if(gSelectedImageIndex > 0)
		{
			imageIndex = gSelectedImageIndex - 1;
		}
		else
		{
			imageIndex = gPhotoCollection.count - 1;
		}
		anchorTagPrev.href = gPhotoCollection.filePath + "/thumbs/" + gPhotoCollection.photos[imageIndex].fileName;


		anchorTagNext = document.getElementById("anchorNextImage");
		if(gSelectedImageIndex < (gPhotoCollection.count - 1))
		{
			imageIndex = gSelectedImageIndex + 1;
		}
		else
		{
			imageIndex = 0;
		}
		anchorTagNext.href = gPhotoCollection.filePath + "/thumbs/" + gPhotoCollection.photos[imageIndex].fileName;
	}
}


function SelectNextImage()
{
	if(gSelectedImageIndex < (gPhotoCollection.count - 1))
	{
		gSelectedImageIndex++;
	}
	else
	{
		gSelectedImageIndex = 0;
	}
}


function SelectPreviousImage()
{
	if(gSelectedImageIndex > 0)
	{
		gSelectedImageIndex--;
	}
	else
	{
		gSelectedImageIndex = gPhotoCollection.count - 1;
	}
}


 function PreloadImages()
 {
	 var i;
	 var mainImages;

	 mainImages = new Array();
	 for (i=0;i<gPhotoCollection.count; i++)
	 {
		mainImages[i] = new Image();
		mainImages[i].src = gPhotoCollection.filePath + "/main/" + gPhotoCollection.photos[i].fileName;
	 }
 } 
 
 // Cross-browser script
function CheckForRightClick(e)
{
      // Declare and initialize variables
      var bRightMouseButtonClicked = false;
      if(!e)
      {
      	 var e = window.event;
      }
      
      // Determine if the right mouse button was clicked
      if (e.which) 
      {
	  bRightMouseButtonClicked = (e.which == 3);
      } 		      	
      else if (e.button) 
      {
	  bRightMouseButtonClicked = (e.button == 2);
      }
      
      if(bRightMouseButtonClicked)
      {
      	alert('This image is copyrighted and may not be copied.');
      }
      
      return bRightMouseButtonClicked;  
}