var _articleId			= -1;
var _column				= -1;
var _row				= -1;
var _pageArticleId		= -1;
var _isCanEdit			= false;
var _isCanLayout		= false;
var _isCanManagePerms	= false;
var _editAction			= "";
var _editJavaScript		= "";
var _clientId			= "";
var _collapseStateText;

function toggleCollapse(id, collapseButton)
{
	if (collapseButton == null)
		var collapseButton = window.event.srcElement
		
	var style = collapseButton.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.nextSibling.style;

	_collapseStateText = setCollapseState();

	if (style.display == "none")
	{
		collapseButton.src = collapseButton.src.replace("Expand","Collapse");
		style.display = "inline";
		collapseState(_collapseStateText, true, id);
	}
	else
	{
		collapseButton.src = collapseButton.src.replace("Collapse","Expand");
		style.display = "none";
		collapseState(_collapseStateText, false, id);
	}
}

function checkCollapse(id, collapseButton)
{
	_collapseStateText = setCollapseState();
	
	if (_collapseStateText)
	{
		if (_collapseStateText.value.indexOf("#"+id+"-") >= 0 && _collapseStateText.value != "")
		{
			var s0 = _collapseStateText.value.indexOf("#"+id+"-");
			var len = ("#"+id+"-").length;
			var sVal = _collapseStateText.value.substr(s0 + len, 1)
			
			if (sVal == "1")
				toggleCollapse(id, document.getElementById(collapseButton).children[0])
		}
	}
}

function collapseState(collapseStateText, isCollapsed, id)
{
	if (isCollapsed)
		sVal = "0"
	else
		sVal = "1"
	
	if (collapseStateText.value.indexOf("#"+id+"-") >= 0 && collapseStateText.value != "")
	{
		var s0 = collapseStateText.value.indexOf("#"+id+"-");
		var len = ("#"+id+"-").length;
		collapseStateText.value = collapseStateText.value.substr(0,s0) + "#"+id+"-" + sVal + collapseStateText.value.substr(s0 + len + 1);
	}
	else
	{
		collapseStateText.value += "#"+id+"-"+sVal
	}
}

function setCollapseState()
{
	var collapseStateText;
	for (i = document.all.length; i >= 0; i--)
	{
		if (document.all(i))
		{
			if (document.all(i).id.indexOf("CollapseState") >= 0)
			{
				collapseStateText =  document.all(i);
				break;
			}
		}
	}
	return collapseStateText;
}

function portalCommands(visible, clientId, articleId, canEdit, canManagePerms, canLayout, column, row, pageArticleId, editAction, editJavaScript)
{
	if (editJavaScript != null)
		_editJavaScript = editJavaScript;
	portalVisibility(visible, clientId, articleId, canEdit, canManagePerms, canLayout, column, row, pageArticleId, editAction)
}

function portalVisibility(visible, clientId, articleId, canEdit, canManagePerms, canLayout, column, row, pageArticleId, editAction)
{
	_clientId		= clientId;
	_column			= column;
	_row			= row;
	_articleId		= articleId;
	_canEdit		= canEdit;
	_canManagePerms	= canManagePerms;
	_canEdit		= canEdit;
	_canLayout		= canLayout;
	_pageArticleId	= pageArticleId;
	
	if (editAction != null)
		_editAction = editAction;
		
	// Set the visiblilty of the different context menu items
	//
		
	if (_canEdit)
		document.getElementById("PortalEditRow").style.display = "inline";
	else
		document.getElementById("PortalEditRow").style.display = "none";
		
	
	if (_canManagePerms)
		document.getElementById("PortalManagePermRow").style.display = "inline";
	else
		document.getElementById("PortalManagePermRow").style.display = "none";

	if (_canLayout)
		setLayoutControlVisibilty(true)
	else
		setLayoutControlVisibilty(false)

	var portalTools = document.getElementById("PortalFunctions")

	if (portalTools)
	{
		if (visible)
		{
			portalTools.style.display = "inline";
			var sizeX = portalTools.offsetWidth;
			
			var table	= document.getElementById(_clientId);
			var x = table.offsetWidth + getOffsetLeft(table);
			var y = table.offsetTop + getOffsetTop(table);
	//		if (y >= sizeY)
	//			y = y - sizeY;
			
			portalTools.style.pixelLeft = x - sizeX;
			portalTools.style.pixelTop = y;
			
			showOverlay(portalTools);
		}
		else
		{
			portalTools.style.display = "none";
			hideOverlay(portalTools);
		}
	}
}

