//-----------------------Wall Javascript Functions-----------------------

var currentPostsPage=1;
var currentRepliesPage={};

$(document).ready(function(){
	$("#adminMessageType").change();	
	$("#deadLineTextBox").datepicker({
		dateFormat: 'yy/mm/dd', //DONT CHANGE FORMAT. SERVICE PARSES IN THIS LOCALE
		minDate: 0,
		defaultDate: 0,
		changeMonth: true,
		changeYear: true,
		onSelect: function(){
			$("#deadLineTextBox").removeClass('invalid');		
		}
	});
	$( "#deadLineTextBox" ).datepicker( "option",
			$.datepicker.regional[ 'en' ] );//TODO "es" for spanish page
	
	$(".replyWallPostForm").submit(function() {
		  return false;
	});
	relativizeDates();
});

function getComment(fullWall, quoteToPost){
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/comment/getCommentAjax',
		data: "symbol=" + quoteToPost + "&currentPage=" + (++currentPostsPage),
		beforeSend: function(){
			$("#CommentLoadMoreStatusDiv").html("<div><img src='/img/loading-bar-evolution.gif' alt='' ></div>");
		},
		dataType: 'html',
		success: function(data){						
			$("#commentsDiv").append(data);						
			$("#CommentLoadMoreStatusDiv").html("");
			setTooltip();
			relativizeDates();
		}				
	});		
}

function getCommentReplies(commentid){
	if(currentRepliesPage["comment_" + commentid]){
		currentRepliesPage["comment_" + commentid]++;		
	}else{
		currentRepliesPage["comment_" + commentid]=2;
	}	
	var currentPage=currentRepliesPage["comment_" + commentid];
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/comment/getRepliesAjax',
		beforeSend: function(){
			$("#morerepliesstatus_" + commentid).html("<div><img src='/img/loading-bar-evolution.gif' alt='' /></div>");
		},
		data: "commentid=" + commentid + "&currentPage=" + currentPage,
		dataType: 'html',
		success: function(data){
			$("#morerepliesstatus_" + commentid).html("");
			$("#replies_" + commentid).prepend(data);
			setTooltip();
			relativizeDates();
		}	
	});	
}       

$("#commentshareform").submit(function(e){
	e.preventDefault();
	return false;
});		       		

function doAjaxComment(onMaxLengthErrorMessage){
	if($("#new_post_content").val()!=""){
		if($("#new_post_content").val().length<20000){
			$.ajax({
				type : 'POST', 
				url: baseAppUrl + '/comment/postToQuote',
				beforeSend: function(){
					$("#CommentShareStatusDiv").html("<div><img src='/img/loading-bar-evolution.gif' alt='' ></div>");
				},
				data: $("#commentshareform").serialize(),
				dataType: 'html',
				success: function(data){
					$("#commentsDiv").prepend(data);
					$("#CommentShareStatusDiv").html("");
					$("#new_post_content").val("");
					setTooltip();
					relativizeDates();
				}				
			});
		}else{
			showErrorMessage(onMaxLengthErrorMessage);
		}
	}
}

function doLikeComment(commentid, vote){ 
	$.ajax({ 
		type : 'POST', 
		url: baseAppUrl + '/comment/likeCommentAjax', 
		data: "commentid=" + commentid + "&vote=" + vote, 
		dataType: 'html', 
		success: function(data){
			$("#commentLikeBox_" + commentid).html(data); 
		} 
	});
}

function doCommentDelete(commentid,ownerId){
	if(confirm("Do you really want to delete this comment?")){				
		$.ajax({
			type : 'POST', 
			url: baseAppUrl + '/comment/deleteComment',
			data: "commentid=" + commentid + "&ownerId=" + ownerId,
			dataType: 'json',
			success: function(data){
				$("#comment_" + commentid).remove();						
			}
		});
	}
}

