var pageSetting = null;
var portal = null;
var ModuleList = new Array();
ModuleList.push(new Module('profile','个人档案',null,true));
ModuleList.push(new Module('friendList','好友列表',null,true));
ModuleList.push(new Module('groupList','群组列表',null,true));
ModuleList.push(new Module('blog','博客',null,true));
ModuleList.push(new Module('photo','相册',null,true));
ModuleList.push(new Module('music','音乐',null,true));
ModuleList.push(new Module('bookmark','网摘',null,true));
ModuleList.push(new Module('linkBox','自定义列表',null,false));
ModuleList.push(new Module('quickReply','留言板',null,true));
ModuleList.push(new Module('visitor','最近访客',null,true));
ModuleList.push(new Module('comment','最新评论',null,true));
ModuleList.push(new Module('voteBox', '投票箱', null, false));
ModuleList.push(new Module('calendar','日历',null,true));
ModuleList.push(new Module('login','快速登陆',null,true));
ModuleList.push(new Module('customBox','自定义HTML',null,false));
//ModuleList.push(new Module('rssBox','阅读列表(RSS)',null,false));
var GroupModuleList = new Array();
GroupModuleList.push(new Module('profile_group','圈子档案',null,true));
GroupModuleList.push(new Module('webnote','群组公告',null,true));
GroupModuleList.push(new Module('memberList','成员列表',null,true));
GroupModuleList.push(new Module('blog_group','讨论版',null,true));
GroupModuleList.push(new Module('photo','图库',null,true));
GroupModuleList.push(new Module('music','音乐',null,true));
GroupModuleList.push(new Module('bookmark','网摘',null,true));
GroupModuleList.push(new Module('calendar','日历',null,true));
GroupModuleList.push(new Module('voteBox', '投票箱', null, false));
GroupModuleList.push(new Module('linkBox','自定义列表',null,false));
GroupModuleList.push(new Module('visitor','最近访客',null,true));
GroupModuleList.push(new Module('comment','最新评论',null,true));
GroupModuleList.push(new Module('quickReply','留言板',null,true));
GroupModuleList.push(new Module('customBox','自定义HTML',null,false));
function Module(moduleType,title,icon,single)
{
    this.ModuleType = moduleType;
    this.Title = title;
    this.Icon = icon;
    this.Single = single;
}
var Module = new Object();
Module.URL = new Object();
Module.URL.music = 'music';
Module.URL.photo = 'photo';
Module.URL.profile = 'profile';
Module.URL.blog = 'blog';
Module.URL.bookmark = 'bookmark';
Module.URL.blog_group = 'forum';
Module.URL.memberList = 'member';
function InitPortalSetting()
{
    Clozone.Web.WebService.Personalization.GetPortal(prefix,GetPortalSuccess);    
}
function GetPortalSuccess(result)
{
    portal = result;
}
function InitPageSetting()
{
    Clozone.Web.WebService.Personalization.GetPageSetting(prefix, GetPageSettingSuccess);
}
function GetPageSettingSuccess(result)
{
    pageSetting = result;
    WaitForPortal();
}
function WaitForPortal()
{
    if(portal == null)
    {
        setTimeout(WaitForPortal,10);
    }
    else
    {
        BuildPage();
    }
}
function BuildPage()
{
    var divBox = $('divBox');
    divBox.divZones = new Array();
    divBox.divMargins = new Array();
    if(isAdmin)
    {
        var panel = new Panel();
        divBox.parentNode.insertBefore(panel.DivPanel,divBox);
    }    
    divBox.innerHTML = '';
    var zones = pageSetting.Zones;
    for(var i = 0; i < zones.length; i++)
    {
        var zone = new Zone(zones[i]);
        divBox.appendChild(zone.divZone);
        divBox.divZones.push(zone.divZone);
    }
    var divClear = document.createElement('div');
    divClear.style.clear = 'both';
    divBox.appendChild(divClear);
    AdjustZoneMargin();
}
function AdjustZoneMargin()
{
    var divBox = $('divBox');
    for(var i = 0; i < divBox.childNodes.length; i++)
    {
        if(divBox.childNodes[i].className == 'divMargin')
        {
            divBox.removeChild(divBox.childNodes[i]);
        }
    }
    divBox.divMargins.splice(0,divBox.divMargins.length);
    for(var i = 0; i < divBox.divZones.length; i++)
    {
        var divZone = divBox.divZones[i];
        
        if(i != divBox.divZones.length - 1)
        {            
            SetFloatLeft(divZone);
            
            var margin = new Margin();
            var divMargin = margin.DivMargin;
            divBox.insertBefore(divMargin,divZone.nextSibling);
            divBox.divMargins.push(divMargin);
        }
        else
        {
            SetFloatRight(divZone);
        }
    }
}
function Margin()
{
    this.DivMargin = document.createElement('div');
    this.DivMargin.className = 'divMargin';
    this.DivMargin.style.width = '10px';
    this.DivMargin.style.height = '200px';
    SetFloatLeft(this.DivMargin);
    if(isAdmin)
    {
        this.DivMargin.style.cursor = 'e-resize';
        Drag.init(this.DivMargin, this.DivMargin, true);
    }
    this.DivMargin.onDragStart = function(x,y,mousex, mousey)
    {
        this.OriginalX = mousex;
        this.LeftOriginalWidth = this.previousSibling.offsetWidth;
        this.RightOriginalWidth = this.nextSibling.offsetWidth;
        this.style.height = $('divBox').offsetHeight + 'px';        
        this.style.backgroundColor = 'red';
        
        document.body.onselectstart = ReturnFalse;
	}
	this.DivMargin.onDrag = function(x,y, mousex, mousey) 
	{
	    var deltX = mousex - this.OriginalX;
	    if(this.LeftOriginalWidth + deltX < 150 || this.RightOriginalWidth - deltX < 150)
	    {
	        return;
	    }
	    this.previousSibling.style.width = this.LeftOriginalWidth + deltX + 'px';
	    this.nextSibling.style.width = this.RightOriginalWidth - deltX + 'px';
	}
	this.DivMargin.onDragEnd = function(x,y,el) 
	{
	    SaveFormat();	    
	    this.style.backgroundColor = '';
	    
	    document.body.onselectstart = '';
	}
}
function SaveFormat()
{
    var countZones = $('divBox').divZones.length;
    var totalWidth = $('divBox').offsetWidth;
    var w = new Array();
    for(var i = 0; i < countZones; i++)
    {
        var width = $('divBox').divZones[i].offsetWidth;
        w[i] = width / totalWidth + '';
    }    
    Clozone.Web.WebService.Personalization.SetZoneStyle(prefix,countZones,w,ChangeFormatSuccess);
    ShowProcess('正在保存版式，请稍候...');
    ChangeClientFormat(w);
}
function Panel()
{
    this.DivPanel = document.createElement('div');
    this.DivPanel.id = 'divPanel';
    this.BtnModule = document.createElement('div');
    this.BtnModule.innerHTML = '模块';
    this.BtnModule.className = 'btnMenu';
    this.BtnModule.onclick = ToggleModuleBox;
    this.BtnFormat = document.createElement('div');
    this.BtnFormat.innerHTML = '版式';
    this.BtnFormat.className = 'btnMenu';
    this.BtnFormat.onclick = function()
    {
        var html = '<div id="divFormats">';
        for(var i = 1; i <= 6; i++)
        {
            html += '<img id="Format' + i + '" src="http://www.clozone.com/images/Format' + i + '.gif" onclick="ChangeFormat(' + i + ')" style="width:55px;height:69px;cursor:pointer;margin:3px 5px 3px 5px"/>';
        }
        html += '</div>';
        var left = GetOffsetLeft(this);             
        var top = GetOffsetTop(this) + this.offsetHeight;
        TogglePopupMenu(html,left,top,65,0,true,true);
    }
    this.BtnTheme = document.createElement('div');
    this.BtnTheme.innerHTML = '主题';
    this.BtnTheme.className = 'btnMenu';
    this.BtnTheme.onclick = function()
    {
        var html = '<div id="divThemes">';
        for(var i = 1; i <= 5; i++)
        {
            html += '<img id="Theme' + i + '" src="http://www.clozone.com/Themes/Theme' + i + '/thumbnail.gif" onclick="ChangeTheme(\'Theme\' + ' + i + ')" style="width:75px;height:75px;cursor:pointer;margin:5px"/>';
        }
        html += '</div>';
        var left = GetOffsetLeft(this);             
        var top = GetOffsetTop(this) + this.offsetHeight;
        TogglePopupMenu(html,left,top,85,0,true,true);
    }
    this.DivModuleBox = document.createElement('div');
    this.DivModuleBox.id = 'divModuleBox';
    this.DivModuleBox.className = 'divModuleBox';    
    document.body.insertBefore(this.DivModuleBox,document.body.firstChild);
    BuildModuleBox();  
    this.DivPanel.appendChild(this.BtnModule);
    this.DivPanel.appendChild(this.BtnFormat);
    this.DivPanel.appendChild(this.BtnTheme);    
}
var dir = 'left';
var moveTimer = 0;
function ToggleModuleBox()
{
    if(moveTimer != 0)
    {
        clearTimeout(moveTimer);
    }
    if(dir == 'left')
    {
        ShowModuleBox();
        dir = 'right';
    }
    else
    {
        HideModuleBox();
        dir = 'left';
    }
}
function HideModuleBox()
{    
    if($('divModuleBox').offsetLeft > -105)
    {
        $('divModuleBox').style.left =  $('divModuleBox').offsetLeft - 5 + 'px';
        moveTimer = setTimeout(HideModuleBox,10);
    }
}
function ShowModuleBox()
{
    $('divModuleBox').style.top = '200px';
    if($('divModuleBox').offsetLeft < 0)
    {
        $('divModuleBox').style.left = $('divModuleBox').offsetLeft + 5 + 'px';
        moveTimer = setTimeout(ShowModuleBox,10);
    }
}
function BuildModuleBox()
{
    $('divModuleBox').innerHTML = '<div class="divModuleBoxHeader"><div class="divModuleBoxClose" onclick="ToggleModuleBox();"></div></div>';
    var divModuleBoxContent = document.createElement('div');
    divModuleBoxContent.className = 'divModuleBoxContent';
    if(isGroup)
    {
        for(var i = 0; i < GroupModuleList.length; i++)
        {
            if(GroupModuleList[i].Single && ExistModule(GroupModuleList[i].ModuleType))
            {
                continue;
            }
            var moduleItem = new ModuleItem(GroupModuleList[i]);        
            divModuleBoxContent.appendChild(moduleItem.DivModuleItem);
        }
    }
    else
    {
        for(var i = 0; i < ModuleList.length; i++)
        {
            if(ModuleList[i].Single && ExistModule(ModuleList[i].ModuleType))
            {
                continue;
            }
            var moduleItem = new ModuleItem(ModuleList[i]);        
            divModuleBoxContent.appendChild(moduleItem.DivModuleItem);
        }
    }
    $('divModuleBox').appendChild(divModuleBoxContent);
    var divModuleBoxFooter = document.createElement('div');
    divModuleBoxFooter.className = 'divModuleBoxFooter';
    $('divModuleBox').appendChild(divModuleBoxFooter);
    if(window.addEventListener)
    {
        window.addEventListener('scroll', MoveModuleBox, false);
    }
    else if(window.attachEvent)
    {
        window.attachEvent('onscroll', MoveModuleBox);
    }
}
function MoveModuleBox()
{
    $('divModuleBox').style.top = 200 + document.documentElement.scrollTop + 'px'; 
}
function ExistModule(moduleType)
{
    for(var i = 0; i < pageSetting.Zones.length; i++)
    {
        for(var j = 0; j < pageSetting.Zones[i].Parts.length; j++)
        {
            if(pageSetting.Zones[i].Parts[j].ModuleType == moduleType)
            {
                return true;
            }
        }
    }
    return false;
}
function ModuleItem(module)
{
    this.DivModuleItem = document.createElement('div');
    this.DivModuleItem.className = 'divModuleItem';
    this.DivModuleItem.Module= module;
    if(module.Icon != null)
    {
        var icon = document.createElement('img');
        icon.src = module.Icon;
        this.DivModuleItem.appendChild(icon);
    }
    this.DivModuleItem.appendChild(document.createTextNode(module.Title));
    Drag.init(this.DivModuleItem,this.DivModuleItem);
    this.DivModuleItem.onDragStart = function(x,y,mousex, mousey)
    {				
		this.style.width = this.offsetWidth + "px";
		this.style.left = GetOffsetLeft(this) + "px";
	    this.style.top = this.offsetTop + "px";
		this.style.zIndex = 100;
		this.style.position = "absolute";
		partGhost.style.height = this.offsetHeight + 'px';;
		partGhost.out = true;
		
		document.body.onselectstart = ReturnFalse;
		document.body.style.MozUserSelect = 'none';
	}
	this.DivModuleItem.onDrag = function(x,y, mousex, mousey) 
	{
	    var divBox = $('divBox');
	    var zone = null;
	    for(var i = 0; i < divBox.divZones.length; i++)
	    {
	        if((x + this.offsetWidth / 2) >= GetOffsetLeft(divBox.divZones[i]))
	        {
	            zone = divBox.divZones[i];
	        }
	    }
	    if(zone == null)
	    {
	        if(partGhost.parentNode != null)
	        {
	            partGhost.parentNode.removeChild(partGhost);
	            
	        }
	        partGhost.out = true;
	        return;
	    }
	    else
	    {
	        partGhost.out = false;
	    }
		partGhost.zone = zone;
		zone.appendChild(partGhost);
		var partDown = null;
		for(var i = zone.childNodes.length - 1; i >=0 ; i--)
		{
		    if(y < GetOffsetTop(zone.childNodes[i]) && zone.childNodes[i] != partGhost)
		    {
		        partDown = zone.childNodes[i];
		    }
		}
		partGhost.parentNode.removeChild(partGhost);		
		if(partDown == null)
		{
		    zone.appendChild(partGhost);
		}
		else
		{
		    zone.insertBefore(partGhost,partDown);
		}
	}
	this.DivModuleItem.onDragEnd = function(x,y,el) 
	{
	    if(partGhost.out)
        {
            this.style.position = "static";
		    this.style.zIndex = 0;
        }
		else
		{
			var zone = partGhost.zone;
			var zoneIndex = GetZoneIndex(zone);
			var partIndex = GetPartIndex(zone,partGhost);
			var moduleType = this.Module.ModuleType;
			var titleName = this.Module.Title;
			var single = this.Module.Single;
			var p = new Object();
			p.ModuleType = moduleType;
			p.ModuleDataID = 0;
			p.Title = new Object();
			p.Title.Name = titleName;
			p.ContentBody = new Object();
			p.EditBody = new Object();
			pageSetting.Zones[zoneIndex].Parts.splice(partIndex,0,p);
			
			var part = new Part(p);
			partGhost.parentNode.insertBefore(part.divPart,partGhost);
			partGhost.parentNode.removeChild(partGhost);
			if(single)
			{
				this.parentNode.removeChild(this);
			}
			else
			{
				this.style.position = "static";
				this.style.zIndex = 0;
			}
			Clozone.Web.WebService.Personalization.AddPart(prefix,zoneIndex,partIndex,moduleType,titleName,AddPartSuccess);
			ShowProcess('正在添加模块，请稍候...');
		}

		document.body.onselectstart = '';
		document.body.style.MozUserSelect = '';
	}
}
function AddPartSuccess(result)
{  
    HideProcess();
    if(result == false)
    {
        ShowResultBox('添加模块失败，请刷新后重试',false);
    }
}
function ChangeFormat(format)
{
    var w = new Array();
    switch(format)
    {
        case 1:            
            w[0] = '1.0';
            Clozone.Web.WebService.Personalization.SetZoneStyle(prefix,1,w,ChangeFormatSuccess);
            break;
        case 2:
            w[0] = '0.25';
            w[1] = '0.739';
            Clozone.Web.WebService.Personalization.SetZoneStyle(prefix,2,w,ChangeFormatSuccess);
            break;
        case 3:
            w[0] = '0.494';
            w[1] = '0.495';
            Clozone.Web.WebService.Personalization.SetZoneStyle(prefix,2,w,ChangeFormatSuccess);
            break;
        case 4:
            w[0] = '0.739';
            w[1] = '0.25';
            Clozone.Web.WebService.Personalization.SetZoneStyle(prefix,2,w,ChangeFormatSuccess);
            break;
        case 5:
            w[0] = '0.326';
            w[1] = '0.327';
            w[2] = '0.326';
            Clozone.Web.WebService.Personalization.SetZoneStyle(prefix,3,w,ChangeFormatSuccess);
            break;
        case 6:
            w[0] = '0.25';
            w[1] = '0.479';
            w[2] = '0.25';
            Clozone.Web.WebService.Personalization.SetZoneStyle(prefix,3,w,ChangeFormatSuccess);
            break;
    }
    ShowProcess('正在保存版式，请稍候...');
    ChangeClientFormat(w);
}
function ChangeFormatSuccess(result)
{
    HideProcess();
    if(result == true)
    {
    }
    else
    {
        ShowResultBox('保存版式时有错误发生，请刷新后重试',false);
    }
}
function ChangeClientFormat(w)
{
    var zoneCount = w.length;
    var currentZoneCount = pageSetting.Zones.length;
    if(currentZoneCount < zoneCount)
    {
        var zoneCountAdd = zoneCount - currentZoneCount;
        for (var i = 0; i < zoneCountAdd; i++)
        {
            var parts = new Array();
            var zone = new Object();
            zone.Parts = parts;
            zone.WidthPercent = 0;
            pageSetting.Zones.push(zone);
            var divZone = (new Zone(zone)).divZone;
            $('divBox').insertBefore(divZone,$('divBox').lastChild);
            $('divBox').divZones.push(divZone);
        }
    }
    else
    {
        var zoneCountDelete = currentZoneCount - zoneCount;
        for (var i = zoneCount,j = 0; i < currentZoneCount; i++)
        {
            var partCount = pageSetting.Zones[zoneCount].Parts.length;
            for (var k = 0; k < partCount; k++,j++)
            {
                var part = pageSetting.Zones[zoneCount].Parts.splice(0,1);
                var divZone = $('divBox').divZones[zoneCount];
                var divPart = divZone.removeChild(divZone.childNodes[0]);
                pageSetting.Zones[j % zoneCount].Parts.push(part);
                $('divBox').divZones[j % zoneCount].appendChild(divPart);
            }
            pageSetting.Zones.splice(zoneCount,1);
            $('divBox').removeChild($('divBox').divZones[zoneCount]);
            $('divBox').divZones.splice(zoneCount,1);
        }
    }
    for(var i = 0; i < zoneCount; i++)
    {
        ChangeZoneSize($('divBox').divZones[i],w[i]);
    }
    AdjustZoneMargin();
}
function ChangeZoneSize(divZone,widthPercent)
{
    divZone.style.width = $('divBox').offsetWidth * widthPercent + 'px';
    for(var i = 0; i < divZone.childNodes.length; i++)
    {
        var divPart = divZone.childNodes[i];
        if(divPart.module.Resize)
        {
            divPart.module.Resize();
        }
    }
}
function ChangeTheme(theme)
{
    if(theme == portal.Theme)
    {
        return;
    }
    var head = document.getElementsByTagName('head')[0];
    var links = head.getElementsByTagName('link');
    for(var i = 0; i < links.length; i++)
    {
        if(links[i].rel == 'stylesheet' && links[i].type == 'text/css' && links[i].href.indexOf('Themes') != -1)
        {
            head.removeChild(links[i]);
        }
    }
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.type = 'text/css';
    if(Navigator.IE6)
    {
        link.href = 'http://www.clozone.com/Themes/' + theme + '/style_ie6.css';
    }
    else
    {
        link.href = 'http://www.clozone.com/Themes/' + theme + '/style.css';
    }
    head.appendChild(link);
    portal.Theme = theme;
    Clozone.Web.WebService.Personalization.ChangeTheme(prefix,theme,ChangeThemeSuccess);
    ShowProcess('正在保存主题，请稍候...');
}
function ChangeThemeSuccess(result)
{
    HideProcess();
    if(result = false)
    {
        ShowResultBox('保存主题失败，请刷新后重试');
    }
}
function Zone(zone)
{
    this.divZone = document.createElement('div');
    this.divZone.jsObj = this;
    this.divZone.svObj = zone;
    this.divZone.style.width = $('divBox').offsetWidth * zone.WidthPercent + 'px';
	this.divZone.style.overflow = 'hidden';
    if(!document.all)
    {
        this.divZone.style.borderTop = 'solid 1px transparent';
    }
    for(var i = 0; i < zone.Parts.length; i++)
    {
        var part = new Part(zone.Parts[i]);
        this.divZone.appendChild(part.divPart);
    }
}
var partGhost = document.createElement('div');
partGhost.className = 'divPartGhost';
function Part(part)
{
    this.divPart = document.createElement('div');
    this.divPart.jsObj = this;
    this.divPart.svObj = part;
    this.divPart.className = 'divPart';
    var title = new Title(part);
    var contentBody = new ContentBody(part);
    var editBody = new EditBody(part);
    this.divPart.appendChild(title.divTitle);
    this.divPart.appendChild(editBody.divEdit);
    this.divPart.appendChild(contentBody.divContent);
    title.divClose.onclick = Function.createDelegate(this,this.DeletePart);
    title.divEdit.onclick = Function.createDelegate(this,this.ToggleEditBody);
    title.divRefresh.onclick = Function.createDelegate(this,this.Refresh);
    if(isAdmin)
    {
        Drag.init(title.divTitle,this.divPart);
    }
    this.divPart.onDragStart = function(x,y,mousex, mousey)
    {		
		if(this.module.DragStart)
		{
		    this.module.DragStart();
		}	
		
        this.OldZone = this.parentNode;
		this.OldZoneIndex = GetZoneIndex(this.OldZone);
		this.OldPartIndex = GetPartIndex(this.OldZone,this);
				
		this.style.width = this.offsetWidth + "px";
		this.style.left = GetOffsetLeft(this) + "px";
		this.style.top = GetOffsetTop(this) + "px";
		this.style.zIndex = 100;
		
		partGhost.style.height = this.offsetHeight + "px";
		this.parentNode.insertBefore(partGhost, this);
		this.style.position = "absolute";
		partGhost.zone = this.parentNode;
		partGhost.style.display = "block";
		
		document.body.onselectstart = ReturnFalse;
		document.body.style.MozUserSelect = 'none';
	}
	this.divPart.onDrag = function(x,y, mousex, mousey) 
	{	
	    if(mousey < 5)
	    {
	        window.scrollTo(document.documentElement.scrollLeft,document.documentElement.scrollTop - 25);
	    }
	    if(mousey > document.documentElement.clientHeight - 5)
	    {
	        window.scrollTo(document.documentElement.scrollLeft,document.documentElement.scrollTop + 25);
	    }
	    var divBox = $('divBox');
	    var zone = divBox.divZones[0];
	    for(var i = 0; i < divBox.divZones.length; i++)
	    {
	        if((x + this.offsetWidth / 2) >= GetOffsetLeft(divBox.divZones[i]))
	        {
	            zone = divBox.divZones[i];
	        }
	    }
		if((x + this.offsetWidth / 2) < $('divModuleBox').offsetLeft + $('divModuleBox').offsetWidth && y > $('divModuleBox').offsetTop && y < $('divModuleBox').offsetTop + $('divModuleBox').offsetHeight)
		{
		    if(partGhost.parentNode!=null)
		    {
		        partGhost.parentNode.removeChild(partGhost);
		    }
		    partGhost.zone = null;
		    return;
		}
		if (partGhost.zone != zone)
		{
			if(partGhost.parentNode!=null)
		    {
		        partGhost.parentNode.removeChild(partGhost);
		    }
			partGhost.zone = zone;
			zone.appendChild(partGhost);
	    }
		var partDown = null;
		for(var i = zone.childNodes.length - 1; i >=0 ; i--)
		{
		    if(y < GetOffsetTop(zone.childNodes[i]) && zone.childNodes[i] != partGhost)
		    {
		        partDown = zone.childNodes[i];
		    }
		}
		partGhost.parentNode.removeChild(partGhost);		
		if(partDown == null)
		{
		    zone.appendChild(partGhost);
		}
		else
		{
		    zone.insertBefore(partGhost,partDown);
		}
	}
	this.divPart.onDragEnd = function(x,y,el) 
	{	
	    if((x + this.offsetWidth / 2) < $('divModuleBox').offsetLeft + $('divModuleBox').offsetWidth && y > $('divModuleBox').offsetTop && y < $('divModuleBox').offsetTop + $('divModuleBox').offsetHeight)
		{
		    this.jsObj.DeletePart();
		}
		else
		{
			var zone = partGhost.zone;	
			zone.insertBefore(this, partGhost);
			partGhost.parentNode.removeChild(partGhost);		
			this.style.position = "static";
			this.style.width = '100%';
			this.style.zIndex = 0;
			
			var oldZone = this.OldZone;
			var oldZoneIndex = this.OldZoneIndex;
			var oldPartIndex = this.OldPartIndex;
			var newZone = this.parentNode;
			var newZoneIndex = GetZoneIndex(newZone);
			var newPartIndex = GetPartIndex(newZone,this);
			
			//this.childNodes[0].jsObj.UnHighLight();	
			
			oldZone.svObj.Parts.splice(oldPartIndex,1);
			newZone.svObj.Parts.splice(newPartIndex,0,this.svObj);	    
			if(oldZoneIndex != newZoneIndex || oldPartIndex != newPartIndex)
			{
				Clozone.Web.WebService.Personalization.MovePart(prefix,oldZoneIndex,oldPartIndex,newZoneIndex,newPartIndex,MovePartSuccess);
				ShowProcess('正在保存版式信息，请稍候...');
			}
			if(this.module.DragEnd)
			{
				this.module.DragEnd();
			}
			if(oldZoneIndex != newZoneIndex)
			{
				if(this.module.Resize)
				{
					this.module.Resize();
				}
			}
		}

		document.body.onselectstart = '';
		document.body.style.MozUserSelect = '';
	}
    LoadContent(this.divPart); 
}
Part.prototype.DeletePart = function()
{
    if(confirm('确认关闭此模块？') == true)
    {
        var divPart = this.divPart;
        var divZone = divPart.parentNode;
        var zoneIndex = GetZoneIndex(divZone);
        var partIndex = GetPartIndex(divZone,divPart);
        divZone.removeChild(divPart);
        pageSetting.Zones[zoneIndex].Parts.splice(partIndex,1);
        BuildModuleBox();
        Clozone.Web.WebService.Personalization.DeletePart(prefix,zoneIndex,partIndex,Function.createDelegate(this,this.DeletePartSuccess));
        ShowProcess('正在关闭模块，请稍候...');        
    }
}
Part.prototype.DeletePartSuccess = function(result)
{
    HideProcess();
    if(result == false)
    {
        ShowResultBox('关闭模块失败，请刷新页面后重试');
    }
}
Part.prototype.ChangeSetting = function()
{
    var oldTitleName = HtmlDecode(this.divPart.svObj.Title.Name);
    var newTitleName = TrimEnd(this.divPart.childNodes[1].childNodes[0].childNodes[1].value);
    if(oldTitleName != newTitleName)
    {
        this.divPart.childNodes[0].jsObj.ChangeTitleName(newTitleName);
    }
    this.ToggleEditBody();
}
Part.prototype.ToggleEditBody = function(e)
{
    var title = this.divPart.childNodes[0].jsObj;
    if(!this.isEditMode)
    {
        this.isEditMode = true;
        this.divPart.childNodes[1].style.display = 'block';
    }
    else
    {
        this.isEditMode = false;
        this.divPart.childNodes[1].style.display = 'none';
        //title.UnHighLight();
    }   
}
Part.prototype.Refresh = function(e)
{
    if(this.divPart.module.Refresh)
    {
        this.divPart.module.Refresh();
    }
}
Part.prototype.ChangeBgColor = function(titleColor,bodyColor)
{
    var divPart = this.divPart;
    var divZone = divPart.parentNode;
    var zoneIndex = GetZoneIndex(divZone);
    var partIndex = GetPartIndex(divZone,divPart);
    Clozone.Web.WebService.Personalization.ChangeBgColor(prefix,zoneIndex,partIndex,titleColor,bodyColor,Function.createDelegate(this,this.ChangeBgColorSuccess));
    ShowProcess('正在保存背景色，请稍候...');
}
Part.prototype.ChangeBgColorSuccess = function(result)
{
    HideProcess();
    if(result == true)
    {
        this.divPart.childNodes[0].svObj.BackgroundColor = this.divPart.childNodes[0].style.backgroundColor;
        this.divPart.childNodes[2].svObj.BackgroundColor = this.divPart.childNodes[2].style.backgroundColor;
    }
    else
    {
        ShowResultBox('保存标题背景色失败，请重试...');
        this.divPart.childNodes[0].style.backgroundColor = this.divPart.childNodes[0].svObj.BackgroundColor;
        this.divPart.childNodes[2].style.backgroundColor = this.divPart.childNodes[2].svObj.BackgroundColor;
    }
}
function Title(part)
{
    var title = part.Title;
    this.divTitle = document.createElement('div');
    this.divTitle.jsObj = this;
    this.divTitle.svObj = title;
    this.divTitle.className = 'divPartTitle';
    var table = document.createElement('table');
    table.border = '0';
    table.cellPadding = '0';
    table.cellSpacing = '0';
    table.style.width = '100%';
    var row = table.insertRow(0);
    var cellLeft = row.insertCell(0);
    cellLeft.className = 'titleLeft';
    var cellCenter = row.insertCell(1);
    cellCenter.className = 'titleCenter';
    var cellRight = row.insertCell(2);
    cellRight.className = 'titleRight';
    this.divTitle.appendChild(table);
    
    this.aTitle = document.createElement('a');
    this.aTitle.innerHTML = title.Name;
    if(Module.URL[part.ModuleType])
    {
        this.aTitle.href = 'http://' + prefix + '.clozone.com/' + Module.URL[part.ModuleType];
        this.aTitle.onmousedown = CancelBubble;
        
    }
    this.aTitle.className = 'aTitle';
    this.divRefresh = document.createElement('div');
    this.divRefresh.onmousedown = CancelBubble;
    this.divRefresh.style.cursor = 'pointer';
    //this.divRefresh.style.visibility = 'hidden';
    this.divRefresh.className = 'divRefresh';
    this.divRefresh.title = '刷新';
    SetFloatRight(this.divRefresh);
    this.divEdit = document.createElement('div');
    this.divEdit.onmousedown = CancelBubble;
    this.divEdit.style.cursor = 'pointer';
    //this.divEdit.style.visibility = 'hidden';
    this.divEdit.className = 'divEdit';
    this.divEdit.title = '定制';
    SetFloatRight(this.divEdit);
    this.divClose = document.createElement('div');
    this.divClose.onmousedown = CancelBubble;    
    this.divClose.style.cursor = 'pointer';
    //this.divClose.style.visibility = 'hidden';
    this.divClose.className = 'divClose';
    this.divClose.title = '关闭';
    SetFloatRight(this.divClose);    
    if(isAdmin)
    {
        //this.divTitle.onmouseover = Function.createDelegate(this,this.HighLight);
        //this.divTitle.onmouseout = Function.createDelegate(this,this.UnHighLight);
        this.divTitle.onmouseover = function(){this.style.cursor = "move";}
        cellCenter.appendChild(this.divClose);
        cellCenter.appendChild(this.divEdit);
        cellCenter.appendChild(this.divRefresh);
    }
    cellCenter.appendChild(this.aTitle);
}
/*Title.prototype.HighLight = function()
{
    if(this.divTitle.parentNode.jsObj.isEditMode == true)
    {
        return;
    }
    this.divTitle.style.cursor = 'move';
    this.divRefresh.style.visibility = 'visible';
    this.divEdit.style.visibility = 'visible';
    this.divClose.style.visibility = 'visible';
}
Title.prototype.UnHighLight = function()
{
    if(this.divTitle.parentNode.jsObj.isEditMode == true)
    {
        return;
    }
    this.divTitle.style.cursor = 'default';
    this.divRefresh.style.visibility = 'hidden';
    this.divEdit.style.visibility = 'hidden';
    this.divClose.style.visibility = 'hidden';
}*/
Title.prototype.ChangeTitleName = function(newValue)
{
    var divPart = this.divTitle.parentNode;
    var divZone = divPart.parentNode;
    var zoneIndex = GetZoneIndex(divZone);
    var partIndex = GetPartIndex(divZone,divPart);
    this.aTitle.innerHTML = HtmlEncode(newValue);
    Clozone.Web.WebService.Personalization.ChangeTitleName(prefix,zoneIndex,partIndex,newValue,Function.createDelegate(this,this.ChangeTitleNameSuccess));
    ShowProcess('正在保存标题，请稍候...');
}
Title.prototype.ChangeTitleNameSuccess = function(result)
{
    HideProcess();
    if(result == true)
    {
        this.divTitle.svObj.Name = this.aTitle.innerHTML;
    }
    else
    {
        ShowResultBox('保存标题失败，请重试...');
        this.aTitle.innerHTML = this.divTitle.svObj.Name;
    }
}
function ContentBody(part)
{
    this.divContent = document.createElement('div');
    this.divContent.className = 'divPartContent';
    this.divContent.innerHTML = '加载中...';
    if(part.ContentBody.BackgroundColor != null && part.ContentBody.BackgroundColor != '')
    {
        this.divContent.style.backgroundColor = part.ContentBody.BackgroundColor;
        this.divContent.style.backgroundImage = 'url("")';
        this.divContent.style.borderWidth = '0px';
    }
    this.divContent.jsObj = this;
    this.divContent.svObj = part.ContentBody;
}
function EditBody(part)
{
    this.divEdit = document.createElement('div');
    this.divEdit.className = 'divPartEdit';
    this.divEdit.style.display = 'none';
    this.divEdit.jsObj = this;
    this.divEdit.svObj = part.EditBody;
    
    var txtTitleName = document.createElement('input');
    txtTitleName.className = 'txt';
    txtTitleName.style.width = '100px';
    txtTitleName.value = part.Title.Name;
    var div = document.createElement('div');
    div.appendChild(document.createTextNode('模块名称：'));
    div.appendChild(txtTitleName);
    this.divEdit.appendChild(div);
}
EditBody.prototype.SetTitleRed = function(value)
{
    var red = DecimalToHex(Math.floor(value * 2.58));
    var green = DecimalToHex(Math.floor(this.titleSlideGreen.value * 2.58));
    var blue = DecimalToHex(Math.floor(this.titleSlideBlue.value * 2.58));
    this.divEdit.previousSibling.style.backgroundColor = this.txtTitleColor.value = '#' + red + green + blue;
}
EditBody.prototype.SetTitleGreen = function(value)
{
    var red = DecimalToHex(Math.floor(this.titleSlideRed.value * 2.58));
    var green = DecimalToHex(Math.floor(value * 2.58));
    var blue = DecimalToHex(Math.floor(this.titleSlideBlue.value * 2.58));
    this.divEdit.previousSibling.style.backgroundColor = this.txtTitleColor.value = '#' + red + green + blue;
}
EditBody.prototype.SetTitleBlue = function(value)
{
    var red = DecimalToHex(Math.floor(this.titleSlideRed.value * 2.58));
    var green = DecimalToHex(Math.floor(this.titleSlideGreen.value * 2.58));
    var blue = DecimalToHex(Math.floor(value * 2.58));
    this.divEdit.previousSibling.style.backgroundColor = this.txtTitleColor.value = '#' + red + green + blue;
}
EditBody.prototype.SetBodyRed = function(value)
{
    var red = DecimalToHex(Math.floor(value * 2.58));
    var green = DecimalToHex(Math.floor(this.bodySlideGreen.value * 2.58));
    var blue = DecimalToHex(Math.floor(this.bodySlideBlue.value * 2.58));
    this.divEdit.nextSibling.style.backgroundColor = this.txtBodyColor.value = '#' + red + green + blue;
    this.divEdit.nextSibling.style.backgroundImage = 'url("")';
    this.divEdit.nextSibling.style.borderWidth = '0px';
}
EditBody.prototype.SetBodyGreen = function(value)
{
    var red = DecimalToHex(Math.floor(this.bodySlideRed.value * 2.58));
    var green = DecimalToHex(Math.floor(value * 2.58));
    var blue = DecimalToHex(Math.floor(this.bodySlideBlue.value * 2.58));
    this.divEdit.nextSibling.style.backgroundColor = this.txtBodyColor.value = '#' + red + green + blue;
    this.divEdit.nextSibling.style.backgroundImage = 'url("")';
    this.divEdit.nextSibling.style.borderWidth = '0px';
}
EditBody.prototype.SetBodyBlue = function(value)
{
    var red = DecimalToHex(Math.floor(this.bodySlideRed.value * 2.58));
    var green = DecimalToHex(Math.floor(this.bodySlideGreen.value * 2.58));
    var blue = DecimalToHex(Math.floor(value * 2.58));
    this.divEdit.nextSibling.style.backgroundColor = this.txtBodyColor.value = '#' + red + green + blue;
    this.divEdit.nextSibling.style.backgroundImage = 'url("")';
    this.divEdit.nextSibling.style.borderWidth = '0px';
}
function DecimalToHex(dec)
{
    var h = Math.floor(dec / 16);
    var l = dec % 16;
    return GetHexNum(h) + GetHexNum(l);
}
function GetHexNum(n)
{
    if(n <= 9)
    {
        return n + '';
    }
    else
    {
        switch(n)
        {
            case 10:
                return 'A';
            case 11:
                return 'B';
            case 12:
                return 'C';
            case 13:
                return 'D';
            case 14:
                return 'E';
            default:
                return 'F';         
        }
    }
}
function GetZoneIndex(divZone)
{
    var divBox = $('divBox');
    for(var i = 0; i < divBox.divZones.length; i++)
	{
	    if(divZone == divBox.divZones[i])
	    {
	        return i;
	    }
	}
	return -1;
}
function GetPartIndex(divZone,divPart)
{
    for(var i = 0; i < divZone.childNodes.length; i++)
	{
	    if(divPart == divZone.childNodes[i])
	    {
	        return i;
	    }
	}
	return -1;
}
function MovePartSuccess(result)
{
    HideProcess();
    if(result == false)
    {
        ShowResultBox('保存版式信息时发生错误，请刷新页面后重试',false);
    }
}
function LoadContent(divPart)
{
    var moduleType = divPart.svObj.ModuleType;
    switch(moduleType)
    {
        case 'linkBox':
            new PartScript('http://www.clozone.com/Javascript/Module/LinkBox.js','LinkBox',divPart);
            break;
        case 'login':
            new PartScript('http://www.clozone.com/Javascript/Module/Login.js','Login',divPart);
            break;
        case 'music':
            new PartScript('http://www.clozone.com/Javascript/Module/Music_Flash.js','Music',divPart);
            break;
        case 'visitor':
            new PartScript('http://www.clozone.com/Javascript/Module/Visitor.js','Visitor',divPart);
            break;
        case 'photo':
            new PartScript('http://www.clozone.com/Javascript/Module/Photo.js','Photo',divPart);
            break;
        case 'profile':
            new PartScript('http://www.clozone.com/Javascript/Module/Profile.js','Profile',divPart);
            break;  
        case 'blog':
            AddScript('http://www.clozone.com/Javascript/MutiCommentContainer.js');
            new PartScript('http://www.clozone.com/Javascript/Module/Blog.js','Blog',divPart);
            break;  
        case 'blog_group':
            new PartScript('http://www.clozone.com/Javascript/Module/Blog_Group.js','Blog_Group',divPart);
            break; 
        case 'bookmark':
            AddScript('http://www.clozone.com/Javascript/Bookmark.js');
            AddScript('http://www.clozone.com/Javascript/MutiCommentContainer.js');
            new PartScript('http://www.clozone.com/Javascript/Module/Bookmark.js','Bookmark',divPart);
            break;  
        case 'quickReply':
            new PartScript('http://www.clozone.com/Javascript/Module/QuickReply.js','QuickReply',divPart);
            break; 
        case 'comment':
            new PartScript('http://www.clozone.com/Javascript/Module/Comment.js','Comment',divPart);
            break;
        case 'profile_group':
            AddScript('http://www.clozone.com/Javascript/InviteUser.js');
            new PartScript('http://www.clozone.com/Javascript/Module/Profile_Group.js','Profile_Group',divPart);
            break;   
        case 'webnote':
            AddScript('http://www.clozone.com/FCKEditor/FCKEditor.js');
            new PartScript('http://www.clozone.com/Javascript/Module/Webnote.js','Webnote',divPart);
            break;
        case 'voteBox':
            new PartScript('http://www.clozone.com/Javascript/Module/VoteBox.js','VoteBox',divPart);
            break;
        case 'calendar':
            AddScript('http://www.clozone.com/Javascript/NCalendar.js');
            new PartScript('http://www.clozone.com/Javascript/Module/Calendar.js','Calendar',divPart);
            break;
        case 'memberList':
            AddScript('http://www.clozone.com/Javascript/InviteUser.js');
            new PartScript('http://www.clozone.com/Javascript/Module/MemberList.js','MemberList',divPart);
            break;
        case 'friendList':
            AddScript('http://www.clozone.com/Javascript/AddFriend.js');
            new PartScript('http://www.clozone.com/Javascript/Module/FriendList.js','FriendList',divPart);
            break;
        case 'groupList':
            AddScript('http://www.clozone.com/Javascript/JoinGroup.js');
            new PartScript('http://www.clozone.com/Javascript/Module/GroupList.js','GroupList',divPart);
            break;
        case 'customBox':
            new PartScript('http://www.clozone.com/Javascript/Module/CustomBox.js','CustomBox',divPart);
            break;
        default:
            break;
    }
}
function PartScript(src,method,divPart)
{
    this.Src = src;
    this.Method = method;
    this.DivPart = divPart;
    setTimeout(Function.createDelegate(this,this.StartLoad),0);
}
PartScript.prototype.StartLoad = function()
{
    new AddScript(this.Src,Function.createDelegate(this,this.LoadDone));
}
var same = 0;
PartScript.prototype.LoadDone = function()
{         
    if(Module[this.Method])
    {
        this.DivPart.module = new Module[this.Method](prefix,this.DivPart);
    }
}
var Drag = new Object();
Drag.obj = null;
Drag.init = function(a, aRoot,horizontal)
{
    a.onmousedown=Drag.start;
	a.root = aRoot;
	if(isNaN(parseInt(a.root.style.left)))a.root.style.left="0px";
	if(isNaN(parseInt(a.root.style.top)))a.root.style.top="0px";
	a.root.onDragStart=new Function();
	a.root.onDragEnd=new Function();
	a.root.onDrag=new Function();
	a.root.horizontal=horizontal;
}
Drag.start = function(a)
{
	var b=Drag.obj=this;
	if(document.all)
	{
	    a=window.event;
	}
	var c=parseInt(b.root.style.top);
	var d=parseInt(b.root.style.left);
	b.root.onDragStart(d,c,a.clientX,a.clientY);
	b.lastMouseX=a.clientX + document.documentElement.scrollLeft;
	b.lastMouseY=a.clientY + document.documentElement.scrollTop;
	document.onmousemove=Drag.drag;
	document.onmouseup=Drag.end;
}
Drag.drag = function(a)
{
	if(document.all)
	{
	    a=window.event;
	}
	var b=Drag.obj;
	var c=a.clientY + document.documentElement.scrollTop;
	var d=a.clientX + document.documentElement.scrollLeft;
	var e=parseInt(b.root.style.top);
	var f=parseInt(b.root.style.left);
	var h,g;
	h=f+d-b.lastMouseX;
	g=e+c-b.lastMouseY;
	b.root.style.left=h+"px";
	if(!b.root.horizontal)
	{
	    b.root.style.top=g+"px";
	}
	b.lastMouseX=d;
	b.lastMouseY=c;
	g = GetOffsetTop(b.root);
	h = GetOffsetLeft(b.root);
	b.root.onDrag(h,g,a.clientX,a.clientY);
}
Drag.end = function()
{
	document.onmousemove=null;
	document.onmouseup=null;
	Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style.left),parseInt(Drag.obj.root.style.top));
	Drag.obj=null;
}