function setLayoutControlVisibilty(visibilty)
{
	var display = "none";
	
	if (visibilty)
		display = "inline";
	
	var i = 0;
	var row;
	
	while (1)
	{
		row = document.getElementById("PortalLayoutRow" + i);
		if (row)
			row.style.display = display;
		else
			break;
			
		i++;
	}
}

function getOffsetLeft(table)
{
	var element = table;
	var offset = element.offsetLeft; 
	while ( element.offsetParent != null )
	{
		offset += element.offsetParent.offsetLeft
		if (element.tagName == "TABLE")
		{
			offset = offset - parseInt(element.cellPadding)
			//offset = offset - parseInt(element.cellSpacing)
		}
		
//		if (element.tagName == "BODY")
//			offset += parseInt(element.leftMargin)

		element = element.parentElement
	}
	
	return offset + 5;
}

function getOffsetTop(table)
{
	var element = table;
	var offset = element.offsetTop; 
	while ( element.offsetParent != null )
	{
		offset = offset + element.offsetParent.offsetTop
		
		if (element.tagName == "TABLE")
		{
			offset = offset - parseInt(element.cellPadding)
		}
		
//		if (element.tagName == "BODY")
//			offset += parseInt(element.topMargin)

		element = element.parentElement
	}
	
	return offset;
}

function portalRemove()
{
	href = document.location.href + getDelim() + "PortalRemove=&ArticleId=" + _articleId + "&PageArticleId=" + _pageArticleId + "&Col=" + _column + "&Row=" + _row;
	portalVisibility(false, 0)
	document.location = href;
}

function portalEdit()
{
	if (_editAction == "")
	{
		openContentEditWindow(_articleId);
		portalVisibility(false, 0)
	}
	else
	{	
		document.location = _editAction;
	}
}

function articleEdit()
{
	if (_editJavaScript != "")
	{
		// TODO: How do I execute the script in _editJavaScript?
		// Use eval.
		// NOTE: This requires that a forward slash (/) be used as a seperator
		// rather than a backward slash (\)! Example:
		// This will work: CntlPath=UserControls/Content/ContentEditor.ascx&art=7
		// This will NOT: CntlPath=UserControls\Content\ContentEditor.ascx&art=7
		// This will NOT: CntlPath=UserControls\\Content\\ContentEditor.ascx&art=7
		eval(_editJavaScript);
	}
	else
		openDialogueWindow(_editAction + "&art=" + _articleId, "Editor", 510, 780);
	portalVisibility(false, 0)
}

function portalInsert(position)
{
	href = document.location.href + getDelim() + "PortalInsert=" + position + "&ArticleId=" + _articleId + "&PageArticleId=" + _pageArticleId + "&Col=" + _column + "&Row=" + _row;
	document.location = href;
}

function portalMove(direction)
{
	href = document.location.href + getDelim() + "PortalMove=" + direction + "&ArticleId=" + _articleId + "&PageArticleId=" + _pageArticleId + "&Col=" + _column + "&Row=" + _row;
	portalVisibility(false, 0)
	document.location = href;
}


function portalPerm()
{
	openDialogueWindow("defaultDialog.aspx?CntlPath=UserControls\\ArticleAccess\\ArticlePermissionsControl.ascx&art=" + _articleId,
		"MangagePerminssion", 730, 680);
	portalVisibility(false, 0)
}

function getDelim()
{
	if (document.location.href.indexOf("?") > -1)
		return "&"
	else
		return "?"
}
function showOverlay(overLay)
{	
	checkBrowser()
	if (ie6)
	{
		var overID = overLay.ID + "_over";
		if (!document.getElementById(overID))
			document.body.insertAdjacentHTML("beforeEnd","<iframe id='" + overID + "'   style='position:absolute;left:0px;top:0x;z-index:500;display:none' scrolling='no' frameborder='0'></iframe>");
		if (document.getElementById(overID))
		{
			var overs = document.getElementById(overID).style;
			overs.top = overLay.style.top;
			overs.left = overLay.style.left;
			overs.width = overLay.offsetWidth;
			overs.height = overLay.offsetHeight;
			overs.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			overs.display = 'block';
		}
	}
}

function hideOverlay(overLay)
{
	checkBrowser()
	if (ie6)
	{
		var overID = overLay.ID + "_over";
		if (document.getElementById(overID))
		{
			var overs = document.getElementById(overID).style;
			overs.display = 'none';
		}
	}
}

function checkBrowser()
{
	ver = navigator.appVersion
	ie55 = (ver.indexOf("MSIE 5.5")>-1)?true:false;
	ie5 = (ver.indexOf("MSIE 5")>-1 && !ie55)?true:false;
	ie6 = (ver.indexOf("MSIE 6")>-1)?true:false;
	ie = (ie5 || ie55 || ie6)
}