// Star Rating //

// Insert the iframe into the main page.
function initStarRating() {
	// Make sure browser supports getElementById method, and star_rating element exists
	if (document.getElementById && document.getElementById('star_rating') ) {

		try
		{
			document.domain="ca.gov"; // Allows ca.gov sites to access this xml feed.
		}
		catch (e)
		{
			return(0); // Site is not a ca.gov site.
		}

		var URLParams = GetURL();

		document.getElementById("star_rating").style.visibility="visible"; /* Unhide the div */

		document.getElementById('star_rating').innerHTML = '<iframe src="http://webtools.ca.gov/javascript/shared/star_rating/star_rating_frame.html?url=' + URLParams + '" width="1" height="1" frameborder="0"></iframe>';
	}
}

// User clicked a star
function StarRatingVote(rating) {

	var URLParams = GetURL();

	document.getElementById('star_rating').innerHTML = '<iframe src="http://webtools.ca.gov/javascript/shared/star_rating/star_rating_frame.html?url=' + URLParams + '&rating=' + rating + '" width="1" height="1" frameborder="0"></iframe>';

}

function GetURL() {
	var URLParams = location.href.replace(/#.*$/,''); // Strip off # targets from the end of the URL
	URLParams = URLParams.replace(/\/index.html?$|\/default.html?$/i,''); // Strip off index.htm(l) and default.htm(l)
	URLParams = URLParams.replace(/\/$/,''); // Strip off trailing slash
	URLParams = URLParams.replace(/^https?:\/\//i,''); // Strip off http(s)
	URLParams = URLParams.replace(/^www./i,''); // Strip off www.
	URLParams = URLParams.replace(/^my.ca.gov/i,'ca.gov'); // Replace my.ca.gov with ca.gov
	return URLParams;
}

// show the XML content in the browser
function StarRatingShowXML(objRating) {
	if (objRating.avg_rating && objRating.num_ratings) {
		//// Display the rating ////
		var currentRatingWidth = Math.round( (objRating.avg_rating / 5) * 150 ); // Adjust this if you change the size of the stars

		var htmlContent = '<div class="star_rating_content"><p class="star_rating_title">Rate this page</p>';

		htmlContent += '<ul>';
		htmlContent += '<li class="current_rating" style="width:' + currentRatingWidth + 'px;"></li>';
		htmlContent += '<li><a href="#" onclick="StarRatingVote(1);return false;" class="star_1">1</a></li>';
		htmlContent += '<li><a href="#" onclick="StarRatingVote(2);return false;" class="star_2">2</a></li>';
		htmlContent += '<li><a href="#" onclick="StarRatingVote(3);return false;" class="star_3">3</a></li>';
		htmlContent += '<li><a href="#" onclick="StarRatingVote(4);return false;" class="star_4">4</a></li>';
		htmlContent += '<li><a href="#" onclick="StarRatingVote(5);return false;" class="star_5">5</a></li>';
		htmlContent += '</ul>';

		htmlContent += '<p class="star_rating_stats">Rated ' + objRating.avg_rating + ' / 5 stars (' + objRating.num_ratings + ' votes cast)</p></div>';

		htmlContent += '<div class="star_rating_extra"></div>';


		document.getElementById('star_rating').innerHTML = htmlContent;

	} else {
		// Could not retrieve XML data, so just remove the iframe.
		document.getElementById('star_rating').innerHTML = '';
	}
}

addLoadEvent(initStarRating); // Add initStarRating to the page onload event handler

