//组管理 2011-7-13
var objAjax = new JAjax();
function createObject(){
	return function(){
		this.load.apply(this,arguments);	
	}
}

var mangeClass=null;
setTimeout("loadMange()",100);
function loadMange(){
	mangeClass=new mangeObject();//间隔时间加载数据
}

var mangeObject=new createObject();
mangeObject.prototype.load=function(){//系统加载时候执行
	this.user=urlPar("user");
	if(this.user==null){		
		this.user=userName;
	}	
	this.nowArray=new Array();//保存数据信息
	this.menuType=1;//栏目类型编号
	this.menuName="文章分类";
	this.selMenu=0;//当前编辑的分类编号
	this.useType=urlPar("useType");//用户当前所操作的身份
	if(parseInt(urlPar("typeid"))>0){
	   this.menuType=urlPar("typeid");//栏目类型编号
	   var objParent=$("topOptionList");
	   for(var i=0;i<objParent.childNodes.length;i++){
			if(i==(this.menuType-1)){
				this.menuType=(i+1);
				if(i==0){
					this.menuName="文章分类";
				}else if(i==2){
					this.menuName="相册分类";
				}else if(i==3){
					this.menuName="音乐分类";
				}else{
					this.menuName="好友分类";
				}
				this.menuName=objParent.childNodes[i].innerText;
				objParent.childNodes[i].className="optionCar"
			}else{
				objParent.childNodes[i].className="optionCar_1"
			}
		}
	}
	this.loadDate();
}

//点击加载指定的数据信息
mangeObject.prototype.clickTab=function(obj){
	this.obj=obj;
	var objParent=obj.parentNode;
	for(var i=0;i<objParent.childNodes.length;i++){
		if(objParent.childNodes[i]==obj){
			this.menuType=(i+1);
			this.menuName=objParent.childNodes[i].innerHTML
			objParent.childNodes[i].className="optionCar"
		}else{
			objParent.childNodes[i].className="optionCar_1"
		}
	}
	this.loadDate();
}

//加载用户已经存在指定类型的栏目信息
mangeObject.prototype.loadDate=function(){
		if(!this.showTab){
			this.showTab=$("showTab");	
		}
		for(var i=this.showTab.rows.length-1;i>-1;i--){
			this.showTab.deleteRow(i);	
		}		
		var tr=this.showTab.insertRow(this.showTab.rows.length);		
		tr.className="closeDivStyle";
		var td=tr.insertCell(-1);
		td.className="biaotouTdMangaement";
		td.style.cssText="background-image:url(./img/pn.gif);width:630px;"
		if(typeof(this.obj)!="undefined"){
			this.menuName=this.obj.innerHTML;
		}else{
			this.menuName="日志分类";
		}
		td.innerHTML="分类名称<span style='color:red'>("+this.menuName+")</span>";		
		
		td=tr.insertCell(-1);
		td.className="biaotouTdMangaement";
		td.style.cssText="background-image:url(./img/pn.gif);text-align:center;"
		td.setAttribute("colSpan",2);
		td.innerHTML="&nbsp;";	
		
		var objResponse=new JHttpResponse();
		objResponse.write("typeId",this.menuType);
    	var out=objAjax.getText("post","./cs/saveContent.aspx?type=readMenuLst&useType="+this.useType+"&user="+this.user,objResponse,null); 		
		this.nowArray=new Array();
		var isBool=false;		
		if(parseInt(homeUserID)==parseInt(userID)||parseInt(cUserID)==parseInt(userID)){
			isBool=true;	
		}
		if(out.indexOf(c1)>-1){
			out=out.split(c2);			
			for(var i=0;i<out.length;i++){
				var sp=out[i].split(c1);
				this.nowArray.push(sp);
				tr=this.showTab.insertRow(this.showTab.rows.length);
				tr.onmouseover=function(){this.style.backgroundColor="#efefef";}
				tr.onmouseout=function(){this.style.backgroundColor="";}
				tr.setAttribute("id",sp[0]);
				td=tr.insertCell(-1);
				td.className="jishutd";
				td.style.cssText="padding-left:20px;"
				td.innerHTML=sp[1];
				
				td=tr.insertCell(-1);
				td.className="jishutd";
				if(isBool){					
					td.innerHTML="<span id='"+sp[0]+"' title='删除' style='cursor:hand;background-image:url(./img/del.gif);width:13px;height:13px;margin:4px;background-repeat:no-repeat;' onclick='mangeClass.del(this)'>&nbsp;</span>";					
					
					td=tr.insertCell(-1);
					td.className="jishutd";
					td.innerHTML="<span id='"+sp[0]+"' title='编辑' style='cursor:hand;background-image:url(./img/editor.gif);width:13px;height:13px;margin:4px;background-repeat:no-repeat;' onclick='mangeClass.editor(this)'>&nbsp;</span>";
				}else{
					td.setAttribute("colSpan",2);
					td.innerHTML="&nbsp;";
				}
			}
		}else{
			if(out=="no"){
				//无分组信息				
			}else{
				alert(out);	
			}
		}
		if(isBool){
			tr=this.showTab.insertRow(this.showTab.rows.length);
			td=tr.insertCell(-1);
			td.className="jishutd";
			td.setAttribute("colSpan",3);
			td.style.textAlign="center";
			td.innerHTML="<div id=\"btnAdd\" class=\"addNewStyleCss\" title=\"添加新分类\" onclick='mangeClass.writeHtml()'></div>";
		}
}

