
var btnConfirm, btnRunPause, btnBakRestore, btnDelete, btnClone, btnPublish, btnCancel, MaskLayer;
function Page_Init()
    {
	btnConfirm = $('btnConfirm');
	btnRunPause = $('btnRunPause');
	btnBakRestore = $('btnBakRestore');
	btnDelete = $('btnDelete');
	btnClone = $('btnClone');
	btnPublish = $('btnPublish');
	btnRename = $('btnRename');
	btnCancel = $('btnCancel');
	MaskLayer = $('MaskLayer');
    }

var SiteName, SiteInfo={}, SiteSta={};
var Ajax, isOneSite = false;
function Page_Load()
    {	
	SiteName = Common.GetQueryString('sitename');
	if( !SiteName ) return;

	var mode = Common.GetQueryString('mode');//alert(mode);
	if( mode && mode=='attrib'){    //arg && arg.SiteName ){
	    isOneSite = true;
	    btnConfirm.style.display = 'none';
	    $('lkAllSites').style.display = 'none';
	    btnCancel.value = '关闭';
	}
	
	Ajax = new AjaxRequest( 'SiteOp.aspx', RespProc, ErrProc );
	Ajax.Post( 'optype=detail&sitename='+ SiteName );
    }
//	var arg = window.dialogArguments;
//	    SiteName = arg.SiteName;
//	} else{
//	    SiteName = Common.GetQueryString('sitename');
//	    if( !SiteName ) return;

function btnConfirm_Click()
    {
	if( SiteName ){
//	    window.returnValue = {EditSite: SiteName };
	    window.returnValue = { OpType: 'open', SiteName: SiteName };
	    window.close();
	}
    }
function btnCancel_Click()
    {
	if( isOneSite ){
	    window.returnValue = { OpType: 'fresh', SiteName: SiteName, SiteInfo: SiteInfo };
//	    window.returnValue = SiteInfo;
	}
	window.close();
    }
function btnRename_Click()
    {
	if( !Ajax || !SiteName ) {
	    throw new Error('内部错误! 无法执行重命名操作!');
	}
	var arg = { DialogMode: 'RenSite', OldTitle: SiteInfo.title };
	var ret = Common.ShowDialog( 11, '../CommonUI/NewTitle.htm', arg );
	if( ret && ret.title ){
	    ShowWait('重命名站点');
//	    var postText = 'optype=rensite&sitename='+ SiteName +'&newtitle='+ escape(ret.title);
//	    alert( postText );//////////////////////////////////////////////////////////////////
	    var post = new UrlText( ['optype','rensite'], ['sitename', SiteName], ['newtitle', ret.title] );
	    Ajax.Post( post.Text );
	}
    }
function btnDelete_Click()
    {
	if( !Ajax || !SiteName ) {
	    throw new Error('内部错误! 无法执行删除操作!');
	}
	if( confirm('您确认要删除该站点?\n\n注意：删除后无法恢复!') ){
	    ShowWait('删除站点');
	    var post = new UrlText( ['optype','delete'], ['sitename', SiteName] );
	    Ajax.Post( post.Text, null, DeleteProc );
	}
    }
function DeleteProc()
    {
	if( isOneSite && SiteName ){
	    window.returnValue = { OpType: 'close', SiteName: SiteName };
	    window.close();
	} else{
	    location = 'AllSites.htm';
	}
    }
function btnClone_Click()
    {
	if( !Ajax || !SiteName ) {
	    throw new Error('内部错误! 无法执行复制站点操作!');
	}
	var arg = { DialogMode: 'CloneSite', Tips:'' };
	var ret = Common.ShowDialog( 11, '../CommonUI/NewTitle.htm', arg );
	if( ret && ret.name && ret.title ){
	    ShowWait('复制新站点');
	    var post = new UrlText( ['opType','clone'], ['SiteName', SiteName], ['newName', ret.name], ['newTitle', ret.title] );
	    Ajax.Post( post.Text );
	}
    }
function btnRunPause_Click( self )
    {
	if( !SiteSta.RunPusText ) return;

	if( !Ajax || !SiteName ) {
	    throw new Error('内部错误! 无法执行'+ SiteSta.RunPusText +'操作!');
	}
	if( self.postText ){
	    if( SiteSta.AllowEdit && !confirm('您确认要暂停该站点?\n\n注意：暂停后将无法编辑该站点!') ){
	        return;
	    }
	    ShowWait( SiteSta.RunPusText+'站点');
	    Ajax.Post( self.postText );
	}
    }
function btnBakRestore_Click( self )
    {
	if( !SiteSta.BakRstText ) return;
	
	if( !Ajax || !SiteName ) {
	    throw new Error('内部错误! 无法执行'+ SiteSta.BakRstText +'操作!');
	}
	if( self.postText ){
	    if( SiteSta.IsAvail && !confirm('您确认要将该站点转为备份?\n\n注意：转为备份后将无法编辑该站点!') ){
	        return;
	    }
	    ShowWait('备份/还原站点');
	    Ajax.Post( self.postText );
	}
    }
function btnPublish_Click( self )
    {
	if( !Ajax || !SiteName ) {
	    throw new Error('内部错误! 无法执行发布操作!');
	}
	if( SiteSta.IsAvail ){
	    ShowWait('发布站点');
//	    var postText = 'optype=publish&sitename='+ SiteName;
	    var post = new UrlText( ['opType','publish'], ['SiteName', SiteName] );
	    Ajax.Post( post.Text );
	}
    }

