// 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 actions on the quote/comments page.
 */

function VBY_Quote(win, doc, isAuthenticated) {

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

  this.lineId = null;
  this.movieId = null;

  this.quoteText_ = $('vby-quote');

  // Add Video ids
  this.addClipArea_ = $("movie_clips2");
  this.addClipContainer_ = $("no_video");
  this.addLink_ = $("mc_msg2");
  this.addMessage_ = $("add_video_msg");
  this.videoId_ = $("video_id");
  this.videoAdd_ = $("video_add");
}

VBY_Quote.prototype.stylePage = function () {
	roundClass("div", "comment_list_header", {corners: "tl"});
	roundClass("div", "comment_list_comment", {corners: "br"});
};

VBY_Quote.prototype.toggleMovieClips = function (num, time, msg1, msg2) {
  var mclip = $("movie_clips" + num);
  var message = $("mc_msg" + num);
  if (mclip.style.display == "none") {
    toggle(mclip, "blind", {afterFinish: function () {
      showElement("flash_clips" + num);
      message.innerHTML = msg2;
    }, duration: time});
  } else {
    hideElement("flash_clips" + num);
    toggle(mclip, "blind", {duration: time});
    message.innerHTML = msg1;
  }
};

VBY_Quote.prototype.toggleClipAdd = function () {
  var mclip = $("movie_clips2");
  var message = $("mc_msg2");
  var videoId = $("video_id");
  if (mclip.style.display == "none") {
    toggle(mclip, "blind", {afterFinish: function () {
      videoId.focus();
      message.innerHTML = "cancel";
    }, duration:0.5});
  } else {
    toggle(mclip, "blind", {duration: 0.5});
    message.innerHTML = "add a video";
  }
};

VBY_Quote.prototype.addMovieClip = function () {
  if (this.isAuthenticated_) {
    var videoIdStr = strip(this.videoId_.value);
    if (videoIdStr) {
      hideElement(this.videoAdd_);
      showNotification(this.addMessage_, 'Adding...');
      
    	var uri = "/moviequotes/add_video/";
    	var d = doSimpleXMLHttpRequest(uri, {'video_id': videoIdStr, 
    	    'line_id': this.lineId, 'movie_id': this.movieId});
    	d.addCallback(bind(this.addMovieClipResponse_, this, videoIdStr));
    }
  } else {
    revealModal('login_dialog');
  }
};

VBY_Quote.prototype.addMovieClipResponse_ = function (videoId, req) {
	if (req.status == "200") {
		var res = req.responseText;
		if (res != '0') {
		  hideNotification(this.addMessage_);
		  showElement(this.videoAdd_);
		  this.toggleClipAdd();
		  hideElement(this.addClipContainer_);
		  this.videoId_.value.innerHTML = "";
		  this.doc_.location.reload(true);
		}
	}
}

VBY_Quote.prototype.removeClip = function () {
  if (this.isAuthenticated_) {
  	var uri = "/moviequotes/unassign_video/";
  	var d = doSimpleXMLHttpRequest(uri, {'line_id': this.lineId});
  	hideElement("video_clip");
  	callLater(.8, function () {toggle("no_video", "appear");});
  	
  }
};

VBY_Quote.prototype.showEdit = function (visible) {
  if (visible) {
    hideElement('vby-quote-sec');
    showElement('vby-quote-edit');
  } else {
    showElement('vby-quote-sec');
    hideElement('vby-quote-edit');
  }
};

VBY_Quote.prototype.saveQuote = function() {
  var quote = $('vby-quote-text');
  var msg = $('vby-edit-msg');
  msg.style.display = '';
  
  var uri = "/moviequotes/editquote/";
  var query = {'line': quote.value, 'line_id': this.lineId};
	var d = doXHR(uri, {method: 'POST', sendContent: queryString(query),
	    headers: {'Content-Type': 'application/x-www-form-urlencoded'}});

  d.addCallback(bind(this.saveQuoteResponse_, this));
};

VBY_Quote.prototype.saveQuoteResponse_ = function (req) {
	if (req.status == "200") {
	  this.quoteText_.innerHTML = req.responseText;
		this.showEdit(false);
		$('vby-quote-text').value = req.responseText;
    var msg = $('vby-edit-msg');
    msg.style.display = 'none';
	}
};

VBY_Quote.prototype.deleteQuote = function() {
  if (confirm('Delete Quote?')) {
    var msg = $('vby-edit-msg');
    msg.style.display = '';
  	var uri = '/moviequotes/removequote/';
  	var d = doSimpleXMLHttpRequest(uri, {'line_id': this.lineId});

    d.addCallback(bind(this.deleteQuoteResponse_, this));
  }
};

VBY_Quote.prototype.deleteQuoteResponse_ = function(req) {
	if (req.status == "200" && req.responseText == '1') {
	  this.doc_.location.href = '/moviequotes/movies/' + this.movieId + '/';
	}
};

VBY_Quote.prototype.removeComment = function(key) {
  var uri = '/moviequotes/removecomment/';
  var d = doSimpleXMLHttpRequest(uri, {'ckey': key});
  d.addCallback(bind(this.removeCommentResponse_, this, key));
};

VBY_Quote.prototype.removeCommentResponse_ = function(key) {
  removeElement(key);
};