//编辑信息
mangeObject.prototype.editor=function(obj){
	var sArray=new Array();
	this.selMenu=obj.id;
	for(var i=0;i<this.nowArray.length;i++){
		if(parseInt(this.nowArray[i][0])==parseInt(this.selMenu)){
			sArray.push(this.nowArray[i][1]);
			sArray.push(this.nowArray[i][2]);
			sArray.push(this.nowArray[i][3]);
			break;
		}
	}
	this.writeHtml(sArray);
}

//关闭弹出框
mangeObject.prototype.close=function(){
	$("divPanel").removeChild(this.cTab.parentNode.previousSibling);
	$("divPanel").removeChild(this.cTab.parentNode);
}

//新增或者修改分类
mangeObject.prototype.saveNewMenu=function(){
	var sK=new RegExp("\\s+","gi");
	sK.compile("(^\\s+)|(\\s+$)","gi");	
	var titleMenu=$("titleMenu").value;//分类标题
    titleMenu=titleMenu.replace(sK,"");
	$("titleMenu").value=titleMenu;

	sK.compile("(^\\s+)|(\\s+$)","gi");	
	var bPhoto=$("bPhoto").value;//分类封面
    bPhoto=bPhoto.replace(sK,"");
	$("bPhoto").value=bPhoto;

	sK.compile("(^\\s+)|(\\s+$)","gi");		
    var bNote=$("bNote").innerHTML;//分类备注
    bNote=bNote.replace(sK,"");
	$("bNote").value=bNote;
	var v=new RegExp("\\S+","gi");
	v.compile("\\S+","gi");
	if(!v.test(titleMenu)){
		alert($("titleMenu").title+"不能够为空");
		$("titleMenu").focus();
		return;
	}
	v.compile("\\S+","gi");
	if(!v.test(bPhoto)){		
		alert($("bPhoto").title+"不能够为空");
		$("bPhoto").nextSibling.focus();
		return;
	}
	
	var objResponse=new JHttpResponse();
	objResponse.write("titleMenu",titleMenu);
	objResponse.write("bPhoto",bPhoto);
	objResponse.write("bNote",bNote);
	objResponse.write("menuType",this.menuType);
	objResponse.write("selMenu",this.selMenu);	
	var out=objAjax.getText("post","./Management.aspx?type=upMenuDate&user="+this.user+"&useType="+this.useType,objResponse,null); 
	if(out=="1"){
		alert("操作成功");
		this.close();
		this.loadDate();
	}else{
		alert(out);
	}       
}

