// Copyright 2008 Google Inc.
// All Rights Reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/** 
 * Javascript for adding comments.
 */

function Comment(win, doc, isAuthenticated) {

  this.doc_ = doc;
  this.win_ = win;
  this.isAuthenticated_ = isAuthenticated;

  this.commentInput_ = $("comment_input");
  this.commentInput2_ = $("comment_input2");

  this.addMsg_ = $("add_comment_msg");

  roundElement("comment_area", {bgColor: "#ffffff", color: "#d6edd6"});
}

Comment.prototype.loadCommentDialog = function (lineId) {
  if (this.isAuthenticated_) {
		this.lineId = lineId;
		$("comment_movie").innerHTML = $("movie_" + lineId).innerHTML;
		$("comment_char").innerHTML = $("char_" + lineId).innerHTML;
		$("comment_line").innerHTML = $("line_" + lineId).innerHTML;
		revealModal("comment_dialog");	
		var cInput = $("comment_input");
		cInput.value = "";
		cInput.focus();
  } else {
    revealModal('login_dialog');
  }
};

Comment.prototype.submitComment = function (ctype) {
	var uri = "/moviequotes/add_comment/";

  if (this.isAuthenticated_) {
		if (ctype == 2 && this.commentInput2_.value) {
		  showNotification(this.addMsg_, "Adding...");
		  var query = {'line_id': this.lineId,
			    'comment': this.commentInput2_.value};
			var d = doXHR(uri, {
			  method: 'POST', 
			  sendContent: queryString(query),
			  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			});
			this.commentInput2_.value = "";
			d.addCallback(bind(this.submitCommentHandle2, this));
		} else if (this.commentInput_.value) {
		  var query = {
			    'comment': this.commentInput_.value, 'line_id': this.lineId};
			var d = doXHR(uri, {method: 'POST', sendContent: queryString(query),
			    headers: {'Content-Type': 'application/x-www-form-urlencoded'}});
			hideModal('comment_dialog');
		  d.addCallback(bind(this.submitCommentHandle, this));
		}
  } else {
    revealModal('login_dialog');
  }
};

Comment.prototype.submitCommentHandle = function (req) {
	if (req.status == "200") {
		var ccnt = $("comment_cnt_" + this.lineId);
		this.cmtCnt_ = parseInt(getNodeAttribute(ccnt, "vby_ccnt")) + 1;
	  setNodeAttribute(ccnt, "vby_ccnt", this.cmtCnt_);
		ccnt.innerHTML = "(" + this.cmtCnt_ + ")";
		Highlight(ccnt, {startcolor: "#ccf9ce", duration: "3.0"});
	}
};

Comment.prototype.submitCommentHandle2 = function (req) {
	if (req.status == "200") {
		var commentBlock = DIV({"class": "comment_block", 
														"style": "display: none;"}, 
													  " ");
		commentBlock.innerHTML = req.responseText;
		var header = getFirstElementByTagAndClassName("div", "comment_list_header", 
		    commentBlock);
		var footer = getFirstElementByTagAndClassName("div", "comment_list_comment", 
		    commentBlock);

	  insertSiblingNodesBefore("add_block", commentBlock);
	  appear(commentBlock, {duration: "2.4", beforeStart: function () {
	  	roundElement(header, {corners: "tl", bgColor: "#fff"});
	  	roundElement(footer, {corners: "br"});
	  	$("num_comments").innerHTML = parseInt($("num_comments").innerHTML) + 1;
	    }});
	}
	hideNotification(this.addMsg_);
};
