var $ = jQuery.noConflict();
$(document).ready(function() {
		
	$("[id^=rating_]").mouseover(function() {
		var rid = $(this).attr("id").split("_")[1];
		var rated = $.cookie('rated_'+rid);
		
		$("#rating_"+rid).children("[class^=star_]").children('img').mousemove(function() {			
			$("#rating_"+rid).children("[class^=star_]").children('img').removeClass();
			$("#rating_"+rid).children("[class^=star_]").children('img').removeAttr("style");

			/* The hovered item number */
			var hovered = $(this).parent().attr("class").split("_")[1];	
			while(hovered > 0) {
				$("#rating_"+rid).children(".star_"+hovered).children('img').addClass("hover");
				hovered--;
			}
			
			if(rated == null) {
				$("#click_to_vote").html("<div class='highlight'>Click to vote</div>").show();
			}
		});		
	});


	$(".floatleft").hover(function() {							   
		null;
	}, function() {	
		var star = 1;
		while(star < 6) {
			$(".star_"+star).children('img').removeClass();
			$(".star_"+star).children('img').addClass("star_img_"+star);			
			star++;
		}
		$("#click_to_vote").hide();
	});	

	/* xxxxx */
	$("[id^=rating_]").children("[class^=star_]").hover(function() {		
		var current_star = $(this).attr("class").split("_")[1];
		var rid = $(this).parent().attr("id").split("_")[1];
		var uid = $("#user_id").val();
		var dataString = 'rating=' + current_star + '&id=' + rid + '&user_id=' + uid;
		var rated = $.cookie('rated_'+rid);
	
		$("#star_submit").val(current_star);
		
		if(rated == 'rated_'+rid) {
			$("#click_to_vote").html("<div class='highlight'>You have already voted</div>").show();	
		}
	});

	$("[id^=rating_]").hover(function() {
		rid = $(this).attr("id").split("_")[1];
		$("#rating_"+rid).children("[class^=submit_star_]").children('img').mouseover(function() {			
			$("#click_to_vote").show();			
		});
	});
	/* xxxxx */


	$("[id^=rating_]").children("[class^=star_]").click(function() {		
		var current_star = $(this).attr("class").split("_")[1];
		var rid = $(this).parent().attr("id").split("_")[1];
		var uid = $("#user_id").val();
		var dataString = 'rating=' + current_star + '&id=' + rid + '&user_id=' + uid;
		var rated = $.cookie('rated_'+rid);
	
		$("#star_submit").val(current_star);	

		if(rated == 'rated_'+rid) {
			$("#star_msg").removeAttr("style");
			//$("#star_msg").html("<div class='highlight'>You have already voted</div>");
			//$("#star_msg").fadeOut(3000);
		} else {
			$.ajax({
				type: "POST",
				url: "rating_product_star.php",
				data: dataString,
				cache: false,
				success: function(html){
					$.cookie('rated_'+rid, 'rated_'+rid);
					var old_voted = $(".star_rating").text().match(/\d+/);
					var new_voted = parseInt(old_voted) + 1;
					$("#div-1c-1 > .star_rating").html("<strong>Customer Rating</strong> (" + new_voted + " Votes)");
					$("#click_to_vote").html("<div class='highlight'>Thanks for rating</div>");					
					//$("#click_to_vote").html("<div class='highlight'>You have already voted</div>");
					
					var submit_star = 1;
					while(submit_star < 6) {
						$(".star_"+submit_star).hide();							
						submit_star++;
					}

					var star = 1;
					while(star < 6) {
						$(".submit_star_"+star).removeAttr("style");
						$(".submit_star_"+star).children('img').addClass("hout");						
						star++;
					}

					var star = 1;
					while(star <= current_star) {
						$(".submit_star_"+star).removeAttr("style");
						$(".submit_star_"+star).children('img').removeClass();
						$(".submit_star_"+star).children('img').addClass("hover");						
						star++;
					}	
				}
			});			
		}


	//		$('#rating_'+rid).load('block_products_star_send.php', {rating: current_star, id: rid, user_id: uid});
	
		});	
});