/**
 * @author Isaiah
 */

 jQuery.fn.rating = function()
 {
 	return this.each(function()
	{
		$(this).hide();
		var rating = $("<ul></ul>").addClass("rating");
		var a = 1;
		$(this).find("option").each(function(){
			var link = "#" + a;
			var star = $("<a class=star>star</a>").val("href", link).addClass("star").attr("title", "Vote "+a);
			var name = $(this).parent().attr("name");
			var list = $("<li></li>").append(star);
			list.attr("id", name+a);
			list.attr("title", name);
			rating.append(list);

			if ($(this).attr("selected")==true && $(this).val() != 1 || $(this).attr("disabled") == true)
			{
				starID = "#" + $(this).parent().attr("name") + $(this).val();
				rating.find(starID).addClass("selected").prevAll().addClass("beforeSelected");
			}
			a++;
		});
		if ($(this).attr("readonly") != true && $(this).attr("disabled") == false) {
			$(rating).find("li").each(function(){
				$(this).hover(function(){
					$(this).parent().find("li").addClass("afterHover");
					$(this).addClass("hover").prevAll().addClass("beforeHover").andSelf().removeClass("afterHover");
				}, function(){
					$(this).parent().find("li").removeClass("afterHover");
					$(this).removeClass("hover").prevAll().removeClass("beforeHover");
				});
				$(this).click(function(){
					$(this).parent().find("li").removeClass("selected beforeSelected");
					$(this).addClass("selected");
					$(this).prevAll().addClass("beforeSelected");
					var name = "[name=" + $(this).attr("id").replace(/[\d]/g, "") + "]";
					$(name).attr("selectedIndex",$(this).attr("id").replace(/[^\d]/g, "")-1);
					$(name).change();
				});
			});
		}
		rating.insertBefore(this);
	});
 };

 
 