var miniBoxConst = "miniBox";
var boxBottomConst = "boxBottom";
var boxTopConst = "boxTop";
var subcategoriesBoxConst = "subcategoriesBox";
var galleriesBoxConst = "galleriesBox";
var categoriesBoxConst = "categoriesBox";
var breadcrumbConst = "breadcrumb";
var boxSettingsClassConst = "boxSettings";
var originalOnChangeHandler;

function IsGalleryPage()
{
	var bodyClasses = document.body.className;
	var galleryPage = false;
	
	if (bodyClasses.indexOf('galleryPage') > -1)
	{
	   galleryPage = true;
	}
	
	return galleryPage;
}

function IsLoggedIn()
{
	var bodyClasses = document.body.className;
	var loggedIn = false;
	
	if (bodyClasses.indexOf('loggedIn') > -1)
	{
	   loggedIn = true;
	}
	
	return loggedIn;
}

function IsHomePage()
{
	var bodyClasses = document.body.className;
	var isHomePage = false;
	
	if (bodyClasses.indexOf('homepage') > -1)
	{
	   isHomePage = true;
	}
	
	return isHomePage;	
}

// This locks down the homepage so users with just a link cannot access the galleries/categories
function LockDownHomePage()
{
	if (!IsLoggedIn() && IsHomePage()) 
	{
		var mainDiv = null;
		mainDiv = document.getElementById(galleriesBoxConst);
		if (mainDiv == null)
		{
			mainDiv = document.getElementById(categoriesBoxConst);
		}
		
		if (mainDiv != null)
		{
			// Create a new title element to indicate it is a restricted access			
			var titleElem = null;
			titleElem = document.getElementById('galleryTitle');
			if (titleElem == null)
			{
				titleElem = document.getElementById('categoryTitle');
			}
			
			if (titleElem != null)
			{
				var newTitleElem = document.createElement('H3');
				newTitleElem.className = titleElem.className;
				newTitleElem.id = 'restrictedTitle';
				newTitleElem.appendChild(document.createTextNode('Restricted Access'));
				newTitleElem.position = 'relative';
				newTitleElem.top = '11px';
				
				titleElem.style.display = 'none';		
								
				titleElem.parentNode.insertBefore(newTitleElem, titleElem);				
			}
			
			// Hide all gallery boxes
			var bottomBox = GetChildByClassName(mainDiv, boxBottomConst);
			if (bottomBox != null)
			{
				var itemArray = new Array();
				for (i=0; i < bottomBox.childNodes.length; i++)
				{
					var bottomBoxElem = bottomBox.childNodes[i];
	
					if (bottomBoxElem.attributes != null && bottomBoxElem.attributes.getNamedItem('class') != null && bottomBoxElem.attributes.getNamedItem('class').nodeValue.indexOf(miniBoxConst) > -1)
					{
						itemArray[itemArray.length] = bottomBoxElem;
					}
				}					
				
				// iterate through our array of sub categories
				var boxItem = null;
				for (i=0; i < itemArray.length; i++)
				{
					boxItem = itemArray[i];
					boxItem.style.display = 'none';
				}
				
				// Add password info
				CreatePasswordElements(bottomBox);
			}
		}		
	}
}

function CreatePasswordElements(parentContainer)
{
	var labelSpan = document.createElement('SPAN');
	labelSpan.id = 'passwordSpan';
	labelSpan.innerHTML = 'Enter password to view: ';	
	
	var passwordBox = document.createElement('INPUT');
	passwordBox.id = 'passwordBox';
	passwordBox.type = 'password';
	passwordBox.width = '100';
	
	var submitButton = document.createElement('INPUT');
	submitButton.id = 'submitButton';
	submitButton.type = 'button';
	submitButton.value = 'Submit';
	submitButton.style.marginLeft = '5px';
	
	if (window.addEventListener)
	{
		submitButton.addEventListener('click',SubmitButtonClicked, false);
	}
	else
	{
		submitButton.attachEvent('onclick',SubmitButtonClicked);
	}
	
	
	parentContainer.appendChild(labelSpan);
	parentContainer.appendChild(passwordBox);
	parentContainer.appendChild(submitButton);
}

function SubmitButtonClicked()
{
	if (document.getElementById('passwordBox') != null && ValidatePassword(document.getElementById('passwordBox').value))
	{
		EnablePhotos();
	}
	else 
	{
		alert('Incorrect Password!');
	}
}

