
var readOnlyRating = false;


// COOKIE
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}



// removing error message
function removeLayer(currentForm){
	msgView = window.setTimeout(function(){
		
		jQuery(currentForm).find(".rating-msg").fadeOut(800,function(){
			jQuery(this).remove();
		})
		
	}, 3000);
}

//showing error message
function displayRatingMsg(currentForm,displayMsg){
	if(!jQuery(currentForm).find(".rating-msg").length){
		jQuery(currentForm).find(".article-rating-votes").after("<div style=\"display:none\" class=\"rating-msg\"><span class=\"msg-trick\"></span><span class=\"msg-content\">"+displayMsg+"</span></div>").siblings(".rating-msg").fadeIn(800,function(){
			removeLayer(currentForm);
		});
	}
	else{
		jQuery(currentForm).find(".rating-msg").html(displayMsg);
		removeLayer(currentForm);
	}
}

// document.ready
jQuery(document).ready(function(){
	
	jQuery(".article-rating-form input[type=submit]").hide();
	
	jQuery(".article-rating-form").bind("submit",function(){
		var currentForm =  jQuery(this);
		var currentFormId =  jQuery(this).attr("id");
		
		if (!readOnlyRating){

			var submitUrl = jQuery(this).attr("action");
			var serializeData = jQuery(this).serialize();
			var displayMsg = "";
			
			jQuery.ajax({
				type: "GET",
				url: submitUrl,
				data: serializeData,
				dataType: "xml",
				success: function(XMLHttpRequest, textStatus) {
					
					var ratingAverage = Math.ceil(jQuery(XMLHttpRequest).find("average").text());
					var ratingVotes = jQuery(XMLHttpRequest).find("votes").text();
					
					jQuery(currentForm).find(".article-rating-votes").html("("+ratingVotes+")");
					jQuery(currentForm).find("input.article-star").rating("selectDisplay",ratingAverage-1);
					jQuery(currentForm).find("input.article-star").rating("readOnly");
					
					displayMsg = ratingMsg.confirm;
					readOnlyRating = true;
					
					createCookie("MyNissanRating_"+currentFormId,ratingAverage);
					
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					displayMsg = "<span class=\"error\">"+ratingMsg.serverError+"</span>";
				},
				complete: function(XMLHttpRequest, textStatus){
					displayRatingMsg(currentForm,displayMsg);					
					
				}
			});
		}
		else{
			displayRatingMsg(currentForm,ratingMsg.alreadyVoted);
		}
		
		return false;
	});
	
	
	// init rating
	jQuery(".article-rating-form").each(function(){
		
		var ratingform = jQuery(this);
		var formId = jQuery(this).attr("id");
		var initXML = jQuery(this).attr("action");
		var initRatingAverage = 0;
		var initRatingVotes = "0"
		
		jQuery.ajax({
			type: "GET",
			url: initXML + "?",
			dataType: "xml",
			success: function(XMLHttpRequest, textStatus) {
			
				initRatingAverage = Math.ceil(jQuery(XMLHttpRequest).find("average").text());
				initRatingVotes = jQuery(XMLHttpRequest).find("votes").text();
				jQuery(ratingform).find(".article-rating-votes").html("("+initRatingVotes+")");				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				
				displayMsg = "<span class=\"error\">"+ratingMsg.serverError+"</span>";
			},
			complete: function(){
				
				var cookieDetect = readCookie("MyNissanRating_"+formId)
				
				if(cookieDetect){
					readOnlyRating = true
				}
				
				jQuery(ratingform).find(".article-star").rating({
					callback: function(value){
						jQuery(this.form).trigger("submit");
					}
				});
				jQuery(ratingform).find(".article-star").rating("selectDisplay",initRatingAverage-1);
				if (readOnlyRating){
					jQuery(ratingform).find(".article-star").rating("readOnly")
				}
			}
		});
		
		
	})
	
});