function CommunityGroup(jsonObj){
	this.setData(jsonObj);
	this.decorators = new Array();
}
CommunityGroup.prototype.setData = function(data){
	for(i in data){
		this[i] = data[i];
	}
};
CommunityGroup.prototype.addDecorator = function(obj){
	this.decorators.push(obj);
	obj.setItem(this);
};
CommunityGroup.prototype.render = function(el){
	var html = el;
	for(i in this.decorators){
		this.decorators[i].render(el);
	}
	return el;
};

CommunityGroup.prototype.id = "";
CommunityGroup.prototype.name = "";
CommunityGroup.prototype.description = "";
CommunityGroup.prototype.logo = {path: "", name: "", extension: ""};
CommunityGroup.prototype.url = "";
CommunityGroup.prototype.website = "";
CommunityGroup.prototype.members = {count: 0, url: "#"};
CommunityGroup.prototype.topics = {count: 0, url : "#"};
CommunityGroup.prototype.decorators = null;


function CommunityGroupMember(data){
	this.setData(data);
	this.decorators = new Array();
}
CommunityGroupMember.prototype.setData = function(data){
	for(i in data){
		this[i] = data[i];
	}
};
CommunityGroupMember.prototype.addDecorator = function(obj){
	this.decorators.push(obj);
	obj.setItem(this);
};
CommunityGroupMember.prototype.render = function(el){
	var html = el;
	for(i in this.decorators){
		this.decorators[i].render(el);
	}
	return el;
};

CommunityGroupMember.prototype.avatar = "";
CommunityGroupMember.prototype.name = "";
CommunityGroupMember.prototype.status = "";
CommunityGroupMember.prototype.uid = "";
CommunityGroupMember.prototype.url = "";
CommunityGroupMember.prototype.friends = {url: ""};
CommunityGroupMember.prototype.gallery = {count: 0, url: ""};
CommunityGroupMember.prototype.decorators = null;


function CommunityGroupDiscussion(data){
	this.setData(data);
	this.decorators = new Array();
}
CommunityGroupDiscussion.prototype.setData = function(data){
	for(i in data){
		if(i == "last_comment"){
			this[i]= new CommunityDiscussionComment(data[i]);
		} else {
			this[i] = data[i];
		}
	}
};
CommunityGroupDiscussion.prototype.addDecorator = function(obj){
	this.decorators.push(obj);
	obj.setItem(this);
};
CommunityGroupDiscussion.prototype.render = function(el){
	var html = el;
	for(i in this.decorators){
		this.decorators[i].render(el);
	}
	return el;
};

CommunityGroupDiscussion.prototype.url = "";
CommunityGroupDiscussion.prototype.title = "";
CommunityGroupDiscussion.prototype.last_comment =  new CommunityDiscussionComment();
CommunityGroupDiscussion.prototype.decorators = null;

function CommunityDiscussionComment(data){
	if (data == undefined){
		return;
	}
	this.setData(data);
	this.decorators = new Array();
}
CommunityDiscussionComment.prototype.setData = function(data){
	for(i in data){
		this[i] = data[i];
	}
};
CommunityDiscussionComment.prototype.addDecorator = function(obj){
	this.decorators.push(obj);
	obj.setItem(this);
};
CommunityDiscussionComment.prototype.render = function(el){
	var html = el;
	for(i in this.decorators){
		this.decorators[i].render(el);
	}
	return el;
};
CommunityDiscussionComment.prototype.title = "";
CommunityDiscussionComment.prototype.message = "";
CommunityDiscussionComment.prototype.user = {avatar: "", name: "", url: ""};
CommunityDiscussionComment.prototype.user.url = "";
CommunityDiscussionComment.prototype.decorators = null;

function ApropoCommunityQueryParams (){
	this.id = 0;
	this.start = 0;
	this.limit = 5;
	this.sortOrder = null;
};

ApropoCommunityQueryParams.prototype.toString = function () {
	var objToStr =  '?start=' + this.start + '&limit=' + this.limit;
	if (null != this.sortField) {
		objToStr += 'sort=' + this.sortField;
	}
	if (null != this.sortOrder) {
		objToStr += 'order=' + this.sortOrder;
	}
	return objToStr;
};

ApropoCommunityQueryParams.prototype.toPostData = function (){
	var postData = {};
	postData.start = this.start;
	postData.limit = this.limit;
	if (null != this.sortField) {
		postData.sort = this.sortField;
	}
	if (null != this.sortOrder) {
		postData.order = this.sortOrder;
	}
	return postData;
};

function ApropoCommunity(){
	this.groups = new Array();
	this.members = {};
	this.discussions = {};
};

ApropoCommunity.prototype.setOnSuccessCallback = function (callback, objectScope) {
	this[objectScope].callback = callback;
};

ApropoCommunity.prototype.getGroups = function(groupsArr,callback){
	var communityObj = this;
	$.each(groupsArr,function(k,v){
		$.getJSON(baseURL+'community/proxy/'+encodeURI('grup/'+v), function(data) {
			var group = new CommunityGroup(data);
			communityObj.groups.push(group);
			if(callback){
				callback(group);
			}
		});
	});
	return this;
};

ApropoCommunity.prototype.getRecommandedGroups = function (params){
	var recommandedGroups = null;
	$.ajax({
		url: baseURL + 'community/proxy/' + encodeURI('grupuri_recomandate'),
		type: "POST",
		async: false,
		data : params,
		dataType : 'json',
		success: function(data){
			recommandedGroups = data;
		}
	});
	return recommandedGroups;
};

ApropoCommunity.prototype.getGroupMembers = function(params,callback){
	var communityObj = this;
	$.post(baseURL+'community/proxy/'+encodeURI('grup/'+params.id+'/membri'), params.toPostData(), function(data) {
		communityObj.members[params.id] = new Array();
		$.each(data,function(k,v){
			var member = new CommunityGroupMember(v);
			communityObj.members[params.id].push(member);
			if(callback){
				callback(member);
			}
		});
		if (communityObj.members.callback !== undefined && typeof communityObj.members.callback == 'function' ){
			communityObj.members.callback(communityObj.members[params.id]);
		}
	}, "json");
	return this;
};
ApropoCommunity.prototype.getGroupDiscussions = function(params,callback){
	var communityObj = this;
	$.post(baseURL+'community/proxy/'+encodeURI('grup/'+params.id+'/discutii'), params.toPostData(), function(data) {
		communityObj.discussions[params.id] = new Array();
		$.each(data,function(k,v){
			var discussion = new CommunityGroupDiscussion(v);
			communityObj.discussions[params.id].push(discussion);
			if(callback){
				callback(discussion);
			}
		});
		if (communityObj.discussions.callback !== undefined && typeof communityObj.discussions.callback == 'function' ){
			communityObj.discussions.callback(communityObj.discussions[params.id]);
		}
	}, "json");
	
	return this;
};

ApropoCommunity.prototype.groups = null;
ApropoCommunity.prototype.recommandedGroups = null;
ApropoCommunity.prototype.members = null;
ApropoCommunity.prototype.discussions = null;