//生成添加或者修改分类页面
mangeObject.prototype.writeHtml=function(){
	var sArray=new Array("","","");
	var v="添加新分类";
	if(arguments.length>0){
		sArray=arguments[0];
		v="修改分类"
	}else{
		this.selMenu=0;	
	}
	var pNode=$("divPanel");
	var w=pNode.offsetWidth;
	var h=pNode.offsetHeight;
	
	var cDiv=document.createElement("div");
	cDiv.style.cssText="z-index:1;background-color:#e4e4e4;position:absolute;filter:Alpha(opacity=50);top:0px;left:0px;width:100%;height:100%;border:solid 1px #cccccc;";
	cDiv.oncontextmenu=function(){return false}
	pNode.appendChild(cDiv);
	
	var mDiv=document.createElement("div");
	mDiv.style.cssText="z-index:110;position:absolute;background-color:#ffffff;border:solid 1px #cccccc;width:400px;left:"+((w-400)/2)+"px;top:100px;";
	pNode.appendChild(mDiv);
	
	this.cTab=document.createElement("table");
	this.cTab.setAttribute("border",0);
	this.cTab.setAttribute("cellPadding",0);
	this.cTab.setAttribute("cellSpacing",0);
	this.cTab.style.cssText="width:390px;margin:5px;";
	var tr=this.cTab.insertRow(this.cTab.rows.length);
	td=tr.insertCell(-1);
	td.setAttribute("colSpan",2);
	td.style.cssText="background-image:url(./img/pn.gif);height:34px;border:solid 1px #cccccc;position:relative;";	
	td.innerHTML="<div style='font-size:13px;font-weight:bold;margin:10px;float:left;'>"+v+"</div><div style='float:right;margin:0px;background-image:url(./img/close.gif);width:28px;height:28px;' onclick='mangeClass.close(this)' onmouseover=\"javascript:this.style.backgroundImage='url(./img/close_1.gif)'\" onmouseout=\"javascript:this.style.backgroundImage='url(./img/close.gif)'\"  title='关闭'>&nbsp;</div>";
	tr=this.cTab.insertRow(this.cTab.rows.length);
	td=tr.insertCell(-1);
	td.className="jishutd";
	td.style.cssText="width:80px;text-align: right; border-left:solid 1px #cccccc;";
	td.innerHTML="分类名称："
    
	td=tr.insertCell(-1);
	td.className="jishutd";	
	td.innerHTML="<input type='text' maxlength='10' title='分类名称' value='"+sArray[0]+"' id='titleMenu' name='titleMenu'><span style='color:red'>*</span>'";
		
	tr=this.cTab.insertRow(this.cTab.rows.length);
	td=tr.insertCell(-1);
	td.className="oushutd";
	td.style.cssText="width: 80px; text-align: right; border-left:solid 1px #cccccc;";
	td.innerHTML="分类封面："
 	
	td=tr.insertCell(-1);
	td.className="oushutd";	
	td.innerHTML="<input maxlength='200' title='分类封面' value='"+sArray[1]+"'  type='text' name='bPhoto' id='bPhoto' style='width:260px;'/><input type='button' value='上传' onclick='selectFile(this)'/><span style='color:Red;'>*</span>";
	   
    tr=this.cTab.insertRow(this.cTab.rows.length);
	td=tr.insertCell(-1);
	td.className="jishutd";
	td.style.cssText="width: 80px; text-align: right; border-left:solid 1px #cccccc;";
	td.innerHTML="分类描述："
	
	td=tr.insertCell(-1);
	td.className="jishutd";	
	td.innerHTML="<textarea  title='分类描述' name='bNote' id='bNote' style='width:300px;height:141px;'>"+sArray[2]+"</textarea>";
	
	tr=this.cTab.insertRow(this.cTab.rows.length);
	td=tr.insertCell(-1);
	td.className="oushutd";
	td.setAttribute("colSpan",2);
	td.style.cssText="text-align: center; border-left:solid 1px #cccccc;";
	td.innerHTML="<input type='button' value='"+v+"' onclick='mangeClass.saveNewMenu()' />";	
	mDiv.appendChild(this.cTab);
}

//删除指定分类
mangeObject.prototype.del=function(obj){
	var id=obj.id;
	if(confirm("删除分类将删除该分类下面的数据信息,你确定要删除吗?")){
		var out=delUserMenuClass(this.menuType,obj.id,this.user,this.useType);
		if(out=="ok"){
			alert("删除成功");
			this.loadDate();	
		}else{
			alert(out);	
		}
	}
}