/*
Title: Javascript Functions
Author:  Simon Coope
*/

function navigatePage(sPageName)
{
	if(sPageName != "")
	{
		
		try
		{
			var frMain = parent.frames['frMain'];
			
			if(frMain != null)
			{
				var hdnPage = frMain.document.getElementById("hdnPage");
				
				if(hdnPage != null)
				{
					hdnPage.value = sPageName;
					frMain.document.frmMain.submit();
				}
				else
				{
					alert('Main page is loading\n\nPlease wait...');
				}
			}
			else
			{
				alert('frMain is null. [navigatePage]');
			}
		}
		catch(e)
		{
			// Error occurred
			alert('Error: ' + e.message);
			return false;
		}
	}
}

// Notes:  Used to change the style of the siteButton custom control.
function changeStyle(strControlId, strCssStyle)
{
	// Get the control
	var objControl = document.getElementById(strControlId);
	
	if(objControl != null)
	{
		// Change the style
		objControl.className = strCssStyle;
	}
}

// Notes:  Changes the style of a row in a table.
function rowLinkHover(row)
{
	// Change the class of the row to show it's being hovered
	row.className = 'RowHover'
}

// Notes:  Changes the style of a row in a table.
function rowLinkOut(row)
{
	// Change the class of the row to show it's being hovered
	row.className = 'RowLink'
}

function showMovie(sURL)
{
	// Open the window
	var movie_window = window.open(sURL, "MovieWindow", "width=400, height=400, resizable=yes, modal=yes");

	// Focus on the window
	movie_window.focus();
}


function showPhoto(sImageName)
{
	var sPageURL = "../shared/aspx/photo_viewer.aspx" + "?sImageName=" + sImageName;
	var sHeight, sWidth;

	try
	{
		// Define the window controls
		var sControls = "width=10, height=10, modal=yes, resizable=yes, location=no, menubar=no";
		
		// Load the window
		var photo_window = window.open(sPageURL, "PhotoWindow", sControls);
		
		// Focus on the window.
		photo_window.focus();
	}
	catch (ex)
	{
		alert('Error: ' + ex);
	}
}

function manageChildren(sParentId)
{
	// Get the parent
	var parent = document.getElementById(sParentId);

	// Check if this parent has children
	if(parent.childNodes.length > 0)
	{
		// cycle through the children and hide them
		// if they're currently shown, or show them
		// if they're currently hidden.
		
		for(var i = 0; i < parent.childNodes.length; i++)
		{
			var child = parent.childNodes[i];
			
			if(child.nodeType == 1)
			{
				// element type
				if(child.tagName == "UL")
				{
					if(child.className == "menuItemHide")
					{
						// Show the UL tag and all it's child LI tags
						child.className = "menuItemShow";
					}
					else if(child.className == "menuItemShow")
					{
						// Hide the UL tag and all it's child LI tags
						child.className = "menuItemHide";
					}

				}
			}
		}
	}
	else
	{
		// No children
		alert("Error: can't find children of selected parent. [manageChildren]");
	}
}

// Notes:  Used to set the popup photo window to the size of the photo.
function setWindowSize()
{
	var img = document.getElementById('imgPhoto');

	// Resize the window accordingly
	window.resizeTo(img.width, img.height);
}

// Notes:  Loads the contacts popup
function showContacts()
{
		// Open window
		var contacts_window = window.open("../shared/aspx/contacts.aspx", "ContactsWindow", "width=450, height=300, modal=yes");

		// Focus on the new window.
		contacts_window.focus();
}

// Notes: Loads the feedback window
function showSiteFeedback()
{
	// Open window
	var feedback_window = window.open("../shared/aspx/feedback.aspx", "FeedbackWindow", "width=450, height=300, modal=yes");

	// Focus on the new window.
	feedback_window.focus();
}

// Notes:  Loads the MP3 window
function openFileForDownload(sFilePath, sFileDesc)
{
	var sURL = "../shared/aspx/download_win.aspx?sFilePath=" + sFilePath + "&sFileDesc=" + sFileDesc;
	
	var new_window = window.open(sURL, "PopupWindow", "width=350, height=100, resizable=yes, modal=yes");
	
	new_window.focus();
}

// Notes:  Resizes the photo window once it's opened.
function setWindowSize()
{
	var img = document.getElementById('imgPhoto');

	// Resize the window accordingly
	window.resizeTo(img.width + 40, img.height + 65);
}

// Used to maintain the position of the welcome box on the first page.
function maintainBoxPos()
{
	// Set position first
	checkPos();
	
	// Now set timer to maintain position
	objTimer = setInterval("checkPos()", 100);
}
	
// Notes: Used to set position of welcome box on first page.	
function checkPos()
{
	// Get the welcome div
	var divWelcome = document.getElementById("welcomeInfo");
	var iboxWidth = 400;
	var iboxHeight = 300;
	
	if(divWelcome != null)
	{
		// Get current window size
		var iHeight, iWidth;
		
		if(document.body.clientHeight)
		{
			iHeight = document.body.clientHeight;
			iWidth = document.body.clientWidth;
		}
		else
		{
			iHeight = window.innerHeight;
			iWidth = window.innerWidth;
		}
	
		// Set the position of the div
		var iNewWidth = (iWidth - iboxWidth) / 2;
		var iNewHeight = (iHeight - iboxHeight) / 2;
		divWelcome.style.left = iNewWidth + 'px';
		divWelcome.style.top =  iNewHeight + 'px';
	}
	else
	{
		alert('Error: Cannot reposition welcome box. [checkPos]');
	}
}

/* Used to change the title of the parent window */
function checkWindowTitle()
{
	// Get the title of the window
	var sTitle = document.getElementById("hdnTitle").value;
	
	// Set the title of the main window
	parent.document.title = sTitle;
}
