/**
 * @author gsandrigo
 * 
 */

 
function breakRatingClass(ratingContentId, theName){
	
	//private vars
	var that = this;
	var voted = false;
	var oldRating;
	var myName = theName;
	
	var starMap = new Array(
	'0,0,2,20',
	'3,0,20,20',
	'21,0,35,20',
	'36,0,59,20',
	'60,0,76,20',
	'77,0,94,20');
	
	var messages = new Array("Rate this", "Poor", "Okay", "Good", "Great", "WOW!");
	
	//public vars
	this.contentId = ratingContentId;
	this.isUserLoggedIn = false;
	this.showText = false;
	this.isOwnerWatching = false;
	this.starOutDiv;
	this.messageOutDiv;
	this.ratingCallback;
		
	//private methods
	function renderStars(rating, active){
		var starID = "stars." + that.contentId;
		var html = "<map name='starmap" + that.contentId +"'>";
		for (var i = 1; i < 6; i++) {
			html += "<area shape=rect " + 
			"coords='" + starMap[i] + "' " +
			"onMouseOver=\"" + myName + ".StarMouseOver('" + that.contentId + "'," + i + ");\" " +
			"onMouseOut=\"" + myName + ".StarMouseOut('" + that.contentId + "');\" ";
			if(active){
				html += "onClick=\"" + myName + ".saveRating(" + i + ");\" >";
			}
			else{
				html += "onClick=\"GoLogin();\" >";
			}	
		}
		html += "</map>";
		html += "<img vspace=2  src='" + "http://media1.break.com/static/v3/img/rating/" + rating + "star.gif"+ "'";
		html += " border=0 usemap='#starmap" + that.contentId;
		html += "' id='" + starID + "'>";
		that.starOutDiv.innerHTML = html;
		if(that.showText){
			renderMessage(0);
		}
	}
	
	function renderStarsActive(rating){
		renderStars(rating, true);
	}
	
	function renderStarsInactive(rating){
		renderStars(rating, false);
	}
	
	function renderStarsDisabled(rating){
		var starID = "stars." + that.contentId;
		var html = "<map name='starmap" + that.contentId +"'>";
		for (var i = 1; i < 6; i++) {
			html += "<area shape=rect " + 
			"coords='" + starMap[i] + "' " +
			" >";	
		}
		html += "</map>";
		html += "<img vspace=2  src='" + "http://media1.break.com/static/v3/img/rating/" + rating + "star.gif"+ "'";
		html += " border=0 usemap='#starmap" + that.contentId;
		html += "' id='" + starID + "'>";
		that.starOutDiv.innerHTML = html;
		if(that.showText){
			renderMessage(rating);
		}
	}
	
	function renderMessage(rating){
		that.messageOutDiv.innerHTML = messages[Math.ceil(rating)];
	}
	
	//public methods
	this.saveRating = function (newRating) {
		voted = true;
		this.renderStarsInDiv(newRating);
		var memberId = document.cookie;
    	var index = memberId.search(/MemberId=/gi);
	    if(index != -1){
	    memberId = memberId.substr(index+9, memberId.length);
	    index = memberId.search(/;/gi);
	    if(index != -1)
	      memberId = memberId.substr(0, index);
	    }
		
		var ratingService = new BreakJSON();
		ratingService.sendBegin("http://websvc.break.com/CMS/handlers/Ratings/contentratinghandler.ashx?points=" + newRating + "&p2=" + that.contentId , that.ratingCallback);
	}
	
	this.renderStarsInDiv = function (rating){
		oldRating = rating;
		if(this.isUserLoggedIn){
			if(!voted && !that.isOwnerWatching){
				renderStarsActive(rating);
			}
			else{
				renderStarsDisabled(rating);
			}
		}
		else {
			renderStarsInactive(rating);
		}
	}
	
	this.getOldRating = function (){
		return oldRating;
	}
	
	this.updateMessage = function (rating) {
		renderMessage(rating);
	}
}

breakRatingClass.prototype.StarMouseOver = function (id, rating){
		var image = document.getElementById("stars." + id);
		image.src = "http://media1.break.com/static/v3/img/rating/mo/" + rating + "star.gif";
		if(this.showText){
			this.updateMessage(rating);
		}
	}

breakRatingClass.prototype.StarMouseOut = function (){
		var image = document.getElementById("stars." + this.contentId);
		image.src = "http://media1.break.com/static/v3/img/rating/" + this.getOldRating() + "star.gif";
		if(this.showText){
			this.updateMessage(0);
		}
}