

function psw_ratings (options) {
	var j = jQuery;
	function disableStars() {
		console.log(rating_html);
		j('.psw_stars').remove();
		j('.rate form').prepend('<div class="psw_stars"></div>');
		j('.psw_stars').html(rating_html);
		createStars({readOnly:true, groups: {}});
		
	}
	
	j('.psw_testReset').click(function() { alert('resetting'); disableStars(); });
	
	function createStars(opt) {
		j('.psw_stars input[type=radio]').rating(opt);
		if (opt && opt.readOnly) {
			j('.psw_stars .stars').addClass('stars_readonly');
		}
	}
	
	function getIds() {
		var ids;
		var json = j.cookie('psw_rating') || '{}';
		eval('ids = ' + json + ';') ;
		return ids || {};
	}

	function getContentId() {
		return j('.rate form').attr('title');
	}

	function thankYou(vote) {
		//var s = "margin-top:10px; color:#666666; font-family:Verdana; font-size:12px;";
		//j('.rate form .rateButton').replaceWith('<div class="yourRating rateLabel"><label>Your Rating: ' + vote + "</label></div>");
		//j('.rate .rateLabel').html("<label>This page is rated:</label>");
		j('.rate form .rateButton').remove();
		j('.rate .rateLabel label').html('Your Rating: ' + vote);
	}

	function getVote() {
		var cid = getContentId();
		var ids = getIds();
		return ids ? ids['cid_' + cid] : null;

	}
	
	function onVote(responseText, statusText) {
		var ids = getIds();
		var cid = getContentId();
		var vote = j('.rate form input[name=psw_rating]').fieldValue()[0];
		ids['cid_' + cid] = vote;
		var idstext = JSON.stringify(ids);

		j.cookie('psw_rating', idstext);
		thankYou(vote);
		disableStars();
	}


	var vote = getVote();
	if (vote) {
		createStars( {readOnly:true} );
		thankYou(vote);
	}
	else {
		createStars();
		j('.rate form').submit(function () {
			if (getVote()) {
				//do nothing as we've already voted this should not happen :)
				thankYou();
			}
			else {
				j(this).ajaxSubmit(opt);
			}

			return false;
		});
	}
	
	
	var defaults = {
		url: "/soln-ugc/comments.do",
		dataType: 'text',
		beforeSubmit: function(data,form,options) {},
		success: onVote
	};

	var opt = {};
	j.extend(opt, defaults, (options || {}));
	var rating_html = j('.psw_stars').html();
}