var winModalDialog;
var returnValueShowModalDialogPopUp;


function WaitWindowIsClosed(expresion) 
{ 
	if (!winModalDialog.closed)
	{
		setTimeout('WaitWindowIsClosed("' + expresion + '")',500) 
	}
	else
	{
		eval(expresion); 
	}
} 

function windowShowModalDialog(popUpUrl, windowPixelsHeight, windowPixelsWidth,  windowParentRefresh)
{
	if(window.showModalDialog)
	{	
		window.showModalDialog(popUpUrl,'','dialogHeight:' + windowPixelsHeight + 'px;dialogWidth=' + windowPixelsWidth + 'px;scroll:yes;status:no;resizable:yes;center:yes');
		EndNewPublication(windowParentRefresh);
	}
	else
	{
		winModalDialog  = window.open(popUpUrl,'','modal=yes,height=' + windowPixelsHeight + ',width=' + windowPixelsWidth + ',scrollbars=yes,status=no');
		WaitWindowIsClosed('EndNewPublication(' + windowParentRefresh + ');');
	}
}


function NewPublication(folderId)
{
	if(folderId == null)
	{
		folderId = eval(TreeViewPublicationsObject).SelectedNode.ID;
		contextFolderID = folderId;
	}
	if(folderId == -1)
	{
		alert("You cannot create a document in the 'Recycle Bin' folder");
		return;
	}
    var querystring = null;
	var aParams = document.location.search.substr(1).split('&') ; 
	for (i = 0; i< aParams.length; i++)
	{
		if(aParams[i] == null
		     || aParams[i] == ""
		     || aParams[i].indexOf("PublicationFolderId") != -1
		     || aParams[i].indexOf("Id") != -1)
		{
			continue;
		}
		
		if(querystring == null)
		{
		    querystring = "?";
		}
		else
		{
		    querystring += "&";
		}
		querystring += aParams[i];
	}
	if(querystring == null)
	{
	    querystring = "?Id=" + eval(TreeViewPublicationsObject).SelectedNode.SectionId + 
	        "&PublicationFolderId=" + folderId;
	}
	windowShowModalDialog(_urlPartMountInfo + "/Content/NewEditPublication.aspx" + querystring,700,650,true);
	if(!window.showModalDialog)
	{	
		return false;
	}
}

function EditPublication(publicationId)
{
	var querystring = "";
	var aParams = document.location.search.substr(1).split('&') ; 
	for (i = 0; i< aParams.length; i++)
	{
		if(aParams[i].indexOf("PublicationId") != -1)
		{
			continue;
		}
		querystring = querystring + (querystring == "" ? "?" : "&") + aParams[i];
	}
	querystring = querystring + (querystring == "" ? "?" : "&") + "PublicationId=" + publicationId;
	windowShowModalDialog(_urlPartMountInfo + "/Content/NewEditPublication.aspx" + querystring  ,700,650,true);
//	windowShowModalDialog("NewEditPublication.aspx" + querystring,700,650,true);
	if(!window.showModalDialog)
	{	
		return false;
	}
}

// Handles the Grid double-click event 
function GridDrillDown(row, column)
{
	if(row.Cells[4].Value == 0)
	{
	
		eval(TreeViewPublicationsObject).SelectNodeById(row.Cells[9].Value); 
		eval(TreeViewPublicationsObject).SelectedNode.ExpandAncestors();
	}
	else
	{
		DownloadDocument(row.Cells[10].Value);
	}
}

// Handles the TreeView node select event 
function TreeViewRefresh()
{
	eval(TreeViewCallBackObject).Callback("RELOAD", contextFolderID);
}
// Context menu global variables     
var contextMenuX = 0; 
var contextMenuY = 0; 
var contextFolderID = ""; 
var contextPublicationID = ""; 

// Loads the TreeView context menu through an AJAX-style callback 
function LoadTreeViewContextMenu(treeNode, evt) 
{
	if(treeNode.ID != "-1")
	{
		SetupContextMenuCallbackContainer(eval(MenuTreeViewCallBackObject), evt); 
		contextFolderID = treeNode.ID; 
		contextPublicationID = "";
		// Load the menu from the server, given the directory 
		// (empty string) extension 
		eval(MenuTreeViewCallBackObject).Callback(treeNode.ID, (treeNode.ParentNode == null)); 
    }
}

// Positions the context menu CallBack container
function SetupContextMenuCallbackContainer(menuobj, evt)
{
    evt = (evt == null) ? window.event : evt;
    contextMenuX = evt.pageX ? evt.pageX : evt.x;
    contextMenuY = evt.pageY ? evt.pageY : evt.y;
    contextMenuX += document.body.scrollLeft; 
    contextMenuY += document.body.scrollTop; 
    menuobj.DomElement.style.position = 'absolute'; 
    menuobj.DomElement.style.left = contextMenuX + 'px'; 
    menuobj.DomElement.style.top = contextMenuY + 'px' ; 
}
  
