var activevotes = 0;
var voteQueue = new Array();
var quandaryData = new Array();
var lastid = 0;
var juststarred = false;

String.prototype.trim=function(){ return this.replace(/^\s*|\s*$/g,''); }
String.prototype.ltrim=function(){ return this.replace(/^\s*/g,''); }
String.prototype.rtrim=function(){ return this.replace(/\s*$/g,''); }

function mySuperSubmit(ev, ov)  { 
	ev = $("#eitherval").val();
	ov = $("#orval").val();
	if (ev.charAt(ev.length-1) == "?") { ev = ev.substring(0, ev.length-1); }
	if (ov.charAt(ov.length-1) == "?") { ov = ov.substring(0, ov.length-1);	}
	ev = ev.trim();
	ov = ov.trim();
	
	if (ev.toLowerCase() == ov.toLowerCase()) {
		$("#errorzone").text("Please use two distinct values.");
		return;
	}
	
	if ((ev.toLowerCase() == "to be") && (ov.toLowerCase() == "not to be")) {
		$("#errorzone").text("Please refer to Act III, Scene I of Shakespeare's Hamlet");
		return;
	}		
	finishSubmit(ev, ov);
}
function finishSubmit(thing1, thing2) {
	$.ajax({
			type: "POST",
			url: "submitquandary",
			timeout:10000,
			data: "eitherOption="+escape(thing1)+"&orOption="+escape(thing2)+"&userid="+userid,
			error: function(){
				window.location = "/";
			},					
			success: function(data){
				window.location = "/submit/" + data;
			}			   
			
	});
}

function addStar() {
	$("#addstar").hide();
	$("#votestar").show();
	sendStar(lastid);
}

function starVoting() {
	juststarred = true;
	voteQueue["stars"] = parseInt(voteQueue["stars"]) + 1;
	$("#addstarvoting").hide();
	$("#starredvoting").show();
	sendStar(parseInt(voteQueue["quandaryID"]));
}

function addStarPermalink(id) {
	$("#addstarperm").hide();
	$("#starperm").show();
	sendStar(id);
}

function addStarStats(id, div) {
	$("#statstar"+div).hide();
	$("#statstarred"+div).show();
	sendStar(id);
}

function sendStar(id) {
	$.ajax({
			type: "POST",
			url: "/addstar",
			timeout:10000,
			data: "userid="+userid+"&quandaryid="+id,
			error: function(){

			},					
			success: function(){
			}			   
			
	});
}

function submitVote(vote) {
	$("#voteoptions").hide();
	$("#graph").hide();
	$("#footer").hide();
	$("#showloader").show();	
	$("#ihaveaquandary").hide();	

	var quandaryVal = parseInt(voteQueue["quandaryID"]);
	lastid = quandaryVal;	
	if (vote == 0) {
		voteQueue["eitherVotes"] = parseInt(voteQueue["eitherVotes"]) + 1;
	} else if (vote == 1) {
		voteQueue["orVotes"] = parseInt(voteQueue["orVotes"]) + 1;
	} else {
		voteQueue["passes"] = parseInt(voteQueue["passes"]) + 1;
	}

	if (submitted) {
		if (activevotes >= 4) {
			if (vote != 2) {
				activevotes = 0;
				sendVote(quandaryVal, vote, 1);
			} else {
				sendVote(quandaryVal, vote, 2);
			}
		} else {
			if (vote != 2) {
				activevotes = activevotes + 1;
			}
			if (activevotes == 4) {
				$("#votestatus").text("One more vote to go...");
			} else {
				$("#votestatus").text((5 - activevotes) + " votes until your results...");
			}			
			populateLittleGraph(voteQueue, vote);
			sendVote(quandaryVal, vote, 2);	
		}
	} else {
		populateLittleGraph(voteQueue, vote);
		sendVote(quandaryVal, vote, 2);		
	}
}

function clearLiveness(id) {
	$.ajax({
			type: "POST",
			url: "/clear",
			timeout:10000,
			data: "quandaryid="+id,
			error: function(){

			},					
			success: function(){
			}			   
			
	});	
}

