/**
 * @Author Suk Honzeon
 *
 * @description
 * 		来吧成员新鲜事数据js封装。
 * 		使用方式：
 *			memberNewsConf = new MemberNewsConf();
 *          // 显示内容的回调函数
 *			// function handleResponse(MemberNewsConf, groupAppId, htmlId, curNo){}
 *			// 		MemberNewsConf 配置
 *			//		groupAppId	应用AppId
 *			//  	htmlId	用户显示内容的容器ID，通常用div容器
 *			//		curNo页码
 * 			memberNewsConf.setHandler(handleResponse);
 *          // 设置页大小，默认9条
 *			// memberNewsConf.pageSize=9;
 *          // 设置需要过滤的应用
 *			// memberNewsConf.ignoreAppIds = ['photo','pic_upload'];
 *			// 初始化数据   设置好参数，再调用
 * 			memberNewsConf.init(jsondata);
 *			// 显示内容
 * 			memberNewsConf.handleResponse(groupAppId, htmlId, curNo);
 */
var MemberNewsConf = function(pSize) {
    this.pageSize = 9;  
    if (pSize && !isNaN(pSize)) {
        if (pSize > 0) {
            this.pageSize = pSize;
        }
    }
	this.origData = null; 
	this.response = null;
	
	this.totalResults = 0;
	this.allPageNo = 1;	 
	this.ignoreAppIds = [];
	
	this.handleResponseCallBack = function(conf, groupAppId, htmlId, curNo){};
	
	this.init = function(data) {
		this.origData = data || null;
		if (this.origData == null || this.ignoreAppIds.length < 1) {
			this.response =  this.origData;
			this.totalResults = this.response.totalResults || 0;
		} else {
			// 过滤掉相册数据
			this.response = new Object();
			this.response['entry'] = jQuery.grep(this.origData.entry,this.grepData.bind(this));
			this.response['totalResults'] = this.response['entry'].length || 0;
			this.totalResults = this.response.totalResults;
		}
		
		this.allPageNo = (this.totalResults/this.pageSize
				 		  > parseInt(this.totalResults/this.pageSize))
						 	 ? parseInt(this.totalResults/this.pageSize) + 1
						 	 : parseInt(this.totalResults/this.pageSize);
						 	 
		
		
	}
	
	this.handleResponse = function(groupAppId, htmlId, curNo) {
		this.handleResponseCallBack(this, groupAppId, htmlId, curNo);
	};
		
	this.setHandler = function(fn) {
		if (typeof(fn) == 'function') {
			this.handleResponseCallBack = fn;
		}
	};
	
	/** 
	 * 过滤数据, 属于需要过滤的数据返回false，否则返回true
	 */
	this.grepData = function(n, idx) {
		// 断定 this.ignoreAppIds 一定是数组
		// 不可以被设置为其他的类型
		if (typeof(this.ignoreAppIds) == 'array') {
			for (var i=0; i<this.ignoreAppIds.length; i++) { 
				if (n.appId == this.ignoreAppIds[i])
					return false;
			}
		} 
		
		return true;  
	};
	
	/****************************************************** 
	   这些类型的图片都是自己在天涯上找的，没有文档说明，
	   如果有一天新鲜事接口改变了风格，或者更换了图片，
	   需要自行处理。
	  
	   
	   2010-1-13 社区改变实现
	   
	 ******************************************************/
	/** @deprecated 不再提供图片  */
	this.appIcons = {
		'blog_article' : ' http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/blog_article.gif',
		'blog_create' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/blog_article.gif',
		'bulo_create' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/bulo_create.gif',
		'bulo_join' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/bulo_join.gif',
		'city_bbs_article' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/bbs_article.gif',
		'friend_add' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/friend_add.gif',
		'gadget_install' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/gadget_install.gif',
		'main_bbs_article' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/bbs_article.gif',
		'party_create' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/party_create.gif',
		'party_join' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/party_join.gif',
		'pic_upload' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/pic_upload.gif',
		'sub_bbs_article' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/bbs_article.gif',
		'user_profile' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/user_profile.gif',
		'bulo_article' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/bbs_article.gif',
		'bulo_log' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/bulo_create.gif',
		'co_game' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/bbs_article.gif',
		'ty_adsp' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/blog_article.gif',
		'tymood' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/user_profile.gif',
		'brand_visit' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/blog_article.gif',
		'brand_vouch' : 'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/blog_article.gif',
		'guanzhu_add' :'http://static.tianyaui.com/img/static/2008/OpenPlatform/activity/blog_article.gif', 
		'' : ''
	};
	
	/* icons ues css + png   
	   // 样式名跟应用名一致——日后可能按照一定的规则增加前缀后缀, 
	   // 不需要再做静态的对应
	this.appIconsCss = {
		'blog_article' : 'blog_article',
		'blog_create' : 'blog_create',
		'bulo_create' : 'bulo_create',
		'bulo_join' : 'bulo_join',
		'city_bbs_article' : 'city_bbs_article',
		'friend_add' : 'friend_add',
		'gadget_install' : 'gadget_install',
		'main_bbs_article' : 'main_bbs_article',
		'party_create' : 'party_create',
		'party_join' : 'party_join',
		'pic_upload' : 'pic_upload',
		'sub_bbs_article' : 'sub_bbs_article',
		'user_profile' : 'user_profile',
		'bulo_article' : 'bulo_article',
		'bulo_log' : 'bulo_log',
		'co_game' : 'co_game',
		'ty_adsp' : 'ty_adsp',
		'tymood' : 'tymood',
		'brand_visit' : 'brand_visit',
		'brand_vouch' : 'brand_vouch',
		'guanzhu_add' :'guanzhu_add', 
		'logo_update' : 'logo_update',
		'laiba' : 'laiba',
		'brand' : 'brand',
		'share' : 'share',
		'gift' : 'gift',
		'vote' : 'vote',
		'tour' : 'tour',
		'' : ''
	
	}
	*/
	
	this.apps = {
		 'all' : {id:'all', name:'全部', subApps:[]},
		 'article' : {id:'article', name:'帖子', 
		 	subApps:[
		 				{id:'city_bbs_article'},
		 				{id:'main_bbs_article'},
		 				{id:'sub_bbs_article'}
		 			]},
		 'blog' : {id:'blog', name:'博客', 
		 	subApps:[
		 				{id:'blog_article'},
		 				{id:'blog_create'}
		 			]},
		 /* 业务上不需要相册了	
		 'photo' : {id:'photo', name:'相片', 
		 	subApps:[
		 				{id:'pic_upload'}
		 			]},
		 */	
		 'app' : {id:'app', name:'应用', 
		 	subApps:[
		 				{id:'gadget_install'}
		 			]},
		 'group' : {id:'group', name:'来吧', 
		 	subApps:[
		 				{id:'bulo_create'},
		 				{id:'bulo_join'},
		 				{id:'bulo_article'},
		 				{id:'bulo_log'}
		 			]},
		 'mood': {id:'mood', name:'心情', 
		 	subApps:[
		 				{id:'tymood'}
		 			]},
		'brand_service' : {id:'brand_service', name:'服务', 
		 	subApps:[
		 				{id:'brand_visit'},
		 				{id:'brand_vouch'}
		 			]},
		'other' : {id:'other', name:'其它', 
		 	subApps:[
		 				{id:'user_profile'},
		 				{id:'friend_add'},
		 				{id:'co_game'},
		 				{id:'ty_adsp'},
		 				{id:'guanzhu_add'}
		 			]}
		};
		
		/** @deprecated 不再提供图片  */
		this.getAppIconUrl = function (appId) {
			return this.appIcons[appId];
		};
		
		this.getAppIconCss = function (appId) {
			// 样式名跟应用名一致——日后可能按照一定的规则增加前缀后缀, 
	   		// 不需要再做静态的对应
			//return this.appIconsCss[appId];
			return appId;
		};
		
		this.isAppIdInGroup = function(appId, groupAppId) {
			var groupApps = this.apps[groupAppId];
			if (appId == '' || groupApps == ''
					|| groupApps == undefined ) {
				return false;
			}
			for (var i = 0; i < groupApps.subApps.length; i++) {
				if (appId == groupApps.subApps[i].id) {
					return true;
				}
			}
			return false;
		};
		
		
}


Function.prototype.bind = function(scope) {
  var _function = this;
 
  return function() {
    return _function.apply(scope, arguments);
  }
}