function ValidatePassword(pw)
{
	if (pw == 'letmein')
	{
		return true;
	}
	else 
	{
		return false;
	}
}

function EnablePhotos()
{
	var mainDiv = null;
	mainDiv = document.getElementById(galleriesBoxConst);
	if (mainDiv == null)
	{
		mainDiv = document.getElementById(categoriesBoxConst);
	}

	if (mainDiv != null)
	{
		var titleElem = null;
		titleElem = document.getElementById('galleryTitle');
		if (titleElem == null)
		{
			titleElem = document.getElementById('categoryTitle');
		}
			
		if (titleElem != null)
		{
			titleElem.style.display = 'inline';														
		}
		
		var restrictTitle = document.getElementById('restrictedTitle');
		if (restrictTitle != null)
		{
			restrictTitle.style.display = 'none';
		}
		
		var passwordSpan = document.getElementById('passwordSpan');
		if (passwordSpan != null)
		{
			passwordSpan.style.display = 'none';
		}

		var passwordSpan = document.getElementById('passwordBox');
		if (passwordSpan != null)
		{
			passwordSpan.style.display = 'none';
		}

		var passwordSpan = document.getElementById('submitButton');
		if (passwordSpan != null)
		{
			passwordSpan.style.display = 'none';
		}
					
		// Hide all gallery boxes
		var bottomBox = GetChildByClassName(mainDiv, boxBottomConst);
		if (bottomBox != null)
		{
			var itemArray = new Array();
			for (i=0; i < bottomBox.childNodes.length; i++)
			{
				var bottomBoxElem = bottomBox.childNodes[i];
				if (bottomBoxElem.attributes != null && bottomBoxElem.attributes.getNamedItem('class') != null && bottomBoxElem.attributes.getNamedItem('class').nodeValue.indexOf(miniBoxConst) > -1)
				{
					itemArray[itemArray.length] = bottomBoxElem;
				}
			}					
				
			// iterate through our array of sub categories
			var boxItem = null;
			for (i=0; i < itemArray.length; i++)
			{
				boxItem = itemArray[i];
				boxItem.style.display = 'inline';
			}
		}
	}
}

// This makes it so that only logged in visitors have access to navigate up
// the site using the bread crumbs
function LockDownBreadCrumbs()
{
	var breadCrumbDiv = document.getElementById(breadcrumbConst);
	
	if (!IsLoggedIn() && breadCrumbDiv != null && breadCrumbDiv.childNodes != null)
	{
	   for (i=0; i < breadCrumbDiv.childNodes.length; i++)
	   {
	       if (breadCrumbDiv.childNodes[i].tagName == 'A')
	       {
	           var breadCrumbLink = breadCrumbDiv.childNodes[i];
	           var spanText =document.createElement('SPAN');
	           spanText.innerHTML = breadCrumbLink.innerHTML;
	           breadCrumbLink.parentNode.replaceChild(spanText,breadCrumbLink);
	       }
	   }
	}
}

// This integrates sub-categories into the same box as galleries
function CollapseSubCategories()
{
	try 
	{
		var bodyClasses = document.body.className;
		var loggedIn = IsLoggedIn();
		
		// get subCategories box with its "bottom" div which holds the items we want to move
		var subCategoriesBox = document.getElementById(subcategoriesBoxConst);
		var subCategoriesBottomBox = null;
		if (subCategoriesBox != null) 
		{
			subCategoriesBottomBox = GetChildByClassName(subCategoriesBox, boxBottomConst);
		}
		
		// get galleries box with the "bottom" div which holds our target location
		var galleriesBox = document.getElementById(galleriesBoxConst);
		if (galleriesBox != null) 
		{
			galleriesBottomBox = GetChildByClassName(galleriesBox, boxBottomConst);
		}
		
		// if both a subcat box and gallery box exists
		if (subCategoriesBottomBox != null && subCategoriesBottomBox.hasChildNodes && galleriesBottomBox != null && galleriesBottomBox.hasChildNodes)
		{
			var subCatArray = new Array();
			
			// iterate through subcat box elements if it is a minibox then it is a sub category, add it to our temp array
			for (i=0; i < subCategoriesBottomBox.childNodes.length; i++)
			{
				var subcategoryBox = subCategoriesBottomBox.childNodes[i];
				
				if (subcategoryBox.attributes != null && subcategoryBox.attributes.getNamedItem('class') != null && subcategoryBox.attributes.getNamedItem('class').nodeValue.indexOf(miniBoxConst) > -1)
				{
					subCatArray[subCatArray.length] = subcategoryBox;
				}
			}					
			
			// iterate through our array of sub categories
			for (i=0; i < subCatArray.length; i++)
			{
				subcategoryBox = subCatArray[i];
				
				if (loggedIn)
				{
					subcategoryBox.Height = 100;
				}

				// iterate through the items in the gallery box, find the first one that is a miniBox (the first gallery) and insert before it
				var firstGalleryElem = null;
				for (j=0; j < galleriesBottomBox.childNodes.length; j++)
				{
					if (galleriesBottomBox.childNodes[j] != null && galleriesBottomBox.childNodes[j].attributes != null && galleriesBottomBox.childNodes[j].attributes.getNamedItem('class') != null && galleriesBottomBox.childNodes[j].attributes.getNamedItem('class').nodeValue.indexOf(miniBoxConst) > -1)
					{
						firstGalleryElem = galleriesBottomBox.childNodes[j];
						break;
					}
				}
								
				if (firstGalleryElem != null)
				{
					galleriesBottomBox.insertBefore(subcategoryBox, firstGalleryElem);					
				}
			}
			
			// Hide original sub cat box
			subCategoriesBox.style.display = 'none';
		}
	}
	catch (err)
	{
		alert('Exception: ' + err.message);
	}
}

