var FileTreeFrame, ColumnTreeFrame, MainFrame, FileTab, ColumnTab;
var SiteInfoPanel, BlankInfoPanel, lbSiteTile; //, sltSiteList;     /////////////////////////////////
function Page_Init()
    {
	FileTab   = $('FileTab');
	ColumnTab = $('ColumnTab');
	FileTreeFrame   = $('fileTree');
	ColumnTreeFrame = $('columnTree');
	MainFrame       = $('main');
	SiteInfoPanel  = $('SiteInfoPanel');
	BlankInfoPanel = $('BlankInfoPanel');
	lbSiteTile =  $('lbSiteTile');
//	sltSiteList    = $('sltSiteList');     /////////////////////////////////
//	sltSiteList.onchange = sltSiteList_Click;     /////////////////////////////////
	
	document.onselectstart = function(){ return false; }
	document.oncontextmenu = function(){ return false; }
//	window.onbeforeunload = CloseSiteDialog;
    }
function Page_Resize()
    {
	var w = document.documentElement.clientWidth;
	var h = document.documentElement.clientHeight;
	if( h<240 ) h=240;
	if( w<400 ) w=400;
//	MainFrame.style.width = w-217;
	MainFrame.style.height = h-113;
	FileTreeFrame.style.height = h-133;
	ColumnTreeFrame.style.height = h-133;
    }
function Tab_Click()
    {
	if( this.className == 'NormalTab' ){
	    this.className = 'ActiveTab';
	    this.Panel.style.display = 'block';
	    this.OtherTab.className = 'NormalTab';
	    this.OtherTab.Panel.style.display = 'none';
	}
    }


function ChangePwdDialog()
    {
	var ret = Common.ShowDialog( 1, 'ChangePwd.aspx', null, {width:400, height:220, inRoot: true} );
    }

/* */
var SiteName='', SiteInfo={}, SiteList={};
var isOpened = false, Ajax, RootOpener;
function Page_Load()
    {	
	FileTab.Panel = FileTreeFrame;
	FileTab.OtherTab = ColumnTab;
	FileTab.onclick = Tab_Click.bind( FileTab );
	ColumnTab.Panel = ColumnTreeFrame;
	ColumnTab.OtherTab = FileTab;
	ColumnTab.onclick = Tab_Click.bind( ColumnTab );
	
	SiteName = Common.GetQueryString('sitename');
	var sta = Common.GetQueryString('sta');
	if( SiteName ){
	    _ShowBlank( '正在加载站点...' );
	    
	    Ajax = new AjaxRequest( 'SiteManage/SiteOp.aspx', RespProc, ErrorProc );
	    Ajax.Post( 'optype=detail&sitename='+ SiteName );
	    
	} else{
	    SiteInfoPanel.style.display = 'none';
	    BlankInfoPanel.style.display = 'block';
	    BlankInfoPanel.innerText = '请先打开站点';
	    if( !sta || sta!='blank' ){
	        OpenSiteDialog();
	    }
	}
    }
function RespProc( jsonText )
    {
    	var jsons;
	try{
	    if( !jsonText ) throw new Error();
	    jsons = eval(jsonText);
	    if( !jsons ) throw new Error();
	} catch( e ){ 
	    BreakWait();
	    return;
	}

	SiteInfo = null;
	if( jsons instanceof Array ){	
	    for( var i=0; i<jsons.length; i++ ){
	        var json = jsons[i];
	        if( json && json.name==SiteName && json.state>=0 && json.state<=6 ){
	            SiteInfo = json;
    	            break;
	        }
	    }
	} else{
	    if( jsons && jsons.name==SiteName && json.state>=0 && json.state<=6 ){
	        SiteInfo = jsons;
	    }
	}
	
	if( SiteInfo ){
	    FreshTheSite( SiteInfo );
	    isOpened = true;
	    lbSiteTile.innerText = '“'+ SiteInfo.title +'”站点';
	    /* // 通过 opener 获取已打开站点列表 
	    if( window.opener && window.opener.sltSiteList ){     /////////////////////////////////
    	        var srcList = window.opener.sltSiteList;     /////////////////////////////////
    	        var len = srcList.options.length;
    	        for( var i=0; i<len; i++ ){
    	            // 循环 获取已打开站点，并添加到当前的列表中
    	            var srcOpt = srcList.options[i];    	            
    	            var dstOpt = ListUtil.AddOption( sltSiteList, srcOpt.text, srcOpt.value );     /////////////////////////////////
    	            dstOpt.WinRef = srcOpt.WinRef;    	            
    	            // 循环 添加 打开的站点 到 其他网站列表
    	            if( srcOpt.WinRef && srcOpt.WinRef._AppendSiteToList ){
    	                srcOpt.WinRef._AppendSiteToList( SiteInfo.title, SiteName, window );
    	            }
    	        }
    	    }    
    	    // 添加 打开的站点 到 自己的列表
    	    _AppendSiteToList( SiteInfo.title, SiteName, window );*/
	} else{
	    _ShowBlank( '站点“'+SiteName+'”存在问题，无法打开!' );
	}
    }
