/**
 * global scripts for intranet
 */


/**
 * refreshMenu()
 * can be called from any document if it's loaded inside the layout 
 */ 
function refreshMenu()
{
	if (this.location == top.location)
	{
		var MenuIframe = document.getElementById("navigationiframe");
		MenuIframe.src = 'logic.system_menu.htm';

	}
	else
	{
		window.top.refreshMenu();
	}
}

function listiconswitch()
{
	setContentFrameUrl("logic.listiconswitch.htm");
}

function checkForLoadInFrame()
{
	if (this.location == top.location)
    {
    	newPath = this.location.pathname;
        var pathsArray = newPath.split("/");
        newPath = pathsArray[pathsArray.length-1];
        indexOfDot = newPath.indexOf('.');
        while (indexOfDot != -1)
        {
        	newPath = newPath.replace(".","|");
            indexOfDot = newPath.indexOf('.');
        }
        top.location = "/intranet/logic.system_layout.reload."+newPath+".htm";
	}
}

/**
 * selectMenuItem()
 * 
 */ 
function selectMenuItem(id)
{
	if (this.location == top.location)
	{
		var MenuIframe = document.getElementById("navigationiframe");	

		if (typeof(MenuIframe.contentWindow.selectItem) == "function")
		{
			MenuIframe.contentWindow.selectItem(id);
		}
		else
		{
			window.top.NextSelectItem = id;
		}
	}
	else
	{
		window.top.selectMenuItem(id);
	}
}

/**
 * check onload of menu if we need to select a item
 */
function checkforselect()
{
	if (window.top.NextSelectItem !="")
	{
		myid = top.NextSelectItem;
		window.top.NextSelectItem = "";
		setTimeout("selectMenuItem('"+myid+"')", 100 );
	} 
}

function disableIconsAndClearPath()
{
	setButtonActions(0,0,false);
	setCurrentPath("");
}

function setButtonActions(folderid,partid,writerights)
{	
	if (this.location == top.location)
	{
		var newfolder = document.getElementById("newfoldericon");	
		if (newfolder != null)
		{
			if (writerights)
			{
				newfolder.src = "system/image/newfolder.gif";
				newfolder.onclick = function (){
					setContentFrameUrl('logic.folder_new.folderid.'+folderid+'.partid.'+partid+'.htm');
					};
			}
			else
			{
				newfolder.src = "system/image/nonewfolder.gif";
				newfolder.onclick = function (){
					alert("U heeft geen rechten om hier een map aan te maken");
				};
			}
		}
		var doupload = document.getElementById("uploadicon");	
		if (doupload != null)
		{
			if (writerights)
			{
				doupload.src = "system/image/upload.jpg";
				doupload.onclick = function (){
					setContentFrameUrl('logic.file_new.folderid.'+folderid+'.partid.'+partid+'.htm');
				};
			}
			else
			{
				doupload.src = "system/image/noupload.jpg";
				doupload.onclick = function (){
					alert("U heeft geen rechten om hier bestanden te plaatsen");
				};
			}
		}		
	}
	else
	{
		window.top.setButtonActions(folderid,partid,writerights);
	}
		
}

/**
 * pageloaded()
 * 
 */ 
function pageloaded(folderid,partid,id,path,writerights)
{
	if (this.location == top.location)
	{
		selectMenuItem(id);
		setButtonActions(folderid,partid,writerights);
		setCurrentPath(path);
	}
	else
	{
		window.top.pageloaded(folderid,partid,id,path,writerights);
	}
}

function setCurrentPath(path)
{
	if (this.location == top.location)
	{
		var currP = document.getElementById("currentpath");	
		if (currP != null)
			currP.innerHTML = path;
	}
	else
	{
		window.top.setCurrentPath(path);
	}				
}
/**
 * setContentFrameUrl()
 * can be called from any document if it's loaded inside the layout 
 */ 
function setContentFrameUrl(url)
{
	if (url == -1)
		return;
		
	if (this.location == top.location)
	{
		var ContentIframe = document.getElementById("contentiframe");
		ContentIframe.src = url;
	}
	else
	{
		window.top.setContentFrameUrl(url);
	}
}

