jQuery.fn.rating = function(){
	return this.each(function(){
		var div = jQuery("<span/>").attr({
			title: this.title,
			className: this.className
		}).insertAfter( this );
		
		var averageRating = this.title.split(/:\s*/)[1].split("."),
		url = this.action,
		averageIndex = averageRating[0],
		averagePercent = averageRating[1];
		
		var index_gold = parseInt(averageIndex / 3);
		var index_silver = averageIndex - 3 * index_gold;
		
		var index = 1 + parseInt(averageIndex - 3 * index_gold);
		
		for (x=0; x < index; x++) {
			div.attr('class','rating');
			div.append( this.value == "0" ? "":
				"<span class='star' title='Goldsterne: " + index_gold + " Silbersterne: " + index_silver + "." + averageRating[1] + "'><a target='_blank' href='http://www.willwissen.net/faq_ww.html#points'></a></span>" );
		}
		
		div.addClass('ratingPos').css("width", index * 18 + "px");

		var stars = div.find("span.star");
		reset();

		var div = jQuery("<span/>").attr({
			title: this.title,
			className: this.className
		}).insertAfter(this);

		for (x=0; x < index_gold; x++) {
			div.attr('class','rating_gold');
			div.append( this.value == "0" ? "":
				"<span class='star_gold' title='Goldsterne: " + index_gold + " Silbersterne: " + index_silver + "." + averageRating[1] + "'><a target='_blank' href='http://www.willwissen.net/faq_ww.html#points'></a></span>" );
		}
		div.addClass('ratingPos').css("width", index_gold * 18 + "px");
		var stars = div.find("span.star_gold");
		reset_gold();

		// Reset the stars to the default index.
		function reset(){
		
			stars.slice(0,index_silver).addClass("on");

			var percent = averagePercent ? averagePercent : 0;
			if (percent > 0) {
				stars.eq(index_silver).addClass("on").children("a").css("width", percent + "%");
			}

		}
		
		function reset_gold(){
			
			stars.slice(0,index_gold + 1).addClass("on");
		}
	});
};
jQuery.fn.ratingSmall = function(){
	return this.each(function(){
		var div = jQuery("<span/>").attr({
			title: this.title,
			className: this.className
		}).insertAfter(this);
		
		var averageRating = this.title.split(/:\s*/)[1].split("."),
		url = this.action,
		averageIndex = averageRating[0],
		averagePercent = averageRating[1];
		
		
		var index_gold = parseInt(averageIndex / 3);
		var index_silver = averageIndex - 3 * index_gold;
		var index = 1 + parseInt(averageIndex - 3 * index_gold);
		
		for (x=0; x < index; x++) {
			div.attr('class','rating_sm');
			div.append( this.value == "0" ? "":
				"<span class='star_sm' title='Goldsterne: " + index_gold + " Silbersterne: " + index_silver + "." + averageRating[1] + "'><a target='_blank' href='http://www.willwissen.net/faq_ww.html#points'></a></span>" );
		}

		div.addClass('ratingPos_sm').css("width", index * 11 + "px");
		var stars = div.find("span.star_sm");
		reset();
		
		var div = jQuery("<span/>").attr({
			title: this.title,
			className: this.className
		}).insertAfter(this);

        for (x=0; x < index_gold; x++) {
			div.attr('class','rating_sm_gold');
			div.append( this.value == "0" ? "":
				"<span class='star_sm_gold' title='Goldsterne: " + index_gold + " Silbersterne: " + index_silver + "." + averageRating[1] + "'><a target='_blank' href='http://www.willwissen.net/faq_ww.html#points'></a></span>" );
		}
		div.addClass('ratingPos_sm').css("width", index_gold * 11 + "px");
		var stars = div.find("span.star_sm_gold");
		reset_gold();
		
		
		// Reset the stars to the default index.
		function reset(){
			
			
			stars.slice(0,index_silver).addClass("on");

			var percent = averagePercent ? averagePercent : 0;
			if (percent > 0) {
				stars.eq(index_silver).addClass("on").children("a").css("width", percent + "%");
			}

		}
		
		function reset_gold(){
			
			stars.slice(0,index_gold + 1).addClass("on");
		}
	});
};

jQuery.fn.rateUser = function(){
	return this.each(function(){
		var div = jQuery("<span/>").attr({
			title: this.title,
			className: this.className
		}).insertAfter(this);
		
		
		var averageRating = 0,
		url = this.action,
		averageIndex = 0,
		averagePercent = 0,
		userid;
		
		if (this.title.split(/:\s*/)[1] != null) {
			averageRating = this.title.split(/:\s*/)[1].split(",");
	    	averageIndex = averageRating[0];
		    averagePercent = averageRating[1] * 10;
		    userid = averageRating[2];
		    
		    if (averageRating.length == 2) {
		    	userid = averageRating[1];
		    	averageIndex = 0;
		    	averagePercent =0;
		    }
		}
		
		var index = 6;
		for (x=0; x < index; x++) {
		    perc = Math.round(((averageIndex + "." + averagePercent) / 3) * 100);
			div.attr('class','rating_user');
			div.append( this.value == "0" ? "":
				"<span class='star_user' title='trifft zu " + perc + "% zu'><a target='_self' href='http://www.willwissen.net/willwissen.do?pg=userrate_s&ww=1&userid=" + userid + "'></a></span>" );
		}

		div.addClass('ratingPos_user').css("width", index * 19 + "px");
		var stars = div.find("span.star_user");
		reset();
		
		// Reset the stars to the default index.
		function reset(){
			
			
			var percent = averagePercent ? averagePercent : 0;
			var ons = averageIndex*2;
			
			percent = percent * 2;
			
			if (percent > 100) {
			  ons++;
			  percent = percent - 100;
			}
			
			
			stars.slice(0,ons).addClass("on");
			if (percent > 0) {
			    
				stars.eq(ons).addClass("on").children("a").css("width", percent + "%");
			}

		}
		
		
	});
};