function ErrorProc( errMsg )
    {
	_ShowBlank( '站点“'+SiteName+'”存在问题，无法打开!' );
	alert( '加载站点出现问题: '+ errMsg );
    }
/* *****
OpenSiteDialog      OpenTheSite     FreshTheSite    _AppendSiteToList
NewSiteDialog       OpenTheSite     
SiteDetailDialog    OpenTheSite
CloseSiteDialog     CloseTheSite    _CloseSelfSite  _RemoveSiteFromList
_ProcSiteOp         sltSiteList_Click       []SiteList

SiteOp={ OpType: new / open / close / fresh / ... ,
         SiteName: ... , SiteInfo: {},   }
SiteList=[ (siteName_1): {SiteTitle: ... , WindowRef: ... },
           (siteName_2): {SiteTitle: ... , WindowRef: ... }
         ]
*/
function OpenSiteDialog( siteName, sta )
    {
	var ret = Common.ShowDialog( 3, '../SiteManage/AllSites.htm', null, {width:720, height:480, inRoot: true} );
//	var ret = Common.ShowDialog( 13, 'SiteManage/AllSites.htm' );
	_ProcSiteOp( ret );
    }
function NewSiteDialog( siteName, sta )
    {
	var ret = Common.ShowDialog( 3, '../SiteManage/NewSite.aspx', null, {width:720, height:480, inRoot: true} );
//	var ret = Common.ShowDialog( 13, 'SiteManage/NewSite.aspx' );
	_ProcSiteOp( ret );
    }
function SiteDetailDialog()
    {
	if( isOpened && SiteName ){
	    var url = '../SiteManage/SiteDetail.htm?sitename='+ SiteName +'&mode=attrib';
	    var ret = Common.ShowDialog( 3, url, null, {width:720, height:480, inRoot: true} );
//	    var ret = Common.ShowDialog( 13, 'SiteManage/SiteDetail.htm', {SiteName: SiteName} );
	    _ProcSiteOp( ret );
	}
    }
function CloseSiteDialog()
    {
	if( isOpened && SiteName ){
	    if( !confirm('您确认要关闭站点?\n\n(关闭站点不会丢失任何数据)') ){
	        return false;
	    }
	    var op = { OpType:'close', SiteName: SiteName };
	    _ProcSiteOp( op );
	}
    }
function _ProcSiteOp( opObj )
    {
	if( !opObj || !opObj.OpType || !opObj.SiteName )
	    return;
	
	var optype = opObj.OpType.toLowerCase();
	//alert( optype );	
	switch( optype ){
	case'new':
	    OpenTheSite( opObj.SiteName, 'new' );
	    break;
	case'open':
	    OpenTheSite( opObj.SiteName );
	    break;
	case'close':    // 删除站点处理：关闭已删除的站点
	    CloseTheSite( opObj.SiteName );
	    break;
	case'fresh':
	    if( opObj.SiteName==SiteName && opObj.SiteInfo && opObj.SiteInfo.name==SiteName )
	        FreshTheSite( opObj.SiteInfo );
	    break;
	}
    }

/*function sltSiteList_Click()
    {
	var selSiteName = ListUtil.GetSelectedValue( sltSiteList );      /////////////////////////////////
	OpenTheSite( selSiteName );
	ListUtil.SelectByValue( sltSiteList, SiteName );     /////////////////////////////////
    }*/

