if(!mapness){
	var mapness = {};
};

mapness.comments = {
	flagComment : function(commentId, value){
		var DomHolder = null;
		var request = new RequestO("post", "/xhr/comments.php", { 
			"comment[report]" : commentId,
			"comment[karma]" : value
		}, {
		"loading" : 
			function() {
				switch(value){
					case 1:
					case 2:
						DomHolder = $('.comment-value a[rel='+commentId+']').parent();
						$('.comment-value a[rel='+commentId+']').parent().html('<img src="/images/ajax-loader2.gif">');
					break;
					
					case 3:
						DomHolder = $('.flag[rel='+commentId+']').parent();
						$('.flag[rel='+commentId+']').replaceWith('<img src="/images/ajax-loader2.gif">');
					break;
				}
			},
		"complete" :
			function (response) {
				var response = json_parse(response);
				switch (value) {
					case 1:
					case 2:
						var cssClass = 'zero';
						var sign = '';
						if(response.callback.data.karma > 0){
							cssClass = 'positive';
							sign = '+';
						}else if(response.callback.data.karma < 0){
							cssClass = 'negative';
						}
						DomHolder.html('<span class="'+cssClass+'">'+sign+response.callback.data.karma+'</span>');
						var comment = mapness.comments.getCommentById(commentId);
						comment.karma = response.callback.data.karma;
					break;
					
					case 3:
						DomHolder.find('img').replaceWith('<span class="feedback">'+mapness.dict.words.comment_reported+'</span>');
						DomHolder.find('span.feedback').fadeOut(6000);
					break;		
				}
			}
		});
	},
	
	getCommentById : function(commentId){
		commentId = parseInt(commentId, 10);
		for(var i in mapness.journey.comments){
			if(mapness.journey.comments[i].id === commentId){
				return mapness.journey.comments[i];
			}
		}
		
		for(var i in mapness.journey.points){
			for(var j in mapness.journey.points[i].comments){
				if(mapness.journey.points[i].comments[j].id === commentId){
					return mapness.journey.points[i].comments[j];
				}
			}
		}
	}
};

function Comment(DOMForm){
	
	var that = this;
	
	this.parentObject = null;
	this.oreder = null;
	
	this.id = null;
	this.replyTo = 0;
	this.content = '';
	this.date = '';
	this.karma = 0;
	
	this.authorId = null;
	this.authorName = '';
	this.authorLogin = '';
	this.authorEmail = '';
	
	this.sourceForm = DOMForm;
	this.follow = 0;
	this.routeId = null;
	this.journeyId = null;
	
	this.captcha = '';
	
	
	this.sendComment = function(){
		var tempSourceForm = that.sourceForm.html();
		var request = new RequestO("post", "/xhr/comments.php", { 
			"comment[routeId]" : that.routeId, 
			"comment[journeyId]" : that.journeyId,
			"comment[content]" : that.content,
			"comment[follow]" : that.follow,
			"comment[authorName]" : that.authorName,
			"comment[email]" : that.authorEmail,
			"comment[captcha]" : that.captcha,
			"comment[reply]" : that.replyTo
		}, {
		"loading" : 
			function() {
				DOMcache.get('h3.comment-headline').hide();
				that.sourceForm.html('<div id="" class="overlayLoader"><div><img src="/images/ajax-loader2.gif"><span class="note">' + mapness.dict.words.global_saving +'</span></div></div>');
			},
		"complete" :
			function (response) {
				var response = json_parse(response);
				if(response.isSuccess){
					that.authorId = mapness.data.profile.id;
					if(mapness.profile.id !== 0){
						that.authorName = mapness.profile.name;
						that.authorLogin = mapness.data.profile.login;
					}
					if(that.routeId !== null){
						var point = mapness.journey.getPointById(that.routeId);
						that.id = response.callback.data.id;
						that.date = response.callback.data.date;
						that.autorAvatar = response.callback.data.avatar;
						if(that.replyTo !== 0){
							point.addReplyComment(that);
						}else{
							point.comments.push(that);
						}
					}else{
						that.id = response.callback.data.id;
						that.date = response.callback.data.date;
						that.autorAvatar = response.callback.data.avatar;
						if (that.replyTo !== 0) {
							mapness.journey.addReplyComment(that);						
						}else{
							mapness.journey.comments.push(that);
						}
						
					}
					if(mapness.uiState.commentReply && that.sourceForm.html() == mapness.uiState.commentReply.domElement.html()){
						if($('div.comment[rel='+that.replyTo+']').length > 0){
							$('div.comment[rel='+that.replyTo+']:last').after(that.buildHtml());
						}else{
							$('div.comment[id=com_'+that.replyTo+']').after(that.buildHtml());		
						}
					}else{
						if($("div.comment:last").length > 0){
							$("div.comment:last").after(that.buildHtml());	
						}else{
							$("div.commentList").html(that.buildHtml());
						}
					}
					that.sourceForm.html(mapness.dict.words.commentSent);
					that.sourceForm.fadeOut(5000,function(){
						that.sourceForm.html(tempSourceForm);
					});
				}else{
					that.sourceForm.html(tempSourceForm);
					that.sourceForm.find("textarea").val(that.content);
					that.sourceForm.find(".comment-name").val(that.authorName);
					that.sourceForm.find(".comment-email").val(that.authorEmail);
					if(that.follow == 1){
						that.sourceForm.find('input[type=checkbox]').attr('checked',true);
					}
					that.sourceForm.prepend('<div class="error">'+response.callback.error+'</div>');
				}
			}
		});
	}
	
	this.buildHtml = function(){
		var html = '<div id="com_'+that.id+'" rel="'+that.replyTo+'" class="comment ' +(that.order % 2 == 1? 'even':'odd')  + ' ' +(that.replyTo !== 0 ? 'commentReply':'') + ' ' + (that.authorId === mapness.journey.au_id ? 'commentOwner':'') + '">';
		html +=	'<div class="comment-avatar">';
		if(that.authorId != 0){
			html += '<a href="/traveler/'+that.authorId+'"><img src="'+that.autorAvatar+'"/></a>';
		}else{
			html += '<img src="'+that.autorAvatar+'"/>';
		}
			
		html += '</div>'; // div.comment-avatar
		html += '<div class="comment-author subheader">';
		if(that.authorId === mapness.data.profile.id && mapness.data.profile.id !== 0){
			//html += '<a class="button2 delete" rel="'+(that.id)+'"><span>'+mapness.dict.words.comment_delete+'</span></a>';
		}else{
			html += '<a class="button2 flag" rel="'+(that.id)+'"><span>' + mapness.dict.words.comment_spam + '</span></a>'; 
		}		
		html += '<div class="comment-value">';
		if(that.karma > 0){
			html += '<span class="positive">+' +that.karma +'</span>'; 
		}else if(that.karma < 0){
			html += '<span class="negative">' +that.karma +'</span>';
		}else{
			html += '<span class="zero">' +that.karma +'</span>';
		}
		if(that.authorId !== mapness.profile.id && mapness.profile.id !== 0 ){
			html += '<a href="javascript:;" title="mapness.dict.words.comment_love" rel="'+that.id+'" class="buttonLove">'+ mapness.dict.words.comment_love+'</a>';
			html += '<a href="javascript:;" title="mapness.dict.words.comment_hate" rel="'+that.id+'" class="buttonHate">'+ mapness.dict.words.comment_hate+'</a>';
		}
		
		html += '</div>'; // div.comment-value
		if (that.authorId != 0) {
			html += '<span class="author"><a href="/traveler/' + that.authorLogin + '">' + that.authorName + '</a></span> ' + that.date;
		}else{
			html += '<span class="author">' + that.authorName + '</span> ' + that.date;
		}
		html += '</div>'; //div.comment-author
		html += '<div class="comment-text">\
					<p>'+that.content+'</p>\
				</div>\
				<div class="comment-actions">\
					<a href="javascript:;" rel="'+(that.replyTo !== 0 ? that.replyTo : that.id)+'" class="button2 reply"><span>'+mapness.dict.words.comment_reply+'</span></a>\
					<div class="clearer"></div>\
				</div>\
			</div>'; //div.comment
		return html;
	}
	
}; 

