
var RootMenu   = new cPopupMenu( window.parent, null, 'Style/Menu.css' );
var FolderMenu = new cPopupMenu( window.parent, null, 'Style/Menu.css' );
var PageMenu   = new cPopupMenu( window.parent, null, 'Style/Menu.css' );
var HtmlMenu   = new cPopupMenu( window.parent, null, 'Style/Menu.css' );
var CssMenu    = new cPopupMenu( window.parent, null, 'Style/Menu.css' );
var CommonMenu = new cPopupMenu( window.parent, null, 'Style/Menu.css' );

var Ajax, siteName, filePath, fileName;

function Page_load()	//用户登录 系统启动
    { 
	siteName = Common.GetQueryString('sitename');
	if( !siteName ) return;
	
	if( !self.parent.OpenFrame ) return;
	    window.OpenFrame = self.parent.OpenFrame;
	
	var fileMenuObj = [
//	    {type:'divider'},
	    {type:'link', method:'function', value:'预览(V)', funcRef: FileOperate, parames:['FilePreview'] },
	    {type:'divider'},
	    {type:'link', method:'function', value:'复制(T)', funcRef: FileOperate, parames:['CopyFile'] },
	    {type:'link', method:'function', value:'剪切(C)', funcRef: FileOperate, parames:['CutFile'] },
	    {type:'divider'},
	    {type:'link', method:'function', value:'删除(D)', funcRef: FileOperate, parames:['DelFile'] },
	    {type:'link', method:'function', value:'重命名(M)', funcRef: FileOperate, parames:['RenFile'] }
//	    {type:'divider'},
//	    {type:'link', method:'function', value:'属性(R)', funcRef: FileOperate, parames:[''] }
	];
	var folderMenuObj = [
	    {type:'divider'},
	    {type:'link', method:'function', value:'复制(T)', funcRef: FileOperate, parames:['CopyFolder'] },
	    {type:'link', method:'function', value:'剪切(C)', funcRef: FileOperate, parames:['CutFolder'] },
	    {type:'link', method:'function', value:'粘贴(P)', funcRef: FileOperate, parames:['Paste'] },
	    {type:'divider'},
	    {type:'link', method:'function', value:'删除(D)', funcRef: FileOperate, parames:['DelFolder'] },
	    {type:'link', method:'function', value:'重命名(M)', funcRef: FileOperate, parames:['RenFolder'] }
//	    {type:'divider'},
//	    {type:'link', method:'function', value:'属性(R)', funcRef: FileOperate, parames:[''] }
	];

	RootMenu.AddItem( 'secHead', {value:'站点'});
//	RootMenu.AddItem( 'link', {method:'function', value:'新建页面(N)...', funcRef: FileOperate, parames:['AddPage'] });
	RootMenu.AddItem( 'link', {method:'function', value:'新建子文件夹(F)', funcRef: FileOperate, parames:['AddFolder'] });
	RootMenu.AddItem( 'link', {method:'function', value:'上传文件(U)...', funcRef: FileOperate, parames:['UploadFile'] });
	RootMenu.AddItem( 'divider' );
	RootMenu.AddItem( 'link', {method:'function', value:'粘贴(P)', funcRef: FileOperate, parames:['Paste'] });
//	RootMenu.AddItem( 'divider' );
//	RootMenu.AddItem( 'link', {method:'function', value:'属性(R)', funcRef: FileOperate, parames:[''] });
	       
	FolderMenu.AddItem( 'secHead', {value:'文件夹'});
//	FolderMenu.AddItem( 'link', {method:'function', value:'新建页面(N)...', funcRef: FileOperate, parames:['AddPage'] });
	FolderMenu.AddItem( 'link', {method:'function', value:'新建子文件夹(F)', funcRef: FileOperate, parames:['AddFolder'] });
	FolderMenu.AddItem( 'link', {method:'function', value:'上传文件(U)...', funcRef: FileOperate, parames:['UploadFile'] });
	FolderMenu.AddItemByJson( folderMenuObj );

	PageMenu.AddItem( 'secHead', {value:'动态页面'});
	PageMenu.AddItem( 'link', {method:'function', value:'编辑动态页面(E)...',isBold:true,funcRef: FileOperate, parames:['PageEdit'] });
	PageMenu.AddItem( 'link', {method:'function', value:'预览(V)...', funcRef: FileOperate, parames:['FilePreview'] });
	PageMenu.AddItem( 'link', {method:'function', value:'刷新(F)', funcRef: FileOperate, parames:['FreshPage'] });
	PageMenu.AddItem( 'divider' );
	PageMenu.AddItem( 'link', {method:'function', value:'复制动态页面(O)', funcRef: FileOperate, parames:['ClonePage'] });
	PageMenu.AddItem( 'divider' );
	PageMenu.AddItemByJson( fileMenuObj );
	PageMenu.AddItem( 'divider' );
	PageMenu.AddItem( 'link', {method:'function', value:'属性(R)', funcRef: FileOperate, parames:['PageDetail'] });

	HtmlMenu.AddItem( 'secHead', {value:'Html页面'});
	HtmlMenu.AddItem( 'link', {method:'function', value:'编辑Html页面(E)...',isBold:true,funcRef: FileOperate, parames:['HtmlEdit'] });
	HtmlMenu.AddItem( 'link', {method:'function', value:'预览(V)...', funcRef: FileOperate, parames:['FilePreview'] });
	HtmlMenu.AddItem( 'link', {method:'function', value:'转换为动态页面(D)', funcRef: FileOperate, parames:['Convert'] });
	HtmlMenu.AddItem( 'divider' );
	HtmlMenu.AddItemByJson( fileMenuObj );

	CssMenu.AddItem( 'secHead', {value:'样式文件'});
	CssMenu.AddItem( 'link', {method:'function', value:'编辑样式文件(E)...',isBold:true,funcRef: FileOperate, parames:['CssEdit'] });
	CssMenu.AddItem( 'divider' );
	CssMenu.AddItemByJson( fileMenuObj );
	
	CommonMenu.AddItem( 'secHead', {value:'一般文件'});
	CommonMenu.AddItemByJson( fileMenuObj );
	
	Ajax = new AjaxRequest( 'FileOp.aspx', FreshTree, FileOp_Err );
	
	document.oncontextmenu = function(){ return false; };
	document.onselectstart = function(){ return false; };
    }   