/* */
function OpenTheSite( siteName, sta )
    {
	if( !siteName ) return;
	
	// 先判断 指定站点是否是当前站点 是则返回
	if( isOpened && SiteName==siteName )
	    return;
	/* // 再判断 指定站点是否已打开，如已打开则直接激活。
	var opt = ListUtil.GetOptionByValue( sltSiteList, siteName );      /////////////////////////////////
	if( opt ){
	    try{
	        opt.WinRef.focus();
	    } catch( ex ){
	        ListUtil.RemoveByValue( sltSiteList, siteName );     /////////////////////////////////
	    }
	    return;
	}*/
	// 如没有打开，则打开
	var url = 'Default.aspx?sitename='+ siteName;
	if( sta ) url += ('&sta='+ sta);
	
	// 判断是否当前页面是否 为空
	if( !isOpened ){    // 为空则直接跳转，并返回当前 window 引用
	    location.replace( url );
	    return window;
	} else{             // 不为空则打开新窗口，并返回 window 引用
	    var winObj = window.open( url );
	    return winObj;
	}
    }
function CloseTheSite( siteName )
    {
	// 先判断 指定站点是否是当前站点 是则直接关闭
	if( isOpened && SiteName==siteName ){
	    _CloseSelfSite();
	}
	/*// 再判断 指定站点是否已打开，如已打开则关闭
	var opt = ListUtil.GetOptionByValue( sltSiteList, siteName );      /////////////////////////////////
	if( opt ){
	    opt.WinRef._CloseSelfSite();
	}*/
    }
function _CloseSelfSite()
    {
	// 显示空环境
	_ShowBlank( '请打开站点' );
	/* // 删除 当前列表 中 自身站点的 option
	_RemoveSiteFromList( SiteName );
	if( sltSiteList.options.length <= 0 ){     /////////////////////////////////
//	    if( RootOpener && RootOpener.alert ){
//	        if( window.close ) window.close();
//	    }
	    location.replace( 'Default.aspx?sta=blank' );
	    return;
	}
	// 循环 当前列表，删除 其他站点 列表 中的引用
	for(  var i=0; i<sltSiteList.options.length; i++ ){     /////////////////////////////////
	    var opt = sltSiteList.options[i];     /////////////////////////////////
	    if( opt && opt.WinRef && opt.WinRef._RemoveSiteFromList ){
	        opt.WinRef._RemoveSiteFromList( SiteName );
	    }
	}
	ListUtil.Clear( sltSiteList );     /////////////////////////////////
	
	// 如是 JS打开的窗口 则关闭窗口
	if( window.opener && window.close )
	    window.close();
	else*/
	    location.replace( 'Default.aspx?sta=blank' );
    }
function FreshTheSite()
    {
	if( SiteInfo && SiteInfo.title ){   // 还要处理站点的状态，不能编辑的要报错
	    _ShowSiteOpened( SiteInfo.title, SiteInfo.fullUrl );
	} else{
	    _ShowBlank( '站点“'+SiteName+'”存在问题，无法打开!' );
	}
    }
/* *
function _AppendSiteToList( sTitle, sName, winRef )
    {
	if( !sltSiteList ) return;     /////////////////////////////////

	var opt = ListUtil.AddOption( sltSiteList, '站点“'+ sTitle +'”', sName );     /////////////////////////////////
	opt.WinRef = winRef;
	if( winRef === window ) opt.selected = true;
    }
function _RemoveSiteFromList( sName )
    {
	if( sltSiteList )     /////////////////////////////////
	    ListUtil.RemoveByValue( sltSiteList, sName );     /////////////////////////////////
    }/* */
function _ShowSiteOpened( sTitle, sUrl )
    {
	FreshFileTree();
	FreshColumnTree();
//	SiteInfoPanel.childNodes[5].href = sUrl;
	document.title = '站点“'+ sTitle +'” - WebIntelli';

	SiteInfoPanel.style.display = 'block';
	BlankInfoPanel.style.display = 'none';
	isOpened = true;
    }
function _ShowBlank( msg )
    {
	isOpened = false;
	FileTreeFrame.src   = '';
	ColumnTreeFrame.src = '';
	MainFrame.src       = '';
	document.title = 'WebIntelli';	
	
	SiteInfoPanel.style.display = 'none';
	BlankInfoPanel.style.display = 'block';
	BlankInfoPanel.innerText = msg;
    }
/* */
function OpenFrame( url )
    {
	MainFrame.src= url;
    }
function FreshFileTree()
    {
	var url = new UrlText( 'FileManage/FileTree.aspx', ['sitename', SiteName], 'uni' );	
	FileTreeFrame.src = url.Text;
    }
function FreshColumnTree()
    {
	var url = new UrlText( 'ColumnManage/ColumnTree.aspx', ['sitename', SiteName], 'uni' );	
	ColumnTreeFrame.src = url.Text;
    }
/* */