mapness.ev.bindCommentDom = function(){
	$(".comment-write .write").live("click", function(e){
		var target = mapness.util.getEventTarget(e);
		mapness.ev.handleComment($(target).closest('div.commentFormHolder'));
		return false;
	});	
	
	$(".comment .reply").live("click", function(e){
		var target = mapness.util.getEventTarget(e);
		mapness.ev.clickReply(target);
		return false;			
	});
	
	$(".flag").live("click", function(e){
		var target = mapness.util.getEventTarget(e);
		mapness.ev.flagComment(target);
		return false;			
	});
	
	$(".comment-value a").live("click", function(e){
		var target = mapness.util.getEventTarget(e);
		mapness.ev.rateComment(target);
		return false;			
	});
};

mapness.ev.handleComment = function(sourceForm){
	var comment = new Comment(sourceForm);
	if(mapness.uiState.commentReply && sourceForm.html() == mapness.uiState.commentReply.domElement.html()){
		comment.replyTo = mapness.uiState.commentReply.replyTo;
	}
	comment.journeyId = mapness.journey.j_id;
	if(mapness.journey.currentActive < 0){ // Journey Comment
		comment.routeId = null; 
	}else{ // Route Comment
		comment.routeId = mapness.journey.getPoints(mapness.journey.currentActive).r_id; 
	}
	comment.content = sourceForm.find("textarea").val();
	if(mapness.profile.id == 0){
		comment.authorName = sourceForm.find(".comment-name").val();
		comment.authorEmail = sourceForm.find(".comment-email").val();
		comment.captcha = sourceForm.find("input.comment-captcha").val();
	}
	comment.follow = (sourceForm.find('input[type=checkbox]:checked').length == 1? 1 : 0);
	comment.sendComment();
};

mapness.ev.clickReply = function(parentComment){
	if(mapness.uiState.commentReply){
		mapness.uiState.commentReply.domElement.remove();
	}
	var commentHolder = $(parentComment).closest("div.comment");
	if($("form.comment").html() !== null){
		commentHolder.after('<div class="commentFormHolder reply comment">'+$("form.comment").html()+'</div>');
		mapness.uiState.commentReply = {
			domElement : commentHolder.next(),
			replyTo : $(parentComment).closest('a').attr('rel')
		}
		if(mapness.profile.id == 0){
			mapness.uiState.commentReply.domElement.find("input:first").focus();
		}else{
			mapness.uiState.commentReply.domElement.find("textarea:first").focus();
		}
	}
};

mapness.ev.flagComment = function(parentComment){
	var id = $(parentComment).closest('a').attr('rel');
	mapness.comments.flagComment(id,3);
};

mapness.ev.rateComment = function(target){
	var id = $(target).closest('a').attr('rel');
	if($(target).closest('a').hasClass("buttonLove")){
		mapness.comments.flagComment(id,1);
	}else{
		mapness.comments.flagComment(id,2);
	}
};

mapness.ui.clearCommentForm = function(){
	$(".comment-name").val("");
	$(".comment-email").val("");
	$("textarea.comment-content").val("");
	$("input.comment-captcha").val("");
	$('input[type=checkbox].comment-notifyme').attr('checked',false);
	$('.commentFormHolder div.error').remove();
	DOMcache.get('h3.comment-headline').show();
};
