if(window.cbeebies==undefined){cbeebies={}};
(function() {
	cbeebies.ratings = {
		ratingForm: null,
		currentCookie:null,
		ratedArray:null,
		_overImage:		function(area) {
			document.getElementById('rate-' + area.parentNode.id).src = '/cbeebies/grownups/images/' + area.title.substring(0,1) +'-star.gif';
		},
		_outImage:		function(area) {
			document.getElementById('rate-' + area.parentNode.id).src = '/cbeebies/grownups/images/0-star.gif';
		},
		_submitRating:	function(area) {
			var formId = 'gu-rate-' + area.parentNode.id;
			document.getElementById(formId).getElementsByTagName('select')[0].selectedIndex = area.title.substring(0,1);
			cbeebies.ratings._setCookie(formId);
			document.getElementById(formId).submit();
		},
		_setCookie: 	function(formId) {
			var currentCookie = cbeebies.ratings._getCookie("rated");
			document.cookie = 'rated = '+currentCookie+"&"+formId;
		},
		//Get cookie routine by Shelley Powers 
		_getCookie:		function(Name) {
			var search = Name + "="
			var returnvalue = "";
			if (document.cookie.length > 0) {
				offset = document.cookie.indexOf(search)
				// if cookie exists
				if (offset != -1) { 
					offset += search.length
					// set index of beginning of value
					end = document.cookie.indexOf(";", offset);
					// set index of end of cookie value
					if (end == -1) end = document.cookie.length;
					returnvalue=unescape(document.cookie.substring(offset, end))
				}
			}
			return returnvalue;
		},
		_sayThanks:		function(formId) {
			var thanks = document.createElement('p');
			var clear = document.getElementBy
			thanks.innerHTML = 'Thank you, your rating will be added to the total.  It will take a few moments to update.';
			var formParent = document.getElementById(formId).parentNode;
			formParent.parentNode.insertBefore(thanks, formParent.parentNode.getElementsByTagName('p')[0]);
			formParent.parentNode.removeChild(formParent.parentNode.getElementsByTagName('p')[1]);
			formParent.parentNode.removeChild(formParent);
		},	
		_makeArea:		function(map, title) {
			var n = title.substring(0,1);
			var area = document.createElement('area');
			area.shape = 'rect';
			area.coords = ((n-1)*17) + ',0,' + (n*17) + ',13';
			area.alt = area.title = title;
			area.onmouseover = function() { cbeebies.ratings._overImage(this); };
			area.onmouseout = function() { cbeebies.ratings._outImage(this); };
			area.onclick = function() { cbeebies.ratings._submitRating(this); };
			map.appendChild(area);
		},	
		_showStars:		function(formId) {
			var formName = formId.substr(8);
			var newMap = document.createElement('map');
			newMap.id = newMap.name = formName;
			this._makeArea(newMap,'1/5 - Below average');
			this._makeArea(newMap,'2/5 - Average');
			this._makeArea(newMap,'3/5 - Good');
			this._makeArea(newMap,'4/5 - Very good');
			this._makeArea(newMap,'5/5 - Superb');
			document.body.appendChild(newMap);
			var rateThis = document.createElement('img');
			rateThis.src = '/cbeebies/grownups/images/0-star.gif';
			rateThis.alt = 'score out of 5';
			rateThis.id = 'rate-' + formName;
			rateThis.width = 86;
			rateThis.height = 13;
			rateThis.useMap = '#' + formName;
			document.getElementById(formId).getElementsByTagName('select')[0].className = 'gu-offscreen';
			var inputs = document.getElementById(formId).getElementsByTagName('input')
			for(var i = 0; i<inputs.length;i++){
				if(inputs[i].className == 'gu-submit-rating'){
					inputs[i].className = 'gu-offscreen';
					inputs[i].onclick = function() { _setCookie(this.parentNode.id); };
				}
			}
			document.getElementById(formId).appendChild(rateThis);
			var image1 = new Image();
			image1.src = '/cbeebies/grownups/images/1-star.gif';
			var image2 = new Image();
			image2.src = '/cbeebies/grownups/images/2-star.gif';
			var image3 = new Image();
			image3.src = '/cbeebies/grownups/images/3-star.gif';
			var image4 = new Image();
			image4.src = '/cbeebies/grownups/images/4-star.gif';
			var image5 = new Image();
			image5.src = '/cbeebies/grownups/images/5-star.gif';
		},
		_initRatingForm: 	function () {

		
			currentCookie = cbeebies.ratings._getCookie("rated");
			var allForms = document.getElementsByTagName('form');
			for(var i=0; i<allForms.length; i++) { // will only remember last rating form, ie. one form!
				if(/\bgu-rate\b/.test(allForms[i].id)) { cbeebies.ratings.ratingForm = allForms[i]; }
			}
			if(currentCookie.match(cbeebies.ratings.ratingForm.id) != null){ cbeebies.ratings._sayThanks(cbeebies.ratings.ratingForm.id); } else { cbeebies.ratings._showStars(cbeebies.ratings.ratingForm.id); }
		}
	}
})();