// 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.

/** 
 * Common javascript for all of vorby.
 */


function VBY_Main(win, doc, isAuthenticated) {
  this.doc_ = doc;
  this.win_ = win;
  
  this.ratings_ = {};

  this.isAuthenticated_ = isAuthenticated;
}


VBY_Main.prototype.RATING_URL = '/media/img/ratings1/rating_';

VBY_Main.prototype.setRatings = function (ratings) {
	this.ratings_ = ratings;
}

VBY_Main.prototype.updateRatings = function (ratings) {
	update(this.ratings_, ratings);
}

VBY_Main.prototype.recordRating = function (number, lineId) {
	if (this.isAuthenticated_) {
	  if (this.ratings_[lineId] != number) {
      var uri = '/moviequotes/rate_line/';
      var res = doSimpleXMLHttpRequest(uri, {'line_id': lineId, 
                                             'rating': number});
      this.ratings_[lineId] = number;
    }
	} else {
		revealModal('login_dialog');
	}
};

VBY_Main.prototype.showRating = function (number, lineId) {
  getElement('lr_' + lineId).src = this.RATING_URL + number + '.0.gif';
};

VBY_Main.prototype.resetRating = function (lineId) {
  current_rating = this.ratings_[lineId] ? this.ratings_[lineId] : '0';
  getElement('lr_' + lineId).src = this.RATING_URL + current_rating + '.0.gif';
};

VBY_Main.prototype.setFavorite = function (lineId, imgObj) {
	if (this.isAuthenticated_) {
		var isFav;
		if (/starw_off.png/.test(imgObj.src)) {
			isFav = 0
			imgObj.src = '/media/img/icons/group2/starw.png';
		} else {
			isFav = 1
			imgObj.src = '/media/img/icons/group2/starw_off.png';
		}
		var uri = '/moviequotes/set_favorite/';
		var d = doSimpleXMLHttpRequest(uri, {'line_id': lineId, 'is_fav': isFav});
	} else {
		revealModal('login_dialog');
	}
};

VBY_Main.prototype.markAccurate = function (lineId, accurate) {
  if (this.isAuthenticated_) {
    var acnt = $('accuracy_' + lineId);
    var uri = '/moviequotes/accurate/';
    var d = doSimpleXMLHttpRequest(uri, {'line_id': lineId, 
        'accurate': accurate});
    d.addCallback(bind(this.markAccurateHandle_, this, lineId));
    var yes = $('yes_' + lineId);
    var no = $('no_' + lineId);
    if (accurate == 'yes') {
      yes.className = 'acc_sel';
      no.className = 'acc_dsel';  
    } else {
      yes.className = 'acc_dsel';
      no.className = 'acc_sel';
    }
  } else {
    revealModal('login_dialog');
  }
};

VBY_Main.prototype.markAccurateHandle_ = function (lineId, req) {
	if (req.status == '200') {
	  var res = req.responseText;
	  var acnt = $('accuracy_' + lineId);
	  if (parseInt(res) > 0) {
	    res = '+' + res;
	    acnt.className = 'aplus';
	  } else if (!parseInt(res)) {
	    acnt.className = '';
	  } else {
	    acnt.className = 'aminus';
	  }

	  acnt.innerHTML = res;
	  pulsate(acnt, {pulses: 3, duration: 1.5});
	}
};

VBY_Main.prototype.removeQuote = function (lineId) {
  if (this.isAuthenticated_ && confirm('Delete?')) {
  	var uri = '/moviequotes/removequote/';
  	var d = doSimpleXMLHttpRequest(uri, {'line_id': lineId});
  }
};

function showNotification(element, message) {
  element.innerHTML = message;
  showElement(element);
}


function hideNotification(element) {
  hideElement(element);
  element.innerHTML = '';
}


function revealModal(elemId) {
  var modal = getElement(elemId);
  window.onscroll = function () { 
  	modal.style.top = document.body.scrollTop; 
  };
  showElement(modal);
  modal.style.top = document.body.scrollTop;
}


function hideModal(elemId) {
  hideElement(elemId);
}