function doCommentReplyDelete(commentid, replyid,ownerId){
	if(confirm("Do you really want to delete this reply?")){				
		$.ajax({
			type : 'POST', 
			url: baseAppUrl + '/comment/deleteCommentReply',
			data: "commentid=" + commentid + "&replyid=" + replyid + "&ownerId=" + ownerId,
			dataType: 'json',
			success: function(data){
				$("#reply_" + commentid + "_" + replyid).remove();						
			}
		});
	}
}
   
function doCommentRating(commentid, rate){				
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/comment/rateComment',
		data: "commentid=" + commentid + "&rate=" + rate,
		dataType: 'html',
		success: function(data){
			$("#commentRateBox_" + commentid).html(data);
			$("#rating_" + commentid).hide();
			$("#commentRateBox_" + commentid).append("<span style='display:none;' id='newrateInfo_" + commentid + "'> " + message("operations.justvoted") + " " + rate + "</span>");
			$("#newrateInfo_" + commentid).fadeIn("slow", function(){
				$("#newrateInfo_" + commentid).fadeOut("slow",function(){
					$("#newrateInfo_" + commentid).remove();
					initRatingBox(commentid);
					$("#rating_" + commentid).show();									
				});
			});
		}
	});
}

// -----------------------	

function moveTo(postId, toId){
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/wall/moveTo',
		data: "postId=" + postId + "&toId=" + toId,
		dataType: 'html',
		complete: function(data){
			if(data=="error"){
				showErrorMessage("You're not an admin!");
			}else{		
				$("#wallpost_"+postId).remove();
				showSuccessMessage("The post has been moved successfully");
			}
		}				
	});		
}


function getWall(fullWall, userToPost){
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/wall/getWallAjax',
		data: "fullWall=" + fullWall + "&userOwner=" + userToPost + "&currentPage=" + (++currentPostsPage),
		beforeSend: function(){
			$("#WPLoadMoreStatusDiv").html("<div><img src='/img/loading-bar-evolution.gif' alt='' ></div>");
		},
		dataType: 'html',
		success: function(data){
			if(fullWall){
				$("#wallPostsDiv").html(data);
			}else{
				$("#wallPostsDiv").append(data);
			}
			$("#WPLoadMoreStatusDiv").html("");
			setTooltip();
			relativizeDates();
			$('.highlightIndicator').tipsy({gravity:'s'});
			$("div.wholikes img.user-pic").tipsy({gravity:'s'});
		}				
	});		
}

function getReplies(wpostID,wallId){
	if(currentRepliesPage["post_" + wpostID]){
		currentRepliesPage["post_" + wpostID]++;		
	}else{
		currentRepliesPage["post_" + wpostID]=2;
	}	
	var currentPage=currentRepliesPage["post_" + wpostID];
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/wall/getRepliesAjax',
		beforeSend: function(){
			$("#morerepliesstatus_" + wpostID).html("<div><img src='/img/loading-bar-evolution.gif' alt='' /></div>");
		},
		data: "wallpostid=" + wpostID + "&wallId=" + wallId+ "&currentPage=" + currentPage,
		dataType: 'html',
		success: function(data){
			$("#morerepliesstatus_" + wpostID).html("");
			$("#replies_" + wpostID).children('.wallpost_replies').prepend(data);
			setTooltip();
			relativizeDates();
		}				
	});	
}       	

function doAdminPost(onMaxLengthErrorMessage, messageType){
	if(messageType.toLowerCase()=="wallpost"){
		doAjaxWallPost(onMaxLengthErrorMessage);
	}else{		
		doAjaxAdminMessage(onMaxLengthErrorMessage);
	}
}

function deleteAdminMessage(id, question){
	if(confirm(question)){
		$.ajax({
			type : 'POST', 
			url: baseAppUrl + '/common/deleteAdminMessage',
			data: "Id=" + id,
			dataType: 'html',
			success: function(data){
				$("#adminMessage_" + id).fadeOut(function(){
					$("#adminMessage_" + id).remove();
				});
			}		
		});
	}
}

