/*
*	modello框架
**
*	author 贺博
*	version 2007-7-5
**
*	收藏描述
*/
WebFav = Class.create();
WebFav.register("com.duxiu.js.WebFav");

WebFav.construct = function($self, $class){
	var userid;//当前用户
	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 vip收费库

	//时间戳，用来改变缓存
	var timestamps = new Date().getTime();
	
	this.initialize=function(uid ,rpuid ,rpid , rtype){		
		userid = uid;
		repUserid = rpuid;
		repid = rpid;
		repType = rtype;
		if(userid!=repUserid){			
			bshow="false";
		}		
		if(bshow=="true") writeCookie("rpid" ,rpid);
		
		this.pageController = new PageController( 5 , 10 , 0 ,null);
	}
	
	this.getUrl =  function( action , obj ){		
		var url = action + "?rpid=" +obj.repid
			+"&cataid="		+	notNull(obj.cataid)
			+"&linkname="	+	notNull(encodeURIComponent(obj.linkName))
			+"&curpage="	+	notNull(obj.curpage)
			+"&show="	+	notNull(obj.bshow)
			+"&sw="	+	notNull(encodeURIComponent(obj.sw))
			+"&searchType="	+	notNull(obj.searchType)
			+"&"+this.toUrlTimestamps();
		return url;
	}	
	
	this.transformHTML = function(xml){
		var xsl ;		
		if(userid==repUserid||isGroupUsers){
			xsl = "/rp_showfav.xsl";
		}else{
			xsl = "/tourist_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,
			linkName	: linkName,
			curpage		: _webfav.pageController.getCurPage(),
			bshow		: bshow,
			sw			: sw,
			searchType	: searchType
		});		
		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));					
					getObject('fav').innerHTML  = _webfav.transformHTML(request.responseXML);
					if(!isIE()){
						var memos=document.getElementsByTagName('div');
						for(var i=0;i<memos.length;i++){
							if('cc_content'==memos[i].getAttribute("class") && memos[i].firstChild){
								memos[i].innerHTML=memos[i].firstChild.data;
							}
						}
					}
					var fav = document.getElementsByName('fav_index');					
					//是否显示批量操作
					if(fav.length>=1){
						showBatchOperation(true);					
					}
					else{
						showBatchOperation(false);	
					}					
					_webfav.afterResetPage();
					_screenMap.notify();
				}
				else {
					_screenMap.notify();
					throw new Error("您所请求的"+url+"异常。 异常代号="+request.status);
				}				
			}
		};
		this.beforeResetPage();		
		request.send(null);
	}
	//在更换页数前，重新绘制一些东西
	this.beforeResetPage = function(){
		showIntro();	
	}
	//更换页数后
	this.afterResetPage = function(){
		_webEdit.idManager.resetState();
		_webDisplay = new WebDisplay();
		window.scroll(0, 0);
	}
	
	/**
	*显示分类序
	*/
	var showIntro = function(){
		if(cataid == "0" && _webfav.pageController.getCurPage()==1){			
			_cataIntro.showIntro();			
		}
		else{
			_cataIntro.setDisplay(false);	
		}		
	}

	/**
	*@return 获得收藏总数
	*/
	this.getCount = function(){		
		_screenMap.wait("" , false);
		
		var url = _webfav.getUrl("/s/grc" , {
			repid		: repid,
			cataid		: cataid,
			linkName	: linkName,
			bshow		: bshow,
			sw			: sw,
			searchType	: searchType									  
		});			
		
		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 );
		
		//这里会设置 页面控制
		this.pageController = new PageController( 5 , 10 , totalCount ,
			function(curpage , skipToPage){
				_webfav.reprintFav();
				_webfav.reprintPage();				
			}
		);		
		//不显示批量操作
		if(totalCount==0){
			showBatchOperation(false);	
		}	
		beforeCatalogChange();		
		return totalCount;
	}
	
	//在更换分类时，重新绘制一些东西
	var beforeCatalogChange = function(){
		showIntro();		
		$self.reprintPage();					
		g('check_b').checked=false;
		window.scroll(0, 0);
	}	
	
	var afterCatalogChange = function(){
		_webDisplay = new WebDisplay();		
	}
	
	//跳到某一目录
	this.goToCatalog = function(cid , lnk , _parent_xml,istitle){
		this.setCatalog(cid , lnk);		
		sw = "";		
		g('nav_list').innerHTML=_catalogXHTML.getCatalogNavigation(_parent_xml);
		
					
		var count = this.getCount();
		if(count>0){
			this.pageController.goFirst(true);
		}
		else {
			g('fav').innerHTML ='';
		}

		setTimeout(function(){
			if(!_catalogXHTML.insertChildsHTML(_catalogTree.getSelectNodeXML()) && count ==0){
				g('fav').innerHTML ='<div class="c_right"><div class="cc_content">[' +lnk+ '] 目录下没有收藏。</div></div>';
			}
			afterCatalogChange();
		}, 100);//显示该分类下的子分类
	}
	
	this.searchFav = function(s, t){
		g('show_child_catalog').style.display ="none";
		g('nav_list').innerHTML ='<ul>'
					+'<li style="background: none; margin: 8px 5px 0 3px;"><img src="/catalog_images/icon03.gif" /></li>'
					+'<li style="margin: 0px"><a href=\"javascript:_catalogTree.setSelectCatalog(0)\">全部</a></li>'
					+'<li style="width:15px;"><a href="javascript:void(null)" onclick="_catalogTree.clickChilds(0,this)">'
					+'<img src="/catalog_images/icon04.gif" style="padding-top:3px; padding-left:3px"/></a></li>'
					+'<li style="margin: 0px""><a>搜索结果<a></li>'
					+'</ul>';
		
		sw = s;
		var radios = document.getElementsByName(t);
		for(var i = 0; i<radios.length; i++){
			if(radios[i].checked){
				searchType = radios[i].value;
				break;
			}
		}

		if(this.getCount()>0){
			this.pageController.goFirst(true);
		}
		else{
			g('fav').innerHTML ='<div class="c_right"><div class="cc_content">没有找到与 '+sw+' 有关收藏。</div></div>';
		}
	}
	
	//time stamps to control the page's cache
	this.reflashTimestamps = function(){
		sw = "";
		timestamps = new Date().getTime();
	}
	
	this.toUrlTimestamps = function(){
		return "t="+timestamps;
	}
	
	//setter & getter	
	this.setCatalog = function(c , l){
		cataid = c;
		linkName = l;
	}
	
	this.getUserId = function(){
		return userid;
	}	
	this.getRepUserId = function(){
		return repUserid;
	}	
	this.getRepId = function(){
		return repid;	
	}	
	this.getCatalog = function(){	
		return {c:cataid , l:linkName}
	}
	
	this.getSearchWord = function(){		
		return sw;
	}
	this.setSearchWord = function(s){		
		sw=s;
	}
	this.getRepType = function(){
		return repType;
	}
	
	var getTotalPage = function(totalcount , pagesize){
		var r = parseInt(totalcount/pagesize);
		if( totalcount%pagesize == 0) return r;
		else return r+1;
	}
	
	var showBatchOperation =function(b){
		var dom = g('vb_operation');		
		if(dom){
			if(b){
				dom.style.display="block";	
			}
			else{
				dom.style.display="none";			
			}
		}
	}
	
	this.reprintPage = function(){
		g('pageController1').innerHTML
			= g('pageController2').innerHTML 
			= _webfav.pageController.toNavigationHTML(false);
	}
}