var mozilla = (navigator.userAgent.toLowerCase().indexOf("msie") == -1);
var ModuleSettingLoadder=Class.create();
ModuleSettingLoadder.prototype={
	initialize:function(config){
		var mods=this.getModuleSettings();
		for(var i=0;i<config.length;i++){
			if(mods[config[i]])
				new mods[config[i]]();
		
		}
	},
	getModuleSettings:function(){
		return window.moduleSettings;
	}	
}

var ModSetting = Class.create();

ModSetting.prototype = {
  initialize: function() {
  	this.END_POINT = '/control/control.php';
  	//this.id="t";
  	this.clickId="t";
  	this.width=200;
  	this.height=150;
  	//this.id="";
  	this.modId="";
  	this.modContentId="";
    this.session_id=window.session_id;
  	this.op="bloghome.setting.saveSetting";
  	this.title="模块设置";
  	this.status="";
  	//标题模块数组
  	this.modTitleElement=[];
  	this.params={user_id:window.user_id};
  	//正在加载?
  	this.loading=false;
  	this.init();  
  	  
  },
  init:function(){
  	this.initModSetting();
  },
  _createUI:function(){
  	this.createUI();
  },
  createUI:function(){  	
  	var div=document.createElement("div");
  	div.className="popLayer";
  	div.id="modSetting";

  	div.style.display="none";
  	div.style.width=this.width+"px";
  	div.style.zIndex=10000;
  	div.style.position="absolute";
	var html='<div class="popTop"><div class="topLeft"></div><div class="topRight"></div><div class="topLine"></div></div>';
	html+='<div class="popMain" style="*height:100%"><div class="contentDiv" id="modSettingContent" style="*height:100%">';
	
	html+='<div id="nameSetDiv"><label style="float:left;width:70px;">模块名称</label><input type="text" value="" id="modSetName" size="8" style="float:left;width:100px"></div>';
	html+='<br clear="all" />';
	html+='<div id="orderSetDiv" style="display:none"><label style="float:left;width:70px">显示顺序</label><select id="modSetOrder" style="float:left;width:100px"></select></div>';
	html+='<br clear="all" />';
	html+='<div id="numSetDiv" style="display:none"><label style="float:left;width:70px">显示数量</label><input type="text" id="modSetNum" style="float:left;width:100px"></div>';
	html+='<br clear="all" />';
	html+='<div id="removeSetDiv"><label style="float:left;width:70px">删除模块</label><input type="checkbox" value="1" id="modSetRemoved" style="float:left;"></div>';
	html+='<br clear="all" />';
	
	html+='<table width="100%" height="30"><tr><td><input type="button" class="button" value="保存" id="save_setting_btn">';
	html+='<input type="button" class="button" value="取消" id="cancel_setting_btn"></td></tr></table>';
	html+='</div>';
	
	html+='<div class="contentDiv" id="modSettingTip" style="display:none"><img src="/imgs/icons/16/loading.gif">&nbsp;正在保存</div>';
	
	html+='</div>';
	
	div.innerHTML=html;
	document.body.appendChild(div);
	this.htmlElement=div;
	
	var div=document.createElement("div");
	div.className="popBottom";
	div.innerHTML='<div class="botLeft"></div><div class="botRight"></div><div class="botLine"></div>';
	this.htmlElement.appendChild(div);
	this.bottomElement=div;
	
	var closeElement1=document.createElement('a');
	closeElement1.href="javascript:;";
	closeElement1.className="close1";
	this.htmlElement.appendChild(closeElement1);
	this.closeElement1=closeElement1;
	
	var closeElement=document.createElement('a');
	closeElement.href="javascript:;";
	closeElement.className="close";
	closeElement.style.display="none";
	this.htmlElement.appendChild(closeElement);
	this.closeElement=closeElement;
	
	this.closeElement.onclick=this.hide.bindAsEventListener(this);
	this.closeElement1.onmouseover=this.onMouseOver.bindAsEventListener(this);
	this.closeElement.onmouseout=this.onMouseOut.bindAsEventListener(this);

	var div=document.createElement("div");
	div.className="popTitle";
	this.htmlElement.appendChild(div);
	this.titleElement=div;
	
	var div=document.createElement("div");
	div.className="popNote";
	this.htmlElement.appendChild(div);
	this.contentElement=$("modSettingContent");
	this.tipElement=$("modSettingTip");
	this.statusElement=div;
	//设定标题和状态栏
	this.setTitle(this.title);
	this.setStatus(this.status);
	//绑定保存和取消的点击事件
	$("save_setting_btn").onclick=this.save.bindAsEventListener(this);
	$('cancel_setting_btn').onclick=this.hide.bindAsEventListener(this);
	
	
  },
  save:function(){
  //todu,需要判断变化了没
  	this.params["mod"]=this.modId;
  	if(this.config[this.modId]["order"]){
  		var order=$("modSetOrder").value;
  		this.params["order"]=order;
  	}
  	if(this.config[this.modId]["num"]){
  		var num=$("modSetNum").value;
  		this.params["num"]=num;
  	}
  	if($("modSetRemoved").checked){
  		this.params["removed"]=1;
  	}
  	this.params["modname"]=$("modSetName").value;
  	this._post(this.op,this.onSave.bind(this),this.params);
  	
  	this.contentElement.style.display="none";
  	this.tipElement.style.display="block";
  },
  onSave:function(xmlhttp){
  	if (xmlhttp.status != 200)
        {
            //状态提示
            return new Array();
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1)
        {
            return new Array();
        }
        var status=new Array();
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if(stat=="ok"){
        	window.location.reload();
        	/*
        	var refresh=rsp.getAttribute("refresh");
        	var removed=rsp.getAttribute("removed");
        	var name=rsp.getAttribute("name");
        	if(name&&name.length>0){
        		var title=this.getModTitleElement();
        		title.innerHTML=name;
        		//
        		//this.modTitleElement[this.modId]=name;
        	}
        	//先处理删除
        	if(removed&&removed=="1"){
        		$(this.modId).remove();
        		this.contentElement.style.display="block";
  				this.tipElement.style.display="none";
	    		this.loading=false;
        		return;
        	}
        	if(refresh&&refresh=="1"){
        		$(this.modId+"Content").innerHTML="正在加载数据";
        		new modules[this.modId]();        	
        	}
        	this.bindFunction();
        	this.hide();
        	*/      
        }
	    else{
	    	var err = rsp.firstChild;
            var errorCode = err.getAttribute("code");
            var errorMsg = err.getAttribute("msg");
            alert(errorMsg);
	    }
	    this.contentElement.style.display="block";
  		this.tipElement.style.display="none";
	    this.loading=false;
  },
  //绑定事件的
  bindFunction:function(){
  
  
  },
  onMouseOut:function(){
  	this.closeElement.style.display="none";
	this.closeElement1.style.display="";  
  },
  onMouseOver:function(){
  	this.closeElement1.style.display="none";
	this.closeElement.style.display="";
  
  },
  /**
   * 显示设置界面
   */
  show:function(e){
  	var el=Event.element(e);
  	var id=el.id;
  	this.modId=id.substring(0,id.length-10);
  	this.modContentId=this.modId+"Content";  	
  	if(!this.htmlElement){
  		this._createUI();
  	}
  	$("modSetName").value=this.getModTitleElement().innerHTML;
  	//
  	//如果允许设置顺序的话
  	if(order=this.config[this.modId]["order"]){
  		$("orderSetDiv").style.display="block";
  		$("modSetOrder").innerHTML="";
  		for(var i=0;i<order.length;i++){
  			var oOption = document.createElement("OPTION");
   	   		oOption.text=order[i];
   	   		oOption.value=i+1;
	   		if(!mozilla){
    	 		$("modSetOrder").add(oOption);		
	   		}
	   		else{
    	 		$("modSetOrder").appendChild(oOption);
	   		}
	   	}
  	}
  	//如果允许设置数量的话
  	if(this.config[this.modId]["num"]){
  		$("numSetDiv").style.display="block";
  	}
  	//设置标题
  	this.setTitle(this.config[this.modId]["name"]);
  	this.position(e);
  	this.htmlElement.style.display="";
  	$("modSetName").focus();
  },
  hide:function(){
  	this.htmlElement.style.display="none";
  	//设置其他的默认值
  	$("modSetRemoved").checked=false;
  	$("modSetNum").value="";
  	$("orderSetDiv").style.display="none";
  	$("numSetDiv").style.display="none";
  	
  },
  getContentHTML:function(){
  
  	return "";
  },
  //得到标题元素
  getModTitleElement:function(){
  	if(!this.modTitleElement[this.modId]){
  		var nodes=$(this.modId).childNodes;
  		for(var	i=0;i<nodes.length;i++){
  			if(nodes[i].nodeType==1&&nodes[i].className&&nodes[i].className=="title"){
  				titleElement=nodes[i];break;
  			}
  		}
  		var nodes2=titleElement.childNodes;
  		for(var	i=0;i<nodes2.length;i++){
  			if(nodes2[i].nodeType==1&&nodes2[i].tagName=="H2"){
  				this.modTitleElement[this.modId]=nodes2[i];break;
  			}
  		}
  	
  	}
  	return this.modTitleElement[this.modId];
  
  },
  setTitle:function(title){
  	this.titleElement.innerHTML=title;
  },
  setStatus:function(info){
  	this.statusElement.innerHTML=info;
  },
   position: function(e)
    {
    	var offWidth=parseInt(this.htmlElement.style.width);

        if (this.htmlElement)
        {
             this.deltaX =  window.pageXOffset
                || document.documentElement.scrollLeft
                || document.body.scrollLeft
                || 0;
    this.deltaY =  window.pageYOffset
                || document.documentElement.scrollTop
                || document.body.scrollTop
                || 0;
            var x= e.clientX+ this.deltaX ;
            if((x+offWidth)>=document.body.clientWidth) x = document.body.clientWidth - offWidth-20;
            var y= e.clientY+ this.deltaY+30;
            this.htmlElement.style.left = x + "px";
            this.htmlElement.style.top = y + "px";
            this.htmlElement.style.display="";
        }
    },
  _post: function(method, callback, params, postBody) {
    	var url = this.END_POINT;
    	var query = this._queryString(method, params);
    	this.ajax=new Ajax.Request(url, { parameters:     query,
                         postBody:       postBody,
                         method:         "post",
                         methodName:     method,
                         onComplete:     callback });
   },
   _queryString: function(method, params) {
    	var query = 'op=' + method;
    	if (this.session_id)
      		query += '&session_id=' + this.session_id;
    	for (var name in params) {
      		if (name != 'extend')
        		if (params[name]) {
          			query += '&' + name + '=' + params[name];
        		}
    	}
    return query;
   },
   initModSetting:function(){
   		 this.config=[];
   		 //,,,newPosts,newComments,,,,,,,,
   		 
   		 this.config["myLinks"]={name:"链接设置",order:["新的在前","旧的在前"],remove:true};
   		 //this.config["myLinks"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="myLinksSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=linksList">链接管理</a></div>';
   		 this.config["myLinks"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=linksList">链接管理</a></div>';
		
   		 this.config["newPosts"]={name:"最新文章设置",num:true,remove:true};
   		 //this.config["newPosts"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="newPostsSettingBtn">设置</a></div>';
   		 this.config["newPosts"].html='';
		
   		 this.config["newComments"]={name:"最新回复设置",num:true,remove:true};
   		 //this.config["newComments"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="newCommentsSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=viewPostComments&style=receive">回复管理</a></div>';
   		 this.config["newComments"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=viewPostComments&style=receive">回复管理</a></div>';
		
   		 this.config["categories"]={name:"文章分类设置",remove:true};
   		 //this.config["categories"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="categoriesSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=articleCategoriesList">分类管理</a></div>';
   		 this.config["categories"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=articleCategoriesList">分类管理</a></div>';
		
   		 this.config["pInfo"]={name:"个人资料设置",remove:true};
   		 //this.config["pInfo"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="pInfoSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=editProfile">个人资料管理</a></div>';
   		 this.config["pInfo"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=editProfile">个人资料管理</a></div>';
		
   		 this.config["topPosts"]={name:"置顶设置",remove:true};
   		 //this.config["topPosts"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="topPostsSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=postsList&showStatus=-1">文章管理</a></div>';
   		 this.config["topPosts"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=postsList&showStatus=-1">文章管理</a></div>';
		
   		 this.config["postsList"]={name:"文章列表设置",remove:true};
   		 //this.config["postsList"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="postsListSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=newPost">发表文章</a></div>';
   		 this.config["postsList"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=newPost">发表文章</a></div>';
		
   		 this.config["album"]={name:"相册设置",remove:true};
   		 this.config["stats"]={name:"统计信息设置",remove:true};
   		 //this.config["stats"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="statsSettingBtn">设置</a></div>';
   		 this.config["stats"].html='';
		
   		 this.config["myFriends"]={name:"好友设置",remove:true};
   		 //this.config["myFriends"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="myFriendsSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=contactsList">好友管理</a></div>';
   		 this.config["myFriends"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=contactsList">好友管理</a></div>';
		
   		 this.config["myGroups"]={name:"圈子设置",remove:true};
   		 //this.config["myGroups"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="myGroupsSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=myJoinedGroups">圈子管理</a></div>';
   		 this.config["myGroups"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=myJoinedGroups">圈子管理</a></div>';
		
   		 this.config["calendar"]={name:"日历设置",remove:true};
   		 this.config["music"]={name:"音乐设置",remove:true};
   		 //this.config["music"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="musicSettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/control.php?op=musicsList">音乐管理</a></div>';
   		 this.config["music"].html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="/control.php?op=setBgSound">音乐管理</a></div>';
		
   		 this.config["lastUpdateBlogs"]={name:"最新更新博客设置",remove:true};
   
   },
   initBind:function(){
   		
        this.session_id=window.session_id;
        for(name in this.config){
   			if($(name+"Content")&&this.config[name]&&this.config[name].html){
   				new Insertion.Before($(name+"Content"),this.config[name].html);
   				$(name+'SettingBtn').onclick=this.show.bindAsEventListener(this);
   			
   			}
   			
   		}
   		//if $pinfo
   		if(!$("pInfo"))
   			return;
   		var w=$("pInfoContent").offsetWidth-22;
        var element=$("add_resource_btn");
        var eleSrc=element.src;
        if(eleSrc.indexOf("p_")>0&&w>260)
                element.src=eleSrc.replace('small','medium');
        else if(eleSrc.indexOf('blog_icon')>0&&isOwner)
                element.src='/imgs/no_avastar_login.gif';
        if(w>500)
                w=500;
        element.removeAttribute('width');
        element.removeAttribute('height');
        element.style.width=w+'px';
		if(isOwner){
            if($("add_resource_btn")){
                var element=$("add_resource_btn");
			    element.title="点击换一张图片在这儿显示?";
			    new SetBlogIcon("add_resource_btn");
            }
            //插入常用菜单
            var html='<a href="/control.php?op=newPost" style="float:left;width:48%;" target="_blank"><img src="/imgs/icons/16/control/post.gif"/>发表文章</a>';
            html+='<a href="/control.php?op=newResources" style="float:left;width:48%" target="_blank"><img src="/imgs/icons/16/control/photo.gif"/>上传照片</a><br clear="all"/>';
            html+='<a href="/control.php?op=viewPostComments&style=receive" style="float:left;width:48%" style="_blank"><img src="/imgs/icons/16/control/comment.gif"/>回复管理</a>';
            html+='<a href="javascript:;" id="private_articles_btn" style="float:left;width:48%"><img src="/imgs/icons/16/control/private.gif"/>隐私文章</a><br clear="all"/>';
            new Insertion.Bottom("pInfoContent",html);
            $("private_articles_btn").onclick=function(){
            var el=$("private_articles_btn");
			var id="private";
			if(!el.postMod){
			//todo：修改模块名字
				//el.postMod=Class.create();
				el.postMod=Pager.extend({
					initialize:function(){
						this.parent();
						this.init();
						this.loadFunction();
					},
					init:function(){
						this.id="postsList";
						
						this.contentId="postsListContent";
						$(this.contentId).innerHTML="正在加载数据";
						this.jstId="postsListJST";
						this.op="bloghome.blog.getArticles";
						this.params={user_id:window.user_id,cat_id:id};

				}
			});
			}
			new el.postMod();
            }
		}
		//end  
   }
   
    
  
}

var modSetting=new ModSetting();
/*
var MyLinksSetting=Class.create();
MyLinksSetting.prototype= (new ModSetting()).extend({
	initialize:function(){
		this.modId="myLinks";
  		this.modContentId="myLinksContent";
  		this.clickId="myLinksSettingBtn";
  		this.title="链接设置";
  		this.status="友情链接设置";
  		this.op="bloghome.setting.setMyLinks";
		this.init();
	},
	getContentHTML:function(){
  		var html='<table><tr><td>模块名称</td><td><input type="text" value="'+this.getModTitleElement().innerHTML+'" id="'+this.modId+'SetName" size="8"></td></tr>';
  		html+='<tr><td>显示顺序</td><td><select id="'+this.modId+'SetOrder">';
		html+='<option value="1">新的在前</option><option value="2">旧的在前</option></select></td></tr>';
		html+='<tr><td>不显示此模块</td><td><input type="checkbox" id="'+this.modId+'SetRemoveMod"></td></tr></table>';
  		return html;
  	},
  	save:function(){
  		var modName=$(this.modId+"SetName").value;
  		var order=$(this.modId+"SetOrder").value;
  		var removeMod=$(this.modId+"SetRemoveMod").checked;
  		if(this.modName!=undefined&&this.modName==modName&&this.order!=undefined&&this.order==order&&this.removeMod!=undefined&&this.removeMod==removeMod){
  			alert("您没有做任何变化");
  			return;
  		}  			
  		this.params["modname"]=modName;
  		this.params["order"]=order;
  		this.params["removemod"]=removeMod;
  		this._post(this.op,this.onSave.bind(this),this.params);  	
  	},
  	bindFunction:function(){
  		this.order=$(this.modId+"SetOrder").value;
  		this.removeMod=$(this.modId+"SetRemoveMod").checked;
  	
  	}
});

var ModNameSetting=Class.create();

window.moduleSettings={
	"myLinks":MyLinksSetting

}
*/