function doAjaxAdminMessage(onMaxLengthErrorMessage){
	if($("#new_post_content").val()!=""){
		if($("#deadLineTextBox").val()!=""){				
			if($("#new_post_content").val().length<20000){
				$.ajax({
					type : 'POST', 
					url: baseAppUrl + '/common/postAdminMessage',
					beforeSend: function(){
						$("#WPShareStatusDiv").html("<div><img src='/img/loading-bar-evolution.gif' alt='' /></div>");
					},
					data: $("#wallpostshareform").serialize(),
					dataType: 'html',
					success: function(data){
						$("#announcesList").prepend(data);
						$("#WPShareStatusDiv").html("");
						$("#new_post_content").val("");
//						updateAdminMessagesCount("");
						$("#announcesList").children().first().hide().fadeIn('slow');
					}
				});
			}else{
				showErrorMessage(onMaxLengthErrorMessage);
			}
		}else{
			$("#deadLineTextBox").addClass('invalid');
		}
	}
}

function doAjaxWallPost(onMaxLengthErrorMessage){
	if($("#new_post_content").val()!=""){
		if($("#new_post_content").val().length<20000){
			$.ajax({
				type : 'POST', 
				url: baseAppUrl + '/wall/postToWallAjax',
				beforeSend: function(){
					$("#WPShareStatusDiv").html("<div><img src='/img/loading-bar-evolution.gif' alt='' /></div>");
				},
				data: $("#wallpostshareform").serialize(),
				dataType: 'html',
				success: function(data){
					$("#wallPostsDiv").prepend(data);
					$("#WPShareStatusDiv").html("");
					$("#new_post_content").val("");
					setTooltip();
					relativizeDates();
				}
			});
		}else{
			showErrorMessage(onMaxLengthErrorMessage);
		}
	}
}
			
function doAjaxReply(wallpostid, userToPost, onMaxLengthErrorMessage){
	if($("#reply_" + wallpostid).val()!=""){
		if($("#reply_" + wallpostid).val().length<20000){
			$.ajax({
				type : 'POST', 
				url: baseAppUrl + '/wall/postReply',
				beforeSend: function(){
					$("#morerepliesstatus_" + wallpostid).html("<div><img src='/img/loading-bar-evolution.gif' alt='' /></div>");
				},
				data:$("#replywallpost_" + wallpostid).serialize() + "&userIdTo=" + userToPost,
				dataType: 'html',
				success: function(data){
					$("#morerepliesstatus_" + wallpostid).html("");
					$("#reply_" + wallpostid).val("");
					$("#replywallpost_" + wallpostid).slideUp();
					$("#replies_" + wallpostid).children('.wallpost_replies').append(data);					
					$("#counter_"+ wallpostid).text(parseInt($("#counter_"+ wallpostid).text())+1);
					setTooltip();
					relativizeDates();
				}				
			});
		}else{
			showErrorMessage(onMaxLengthErrorMessage);
		}		
	}
}

function doAjaxCommentReply(commentid, onMaxLengthErrorMessage){
	if($("#reply_" + commentid).val()!=""){
		if($("#reply_" + commentid).val().length<20000){
			$.ajax({
				type : 'POST', 
				url: baseAppUrl + '/comment/postReply',
				beforeSend: function(){
					$("#morerepliesstatus_" + commentid).html("<div><img src='/img/loading-bar-evolution.gif' alt=''/></div>");
				},
				data:$("#replycomment_" + commentid).serialize(),
				dataType: 'html',
				success: function(data){
					$("#morerepliesstatus_" + commentid).html("");
					$("#reply_" + commentid).val("");
					$("#replycomment_" + commentid).slideUp();
					$("#replies_" + commentid).append(data);	
					setTooltip();
					relativizeDates();
				}
			});
		}else{
			showErrorMessage(onMaxLengthErrorMessage);
		}		
	}
}