function RespProc( jsonText )
    {
	var jsons;
	try{
	    if( !jsonText ) throw new Error();
	    jsons = eval(jsonText);
	    if( !jsons ) throw new Error();
	} catch( e ){ 
	    BreakWait();
	    return;
	}

	var info = null;
	if( jsons instanceof Array ){
	    for( var i=0; i<jsons.length; i++ ){
	        var json = jsons[i];
	        if( json && json.state>=0 && json.state<=6 ){
    	            info = json;
    	            break;
	        }
	    }	
	} else{
	    if( jsons && json.state>=0 && json.state<=6 )
	        info = jsons;
	}
	
	if( info ){
	    if( info.name==SiteName ){
	        FreshInfo( info );
	    } else {
	        window.returnValue = { OpType: 'new', SiteName: info.name, SiteInfo: info };
	        window.close();
	    }
	}
    }
function ErrProc( errMsg )
    {
	alert( '操作出现出错：'+ errMsg );
	BreakWait();
    }

function FreshInfo( info )
    {
	SiteInfo = info;
	SiteSta = WebSiteState[info.state];
	
	BreakWait();
	FreshButton( SiteSta );
	
	document.title = '站点“'+ info.title +'”的详情';
	$('lbSiteTitle').innerText = '站点“'+ info.title +'”的详情';

	$('lbState').innerText = SiteSta.Text;
	if( info.template ){
	    if( info.template=='_Upload' )
	        $('lbTemplate').innerText = '(用户上传)';
	    else
	        $('lbTemplate').innerText = info.template;
	}
	if( info.fullUrl )	$('lbFullUrl').innerHTML = '<a href="'+ info.fullUrl +'" target="_blank">'+ info.fullUrl +'</a>';
	if( info.createTime )	$('lbCreateTime').innerText = '[T]2007年7月14日 14:20:31';
	if( info.modifyTime )	$('lbModifyTime').innerText = '[T]2008年1月11日 8:51:25';

	$('lbDbState').innerText = '[T]正常';
	if( info.dbName )	$('lbDbName').innerText = info.dbName;
	if( info.dbFileSize )	$('lbDbSize').innerText = info.dbFileSize;
	if( info.columnCount )	$('lbColumnCount').innerText = info.columnCount +' 个';
	
	$('lbFdState').innerText = '[T]正常';
	if( info.rootPath )	$('lbServerPath').innerText = info.rootPath;
	if( info.allFileSize )	$('lbFdSize').innerText = info.allFileSize;
	if( info.pageCount )	$('lbPageCount').innerText = info.pageCount +' 个';
	if( info.publishFile ){
	    var pubFile = info.publishFile;
	    pubFile = pubFile.replace( /[\\]+/g, '/' );
	    pubFile = pubFile.replace( /[\/]+/g, '/' );
	    var url = location.href;
	    var urlRg = new RegExp( '^(http://[^\\/?#]*/([^\\/?#]*/)+)SiteManage/', 'i' );
	    if( urlRg.test(url) ){
	        var facUrl = RegExp.$1;
	        var pubUrl = '';
	        if( /^~\//.test( pubFile ) ){
	            pubUrl = pubFile.replace( /^~\//, facUrl );
	        } else{
	            pubUrl = facUrl + pubFile;
	        }
	        var pubName = pubFile;
	        if( /\/([^~\\/]*)$/.test( pubFile ) )
	            pubName = RegExp.$1;
	        $('lbPubBag').innerHTML = '<a href="'+ pubUrl +'" target="_blank">'+ pubName +'</a>';
	    } else	    
	        $('lbPubBag').innerHTML = pubFile;	
	}
	if( info.fileCount )	$('lbFileCount').innerText = '[T]79 个文件、11 个文件夹';
	if( info.publishTime )	$('lbPubTime').innerText = '[T]2008年11月12日 11:24:13';
    }

function FreshButton( sta )
    {
	if( !SiteName ) return;
	
	btnDelete.disabled = false;
	btnRename.disabled = false;
	
	btnConfirm.disabled = true;
	btnClone.disabled = true;
	btnPublish.disabled = true;
	btnRunPause.disabled = true;
	btnBakRestore.disabled = true;
	
	if( sta.AllowEdit )
	    btnConfirm.disabled = false;
	if( sta.IsAvail ){
	    btnClone.disabled = false;
	    btnPublish.disabled = false;
	}
	if( sta.RunPusText ){
	    btnRunPause.disabled = false;
	    btnRunPause.value = sta.RunPusText;
	    btnRunPause.postText = 'optype='+ sta.RunPusOp +'&sitename='+ SiteName;
	}
	if( sta.BakRstText ){
	    btnBakRestore.disabled = false;
	    btnBakRestore.value = sta.BakRstText;
	    btnBakRestore.postText = 'optype='+ sta.BakRstOp +'&sitename='+ SiteName;
	}
    }

function ShowWait( opMsg )
    {
	var maskElem = MaskLayer.childNodes[0];//alert(MaskLayer.childNodes[0].className+'    '+document.documentElement.clientHeight);
	maskElem.style.height = document.documentElement.clientHeight;   ///////////////////////////////
	var msgElem = MaskLayer.childNodes[1].childNodes[1];  //alert(msgElem);
	if( opMsg )
	    msgElem.innerText = ('正在'+ opMsg +'，请稍候...');
	else
	    msgElem.innerText = ('正在进行站点操作，请稍候...');
	MaskLayer.style.display = 'block';
    }
function BreakWait()
    {
	MaskLayer.style.display = 'none';
    }

