confoo = {
	session: {}
};

$.fn.moreButton = function(url, $div, callback) {
	return this.each(function(i, element) {
		var
			$btn = $(element);
		$btn.click(function() {
			var nextPage = confoo.session.nextPage || 2;
			queryCfpPage(url, $div, nextPage, callback, $btn);
			return false;
		});
	});
}

$.fn.flyStatus = function(text) {
	return this.each(function(i, element) {
		var
			$btn = $(element),
			$text = $('<span class="fly-status">'+text+'</span>');
		
		$text.css({
			'top': $btn.offset().top - 10,
			'left': $btn.offset().left + 10
		}).animate({
			'top': '-=30',
			'opacity': 0
		}, 600, 'linear', function() {
			$text.remove();
		});
		
		$('#content_wrapper').append($text);
	});
}

$.fn.showSpinner = function() {
	$(this).append('<div class="spinner"></div>');
}

$.fn.hideSpinner = function() {
	$('div.spinner', this).remove();
}

/*
 * Attach events to all voting buttons on the page
 */
function bindBtnVote(url) {
	
	var openComment = null;
	
	$('div.rating a.btnVote.up, div.rating a.btnVote.down').click(function(ev) {
		var $btn    = $(this);
		var id      = $btn.metadata().id;
		var value   = $btn.metadata().value;
		var $rating = $btn.closest('div.rating');

		$.ajax({
			url: url+"/"+id+"/"+value,
			success: function(content, statusText, req) {
				if (req.status == 202) {
					$btn.flyStatus(value);
					$('div.bar', $rating).replaceWith(content.barHtml);
					$('a.btnVote', $rating).removeClass('selected');
					$btn.addClass('selected');
				}
			},
			error: function(req) {
				if (req.status == 400) {
					$btn.flyStatus("<?php echo __('Already voted') ?> "+value);
				}
			}
		});
		return false;
	});
	
	$('div.rating a.btnVote.comment').click(function(ev) {
		var $btn     = $(this);
		var id       = $btn.metadata().id;
		var $block   = $btn.closest('div.text');
		var $form = $('#comment-form');
		
		if ($form.length > 0) {
			$form.animate({
				height: 'hide'
			}, 500, function() {
				$form.remove();
			});
			if (openComment == id) {
				return false;
			}
		}
		
		var $comment = $('<div id="comment-form"></div>');
		
		$block.append($comment);
		$.ajax({
			url: url+"/"+id+"/comment",
			success: function(content, statusText, req) {
				openComment = id;
				bindCommentForm($btn, $comment, content);
			}
		});
		return false;
	});
}

function bindCommentForm($btn, $comment, content) {
	$comment.html(content);
	var $submitBtn = $(':submit', $comment);
	var $form = $submitBtn.closest('form', $comment);
	$submitBtn.click(function() {
		$.ajax({
			url:  $form.attr('action'),
			type: 'POST',
			data: $form.serialize(),
			success: function(content, statusText, req) {
				if (req.status == 202) {
					$comment.animate({
						height: 'hide'
					}, 500, function() {
						$comment.remove();
					});
					$btn.addClass('selected');
				} else {
					bindCommentForm($btn, $comment, content);
				}
			}
		});
		return false;
	});
}

/*
 * Fetch a page (CFP sessions pagination) into the specified div container.
 * Hide/show the "More" button.
 */
function queryCfpPage(url, $div, nextPage, callback, $btn) {
	var tag = confoo.session.tag || null;
	var $pageBtns = $('div.pager .page');
	
	$div.showSpinner();
	$.ajax({
		url: url+'?page='+nextPage+(tag ? '&tag='+tag : ''),
		success: function(content, statusText, req) {
			$div.hideSpinner();
			if (req.status == 200) {
				var numPages = confoo.session.numPages = req.getResponseHeader('Num-Pages');
				confoo.session.nextPage = nextPage+1;
				$div.append(content);
				if (numPages < 2) {
					$btn.hide();
				}
				else if (nextPage == numPages) {
					$btn.hide();
				}
				else {
					$btn.show();
				}
				$pageBtns.each(function(i, pageBtn) {
					$pageBtn = $(pageBtn);
					if (i >= numPages) {
						$pageBtn.hide();
						return;
					} else {
						$pageBtn.show();
					}
					var pageNum = $pageBtn.html();
					$pageBtn.attr('href', url+'?page='+pageNum+(tag ? '&tag='+tag : ''));
				});
				callback();
			}
		}
	});
}
