String.prototype.characterLimiter = function (maxChars, ending){
	var comment = this.toString();
	maxChars = maxChars != undefined && maxChars != null ? maxChars : 50;
	ending = ending != undefined && ending != null ? ending : ' ... ';
	
	var smiles = comment.match(/<.*>/g);
	
	for (i in smiles) {
		comment = comment.replace(smiles[i], "_"+i+"_");
	}
	
	var commentWords = comment.split(" ", 50);

	var charCount = 0;
	
	var matches = null;
	
	for (i in commentWords) {
		matches = commentWords[i].match(/_(\d)_/);
		if (matches !== null){
			commentWords[i] = smiles[matches[1]];
			charCount += 1;
			continue;
		}
		charCount += commentWords[i].length;
		
		if (charCount >= maxChars){
			commentWords.splice(Number(i) + 1, commentWords.length - (Number(i) + 1));
			break;
		}
	}
	
	if (charCount < maxChars) {
		ending = "";
	}
	
	return commentWords.join(" ") + ending;
};


function ItemLogo (logo, width, height) {
	this.logo = width != undefined ? logo : null; 
	this.width = width != undefined ? width : null;
	this.height = height != undefined ? height : null;
};

ItemLogo.prototype.toString = function (){
	return this.logo.path + this.logo.file + '?' + 'width=' + this.width + '&height='+this.height + '&mode=strict';
};


function CommunityGroupDecoratorLarge(){
}