jQuery.fn.rateUserRed = function(){
	return this.each(function(){
		var div = jQuery("<span/>").attr({
			title: this.title,
			className: this.className
		}).insertAfter(this);
		
		
		var averageRating = 0,
		url = this.action,
		averageIndex = 0,
		averagePercent = 0;
		
		if (this.title.split(/:\s*/)[1] != null) {
			averageRating = this.title.split(/:\s*/)[1].split(",");
	    	averageIndex = averageRating[0];
		    averagePercent = averageRating[1] * 10;
		}
		
		var index = 6;
		for (x=0; x < index; x++) {
		    perc = Math.round(((averageIndex + "." + averagePercent) / 3) * 100);
			div.attr('class','rating_user');
			div.append( this.value == "0" ? "":
				"<span class='star_user_red' title='trifft zu " + perc + "% zu'><a target='_blank' href='http://www.willwissen.net/faq_ww.html#points'></a></span>" );
		}

		div.addClass('ratingPos_user').css("width", index * 19 + "px");
		var stars = div.find("span.star_user_red");
		reset();
		
		// Reset the stars to the default index.
		function reset(){
			
			
			var percent = averagePercent ? averagePercent : 0;
			var ons = averageIndex*2;
			
			percent = percent * 2;
			
			if (percent > 100) {
			  ons++;
			  percent = percent - 100;
			}
			
			
			stars.slice(0,ons).addClass("on");
			if (percent > 0) {
			    
				stars.eq(ons).addClass("on").children("a").css("width", percent + "%");
			}

		}
		
		
	});
};

jQuery.fn.ratePolls = function(){
	return this.each(function(){
		var div = jQuery("<span/>").attr({
			title: this.title,
			className: this.className
		}).insertAfter(this);
		
		
		var averageRating = 0,
		url = this.action,
		averageIndex = 0,
		averagePercent = 0,
		jqueid=0,
		titleSplit=0,
		jansid;
		
		totalpercent = 0;
		
		
		if (this.title.split(/:\s*/)[1] != null) {
		    
		    titleSplit = this.title.split(/:\s*/)[0];
		    
		    averageRating = this.title.split(/:\s*/)[1].split(".");
	    	averageIndex = averageRating[0];
		    averagePercent = averageRating[1];
		    jqueid = averageRating[2];
            jansid = averageRating[3];

		    totalpercent = Math.round((parseInt(averageIndex,10) + parseInt(averagePercent,10) / 100) / 6 * 100);
		}
		
		var index = 6;
		for (x=0; x < index; x++) {
			div.attr('class','rating_user');
			if (jansid != 0) {
			
			  if (titleSplit == "RatingQue") {
				div.append( this.value == "0" ? "":
				"<span class='star_user' title='Bewertung:" + totalpercent + "%'><a target='_self' href='http://www.willwissen.net/willwissen.do?pg=i_x&ww=1&s_qus_poll=1&userid=" + jansid+ "&queid=" + jqueid + "'></a></span>" );			    
		      } else {
				div.append( this.value == "0" ? "":
				"<span class='star_user' title='Bewertung:" + totalpercent + "%'><a target='_self' href='javascript:onclick=toggleDiv(&quot;divans" + jansid + "&quot;);document.insans" + jansid + ".addi.focus();'></a></span>" );
			  }
			} else {
			div.append( this.value == "0" ? "":
				"<span class='star_user' title='Bewertung:" + totalpercent + "%'><a target='_self' href='http://www.willwissen.net/willwissen.do?pg=i_s&ww=1&s_ans_poll=1&queid=" + jqueid + "'></a></span>" );
			}
		}

		div.addClass('ratingPos_user').css("width", index * 19 + "px");
		var stars = div.find("span.star_user");
		reset();
		
		// Reset the stars to the default index.
		function reset(){
			
			stars.slice(0,averageIndex).addClass("on");

			var percent = averagePercent ? averagePercent : 0;
			if (percent > 0) {
				stars.eq(averageIndex).addClass("on").children("a").css("width", percent + "%");
			}

		}
		
		
	});
};



// fix ie6 background flicker problem.
if ( jQuery.browser.msie == true )
	document.execCommand('BackgroundImageCache', false, true);

