$(document).ready(function() {
	$('input.clearonfocus').clearOnFocus();
	
	$('#QapTcha').QapTcha({});
	
	$('.change-password').change(function() {
		if ($('.change-password').is(':checked'))
			$('.change-password-fields').show();
		else
			$('.change-password-fields').hide();
	});
	
	$('li.parent').hoverIntent(function() {
		$(this).find('ul.submenu').slideDown();
	}, function(){
		$(this).find('ul.submenu').fadeOut();
	});
	
	$(".cooperation-image").each(function() {
		var id = $(this).attr('id');
		$(this).css('top', $('#'+id+'Top').text()+'px');
		$(this).css('left', $('#'+id+'Left').text()+'px');
	});

	$('.more-comments').click(function() {
		$('.image-loading').show();
		$.getJSON("http://www.paranormap.net/comments/more/"+$('.article-id').text()+'/'+$('.last-comment-id').text(), function(data, textStatus){
			$('.image-loading').hide();
			$.i = 0;
			$.each(data, function(i,comment){
				if (!comment.User.username) {
					$("<div></div>").addClass('comment-header').text('Ένας επισκέπτης είπε:').appendTo('.comments');
				}
				else {
					$("<div></div>").addClass('comment-header').html('Ο χρήστης <span>'+comment.User.username+'</span> είπε:').appendTo('.comments');
				}
				$("<div></div>").addClass('comment-main').html(comment.Comment.comment).appendTo('.comments');
				$("<div></div>").addClass('comment-footer').html(comment.Comment.created).appendTo('.comments');
				$("<hr />").addClass('comments-div').appendTo('.comments');
				$('.last-comment-id').text(comment.Comment.id);
				$.i = i;
			});
			if ($.i < 9) {
				$('.more-comments').hide();
			}
		});
	});
	
	$('.more-my-comments').click(function() {
		$('.image-loading').show();
		$.getJSON("http://www.paranormap.net/comments/my_more/"+$('.last-comment-id').text(), function(data, textStatus){
			$('.image-loading').hide();
			$.i = 0;
			$.each(data, function(i,comment){
				var link = $("<a></a>").attr('href', "/article/"+comment.Article.id).text(comment.Article.name);
				$("<div></div>").addClass('comment-header').html('Στο άρθρο ').append(link).append(' είπατε:').appendTo('.comments');
				$("<div></div>").addClass('comment-main').html(comment.Comment.comment).appendTo('.comments');
				$("<div></div>").addClass('comment-footer').html(comment.Comment.created).appendTo('.comments');
				$("<hr />").addClass('comments-div').appendTo('.comments');
				$('.last-comment-id').text(comment.Comment.id);
				$.i = i;
			});
			if ($.i < 9) {
				$('.more-my-comments').hide();
			}
		});
	});
});
