﻿function CatItem(id,parentid,name,depth,redirection,ishidden,tarurl,tarframe,isnewwindow){
	this.ID=id;
	this.ParentID=parentid;
	this.Name=name;
	this.Depth=depth;
	this.Redirection=redirection;
	this.IsHidden=ishidden
	this.TarUrl=tarurl;
	this.TarFrame=tarframe;
	this.IsNewWindow=isnewwindow;
};

function Category(objname,linenum,currentid,parentid){
	this.ObjName=objname;
	this.LineNum=linenum;
	this.CurrentID=currentid;
	this.oItems=[];
	this.ParentID=parentid;
	this.aNav=[];
};

Category.prototype.Add=function(id,parentid,name,depth,redirection,ishidden,tarurl,tarframe,isnewwindow){
	this.oItems.push(new CatItem(id,parentid,name,depth,redirection,ishidden,tarurl,tarframe,isnewwindow));
};

Category.prototype.getCatName=function(cid){
	for(var i=0;i<this.oItems.length;i++){
		if(this.oItems[i].ID==cid){
			return this.oItems[i].Name;
		}
	}

};

Category.prototype.toString=function(){
	var result="";
	var havecate=false;
	if(this.oItems.length==0){
		result="<div align=center style='color:red'>目前没有任何子栏目</div>";
		return result;
	}
	
	result+="<table width=80% border=0 align=center cellpadding=2 cellspacing=0><tr>";
	var n=1;
	for(var i=0;i<this.oItems.length;i++){
		
		if(this.CurrentID==this.oItems[i].ParentID){
			result+="<td align=center>";
			result+="<a href="+this.oItems[i].TarUrl+" "+(this.oItems[i].TarFrame==''?"":"target="+this.oItems[i].TarFrame+"")+" class=cat>"+(this.oItems[i].ID==this.CurrentID?"<font color=red>":"")+""+this.oItems[i].Name+""+(this.oItems[i].IsHidden?"[隐藏栏目]":"")+""+(this.oItems[i].ID==this.CurrentID?"</font>":"")+"</a>";
			result+="</td>";
			if((n)%this.LineNum==0){
				result+="</tr><tr>";
			}
			n++;
			havecate=true;
		}
	
	}
	if(!havecate)
		result+="<td align=center style='color:red'>目前没有任何子栏目</td>";
	result+="</tr>";
	
	
	result+="</table>";
	
	return result;
	
};



Category.prototype.getNav=function(ncatid,url){

	for(var i=0;i<this.oItems.length;i++){
		if(this.oItems[i].ID==ncatid){
			this.aNav.push(this.oItems[i]);
			this.getNav(this.oItems[i].ParentID,url);
			break;
		}
	}
	var result="";

	for(var i=this.aNav.length-1;i>-1;i--){
		result+="-> <a href="+url+"&id="+this.aNav[i].ID+">"+this.aNav[i].Name+"</a> ";
		
	}

	return result;


};
Category.prototype.getSelect=function(parentid,nid){
	var result="";
	for(var i=0;i<this.oItems.length;i++){
		if(this.oItems[i].ParentID==parentid){
			var sp="";
			for(var ii=0;ii<this.oItems[i].Depth;ii++){
				sp+="｜";
			}
			
			result+="<option value="+this.oItems[i].ID+" "+(nid==this.oItems[i].ID?"selected":"")+">"+sp+""+this.oItems[i].Name+"</option>";
			result+=this.getSelect(this.oItems[i].ID,nid);
		}
	}
	return result;

};
