var ratingwarning = 0;
var cyclecount = 1;
var cycleup = true;
var stopcycle = false;

$(document).ready(function() {
	
	$('#frmComment').ajaxForm({ 
		beforeSubmit: validate,
        dataType:  'json', 
		success:   processJson
    }); 
	
	$.timer(400, function (timer) {
		if (stopcycle)
			timer.stop();
		else 
			CycleStars();			    				
	});
});

function processJson(data) { 
    	if (data.message == 'success') {
			$("#divCommentSubmit").hide();
			//show newly posted comment
			
			var comment = document.createElement('div');
			comment.setAttribute("class","comment new-comment");

			var innerText = '<div class="heading">';
			if($("#nameCheck").val() == '1')
				innerText += '<p class="title">Posted by <strong>'+$("#commentName").val()+'</strong></p>';
			else
				innerText += '<p class="title">Posted <strong>anonymously</strong></p>';
		
			if($("#rating").val() != '')
					innerText += '<div class="stars star'+$("#rating").val()+'"><div class="empty"></div></div>';
			
			innerText += '<div class="clear"></div>';
			innerText += '</div>';
			innerText += '<p class="text">'+$("#comment").val()+'</p>';
			innerText += '</div>';
			comment.innerHTML = innerText;
			
			//add this comment to the top
			$("#comment-list").prepend(comment);
		}
	}
	
function validate(formData, jqForm, options) { 
    
	var EmailRegEx = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
							
    for (var i=0; i < formData.length; i++) { 
        	if (formData[i].name == 'commentName' 
				&& (formData[i].value == 'Required...' || formData[i].value == '')) {
				$("#error").removeClass("hide");	
				$("#error").html("Please enter your Name");
				$("#commentName").removeClass("off");
				$("#commentName").val("");
				$("#commentName").focus();
				return false; 
			}
			else if (formData[i].name == 'commentEmail' 
				&& (formData[i].value == 'Required...' || formData[i].value == '' || !EmailRegEx.exec(formData[i].value))) {
				$("#error").removeClass("hide");	
				$("#error").html("Please enter a valid Email");
				$("#commentEmail").removeClass("off");
				if(formData[i].value == 'Required...')
					$("#commentEmail").val("");
				$("#commentEmail").focus();				
				return false; 
			}        
			else if ($("#comment").val() == "" || formData[i].value == 'Click here to begin your comment...') {
				$("#error").removeClass("hide");
				$("#error").html("Please enter your a comment");
				$("#comment").removeClass("off");
				$("#comment").val("");
				$("#comment").focus();	
				return false; 
			} 
			else if(formData[i].name == 'rating' && formData[i].value == '' && ratingwarning == 0)  
			{
				$("#error").removeClass("hide");
				$("#error").html("Join in the Star Rating system and select some stars!");
				//only warn once, this has been removed and now required to prevent bots from commenting.
				//ratingwarning = 1;
				return false; 
			} 			
    }
    return true;
}

function FocusComment()
{
	$("#comment").focus();
}

function CycleStars()
{
	MarkStars(cyclecount);
	
	if (!cycleup && cyclecount == 0) {
		cycleup = true;
		cyclecount++;
	}
	else if (!cycleup || (cyclecount == 5 && cycleup)) {
		cycleup = false
		cyclecount--;
	}
	else {
		cyclecount++;
	}
}

function ClearBox(element)
{
	if (element.name == 'comment' && element.value == 'Click here to begin your comment...') {
		element.value = '';
		$("#comment").removeClass("off");
		$("#input").removeClass("hide");		
	}
	else if (element.value == 'Required...') {
		if(element.name == 'commentName')
			$("#commentName").removeClass("off");
		else if(element.name == 'commentEmail')
			$("#commentEmail").removeClass("off");
			
			element.value = '';
	}
}

function VoteStars(star)
{	
	stopcycle = true;
	ClearStars(true);
	for (var i = 1; i < 6; i++) {
		if(i <= star)
			$("#star"+i).addClass("active");
	}
	//clear the rating if they click on the same rating again
	if ($("#rating").val() == star) {
		$("#rating").val("");
		ClearStars(true);
	}
	else 
		$("#rating").val(star);
}

function MarkStars(star,mouseover)
{
	if ($("#rating").val() == '') {
		ClearStars();		
		if(mouseover)
			stopcycle = true;
		for (var i = 1; i < 6; i++) {
			if (i <= star) 
				$("#star" + i).addClass("active");
		}
	}
}

function ClearStars(voting)
{
	if (voting || $("#rating").val() == '') {
		for (var i = 1; i < 6; i++) {
			$("#star" + i).removeClass("active");
		}
	}
}

function PromptFlagComment(comment_id)
{
	if($("#flag"+comment_id).is(":hidden"))
		$("#flag"+comment_id).removeClass("hide");	
	else
		$("#flag"+comment_id).addClass("hide");	
}

function CancelFlagComment(comment_id)
{
	$("#flag"+comment_id).addClass("hide");	
}

function FlagComment(comment_id, d)
{
	jQuery.post("/users/flag_comment.php",{comment_id: comment_id, d: d}, flag_callback, "json");
}

function flag_callback(data){
	if (data.message == 'success') {
		$("#comment" + data.comment_id).removeClass("bad-comment");
		$("#comment" + data.comment_id).addClass("bad-comment");
		$("#flag" + data.comment_id).addClass("hide");
	}
}
		