var UT_RATING_IMG = 'images/star_full.gif';
//var UT_RATING_IMG_HALF = 'images/star_half.gif';
var UT_RATING_IMG_BG = 'images/star_empty.gif';

function UTRating(ratingElementId, maxStars, objectName, formName, ratingMessageId, messages)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.formName = formName;
	this.ratingMessageId = ratingMessageId;
	this.messages = messages;

	this.starTimer = null;
	this.starCount = 0;

	
	// pre-fetch image
	(new Image()).src = UT_RATING_IMG;
	//(new Image()).src = UT_RATING_IMG_HALF;

	function showStars(starNum, skipMessageUpdate) {
		this.clearStarTimer();
		this.greyStars();
		this.colorStars(starNum);
		// if(!skipMessageUpdate)
		//   this.setMessage(starNum, messages);
	}

	function setMessage(starNum) {
		messages = new Array("Rate this video", "Poor", "Nothing special", "Worth watching", "Pretty cool", "Awesome!");
		document.getElementById(this.ratingMessageId).innerHTML = this.messages[starNum];
	}

	function colorStars(starNum) {
		for (var i=0; i < starNum; i++) {
			document.getElementById("star__" + (i+1)).src = UT_RATING_IMG;
		}
	}

	function greyStars() {
		for (var i=0; i < this.maxStars; i++)
			if (i <= this.starCount) {
				document.getElementById("star__" + (i+1)).src = UT_RATING_IMG_BG;
			}
			else
			{
				document.getElementById("star__" + (i+1)).src = UT_RATING_IMG_BG;
			}
	}

	function setStars(starNum, cid, contest) {
		var i;
                var ajaxRequest;
                try{
                    ajaxRequest = new XMLHttpRequest();
                }
                catch (e) {
                  try{
                        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                  }
                  catch (e) {
                        try {
                            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {
                            alert("Your browser broke!");
                            return false;
                        }
                  }
                }
		ajaxRequest.open("GET", "rate.php?id=" + starNum + "&cid=" + cid + "&contest=" + contest, true);
		ajaxRequest.send(null); 
		this.starCount = starNum;
		document.getElementById("stars").innerHTML = '<div class="ratings"><img id="star__1" src="images/star_empty.gif" style="border: 0px"><img id="star__2" src="images/star_empty.gif" style="border: 0px"><img id="star__3" src="images/star_empty.gif" style="border: 0px"><img id="star__4" src="images/star_empty.gif" style="border: 0px"><img id="star__5" src="images/star_empty.gif" style="border: 0px"></div><div class="ratingstr">Thanks for the rating!</div>';
                this.showStars(starNum);
	}


	function drawStars(starNum, skipMessageUpdate) {
		this.starCount=starNum;
		this.showStars(starNum, skipMessageUpdate);
	}

	function clearStars() {
		this.starTimer = setTimeout(this.objectName + ".resetStars()", 300);
	}

	function resetStars() {
		this.clearStarTimer();
		if (this.starCount)
			this.drawStars(this.starCount);
		else
			this.greyStars();
		this.setMessage(0);
	}

	function clearStarTimer() {
		if (this.starTimer) {
			clearTimeout(this.starTimer);
			this.starTimer = null;
		}
	}

        function commentSubmit(comment,cid) {
              var ajaxRequest;
              try{
                  ajaxRequest = new XMLHttpRequest();
              }
              catch (e) {
                try{
                      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {
                      try {
                          ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                      } catch (e) {
                          alert("Your browser broke!");
                          return false;
                      }
                }
              }
	      
              unencoded_comment = comment; // to fix %20 issues
              comment = encodeURIComponent(comment);

              ajaxRequest.open("GET", "comment.php?comment=" + comment + "&cid=" + cid, true);
              ajaxRequest.send(null);

              var trunc_comments = unencoded_comment.substring(0, 35);
	
              document.getElementById("comment_box").innerHTML = '<span>Just Posted: ' + trunc_comments + '... Thanks for your critique!</span>';
        }

        function replySubmit(comment, cid, cmt_id) {
              var ajaxRequest;
              try{
                  ajaxRequest = new XMLHttpRequest();
              }
              catch (e) {
                try{
                      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {
                      try {
                          ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                      } catch (e) {
                          alert("Your browser broke!");
                          return false;
                      }
                }
              }
	      
              unencoded_comment = comment; // to fix %20 issues
              comment = encodeURIComponent(comment);

              ajaxRequest.open("GET", "comment.php?comment=" + comment + "&cid=" + cid, true);
              ajaxRequest.send(null);

              var trunc_comments = unencoded_comment.substring(0, 35);
	
              document.getElementById("comment_box_" + cmt_id).innerHTML = '<span>Just Posted: ' + trunc_comments + '... Thanks for your critique!</span>';
        }

        function deleteComment(cmt_id) {
          var ajaxRequest;
          try{
              ajaxRequest = new XMLHttpRequest();
          }
          catch (e) {
              try{
                  ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch (e) {
                  try {
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e) {
                        alert("Your browser broke!");
                        return false;
                  }
              }
          }
          ajaxRequest.open("GET", "deletecomment.php?cmt_id=" + cmt_id, true);
          ajaxRequest.send(null);

	  cmt_id += '';
          document.getElementById('commentHead__' + cmt_id).innerHTML = '';
        }


        function replyComment(cmt_id, cid) {
	  cmt_id += '';
	  var stuff = '<div class="replycomment" id="comment_box_' + cmt_id + '"><p>Leave any useful tips/suggestion</p><p><textarea cols="30" rows="3" style="margin-top:1px;" id="comment_' + cmt_id + '"></textarea></p><p><input id="commentbutton" align="right" onClick="ratingComponent.replySubmit(document.getElementById(\'comment_' + cmt_id + '\').value, ' + cid + ', ' + cmt_id + ')" type="button" name="add_comment_button" value="Post Reply"/></p></div></div>';
          document.getElementById('replyHead__' + cmt_id).innerHTML = stuff;

        }

        this.deleteComment = deleteComment;
        this.replyComment = replyComment;
        this.commentSubmit = commentSubmit;
        this.replySubmit = replySubmit;
	this.clearStars = clearStars;
	this.clearStarTimer = clearStarTimer;
	this.greyStars = greyStars;
	this.colorStars = colorStars;
	this.resetStars = resetStars;
	this.setStars = setStars;
	this.drawStars = drawStars;
	this.showStars = showStars;
	this.setMessage = setMessage;

}



