function RatingAPI(hWin){
	this.hWin = hWin;
	this.url = "xml/ratephoto.jsp?ghost=" + stateWindow.getSession() + "&photoid=";
	this.rateText = 'Rate photo';
}
	
RatingAPI.prototype.rate = function(id, val){
		var url = this.url + id + "&rating=" + val;
		stateWindow.loadXML(url);
		//window.location.href = url;
	}
	
RatingAPI.prototype.showStars = function(id, val){
		for(var i = 1; i <= 5; i++){
			var e = this.hWin.document.getElementById('rating' + id + '' + i);
			if(e){
				e.src = this.getStarSrc(i, val);
			}
		}
	}

RatingAPI.prototype.getStarSrc = function(i, rating){
		var starType = (rating >= i) ? '2' : ((rating >= i - 0.5) ? '1' : '0');
		return 'http://' + stateWindow.staticHost() + '/images/photos/votestar' + starType + '.gif';
	}
	
RatingAPI.prototype.getStarRatingElt = function(id, rating, ratedBy, active){
		var ratingStars = '';
		var starsElt = this.hWin.document.createElement("SPAN");
		starsElt.innerHTML = '<P>' + this.rateText + ':&nbsp;</P>';
		
		for(var i = 1; i <= 5; i++){
			var imgSrc = this.getStarSrc(i, rating);
			var img = this.hWin.document.createElement("IMG");
			img.src = imgSrc;
			img.className = "RatingStar";
			img.border = "0";
			img.align ="absmiddle";
			var imgId = active ? 'rating' + id + '' + i : '';
			if(active){
				img.id = imgId;
				img.value = {obj: this, id: id, val: i};
				img.onmouseover = function(){
					var rating = this.value.obj;
					var id = this.value.id;
					var val = this.value.val;
					rating.showStars(id, val);
				} ;
				img.onclick = function (){
					var rating = this.value.obj;
					var id = this.value.id;
					var val = this.value.val;
					rating.rate(id, val);
				};
			} else {
				img.onclick = function(){ alert("You need to be watching the video in order to rate it. Click on the picture to view."); };
			}
			starsElt.appendChild(img);
		}
		var pElt = this.hWin.document.createElement("P");
		pElt.innerHTML = ' by ' + ratedBy  + (ratedBy == 1 ? ' flirt' : ' flirts');
		starsElt.appendChild(pElt);
		starsElt.value = {obj: this, id: id, val: rating};
		starsElt.onmouseout = function(){
			var rating = this.value.obj;
			var id = this.value.id;
			var val = this.value.val;
			rating.showStars(id, val);
		};		
		var ratingElt = this.hWin.document.createElement("DIV");
		ratingElt.appendChild(starsElt);
		return ratingElt;
	}
	
RatingAPI.prototype.getThumbElt = function(id, imgOff, imgOn, rateValue){
		var hWin = this.hWin;
		var thumb = hWin.document.createElement("IMG");
		thumb.className = "Cursor";
		var pfx = "http://" + stateWindow.staticHost() + "/images/photos/";
		thumb.value = {obj: this, id: id, srcOff: pfx + imgOff + ".gif", srcOn: pfx + imgOn + ".gif", rateValue: rateValue};
		thumb.src = thumb.value.srcOff;
		if(imgOn){
			thumb.onmouseover = function(){ this.src = this.value.srcOn; };
			thumb.onmouseout = function(){ this.src = this.value.srcOff; };
			thumb.onclick = function(){
				this.value.obj.rate(this.value.id, this.value.rateValue);
			}
		} else {
			thumb.onclick = function(){ alert("You need to be watching the video in order to vote for it. Click on the picture to view."); };
		}
		thumb.align = "absmiddle";
		thumb.style.marginLeft = 4;
		thumb.style.marginRight = 4;
		return thumb;
	}
RatingAPI.prototype.getThumbRatingElt = function(id, rating, ratedBy, active){
		var ratedYes = Math.round((ratedBy*(rating - 1)/4)); 
		var ratedNo =  ratedBy - ratedYes;
		var hWin = this.hWin;
		var elt = hWin.document.createElement("DIV");
		var tbl = new stateWindow.TableElement(hWin, '', 1, 5);
		tbl.getCell(0,0).innerHTML = (active ? "<H3>Vote:</H3>" : "<P>Voted:</P>");
		var thumbUp = this.getThumbElt(id, ratedYes > ratedNo ? "thumbup_on" : "thumbup_off", active ? "thumbup_over" : null, 5);
		tbl.getCell(0,1).appendChild(thumbUp);
		tbl.getCell(0,2).innerHTML = '<P>' + ratedYes + (ratedYes == 1 ? ' flirt' : ' flirts') + '</P>';
		var thumbDown = this.getThumbElt(id, ratedYes < ratedNo ? "thumbdown_on" : "thumbdown_off", active ? "thumbdown_over" : null, 1);
		tbl.getCell(0,3).appendChild(thumbDown);
		tbl.getCell(0,4).innerHTML = '<P>' + ratedNo + (ratedNo == 1 ? ' flirt' : ' flirts') + '</P>';
		elt.appendChild(tbl.table);
		elt.style.marginTop = 2;
		return elt;
	}

