$(document).ready(function() {
	
	userNeeds();
	
	fbReplyForms();
	
});

var in_action = false;
var error;
var msg;
var href;
var errColor = 'red';
var sucColor = 'green';


function shortenUrl() {
	if(in_action) return false;
	var longurlVal = $('.longurl').val();
	
	if(longurlVal == '' || isUrl(longurlVal) == false) {
		showMessageDock(3);
		return false;
	}

	in_action = true;
	$.ajax({
		type: "POST",
		url: FOLDER + "scurteaza-link?action=shorten", 
		data: {longurl: longurlVal}, 
		success: function (res) {
			alert(res);
			
			$('.longurl').val(res);
			
			in_action = false;
		}
	});
}

function goUp(commentid) {
	if(!isUser()) { return false; }
	
	var toggle_up = $('.comment-actions a#comup_'+commentid+'').attr('toggle');
	var toggle_down = $('.comment-actions a#comdown_'+commentid+'').attr('toggle');
	
	$.ajax({
		type: "POST",
		async: false,
		url: FOLDER + "ajax/itemup.ajax.php",
		data: ({"commentid" : commentid}),
		success: function(res) {
//			alert(res);
			if(res == 9999) {
				showMessageDock(4);
			} else {
				if(toggle_down == 1) {
					$('.comment-actions a#comdown_'+commentid+'').attr('toggle', '0');
					$('.comment-actions a#comdown_'+commentid+'').attr('class', 'comment-down');
					
				} else {
					$('.comment-actions a#comup_'+commentid+'').attr('toggle', '1');
					$('.comment-actions a#comup_'+commentid+'').attr('class', 'comment-up-active');
				}
				
				if(res != '') {
					$('.comment-actions span#votsnr_'+commentid+'').text(res);
				}
			}
			
			in_action = false;
		}
	});
}

function goDown(commentid) {
	if(!isUser()) { return false; }

	var toggle_up = $('.comment-actions a#comup_'+commentid+'').attr('toggle');
	var toggle_down = $('.comment-actions a#comdown_'+commentid+'').attr('toggle');

	$.ajax({
		type: "POST",
		async: false,
		url: FOLDER + "ajax/itemdown.ajax.php",
		data: ({"commentid" : commentid}),
		success: function(res) {
//			alert(res);
			if(res == 9999) {
				showMessageDock(4);
			} else{
				if(toggle_up == 1) {
					$('.comment-actions a#comup_'+commentid+'').attr('toggle', '0');
					$('.comment-actions a#comup_'+commentid+'').attr('class', 'comment-up');
				} else {
					$('.comment-actions a#comdown_'+commentid+'').attr('toggle', '1');
					$('.comment-actions a#comdown_'+commentid+'').attr('class', 'comment-down-active');
				}
				
				if(res != '') {
					$('.comment-actions span#votsnr_'+commentid+'').text(res);
				}
			}
			in_action = false;
		}
	});
}

function themeUp(themeid) {
	if(!isUser()) { return false; }
	
	$.ajax({
		type: "POST",
		async: false,
		url: FOLDER + "ajax/themeup.ajax.php",
		data: ({"themeid" : themeid}),
		success: function(res) {
//			alert(res);
			if(res == 9999) {
				showMessageDock(4);
			} else {
				$('a#thup_'+themeid+'').attr('toggle', '1');
				$('a#thup_'+themeid+'').attr('class', 'comment-up-active');

				if(res != '') {
					$('span#votsnr_'+themeid+'').text(res);
				}
			}
		}
	});
}

function addFbComment() {
	var ldesc = $('.comment_box textarea[name=fbcomment]').val();
	
	if(ldesc == '') {
		showMessageDock(getMessage(4))
	} else {
		var themeid = $('.comment_box').attr('themeid');
		
		$.ajax({
			type: "POST",
			async: false,
			url: FOLDER + "ajax/addfbcomment.ajax.php",
			data: ({"ldesc" : ldesc, "themeid" : themeid}),
			success: function(res) {
//				alert(res);
				$('.users_comments .items_list').append(res);
				$('.comment_box textarea').val('');
				
				updateCommentsTitle();
				fbReplyForms();
				in_action = false;
			}
		});
	}
}

function addFbReply(commentid) {
	var ldesc = $('.reply_block#repl_'+commentid+' textarea').val();
	var themeid = $('.comment_box').attr('themeid');

	if(ldesc == '') {
		showMessageDock(getMessage(4));
	} else {
		if(in_action) return false;
		in_action = true;
		$.ajax({
			type: "POST",
			async: false,
			url: FOLDER + "ajax/addfbreply.ajax.php",
			data: ({"ldesc" : ldesc, "commentid" : commentid, "themeid" : themeid}),
			success: function(res) {
//				alert(res);
				$('.reply_block#repl_'+commentid+' textarea').val('');
				toggleReplyForm(commentid, 0)
				updateCommentsTitle();
				$('.reply_block#repl_'+commentid+' .replies_list').append(res);
				
				in_action = false;
			}
		});
	}
}

function toggleReplyForm(id, type) {
	if(type == 1) {
		 $('.users_comments .item#comm_'+id+' form').show();
	} else if(type == 0) {
		 $('.users_comments .item#comm_'+id+' form').hide();
	}
}

function updateCommentsTitle() {
	var oldNr = $('.users_comments .list_title .nr').text();
	var commTxt = ' comentariu';
	if(oldNr > 0) { commTxt = ' comentarii'; }
	var comm_title = '<span class="nr">'+(+oldNr + 1)+'</span>' + commTxt;
	$('.users_comments .list_title').html(comm_title);
}

function fbReplyForms() {
	var form;
	var openBtns = $('.item .open');
	var cancelBtns = $('.item .cancel');
	
	openBtns.each(function() {
		var id = $(this).attr('sid');
		$(this).click(function() {
			if(isUser()) {
				$('.item#comm_'+id+' form').show();
			}
		});
	});
	
	cancelBtns.each(function() {
		var id = $(this).attr('sid');
		$(this).click(function() {
			 $('.item#comm_'+id+' form').hide();
		});
	});
}

function userNeeds() {
	var form = $('.users_activities form');
	var name;
	var activityid;
	var text;
	var links = $('.users_activities .top_box li a');
	var title = $('.users_activities input[name="title"]');
	var textarea = $('.users_activities textarea');
	var msgP = $('.users_activities .hint');
	
	links.each(function() {
		var item = $(this);
		item.click(function() {
			activityid = item.attr('activityid');
			msg = item.text();
			
			$('.users_activities input[name="activityid"]').val(activityid);
			showMessage('', msg, msgP);
			links.each(function() {
				$(this).removeClass('active');
			});
			item.addClass('active');
			return false;
		});
	});
	
	form.submit(function() {
		var titleVal = title.val();
		var descVal = textarea.val();
		
		if(titleVal == '' || descVal == '') {
			showMessageDock(getMessage(12))
//			showMessage(errColor, 12, msgP);
			return false;
		} else {
			
		}
	});
}

function isUser() {
	var userid = $('.tooltip').attr('userid');
	if(userid == 0) { return false; } else { return true; }	
}

function showMessage(color, msg, msgP) {
	var myMsg = getMessage(msg);
	msgP.css({'color':color}).text(myMsg);
}









