var MAIN_PAGE = "index.html";

function initPage(homeLinkImg, pageId)
{
	//alert("parent="+parent);
	//alert("parent.mainPageLoaded="+parent.mainPageLoaded);
	
	if (parent.mainPageLoaded)
	{
		//if sub page hide the link back to main page, otherwise show it
		homeLinkImg.style.display = "none";
	}
	else
	{
		if (pageId != null && pageId.length > 0)
		{
			//redirect to main page, load this one
			setPageIdInUrl(pageId);
		}
	}
}

function loadPage(pageId, pageName)
{
	//alert("loadPage: pageId="+pageId+" parent="+parent+" parent.mainPageLoaded="+parent.mainPageLoaded+" window.mainPageLoaded="+window.mainPageLoaded+" window.parent="+window.parent);
	
	//alert("loadPage: window.location="+window.location+" parent="+parent+" parent.location="+parent.location.href);
	//alert("loadPage: window.top.framesDisabled="+window.top.framesDisabled);
	if (!window.top.framesDisabled)
	{
		if (parent && !parent.mainPageLoaded && pageId != null && pageId.length > 0)
		{
			//Chrome isn't able to get the mainPageLoaded var from parent, check for param
			//another option is to compare window.location with parent.location but can't test locally cause chrome won't allow
			if (window.location.href.indexOf("isframe") < 0
					&& window.location.href.indexOf("noFrames=true") < 0)
			{
				//redirect to main page, load this one
				setPageIdInUrl(pageId);
			}
		}
		
//		var hiddenLogoLink = document.getElementById('hiddenLogoLink');
//		alert("hiddenLogoLink="+hiddenLogoLink);
//		if (hiddenLogoLink)
//		{
//			hiddenLogoLink.style.display = "none";
//		}
	}
	
	if (pageName)
	{
		setPageName(pageName);
	}
}

function setPageIdInUrl(pageId)
{
	//alert("pageId="+pageId);
	if (window.location.href.indexOf(MAIN_PAGE) >= 0 || window.location.href.indexOf("file://") >= 0)
	{
		window.location.href = MAIN_PAGE + '?page=' + pageId;
	}
	else
	{
		window.location.href = '/?page=' + pageId;
	}	
}

function setPageIdInParentUrl(pageId)
{
	try
	{
		//alert("pageId="+pageId);
		if (parent && parent.location && parent.location.href 
			&& parent.location.href.indexOf(MAIN_PAGE) >= 0 || parent.location.href.indexOf("file://") >= 0)
		{
			//alert("window.location.href="+window.location.href);
			//alert("parent.location.href="+parent.location.href);
			parent.location.href = MAIN_PAGE + '?page=' + pageId;
		}
		else
		{
			parent.location.href = '/?page=' + pageId;
		}
	}
	catch (e) {
		alert("e="+e);
	}
}


function setPageName(newName)
{
	var pageName = parent.document.getElementById('pageName');
	
	if (pageName.textContent)
	{
		pageName.textContent = newName;
	}
	else
	{
		//IE
		pageName.innerHTML = newName;
	}
}

function showPage(pageId)
{
	if (!window.top.framesDisabled && window.top != self)
	{
		//alert("pageUrl="+pageUrl);
		//change this first since some browsers will refresh page
		setPageIdInParentUrl(pageId);
		
		window.location.href = pageId + ".html?isframe=true";
		
		return false;
	}
	else
	{

		//follow link
		return true;
	}
}