function sendVote(quandaryid, votetype, q) 
{
	$.ajax({
			type: "POST",
			url: "/submitvote",
			timeout:10000,
			dataType: "json",
			data: "userid="+userid+"&votetype="+votetype+"&quandaryid="+quandaryid+"&queue="+q,
			error: function(){ 
				if (q == 2) {
					endOfTheInternet();
				} else {
					window.location = "/" + activequandary;
				}
			},					
			success: function(data){
				if (q == 2) {
					voteQueue = data;
					$("#voteeitheroption").text(voteQueue["decision1"]);
					$("#voteoroption").text(voteQueue["decision2"]);	
					$("#voteoptions").show();
					$("#graph").show();
					$("#footer").show();
					$("#showloader").hide();	
					$("#ihaveaquandary").show();		
					$("#addstarvoting").show();
					$("#starredvoting").hide();
								
				} else {
					window.location = "/" + activequandary;
				}
			}			   						
	});

}	



function populateLittleGraph(voteQueue, decision) {
	
	eitherVotes = parseInt(voteQueue["eitherVotes"]);
	orVotes = parseInt(voteQueue["orVotes"]);
	stars = parseInt(voteQueue["stars"]);
	passes = parseInt(voteQueue["passes"]);
	
	totalVotes = eitherVotes + orVotes;
	eitherAs100 = Math.round((eitherVotes * 100) / totalVotes);
	orAs100 = Math.round((orVotes * 100) / totalVotes);
	eitherAsPixel = (eitherAs100 * 180) / 100;
	orAsPixel = (orAs100 * 180) / 100;

	if (eitherAsPixel < 40) { 
		eitherAsPixel = 40;
	}
	
	orAsPixel = (orAs100 * 180) / 100;
	if (orAsPixel < 40) { 
		orAsPixel = 40;
	}		
	
	$("#littleEitherBar").css("width",eitherAsPixel+"px");
	$("#littleOrBar").css("width",orAsPixel+"px");
	$("#littleEitherBarShadow").css("width",eitherAsPixel+"px");
	$("#littleOrBarShadow").css("width",orAsPixel+"px");
	$("#littleEitherPercent").text(eitherAs100+"%");
	$("#littleOrPercent").text(orAs100+"%");		
	$("#littleEitherKeyText").text(voteQueue["decision1"]);
	$("#littleOrKeyText").text(voteQueue["decision2"]);
	
	if (passes == 1) {
		passStr = "1 pass";
	} else {
		passStr = passes + " passes";
	}

	if (stars == 1) {
		starStr = "1 star";
	} else {
		starStr = stars + " stars";
	}
		
	$("#littleVotes").text(totalVotes+" votes, " + passStr + ", "+starStr);	
	$("#littletimestamp").text(voteQueue["timestamp"]);
	$("#littlepermalink1").wrapInner("<a href=\"/"+voteQueue["quandaryID"]+"\"></a>");
	$("#littlemailto").wrapInner("<a href=\"mailto:?subject=icantdecide&body=http://icantdeci.de/"+voteQueue["quandaryID"]+"\"></a>");	
	$("#littlepermalinktext").val("http://icantdeci.de/"+voteQueue["quandaryID"]);

	if (!juststarred) {
		$("#addstar").show();
		$("#votestar").hide();
	} else {
		$("#addstar").hide();
		$("#votestar").show();	
	}
	juststarred = false;
}


function permalinkSubmitVote(quandaryid, votetype) {
	$.ajax({
			type: "POST",
			url: "/submitvote",
			timeout:10000,
			data: "userid="+userid+"&votetype="+votetype+"&quandaryid="+quandaryid,
			error: function(){

			},					
			success: function(){
				window.location.reload(true);
			}			   
			
	});
}	

function statsSubmitVote(votetype, quandaryid, div) {
	$("#vote"+div).hide();
	$("#stat"+div).show()
	$.ajax({
			type: "POST",
			url: "/submitvote",
			timeout:10000,
			data: "userid="+userid+"&votetype="+votetype+"&quandaryid="+quandaryid,
			error: function(){

			},					
			success: function(){
				
			}			   
			
	});
}			

function submissionError() {
}

function endOfTheInternet() {
	$("#mainbox").hide();
	$("#showloader").hide();
	myStr = '<div id="warning" style="margin:auto;text-align:center;background:#CCFF66;z-index:1;">Hey, you\'re answering quandaries faster than they\'re coming in.<br><br>We\'ve decided it might be time for you to go outside.</div>';
	$("#showloader").after(myStr);
	$("#warning").hide();
	$("#warning").css("width",300);
	$("#warning").show();
	$("#warning").css("z-index", 100);	
}