function doLikePost(wallpostid, vote){
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/wall/likePostAjax',
		data: "wallpostid=" + wallpostid + "&vote=" + vote,
		dataType: 'html',
		success: function(data){
			$("#wallpostLikeBox_" + wallpostid).html(data);
		}				
	});
}

function doLikeReply(wallpostID, replyid, vote){
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/wall/likeReplyAjax',
		data: "replyid=" + replyid + "&vote=" + vote,
		dataType: 'html',
		success: function(data){
			$("#wallpostReplyLikeBox_" + wallpostid + "_" + replyid).html(data);						
		}
	});
}

function doPostDelete(wallpostID, userId,wallUserId){
	if(confirm("Are you sure?")){				
		$.ajax({
			type : 'POST', 
			url: baseAppUrl + '/wall/deletePost',
			data: "wallpostid=" + wallpostID + "&userId=" + userId + "&wUserId=" + wallUserId,
			dataType: 'json',
			success: function(data){
				$("#wallpost_" + wallpostID).remove();						
			}
		});
	}
}

function doReplyDelete(wallpostID, replyID, userId,wallId){
	if(confirm("Are you sure?")){
		$.ajax({
			type : 'POST', 
			url: baseAppUrl + '/wall/deleteReply',
			data: "wallpostid=" + wallpostID + "&replyid=" + replyID + "&userId=" + userId + "&wUserId=" + wallId,
			dataType: 'json',
			success: function(data){
				$("#reply_" + wallpostID + "_" + replyID).parent().remove();
				$("#counter_"+ wallpostID).text(parseInt($("#counter_"+ wallpostID).text())-1);	
			}
		});
	}				
}

function wallpostTemplateReadyActions(wpostID, doShorten){
	$("#post_content_" + wpostID).html(
		taggify(
			$("#post_content_" + wpostID).html(), 
			true, 
			false, 
			false, 
			true
		)
	);
	setTooltip();
	if(doShorten){		
		shortenize();
	}
}

function wallpostReplyTemplateReadyActions(wpostID, replyID){
	$("#reply_content_" + wpostID + "_" + replyID).html(
		taggify(
			$("#reply_content_" + wpostID + "_" + replyID).html(), 
			true, 
			false, 
			false, 
			true
		)
	);
	setTooltip();
}

function commentTemplateReadyActions(commentID){
	$("#post_content_" + commentID).html(
		taggify(
			$("#post_content_" + commentID).html(), 
			true, 
			false, 
			false, 
			true
		)
	);
	setTooltip();	
	initRatingBox(commentID);
}

function initRatingBox(commentID){
	$("#rating_" + commentID).children().not(":radio").hide();
	$("#rating_" + commentID).stars({
		cancelShow: false,
		callback: function(ui, type, value)
		{
			doCommentRating(commentID, value);			
		}
	});
	$("#rating_"+commentID).stars('select', $("#rating_" + commentID + "_avg").html());
}

function commentReplyTemplateReadyActions(commentID, replyID){
	$("#reply_content_" + commentID + "_" + replyID).html(
		taggify(
			$("#reply_content_" + commentID + "_" + replyID).html(), 
			true, 
			false, 
			false, 
			true
		)
	);		
	setTooltip();
}	

function announceTemplateReadyActions(announceID){
	$("#adminMessage_" + announceID).find("p").html(
		taggify(
			$("#adminMessage_" + announceID).find("p").html(), 
			true, 
			false, 
			false, 
			true
		)
	);
}

function loadWalllpostSidebar(wallpostid){
	$.ajax({
		type : 'POST', 
		url: baseAppUrl + '/wall/wallpostSidebar',
		data: "wallpostid=" + wallpostid,
		dataType: 'html',
		success: function(data){						
			$("#sidebarContainer").html(data);
		}				
	});		
}

function getStocksFromMessage(message){
	return message.match(/(^|\s)\$(\.?([a-zA-Z])+(\.[a-zA-Z])?)/g);
}
