var base = '/';
var rated;

// Rollover for image Stars
function rating(star){
	if (!rated) {
		for (i=1; i<=5; i++) {		
			if (i <= star) {
				$("#Rate_" + i).addClass("on");	
			} else {
				$("#Rate_" + i).removeClass("on");
			}
		}
	}
}

// For when you roll out of the the whole thing
function off(){
	if (!rated) {
		for (i=1; i<=5; i++) {		
			$("#Rate_"+i).removeClass("on");
		}
	}
}

function rate(user, id, star) {
	rating(star);
	$.ajax({
		url: base + "ivote.php",
		data: "storyid=" + id + "&user=" + user +"&score=" + star,
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
				alert(rc);
			} else {
				$('#RateStatus').html('Rating saved.');
				rated = 1;
			}			
		} 
	});
}

function changeVote(user, id, htmlid, thumbup) {
//	if (user == '0') {
//		$("#myvote-" + htmlid).html("<span>Please<br>login</span>");
//		return;
//	}
	$.ajax({
		url: base + "ivote.php",
		data: "storyid=" + id + "&user=" + user +"&thumbup=" + thumbup,
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
				alert(rc);
			} else {
				$("#votecnt-" + htmlid).html(rc + ' points');
				if (thumbup == 1 || thumbup == 0) {
					$("#myvote-" + htmlid).html("");
				} else {
					$("#myvote-" + htmlid).html("");
				}
			}			
		} 
	});
}

function save(id, action, htmlid) {
	$.ajax({
		url: base + "save.php",
		data: "storyid=" + id + "&action=" + action,
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
				alert(rc);
			} else {
				if (action == 'save')
					$('#save-' + htmlid).html("Saved");
				else
					$('#save-' + htmlid).html("Unsaved");
			}
		} 
	});
}

function report(user, id, htmlid) {
	$.ajax({
		url: base + "ireport.php",
		data: "storyid=" + id + "&user=" + user,
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
				alert(rc);
			} else {
				$('#vbox-' + htmlid).html("<span class=\"error\">spam</span>");
			}
		} 
	});
}

function changeCommentVote(user, id, thumbup) {
	params = "commentid=" + id + "&user=" + user;
	if (!thumbup) {
		params += "&unvote=true";
	}
	$.ajax({
		url: base + "ivotecomment.php",
		data: params,
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
				alert(rc);
			} else {
				$("#cvote-" + id).html(rc);
				$('#ratebuttons-' + id).hide();
			}
		} 
	});
}