/**
 * changefolder()
 * can be called from any document if it's loaded inside the layout 
 */ 
function changefolder(id,partid)
{
	if (this.location == top.location)
	{
		var ContentIframe = document.getElementById("contentiframe");
		ContentIframe.src = 'logic.folderview.partid.'+partid+'.folder.'+id+'.htm';		
	}
	else
	{
		window.top.changefolder(id,partid);
	}
}

var OpenContextLayer = "";
var contextObj = null;
function openContext(id,object,e)
{
	closeContextLayer();
	if (!e) e =window.event; 
	e.returnValue= false;

	contextObj = object;
	OpenContextLayer = document.getElementById("AGP_GUI_context"+id);
	OpenContextLayer.style.display = 'inline';
	OpenContextLayer.style.position = 'absolute';
	OpenContextLayer.style.top =  e.clientY+"px";
	OpenContextLayer.style.left = e.clientX+"px";	
	
	var rows = OpenContextLayer.getElementsByTagName('tr');
	for (i=0; i<rows.length; i++) {
		if (i==0)
		{
			rows[i].className = AGP_addValueToClassName(rows[i].className,"highlite");
		}
		else
		{
			rows[i].className = AGP_RemoveValueFromClassName(rows[i].className,"highlite");
		}
	}
	
	AGP_GUI_contextLastHoverItem = null;
	
	document.body.onclick = function()
            {
                closeContextLayer();
            }
}

function closeContextLayer()
{
	document.body.onclick = "";
	if (OpenContextLayer.style != null)
	{
		OpenContextLayer.style.display = 'none';
	}
}

function AGP_addValueToClassName(ClassName,Value)
{
    if (ClassName.indexOf(Value) == -1);
		ClassName = ClassName + " "+Value;
    return ClassName;
}

function AGP_RemoveValueFromClassName(ClassName,Value)
{
	eval("ClassName = ClassName.replace(/"+Value+"/gi,\"\");");
	return ClassName;
}

function AGP_GUI_context_highlite(tr)
{		
	var rows = tr.parentNode.getElementsByTagName('tr');
	for (i=0; i<rows.length; i++) {
		if (rows[i] == tr)
		{
			rows[i].className = AGP_addValueToClassName(rows[i].className,"highlite");
		}
		else
		{
			rows[i].className = AGP_RemoveValueFromClassName(rows[i].className,"highlite");
		}
	}
}
	
/**
 * url functions
 */
function changeFolderFromContextUrl()
{
	foldernr = "";
	partnumber = "";
	if(contextObj.getAttribute("number") != null)
	{
    	foldernr = contextObj.getAttribute("number");
        partnumber = contextObj.getAttribute("partnumber");
	}
	if (foldernr != "" && partnumber != "")
	{
		return "logic.folder_change.partid."+partnumber+".folder."+foldernr+".htm";
	}
	return -1;
} 

function deleteFolderFromContextUrl()
{
	foldernr = "";
	partnumber = "";
	if(contextObj.getAttribute("number") != null)
	{
    	foldernr = contextObj.getAttribute("number");
        partnumber = contextObj.getAttribute("partnumber");
	}
	if (foldernr != "" && partnumber != "")
	{
		return "logic.folder_delete.partid."+partnumber+".folder."+foldernr+".htm";
	}
	return -1;
} 
	
function openFolderFromContextUrl()
{
	foldernr = "";
	partnumber = "";
	if(contextObj.getAttribute("number") != null)
	{
    	foldernr = contextObj.getAttribute("number");
        partnumber = contextObj.getAttribute("partnumber");
	}
	if (foldernr != "" && partnumber != "")
	{
		return "logic.folderview.partid."+partnumber+".folder."+foldernr+".htm";
	}
	return -1;
} 


function deleteFileFromContextUrl()
{
	fileid = "";
	partid = "";
	if(contextObj.getAttribute("number") != null)
	{
    	fileid = contextObj.getAttribute("number");
        partid = contextObj.getAttribute("partnumber");
	}
	if (fileid != "" && partid != "")
	{
		return "logic.file_delete.partid."+partid+".fileid."+fileid+".htm";
	}
	return -1;
} 
function openFileFromContextUrl()
{
	return contextObj.href;
	return -1;
} 


