Favs=Class.create();
Favs.register("com.duxiu.js.Favs");

Favs.construct=function($self, $class){
	var userid;//当前用户
	var username;
	var repUserid;//知识库创建者id
	var repid;//知识库id			
	var cataid="0";//目录号
	var linkName = "全部";//目录连接名	
	var sw = "";//查询的字符串	
	this.pageController;//默认情况下为当前页	
	var bshow="true";//是否显示隐藏	
	var totalcount=0;//收藏总数，可能是某个标签下的
	var searchType = 0;//1 为全部收藏全文检索; 0 为标题检索
	var repType = 0;//1 
	var doctype="";
	var favid="";
	var ismore="";
	var pagesize="5";
	
	var union="";
	
	var pxfs="0";//默认 0(降序) "1"升
	var pxzd=0; //默认 1 可选：2title 3creator 4date
	
	var lastcataid="";
	var timestamps=new Date().getTime();
	
	this.initialize=function(uid,uname,rpuid,rpid,_pagesize){
		userid = uid;
		username=uname;
		repUserid = rpuid;
		repid = rpid;
		pagesize=_pagesize;
		if(userid!=repUserid){			
			bshow="false";
		}		
		if(bshow=="true") writeCookie("rpid" ,rpid);
		this.pageController = new PageController( pagesize , 10 , 0 ,null);
	}
	
	this.getUrl =  function( action , obj ){
		var url = action + "?rpid=" +obj.repid
			+"&cataid="		+	notNull(obj.cataid)
			+"&curpage="	+	notNull(obj.curpage)
			+"&ps="         +   notNull(obj.pagesize)
			+"&show="	+	notNull(obj.bshow)
			+"&searchType="	+	notNull(obj.searchType)
			+"&doctype="+notNull(encodeURIComponent(obj.doctype))
			+"&favid="+notNull(obj.favid)
			+"&ismore="+notNull(obj.ismore)
			+"&pxfs="+notNull(obj.pxfs)
			+"&pxzd="+notNull(obj.pxzd)
			+"&union="+notNull(obj.union)
			+"&linkname="	+	notNull(encodeURIComponent(obj.linkName))
			+"&sw="	+	notNull(encodeURIComponent(obj.sw))
			+"&"+this.toUrlTimestamps()
		return url;
	}
	
	this.transformHTML = function(xml){
		var xsl;
		if(showf=="s")
			g('watchinfo').style.display="none";
		if (userid == repUserid || isGroupUsers || isAdmin == "0" || isAdmin == "2" || isAdmin == "3") {
			xsl = "/"+showf+"mrp_showfav.xsl";
		}else {
			xsl = "/"+showf+"mtourist_rp_showfav.xsl";
		}
		return UtilTool.transformXml(xsl, xml);
	}
	this.reprintFav=function(){
	_screenMap.wait(WebEdit.NOTE_DIALOG_HTML ,false);
		var url = _webfav.getUrl("/s/fl" , {
			repid		: repid,
			cataid		: cataid,
			curpage		: _webfav.pageController.getCurPage(),
			pagesize    : pagesize,
			bshow		: bshow,
			searchType	: searchType,
			doctype     : doctype,	
			favid       : favid,
			ismore		: ismore,
			pxfs		: pxfs,
			pxzd		: pxzd,
			union		: union,
			linkName	: linkName,
			sw			: sw
		});		
	
		var request = new XMLRequst(url).createXMLRequst();
		
		request.onreadystatechange =function(){
			if(request.readyState == 4){//over
				if (request.status == 200){					
					//alert(UtilTool.transformXml("/rp_showfav.xsl" , request.responseXML));
					//alert(_webfav.transformHTML(request.responseXML));
					
					document.getElementById('fav').innerHTML  = _webfav.transformHTML(request.responseXML);
					if(!isIE()){//兼容FF下显示
						var memos=document.getElementsByTagName('div');
						for(var i=0;i<memos.length;i++){
							if('title4'==memos[i].getAttribute("class") && memos[i].getElementsByTagName('a')){
								var titleTag=memos[i].getElementsByTagName('a');
								for(var j=0;j<titleTag.length;j++){
									if("content_title"==titleTag[j].getAttribute("class")){
										titleTag[j].innerHTML=titleTag[j].firstChild.data;
										break;
									}
								}
							}
							if('content'==memos[i].getAttribute("class") && memos[i].firstChild){
								memos[i].innerHTML=memos[i].firstChild.data;
							}
						}
					}
					_webfav.afterResetPage();
						_screenMap.notify();				
				}
				else {
					_screenMap.notify();
					throw new Error("您所请求的"+url+"异常。 异常代号="+request.status);
				}				
			}
		};
		request.send(null);
	}
	
	/**
	*@return 获得收藏总数
	*/
	this.getCount = function(){	
		_screenMap.wait("" , false);
		var url = _webfav.getUrl("/s/grc" , {
			repid		: repid,
			cataid		: cataid,
			bshow		: bshow,
			searchType	: searchType,
			doctype     : doctype,
			favid       : favid,	
			ismore      : ismore,
			union		: union,
			linkName	: linkName,
			sw			: sw
		});	
	
		//g("stoolsbody").style.display="";		
		var request = new XMLRequst(url , "get" , false).createXMLRequst();
		request.send(null);
		
		if(request.status!=200){
			_screenMap.notify();
			throw new Error("您所请求的"+url+"异常。 异常代号="+request.status);
		}
		
		_screenMap.notify();
		
		totalCount = parseInt( request.responseText );
		//alert("totalCount:"+totalCount);
		//这里会设置 页面控制

		this.pageController = new PageController( pagesize , 10 , totalCount ,
			function(curpage , skipToPage){
				_webfav.reprintFav();
				_webfav.reprintPage();	
			}
		);		
		//不显示批量操作
//		if(totalCount==0){
//			showBatchOperation(false);	
//		}else{
//			showBatchOperation(true);	
//		}
		beforeCatalogChange();

		return totalCount;
	}
	
	this.goToCatalog=function(cid,lnk,_n,_fgid){
		doctype="";	
		favid="";
		if(g('unionsearch').checked){
			union="1";
		}else{
			sw="";
			union="0";
		}
		
		if(_n){
			if(_fgid)
				getDivContent(_n,cid,_fgid);
		}
		this.setCurCata(cid);
		this.setCatalog(cid,lnk);
		var count = this.getCount();
		
		g('doctype_select').options[0].selected=true ;
		if(count>0){
			this.pageController.goFirst(true);
		}else {
			g('fav').innerHTML ='';	
		}
		setTimeout(function(){
			//if(!_catalogXHTML.insertChildsHTML(_catalogTree.getSelectNodeXML()) && count ==0){
			if(count ==0){
				g('fav').innerHTML ='<div class="c_right"><div class="ss_content">提示：[' +lnk+ '] 目录下没有相关文献。</div></div>';
			}
			afterCatalogChange();
		}, 100);//显示该分类下的子分类

		// 设置导航条
			g('nav_list').innerHTML = setNvg(linkName, "/", 6);
	}
	
//在更换分类时，重新绘制一些东西
	var beforeCatalogChange = function(){	
		$self.reprintPage();					
		//g('check_b').checked=false;
		window.scroll(0, 0);
	}	
	
	//更换页数后
	this.afterResetPage = function(){
		_webDisplay = new WebDisplay();	
		
		if(favid!="")
			_webDisplay.o(favid);
			if(WebDisplay.all_open){
			_webDisplay.doAll("open");
		}
		window.scroll(0, 0);
	}
	this.getUserName=function(){
		return username;
	}
	this.getUserId=function(){
		return userid;
	}
	this.getRepId = function(){
		return repid;	
	}
	this.setCatalog = function(c , l){
		cataid = c;
		linkName = l;
	}
	this.toUrlTimestamps = function(){
		return "t="+timestamps;
	}
	var afterCatalogChange = function(){
		_webDisplay = new WebDisplay();	
	}
	this.reprintPage = function(){
			 g('pageController2').innerHTML 
			= _webfav.pageController.toNavigationHTML(false);
	}
	//time stamps to control the page's cache
	this.reflashTimestamps = function(){
		sw = "";
		timestamps = new Date().getTime();
	}
	
	/* 设置分类导航栏 */
	var setNvg = function(str, span, lenth){
		var nvg = str.split(span);
		var temp = "";
		for(i = 0; i< nvg.length; i++)
		{
			temp += (nvg[i].length > lenth ? nvg[i].substr(0, lenth) + ".." : nvg[i]);
			if(i != nvg.length -1)
				temp += "  >  ";
		}
		return temp;
	}
//设置当前显示的分类
	this.setCurCata=function(_cataid){
		if(lastcataid==_cataid)
			return;
		if(lastcataid!=""){
			var html="";
			if (navigator.appName.indexOf("Explorer") > -1) {
				html = g('atree_' + lastcataid).innerText;
			}
			else {
				html = g('atree_' + lastcataid).textContent;
			}
			g('atree_'+lastcataid).innerHTML=html;
		}			
		lastcataid=_cataid;
			if (g('atree_' + lastcataid)) {
				var html = g('atree_' + lastcataid).innerHTML;
				g('atree_' + lastcataid).innerHTML = "<b>" + html + "</b>";
			}
		
	}
	/* 根据下拉列表框选择文献类型查询 */
	this.searchFavByDoctype = function(dt){
		//选择的文献类型
		var _doctype = dt;//g('doctype_select').value;
		doctype = _doctype;
		favid="";
		if(_doctype=="") 
			return;
		else
			g('nav_list').innerHTML = '<ul><li style="margin:0px; vertical-align:middle;">按文献类型：<font color="red">' + _doctype + '</font> 查看结果</li></ul>';
		
		g('fav').innerHTML = '加载中...';
		
		if(this.getCount()>0)
			this.pageController.goFirst(true);
		else
			g('fav').innerHTML = '<div class="c_right"><div class="ss_content">提示：[' + linkName + '] 分类下没有找到文献类型: <font color="red">' + _doctype + '</font> 相关文献。</div></div>';
	}
	
	/* 根据下拉列表框选择文献类型查询 */
	this.searchFav = function(){
	
		var s = g('sw').value;//检索词
		var dt = g('dt').value;//文献类型
		var st = "";//检索字段
		
		var sts = document.getElementsByName('searchType');
		
		if(g('unionsearch').checked){
			union="1";
		}else{
			union="0";
		}
		for(var i=0; i<sts.length; i++){
		
			if(sts[i].checked)
				st = sts[i].value;
			else
				continue;
		}
		//alert(st);
		if(s.trim()==""){
			alert("检索词为空,请输入检索词！");
			return;
		}
		//设置类参数
		sw = s;	
		searchType=st;
		doctype = dt;
		
		g('nav_list').innerHTML ='<ul><li style="margin: 0px"">'+sw+'搜索结果:</li></ul>';
		
	
		if(this.getCount()>0)
			this.pageController.goFirst(true);
		else{
			g('fav').innerHTML ='<div class="c_right"><div class="ss_content">提示：没有找到与<font color=red> '+sw+' </font>相关文献。'
			+'</div></div>';
			}
	}
	
	this.searchFavFormindex=function(_sw,_doctype,_seachtype){
		sw=_sw;
		
		doctype=_doctype;
		searchType=_seachtype;
		g('nav_list').innerHTML ='<ul><li style="margin: 0px"">'+sw+'搜索结果:</li></ul>';
		if(this.getCount()>0)
			this.pageController.goFirst(true);
		else{
						g('fav').innerHTML ='<div class="c_right"><div class="ss_content">提示：没有找到与<font color=red> '+sw+' </font>相关文献。<br>建议您：<br>'
			+'</div></div>';
		}
	}
	
	this.searchByFavid=function(_favid){
		doctype="";
		g('nav_list').innerHTML ='<ul><li style="margin: 0px"">文献详细信息:</li></ul>';
		favid=_favid;
		if(this.getCount()>0){
			this.pageController.goFirst(true);
		}else{
			g('fav').innerHTML ='<div class="c_right"><div class="cc_content">没有找到该文献的详细信息</div></div>';
		}
	}
	
	this.insearchFav=function(_sw,st){
		if (_sw == "") {
			alert("检索词为空,请输入检索词!");
			return;
		}
			g('nav_list').innerHTML ='<ul><li style="margin: 0px"">搜索结果:</li></ul>';
		favid="";
		doctype="";
		sw=_sw;
		searchType=st;
		if(this.getCount()>0){
			this.pageController.goFirst(true);
		}else{
			g('fav').innerHTML ='<div class="c_right"><div class="ss_content">提示：没有找到与<font color=red> '+sw+' </font>相关文献。<br>建议您：<br>'
			+'</div></div>';
		}
		
	}
	this.searchfs=function(_pxfs){
		if(pxfs==_pxfs)
			return;
			pxfs=_pxfs;
			this.setpxfsboldstyle();
			var count = this.getCount();
			if(count>0){
				this.pageController.goFirst(true);
			}
			else {
				g('fav').innerHTML ='';
			}
	
			setTimeout(function(){
				//if(!_catalogXHTML.insertChildsHTML(_catalogTree.getSelectNodeXML()) && count ==0){
				if(count ==0){
					g('fav').innerHTML ='<div class="c_right"><div class="ss_content">提示：[' +linkName+ '] 目录下没有相关文献。</div></div>';
				}
				afterCatalogChange();
			}, 100);//显示该分类下的子分类
		}
	
	this.searchzd=function(_pxzd){
		if(pxzd==_pxzd)
			return;
			pxzd=_pxzd;
			this.setpxzdboldstyle();
			var count = this.getCount();
			if(count>0){
				this.pageController.goFirst(true);
			}
			else {
				g('fav').innerHTML ='';
			}
	
			setTimeout(function(){
				//if(!_catalogXHTML.insertChildsHTML(_catalogTree.getSelectNodeXML()) && count ==0){
				if(count ==0){
					g('fav').innerHTML ='<div class="c_right"><div class="ss_content">提示：[' +linkName+ '] 目录下没有相关文献。</div></div>';
				}
				afterCatalogChange();
			}, 100);//显示该分类下的子分类
		}
		
	this.setpxfsboldstyle=function(){
			var pxfsshtml="升序";
			var pxfsjhtml="降序";
			if(pxfs=="0"){
				g('a_pxfss').innerHTML=pxfsshtml;
				g('a_pxfsj').innerHTML="<b>"+pxfsjhtml+"</b>";
			}else{
				g('a_pxfss').innerHTML="<b>"+pxfsshtml+"</b>";
				g('a_pxfsj').innerHTML=pxfsjhtml;
			}	
	}
	
	this.setpxzdboldstyle=function(){
		var obj1="录入时间";
		var obj2="题名";
		var obj3="责任者";
		var obj4="年代";
		var objarr=["录入时间","题名","责任者","年代"];
		for(var i=0;i<4;i++){
			if(pxzd==(i+1)){
				g('a_pxzd'+(i+1)).innerHTML="<b>"+objarr[i]+"</b>";
			}else
				g('a_pxzd'+(i+1)).innerHTML=objarr[i];
		}
	}
}