function callbackTreeViewComplete()
{
    if (eval(MenuTreeViewObject).Items().length != 0)
    {
        eval(MenuTreeViewObject).ShowContextMenu(contextMenuX - document.body.scrollLeft, contextMenuY - document.body.scrollTop); 
    }
}   
    
// Forces the treeview to adjust to the new size of its container          
function resizeTree(DomElementId, NewPaneHeight, NewPaneWidth)
{
	if(eval(TreeViewPublicationsObject) && NewPaneWidth && NewPaneWidth > 2 && NewPaneHeight && NewPaneHeight > 33)
	{
		document.getElementById("TreeContainer").style.width = (NewPaneWidth - 2) + 'px';
		document.getElementById("TreeContainer").style.height = (NewPaneHeight - 33) + 'px';

		eval(TreeViewPublicationsObject).Render();
	}
}

// Forces the grid to adjust to the new size of its container          
function resizeGrid(DomElementId, NewPaneHeight, NewPaneWidth)
{        
	if(NewPaneWidth && NewPaneWidth > 2 && NewPaneHeight && NewPaneHeight > 26)
	{
		eval(GridPublicationsObject).DomElement.style.width = (NewPaneWidth - 2) + 'px';
		eval(GridPublicationsObject).DomElement.style.height = (NewPaneHeight - 26) + 'px';
		eval(GridPublicationsObject).Render();
	}
}  
      
// Handles the Menu item select event 
function menuItemSelect(item)
{
	item.ParentMenu.Hide(); 
	if (item.Value) 
	{
		var parameters = item.Value.split('_');
		switch(parameters[0])
		{
			case "NEWFOLDER":
			{
				var newNode = new ComponentArt_TreeViewNode(); 
				newNode.SetProperty('Value', 'NEWFOLDER');
				newNode.SetProperty('Text', '');
				eval(TreeViewPublicationsObject).FindNodeById(contextFolderID).AddNode(newNode);
				eval(TreeViewPublicationsObject).Render();
				newNode.ExpandAncestors();
				newNode.Edit();
				break;
			}
			case "EDITFOLDER":
			{
				eval(TreeViewPublicationsObject).FindNodeById(contextFolderID).Edit();
				break;
			}
			case "DELETEFOLDER":
			{
				if( eval(TreeViewPublicationsObject).FindNodeById(contextFolderID).Value != 0)
				{
					
					message = 'You cannot delete the folder "' + eval(TreeViewPublicationsObject).FindNodeById(contextFolderID).Text + '" because it is not empty.'; 
					alert(message);
					return false;
				}
				else
				{
					message = 'Are you sure that you want to delete the "' + eval(TreeViewPublicationsObject).FindNodeById(contextFolderID).Text + '" folder?'; 
					if(confirm(message))
					{
						eval(TreeViewCallBackObject).Callback("DELETENODE", contextFolderID);
//						eval(GridCallBackObject).Callback("RELOAD",eval(TreeViewPublicationsObject).FindNodeById(contextFolderID).ParentNode.ID);
					}
				}
				break;
			}
			case "NEWDOCUMENT" :
			{
				NewPublication (contextFolderID);
				break;
			}
		}
		return true; 
	}
}
function nodeSelect(theNode)
{
	document.getElementById(hddFolderIdObject).value = theNode.ID;
	PublicationsBrowser1_GridCallBack.Callback("RELOAD", theNode.ID);
}
function nodeRenamed(theNode, theNewLabel)
{
	if(theNewLabel != '')
	{
		var currentFolderId = eval(TreeViewPublicationsObject).SelectedNode.ID;
		theNode.ParentTreeView.EditComplete();
		if(theNode.ID != '')
		{
			eval(TreeViewCallBackObject).Callback("UPDATENODE", theNode.ID, theNewLabel);
		}
		else
		{
			eval(TreeViewCallBackObject).Callback("NEWNODE", theNode.ParentNode.ID, theNewLabel);
		}
		//eval(GridCallBackObject).Callback("RELOAD", currentFolderId);
		return true;
	}
	else
	{
		theNode.Remove();	
		theNode.ParentTreeView.Render();
		return false;
	}
}
	

function downloadPublication(url)
{
	document.getElementById('iframeDownload').src = url;
}

function BeforeGridCallback(theGrid, cancelEventArgs)
{
    if(theGrid.CallbackPrefix == null
            || theGrid.CallbackPrefix == "")
    {
        return;
    }
    var indexAmp = theGrid.CallbackPrefix.indexOf('&');
    var indexQuestionMark = theGrid.CallbackPrefix.indexOf('?');
    if(indexAmp == -1)
    {
        return;
    }
    if(indexQuestionMark == -1
            || indexQuestionMark > indexAmp)
    {
        theGrid.CallbackPrefix = theGrid.CallbackPrefix.substr(0, indexAmp) +
            "?" + theGrid.CallbackPrefix.substr(indexAmp + 1);
    }
}
   document.body.oncontextmenu = function CancelContextMenu(evt)
		{
		evt = (evt == null) ? window.event : evt;
		evt.cancelBubble = true; 
		evt.returnValue = false; 
		return false; 
		}