function ShowMenu( menu )
    {
	filePath = event.srcElement.id;// escape( event.srcElement.id );
	fileName = event.srcElement.value;
	menu.Show( event.x+8, event.y+130 );
	return false;
    }

// 可编辑文件节点的双击事件
function File_OnDblClick( op )
    {
	filePath = event.srcElement.id;// escape( event.srcElement.id );
	fileName = event.srcElement.value;
	FileOperate( op );
    }

function FileOperate( op )
    {
	if( !filePath ) return;

	var SiteUrl = $('hfSiteUrl').value;
	var post = new UrlText( ['optype', op], ['sitename', siteName], ['path', filePath] );

	switch( op ){
	/******** 文件夹/文件 操作 ********/
	case'UploadFile':	// 上传文件(U) // Dialog -- Fresh
	    var url = new UrlText( 'Upload.aspx', ['sitename', siteName], ['dstpath', filePath], 'uni' );
	    var ret = Common.ShowDialog( 11, url.Text );
	    if( /^ok/i.test( ret ) )//ret == 'ok' )
	        FreshTree();
	    break;

	case'AddFolder':	// 新建文件夹(F) // Ajax -- Fresh
	case'Convert':		// 转换为页面(N) // Ajax
	case'ClonePage':	// 复制动态页面(O) // Ajax
	    Ajax.Post( post.Text );
	    break;
	    
	case'FreshPage':	// 刷新动态页面(F) // Ajax
	    Ajax.Post( post.Text );
	    break;

	case'DelFolder':	// 删除(D) // Confirm? -- Ajax	
	    if( confirm('您确认要删除文件夹"'+ fileName +'"吗?') ){
	        Ajax.Post( post.Text );
	    }
	    break;

	case'DelFile':		// 删除(D) // Confirm? -- Ajax
	    if( confirm('您确认要删除文件"'+ fileName +'"吗?') ){
	        Ajax.Post( post.Text );
	    }
	    break;
	    
	case'RenFolder':	// 重命名(M) // Common Dialog -- Ajax
	case'RenFile':		// 重命名(M) // Common Dialog -- Ajax	    
	    var ret = Common.ShowDialog( 11, '../CommonUI/NewTitle.htm', {DialogMode:op, OldTitle:fileName} );
	    if( ret && ret.title ){
	        post.Add( ['newname', ret.title] );
	        Ajax.Post( post.Text );
	    }
	    break;

	case'AddPage':		// 新建页面(N) // dialog
//	    url= "CreateNewPage.aspx?DestPath=" + filePath +'&unique='+ Common.UniqueValue();
//	    var FileName = Common.ShowDialog( 11, url );//topwin(url);
//	    if( FileName )
//	        FreshTree();
	    break;
	    
	case'PageDetail':		// 页面属性(R) // dialog
	    var url = new UrlText( '../Editor/PageDetail.aspx', ['SiteName', siteName], ['PagePath', filePath], 'uni' );

	    var sMsg = Common.ShowDialog( 1, url.Text, null, {width:400,height:500} );
	    if ( /^ok/i.test(sMsg) )
	        ;//btnShowPage_Click();
	    break;
	    
	/******** 单独打开操作 ********/
	
	case 'PageEdit':	// 编辑页面(E)... // frame
	    var url = new UrlText( 'Editor/PageEditor.aspx', ['sitename', siteName], ['path', filePath], 'uni'  );
	    OpenFrame( url.Text );
	    break;
	    
	case 'HtmlEdit':	// 编辑Html文件(E)... // frame
	    var url = new UrlText( 'Editor/HTMLEditor.aspx', ['sitename', siteName], ['path', filePath], 'uni'  );
	    OpenFrame( url.Text );
	    break;
	    
	case 'CssEdit':		// 编辑样式文件(E)... // frame
	    var url = new UrlText( 'Editor/CssEditor.aspx', ['sitename', siteName], ['path', filePath], 'uni' );
	    OpenFrame( url.Text );
	    break;

	case'FilePreview':	// 预览(V)... // frame
	    var file = new UrlText( SiteUrl, filePath );
	    window.open( file.Text );
	    break;

	/******** 复制/粘贴 操作 ********/
	case'CopyFile': 	// 复制(T)
	    Clipboard.Copy( {path:filePath, name:fileName}, 'file');
	    break;
	    
	case'CutFile':  	// 剪切(C)
	    Clipboard.Cut( {path:filePath, name:fileName}, 'file');
	    break;
	    
	case'CopyFolder':	// 复制(T)
	    Clipboard.Copy( {path:filePath, name:fileName}, 'folder');
	    break;
	    
	case'CutFolder':	// 剪切(C)
	    Clipboard.Cut( {path:filePath, name:fileName}, 'folder');
	    break;

	case'Paste':		// 粘贴(P) // Ajax
	    var psObj = Clipboard.Paste();
	    if( !psObj || !psObj.data || !psObj.dataType ) return;

	    var psType = psObj.type;
	    var psDType = psObj.dataType;
	    
	    if( psDType == 'folder' ){
	        var msg = '可能覆盖文件夹"'+ fileName +'"中同名文件夹或文件!\n\n您确认要继续该操作?';
	        if( !confirm( msg ) ) return;
	    }
	    
	    post.Clear();
	    if( psType=='Cut' ){
	    
	        if( psDType=='folder' )    post.Add( ['optype', 'movefolder'] );
	        else if( psDType=='file' ) post.Add( ['optype', 'movefile'] );

	    } else if( psType=='Copy' ){
	    	        
	        if( psDType=='folder' )    post.Add( ['optype', 'copyfolder'] );
	        else if( psDType=='file' ) post.Add( ['optype', 'copyfile'] );	    
	    }
	    if( !post.IsBlank ){
	        post.Add( ['SiteName', siteName], ['srcPath', psObj.data.path], ['srcName', psObj.data.name], ['dstPath', filePath] );
	        Ajax.Post( post.Text );
	    }
	    break;
	}
    }

function FreshTree()
    {
	window.location.reload( true );
    }
function FileOp_Err( msg )
    {
	alert( '进行文件操作出错：'+msg );
    }