/* ===========================================================================
//	lib_rating.js															
// ======================================================================== */

/*	rating 
//	getPostRating: 
//		input -> int 
//		output -> double 
//	ratePost: 
//		input -> postId, bewertung 
//		output: map -> {error [0.. ok, 1.. error], message [error string], rating [current posting double]} 
*/
//var dkWebService	= new xmlrpc.ServiceProxy("/livinglibrary/xmlrpc/ratingBridge.page", new Array('getPostRating','ratePost'));


var rating = {
	postings	:	{},
	imgDir		: 	"/images/stars/",
	stars		: 	{
						"active"	: "star_button_active.gif",
						"half"		: "star_button_half.gif",
						"out"		: "star_button_out.gif"
					},
	
	rate		:	function(postId,newRating) {
						if(!postId || postId=="") { return false; }
						if(!newRating || newRating=="") { return false; }
						
						var response = dkWebService.ratePost(postId,newRating);
						if(!response || response == "") {
							return false;
						} else {
							var error,message,rating,votes;
							for(var o in response) {
								if(o=="error")		{ error		= response[o]; }
								if(o=="message")	{ message	= response[o]; }
								if(o=="rating") 	{ rating	= response[o]; }
								if(o=="votes") 		{ votes		= response[o]; }
							}
							
							if(error==0) {
								this.resetDisplay(postId, rating, votes);
							} else {
								alert(message);
							}
						}
					},
	
	resetDisplay :	function(postId, rating, votes) {
						var idOfVoteCount		= "pd_"+postId+"_voteCount";
						var idOfStarDisplay		= "pd_"+postId+"_star_";
						var idOfRatingButton	= "ratingStarButton_"+postId;
						var idOfRatingMessage	= "ratingMessage_"+postId;
						
						/* reset rate button */
						document.getElementById(idOfRatingButton).style.display = "none";
						document.getElementById(idOfRatingMessage).style.display = "block";
						
						/* reset vote count */
						if(typeof(votes)!="undefined") {
							if(document.getElementById(idOfVoteCount)!="undefined") {
								var voteTextNode = document.getElementById(idOfVoteCount).firstChild;
								voteTextNode.data = (votes==1) ? "1 vote" : votes+" votes";
							}
						}
						
						/* reset star display */
						for(var i=1; i<=5; i++) {
							var imgName = idOfStarDisplay+i;
							var rdStarImage = document.getElementsByName(imgName)[0];
							var newImageSrc;
							if ( 0.75+rating >= i && 0.25+rating<i ) {
								newImageSrc = contextPath+this.imgDir+this.stars['half'];
							} else if (0.25+rating >= i) {
								newImageSrc = contextPath+this.imgDir+this.stars['active'];
							} else {
								newImageSrc = contextPath+this.imgDir+this.stars['out'];
							}
							rdStarImage.src = newImageSrc;
						}
					},
	
	mouseOver	:	function(id,starIndex) {
						var imgNamePrefix = "btn_"+id+"_star_";
						for(i=1; i<=5; i++) {
							var imgName = imgNamePrefix+i;
							if(i <= starIndex) {
								this.imgChange(imgName, this.stars['active']);
							} else {
								this.imgChange(imgName, this.stars['out']);
							}
						}
					},
	
	mouseOut	:	function(id,rating) {
						var imgNamePrefix = "btn_"+id+"_star_";
						for(i=1; i<=5; i++) {
							var imgName = imgNamePrefix+i;
							if( ((0.75+rating) >= i) && ((0.25+rating)<i) ) {
								this.imgChange(imgName, this.stars['half']);
							} else if((0.25+rating) >= i) {
								this.imgChange(imgName, this.stars['active']);
							} else {
								this.imgChange(imgName, this.stars['out']);
							}
						}
					},
	
	imgChange	: 	function(imgName,newImg) {
						var src = document[imgName].src;
						var off = src.lastIndexOf("/");
						if(off == -1) {
							off = src.lastIndexOf("\\");
						}
						if(off > -1) {
							var newsrc = src.substring(0,off+1)+newImg;
							document[imgName].src = newsrc;
						}
					}
}



/* eof ==================================================================== */

