/*
 * Rating class
 * 
 * @version 0.1
 * @author Robin van Baalen
 * @website http://www.stylr.nl
 * 
 * Dependancies:
 * - Requires the "jQuery" library version 1.3 or newer.
 * - Makes use of the scrollTo library
 * 
 */

/*
 * Constructor class
 */
Rating = function(s){
	var s = s || {};
	
	Rating.handleSubmit(s.form);
}

/*
 * Catch the submit event
 */
Rating.handleSubmit = function(objForm){
	objForm.bind("submit", function(){
		Rating.validate($(this));
		return false;
	});
	objForm.find("#submitRating").bind("dblclick", function(){
		$(this).click();
		return false;
	});
}

/*
 * Rating invalid, trigger message (already in the DOM)
 */
Rating.invalid = function(){
	$("#rating-message").fadeIn("fast");
	// $.scrollTo($("#rating-message"), 800);
}

/*
 * Rating submit, for now it just submits the form data to the server
 */
Rating.doSubmit = function(objForm){
	var strData = objForm.serialize();
	$.post("/ajax.php?action=addRating", strData, function(data){
		if(data.status == "success"){
			$("#rating-message").removeAttr("class")
				.addClass("success-message")
				.find("p")
					.text("").text(data.message);
			$("#rating-message").fadeIn("fast");
			// $.scrollTo($("#rating-message").parent(), 800);
			Rating.hideMessage(true);
		}
		else {
			$("#rating-message")
//				.find("p").text("").text(data.message)
				.fadeIn("fast");
			// $.scrollTo($("#rating-message").parent(), 800);
			Rating.hideMessage();
		}
	}, "json");
}

/*
 * Rating.hideMessage hide the notice message after a delay
 */
Rating.hideMessage = function(blnSuccess){	
	setTimeout(function(){ 
		$("#rating-message").fadeOut("slow");
		if (blnSuccess) {
			$.fancybox.close();
			Rating.s.form.get(0).reset();
		}
	}, 2500);
	
	
}

/*
 * Validate the form
 */
Rating.validate = function(objForm){
	var intTotal = 0;
	
	// Get the total, check if there is at least one rating.
	$("input:radio:checked", objForm).each(function(i){
		intTotal = parseInt(intTotal) + parseInt($(this).val());
	});
	
	if(intTotal == 10){ // No ratings
		Rating.invalid();
	}
	else {
		Rating.doSubmit(objForm);
	}
}