function AddVideoLinking()
{
  if (IsLoggedIn() && IsGalleryPage())
  {
    var toolsSelect = document.getElementById('tools');
		if (toolsSelect != null)
		{
			if (window.addEventListener)
			{
				toolsSelect.addEventListener('focus',PhotoToolsFocus, false);
			}
			else
			{
				toolsSelect.attachEvent('onfocus',PhotoToolsFocus);
			}		
		}
  }
}

function NewPhotoToolsChanged()
{
	var embedCode = '';
	var movie = document.getElementById('smugMovie');
	if (movie != null && movie.outerHTML)
	{
		embedCode = movie.outerHTML;
	}
	else
	{
		embedCode = '<object id="smugMovie" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" height="496" width="640">' + movie.innerHTML + '</object>';
	}
	
	var toolsSelect = document.getElementById('tools');
	if (toolsSelect.options[toolsSelect.selectedIndex].text == "Get Video HTML")
	{
		var alertStr = embedCode;

		alertStr = '<DIV align="center" width="440" height="190"><TEXTAREA rows=10 cols=50>' + alertStr + '</TEXTAREA><br><input type="button" value="Close" onclick="window.close()">';
		myWindow = window.open("", "tinyWindow", 'toolbar,width=450,height=230,menubar=0,status=0,resizable=yes,toolbar=0,location=0')		
		myWindow.document.write(  alertStr)
		myWindow.document.bgColor="white"		
	}
	else
	{
		return originalOnChangeHandler();
	}
}

function PhotoToolsFocus()
{
    var toolsSelect = document.getElementById('tools');
    
   	var oldOpt = document.getElementById('getVideoHTML');
    if (oldOpt != null)
    {
    	oldOpt.parentNode.removeChild(oldOpt);
    }
    
		if (toolsSelect != null && IsMovieSelected())
		{
				originalOnChangeHandler = toolsSelect.onchange;
				toolsSelect.onchange = NewPhotoToolsChanged;
				
				var newOption = document.createElement('OPTION');				
				newOption.id = 'getVideoHTML';
				newOption.appendChild(document.createTextNode('Get Video HTML'));
				toolsSelect.appendChild(newOption);
		}
    return true;	
}

function IsMovieSelected()
{
	var movie = document.getElementById('smugMovie');
	if (movie != null)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function ElementHasClass(elem, className)
{
	if (elem.attributes != null && elem.attributes.getNamedItem('class') != null && elem.attributes.getNamedItem('class').nodeValue.indexOf(className) > -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function GetChildByClassName(parent, className)
{
	if (parent != null && parent.hasChildNodes)
	{
		for (i=0; i < parent.childNodes.length; i++)
		{
			var child = parent.childNodes[i];			
			if (child != null && child.attributes != null)
			{
				var classAttrNode = child.attributes.getNamedItem('class');
				if (classAttrNode != null && classAttrNode.nodeValue == className) 
				{
					return parent.childNodes[i];
				}
			}
		}
		return null;
	}
}