function QandA() {
	var qa = this;
	this.answerpref = "answer_";
	this.voteup = "voteUp";
	this.votedown = "voteDown";
	this.sclass = "selected";
	this.hclass = "hover";
	this.processpage = "/store/vote-answer.php";
	this.more = "moreAns";
	this.less = "lessAns";
	this.moreText = "moreAnsText";
	this.voteCount = "count";
	
	$("." + qa.voteup + ", ." + qa.votedown).hover(function() {
		qa.voteHover($(this));
	},function() {
		qa.voteHover($(this));
	});
	
	$("." + qa.voteup + ", ." + qa.votedown).click(function() {
		qa.voteAnswer($(this));
	});
	
	$("." + qa.more).click(function(e) {
		e.preventDefault();
		qa.showMore($(this));
	});
	
	$("." + qa.less).click(function(e) {
		e.preventDefault();
		qa.showLess($(this));
	});
}

QandA.prototype.voteHover = function(hovered) {
	var qa = this;
	if (hovered.hasClass(qa.hclass)) {
		$("." + qa.voteup + ", ." + qa.votedown).removeClass(qa.hclass);
	} else {
		$("." + qa.voteup + ", ." + qa.votedown).removeClass(qa.hclass);
		hovered.addClass(qa.hclass);
	}
}

QandA.prototype.showMore = function(clicked) {
	var qa = this;
	li = clicked.parents("li");
	var thisShowText = $("." + qa.moreText, li);
	thisShowText.css("display","inline");
	clicked.css("display","none");
}

QandA.prototype.showLess = function(clicked) {
	var qa = this;
	li = clicked.parents("li");
	var thisMoreButton = $("." + qa.more, li);
	var thisHideText = $("." + qa.moreText, li);
	thisHideText.css("display","none");
	thisMoreButton.css("display","inline");
}

QandA.prototype.voteAnswer = function(clicked) {
	var qa = this;
	var vote = clicked.index();
	var answerid = parseInt(clicked.parents("li").attr("id").replace(qa.answerpref, ""));
	vote = (vote > 0 ? 0 : 1);
	qa.submitVote(vote, answerid);
}

QandA.prototype.submitVote = function(vote,answerid) {
	var qa = this;
	if (typeof vote != "number" || typeof answerid != "number") return false;
	if (vote > 1) vote = 1;
		else if (vote < 0) vote = 0;
		
	$.ajax({
		type: "GET",
		url: qa.processpage,
		data: "answerid=" + answerid + "&vote=" + vote,
		cache: false,
		success: function(resultCase) {
			qa.highlightSubmitted(vote, answerid, resultCase);
		}
	});
}

QandA.prototype.highlightSubmitted = function(vote, answerid, resultCase) {
	resultCase = "4";


	var qa = this;
	var answer = $("#" + qa.answerpref + answerid);
	var selected = $('.' + (vote > 0 ? qa.voteup : qa.votedown), answer);
	var selectedCount = $("." + qa.voteCount, selected);
		
	$("." + qa.voteup + ", ." + qa.votedown, answer).removeClass(qa.sclass);
	
	selected.addClass(qa.sclass);
	
	switch(resultCase) {
		case "1":	// new vote and voting up
			$("." + qa.voteup + " ." + qa.voteCount, answer).text(parseInt($("." + qa.voteup + " ." + qa.voteCount, answer).text()) + 1);
			break;
		case "2":	// new vote and voting down
			$("." + qa.votedown + " ." + qa.voteCount, answer).text(parseInt($("." + qa.votedown + " ." + qa.voteCount, answer).text()) + 1);
			break;
		case "3":	// already voted and voting up
			$("." + qa.voteup + " ." + qa.voteCount, answer).text(parseInt($("." + qa.voteup + " ." + qa.voteCount, answer).text()) + 1);
			$("." + qa.votedown + " ." + qa.voteCount, answer).text(parseInt($("." + qa.votedown + " ." + qa.voteCount, answer).text()) - 1);
			break;
		case "4":	// already voted and voting down
			$("." + qa.votedown + " ." + qa.voteCount, answer).text(parseInt($("." + qa.votedown + " ." + qa.voteCount, answer).text()) + 1);
			$("." + qa.voteup + " ." + qa.voteCount, answer).text(parseInt($("." + qa.voteup + " ." + qa.voteCount, answer).text()) - 1);
			break;
	}
}

// Update Object Values
// Pass values using an object in the form of "variable_Name":"Value"
// Ex. $.obj.updateVal({"maxresults":"10", "callTo":"autocomplete_binary.php"});
QandA.prototype.updateVal = function(update) {
	var qa = this;
	
	if (!update || update.constructor !== Object) return false;
	
	for (var vname in update) {
		if (!vname || !update[vname]) return false;
		qa[vname] = update[vname];
	}
}