CommunityGroupDecoratorLarge.prototype.setItem = function(obj){
	var objClass = /(\w+)\(/.exec(obj.constructor.toString())[1];
	if(objClass != "CommunityGroup"){
		throw "Invalid object type";
	}
	this.item = obj;
};

CommunityGroupDecoratorLarge.prototype.render = function(el){
	if(!el.nodeType){
		throw "Invalid html holder";
	}
	var communityAvatar = '';
	var latestMember = '';
	var lastUpdate = '';
	var communityGroup = '';
	
	var li = document.createElement("li");
	
	communityAvatar = $(document.createElement("a")).attr('target', 'blank').attr('target', 'blank').addClass("communityAvatar").attr("href", this.item.url).append(
								$(document.createElement("img")).attr("src", new ItemLogo(this.item.logo, 85, 85).toString())
							);

	if (this.item.latest_member.name != undefined && this.item.latest_member.name != " " && this.item.latest_member.name != ""){
		latestMember = $(document.createElement("a")).attr('target', 'blank').attr("href", this.item.latest_member.url).append(
							$(document.createElement("strong")).text("I s-a alaturat: ")
						).append(this.item.latest_member.name);
	}
	
	if (this.item.last_update != undefined) {
		lastUpdate = $(document.createElement("strong")).text("Actualizat: ").append(
							$(document.createElement("em")).text(this.item.last_update)
						);
	}
	
	communityGroup = $(document.createElement("a")).attr('target', 'blank').addClass("communityGroup").attr("href", this.item.url).text(this.item.name).append(
								$(document.createElement("strong")).text(" ("+this.item.members.count+")")
							);
	
	newDiscussions = $(document.createElement("a")).attr('target', 'blank').addClass("communityDiscussionLink").attr("href", this.item.topics.url).text(this.item.topics.count + " discutii noi");
	
	$(li)
	.append(communityAvatar)
	.append(
			 $(document.createElement("div"))
			 .append(communityGroup)
			 .append($(document.createElement("span")).append(latestMember).append(lastUpdate))
			 .append(newDiscussions)
	);

	$(el).append(li);
};
CommunityGroupDecoratorLarge.prototype.item = null;

function CommunityGroupMemberDecoratorLarge(){
	
}
CommunityGroupMemberDecoratorLarge.prototype.setItem = function(obj){
	var objClass = /(\w+)\(/.exec(obj.constructor.toString())[1];
	if(objClass != "CommunityGroupMember"){
		throw "Invalid object type";
	}
	this.item = obj;
};

CommunityGroupMemberDecoratorLarge.prototype.render = function(el){
	if(!el.nodeType){
		throw "Invalid html holder";
	}
	var pictures_plural = this.item.gallery.count + 0 != 1 ? " poze" : " poza";
	var li = document.createElement("li");
	var avatar = "";
	var status = $(document.createElement('span'));
	var communityUser = "";
	var lastUpdate = "";
	var gallery = "";
	
	var actions = $(document.createElement("div")).addClass("communityMembersOptions");
	
	avatar = $(document.createElement("a")).attr('target', 'blank').addClass("communityAvatar").attr("href", this.item.url).append(
				$(document.createElement("img")).attr("src",this.item.avatar)
			);
	if (this.item.url != null && this.item.url != undefined) {
		community_user = $(document.createElement("a")).attr('target', 'blank').addClass("communityUser").attr("href",this.item.url).text(this.item.name)
	}
	
	if (this.item.status != null && this.item.status != undefined) {
		status = $(document.createElement("span")).text("Status: ").append(
					$(document.createElement("strong")).text(this.item.status)
				);
	}
	
	if (status != "" && this.item.last_update != undefined && this.item.last_update != null) {
		status.append($(document.createElement("em")).text(this.item.last_update));
	}
	
	if (this.item.gallery.url != undefined) {
		gallery = $(document.createElement("a")).attr('target', 'blank').attr("href",this.item.gallery.url).text(this.item.gallery.count + pictures_plural);
	}
	
	if (this.item.friends.url != undefined) {
		actions.append(
			$(document.createElement("a")).attr('target', 'blank').attr("href",this.item.friends.url).text("Vezi prieteni")
		);
	}
	
	if (this.item.url != undefined) {
		actions.append("\n|\n").append($(document.createElement("a")).attr('target', 'blank').attr("href", this.item.url).text("Trimite mesaj"));
	}
	
	$(li)
	.append(avatar)
	.append($(document.createElement("div")).append(community_user).append(status).append(gallery))
	.append(actions);
	
	$(el).append(li);
};
CommunityGroupMemberDecoratorLarge.prototype.item = null;

function CommunityGroupMemberDecoratorSmall(){
	
}
CommunityGroupMemberDecoratorSmall.prototype.setItem = function(obj){
	var objClass = /(\w+)\(/.exec(obj.constructor.toString())[1];
	if(objClass != "CommunityGroupMember"){
		throw "Invalid object type";
	}
	this.item = obj;
};
CommunityGroupMemberDecoratorSmall.prototype.render = function(el){
	if(!el.nodeType){
		throw "Invalid html holder";
	}
	var li = document.createElement("li");
	var pictures_plural = this.item.gallery.count + 0 != 1 ? " poze" : " poza";
	$(li).append(
		$(document.createElement("a")).attr('target', 'blank').addClass("communityAvatar").attr("href",this.item.url).append(
			$(document.createElement("img")).attr("src",this.item.avatar)
		)
	)
	.append(
		$(document.createElement("div")).append(
			$(document.createElement("a")).attr('target', 'blank').addClass("communityUser").attr("href",this.item.url).text(this.item.name)
		)
		.append(
			$(document.createElement("span")).text("Status: ").append(
				$(document.createElement("strong")).text(this.item.status)
			)
		)
		.append(
				$(document.createElement("a")).attr('target', 'blank').attr("href", this.item.gallery.url).text(this.item.gallery.count + pictures_plural)
		)
	)
	.append(
		$(document.createElement("div")).addClass("communityMembersOptions").append(
			$(document.createElement("a")).attr('target', 'blank').attr("href", this.item.friends.url).text("Vezi prieteni")
		).append("\n|\n").append(
			$(document.createElement("a")).attr('target', 'blank').attr("href",this.item.url).text("Trimite mesaj")
		)
	);
	$(el).append(li);
};
CommunityGroupMemberDecoratorSmall.prototype.item = null;

function CommunityGroupDecoratorSmall(){
	
}
CommunityGroupDecoratorSmall.prototype.setItem = function(obj){
	var objClass = /(\w+)\(/.exec(obj.constructor.toString())[1];
	if(objClass != "CommunityGroup"){
		throw "Invalid object type";
	}
	this.item = obj;
};

CommunityGroupDecoratorSmall.prototype.render = function(el){
	if(!el.nodeType){
		throw "Invalid html holder";
	}
	var li = document.createElement("li");
	this.item.website.replace(/http:\/\//, '');
	$(li).append(
		$(document.createElement("a")).attr('target', 'blank').addClass("communityAvatar").attr("href",this.item.url).append(
			$(document.createElement("img")).attr("src", new ItemLogo(this.item.logo, 85, 85).toString())
		)
	).append(
		$(document.createElement("div")).append(
			$(document.createElement("a")).attr('target', 'blank').addClass("communityGroup").attr("href",this.item.url).text(this.item.name)
		).append(
			$(document.createElement("strong")).addClass("communityNoMembers").text("Membrii: ").append(
				$(document.createElement("em")).text(this.item.members.count)
			)
		)
	);
	if (this.item.website != "") {
		$(li).find('div').append(
							$(document.createElement("a")).attr('target', 'blank').addClass("site").attr("href", 'http://' + this.item.website).append(
								$(document.createElement("string")).text("Site: ")
							).append(this.item.website)
						);
	}
	
	$(el).append(li);
};
CommunityGroupDecoratorSmall.prototype.item = null;

function CommunityGroupDiscussionLarge(){

}
CommunityGroupDiscussionLarge.prototype.setItem = function(obj){
	var objClass = /(\w+)\(/.exec(obj.constructor.toString())[1];
	if(objClass != "CommunityGroupDiscussion"){
		throw "Invalid object type";
	}
	this.item = obj;
};

CommunityGroupDiscussionLarge.prototype.render = function(el){
	if(!el.nodeType){
		throw "Invalid html holder";
	}

	if (this.item.last_comment.message != undefined && this.item.last_comment.message != "") {
		lastComment = $(document.createElement("a")).attr('target', 'blank').attr("href",this.item.last_comment.url).text("Comentariu: ").append($(document.createElement('span')).append(this.item.last_comment.message.characterLimiter(30, '...')));
	}
	
	var li = document.createElement("li");
	$(li).append(
		$(document.createElement("a")).attr('target', 'blank').addClass("communityAvatar").attr("href",this.item.last_comment.user.url).append(
			$(document.createElement("img")).attr("src",this.item.last_comment.user.avatar)
		)
	).append(
		$(document.createElement("div")).append(
			$(document.createElement("a")).attr('target', 'blank').addClass("communityUser").attr("href",this.item.last_comment.user.url).text(this.item.last_comment.user.name)
		).append(
			$(document.createElement("a")).attr('target', 'blank').attr("href",this.item.url).text("Post: ").append(
				$(document.createElement("span")).text(this.item.title)
			)
		).append(
			lastComment
		)
	);
	$(el).append(li);
};
CommunityGroupDiscussionLarge.prototype.item = null;

function CommunityGroupDiscussionSmall(){}
CommunityGroupDiscussionSmall.prototype.setItem = function(obj){
	var objClass = /(\w+)\(/.exec(obj.constructor.toString())[1];
	if(objClass != "CommunityGroupDiscussion"){
		throw "Invalid object type";
	}
	this.item = obj;
};

CommunityGroupDiscussionSmall.prototype.render = function(el){

	if(!el.nodeType){
		throw "Invalid html holder";
	}
	var li = document.createElement("li");
	this.item.last_comment.url = this.item.last_comment.url != null ? this.item.last_comment.url : "";
	this.item.last_comment.message = this.item.last_comment.message != null ? this.item.last_comment.message : "";
	this.item.last_comment.user.url  = this.item.last_comment.user.url != null ? this.item.last_comment.user.url : 'javascript://';
	
	$(li).append(
		$(document.createElement("a")).attr('target', 'blank').addClass("communityAvatar").attr("href", this.item.last_comment.user.url).append(
				$(document.createElement('img')).attr('src', this.item.last_comment.user.avatar)
		)
	).append(
		$(document.createElement("div")).append(
			$(document.createElement("a")).attr('target', 'blank').addClass("communityUser").attr("href",this.item.last_comment.user.url).text(this.item.last_comment.user.name)
		).append(
			$(document.createElement("a")).attr('target', 'blank').attr("href",this.item.url).text("Post: ").append(
				$(document.createElement("span")).text(this.item.title)
			)
		).append(
			$(document.createElement("a")).attr('target', 'blank').attr("href",this.item.last_comment.url).text('Comentariu: ').append($(document.createElement('span')).append(this.item.last_comment.message.characterLimiter(30, '...')))
		)
	);
	$(el).append(li);
};
CommunityGroupDiscussionSmall.prototype.item = null;
