var voted = false;
var v = 0;
var a;
var xmlhttp;
var vstars = new Array(5); //stars for voting
var currPic;

// once on page load
function loadStars() {
	configVoteStars(currPic);
	star0 = new Image();
	star0.src = "images/star_empty.gif";
	star1 = new Image();
	star1.src = "images/star_full.gif";
}


function highlight(x) {
	if (voted == false) {
		y = x * 1 + 1
		switch (x) {

		case 1:
			getStar(1).src = star1.src;
			$(".voteStr", currPic).html(" one star");
			break;

		case 2:
			for (i = 1; i < y; i++) {
				getStar(i).src = star1.src;
			}
			$(".voteStr", currPic).html(" two stars");
			break;

		case 3:
			for (i = 1; i < y; i++) {
				getStar(i).src = star1.src;
			}
			$(".voteStr", currPic).html(" three stars");
			break;

		case 4:
			for (i = 1; i < y; i++) {
				getStar(i).src = star1.src;
			}
			$(".voteStr", currPic).html(" four stars");
			break;

		case 5:
			for (i = 1; i < y; i++) {
				getStar(i).src = star1.src;
			}
			$(".voteStr", currPic).html(" five stars");
			break;
		}

	}
}

function losehighlight(x) {
	if (voted == false) {
		for (i = 1; i <= 5; i++) {
			getStar(i).src = star0.src;
			$(".voteStr", currPic).html("");
		}
	}
}

function setStar(x) {
	y = x * 1 + 1
	if (voted == false) {
		switch (x) {
		case 1:
			a = "1"
			flash(a);
			break;
		case 2:
			a = "2"
			flash(a);
			break;
		case 3:
			a = "3"
			flash(a);
			break;
		case 4:
			a = "4"
			flash(a);
			break;
		case 5:
			a = "5"
			flash(a);
			break;
		}
		voted = true;
		$(".voteStr", currPic).html("Thank you for your vote!");
		$(".preStarsRateStr", currPic).html("");
							
		// id from navButtons for current pic
		sendVote(getId(), a);		
	}
}

function flash() {
	y = a * 1 + 1
	switch (v) {
	case 0:
		for (i = 1; i < y; i++) {
			getStar(i).src = star0.src;
		}
		v = 1
		setTimeout(flash, 200)
		break;
	case 1:
		for (i = 1; i < y; i++) {
			getStar(i).src = star1.src;
		}
		v = 2
		setTimeout(flash, 200)
		break;
	case 2:
		for (i = 1; i < y; i++) {
			getStar(i).src = star0.src;
		}
		v = 3
		setTimeout(flash, 200)
		break;
	case 3:
		for (i = 1; i < y; i++) {
			getStar(i).src = star1.src;
		}
		v = 4
		setTimeout(flash, 200)
		break;
	case 4:
		for (i = 1; i < y; i++) {
			getStar(i).src = star0.src;
		}
		v = 5
		setTimeout(flash, 200)
		break;
	case 5:
		for (i = 1; i < y; i++) {
			getStar(i).src = star1.src;
		}
		v = 6
		setTimeout(flash, 200)
		break;
	}
}



// get star by its offset 1-5
function getStar(idx) {
	return vstars[idx-1];
}


function configVoteStars(currPic) {
	voted=false;
	//currPic = document.getElementById(getId());
	// re-do the vstars array. all pics of class=vstar in currPic
	vstars = $(".vstar", currPic);
}


function sendVote(psid, vote) {
	var url = "/PopularPics/VoteServlet?psid=" + psid + "&vote=" + vote;
	sendGetReqToServer(url);
}

