// Copyright 2005 Google Inc.  All Rights Reserved.

/**
 * Class that manages a multiple friend selection list input.
 */
function FriendSelector() {
};

/**
 * initialize the objects and render the display
 */
FriendSelector.prototype.init = function() {
  this.createObjects();
  this.loadFriendsList();
};

/**
 * initialize the object pointers
 */
FriendSelector.prototype.createObjects = function() {
  this.container = document.getElementById("friendSelector");

  this.addFriend = document.getElementById("addFriend");
  this.friendCount = document.getElementById("friendCount");

  this.criteria = document.getElementById("criteria");
  this.queryString = document.getElementById("queryString");

  this.friendListMaster = document.getElementById("friendListMaster");
  this.friendList = document.getElementById("friendList");
  this.selectedList = document.getElementById("selectedList");
};

/**
 * return the ASCII for the key press
 *
 * This function is capable of handling both IE and Firefox
 */
FriendSelector.prototype.getKeyPressed = function(e) {
  return (window.event ? e.keyCode : e.which);
};

/**
 * remove HTML-style tags from str
 */
FriendSelector.prototype.removeTags = function(str) {
  var idx = str.search(/<.*>/);

  while (idx > -1) {
    str = str.substr(0, idx) + str.substr(str.indexOf(">", idx) + 1);
    idx = str.search(/<.*>/);
  }

  return str.replace("<", "&lt;").replace(">", "&gt;");
};

/**
 * refresh the display to retain the correct list of friends
 */
FriendSelector.prototype.filterFriends = function() {
  var queryStr = this.addFriend.value.toLowerCase();

  var listLen;

  listLen = this.friendList.options.length;
  for (var i = listLen - 1; i > -1; i--) {
    if (this.friendList.options[i].text.toLowerCase().indexOf(queryStr) == -1) {
      this.insertInOrder(this.friendListMaster, this.friendList.options[i]);
    }
  }

  listLen = this.friendListMaster.options.length;
  for (var i = listLen - 1; i > -1; i--) {
    var opt = this.friendListMaster.options[i];
    if (opt.text.toLowerCase().indexOf(queryStr) > -1) {
      this.insertInOrder(this.friendList, opt);
    }
  }

  if (this.friendCount != null) {
    this.friendCount.innerHTML = this.friendList.options.length;
  }
  if (queryStr != "") {
    if (this.queryString != null) {
      this.queryString.innerHTML = this.removeTags(queryStr);
    }
    if (this.criteria != null) {
      this.criteria.style.display = "inline";
    }
  } else {
    if (this.criteria != null) {
      this.criteria.style.display = "none";
    }
  }
};

/**
 * Load the initial friends list from Master List
 */
FriendSelector.prototype.loadFriendsList = function() {
  listLen = this.friendListMaster.options.length;
  for (var i = listLen - 1; i > -1; i--) {
    var opt = this.friendListMaster.options[i];
    this.insertInOrder(this.friendList, opt);
  }
}

/**
 * select all entries in the selectedList
 */
FriendSelector.prototype.selectAll = function() {
  var listLen = this.selectedList.options.length;

  for (var i = 0; i < listLen; i++) {
    this.selectedList.options[i].selected = true;
  }
};

/**
 * unselect all entries in both the friendList and the selectedList
 */
FriendSelector.prototype.selectNone = function() {
  this.friendList.selectedIndex = -1;
  this.selectedList.selectedIndex = -1;
};

/**
 * insert item into list in a sorted fashion
 */
FriendSelector.prototype.insertInOrder = function(list, item) {
  var listLen = list.options.length;

  if (listLen > 0) {
    for (var i = 0; i < listLen; i++) {
     if (item.text.toLowerCase() <= list.options[i].text.toLowerCase()) {
        list.insertBefore(item, list.options[i]);
        item.selected = false;
        return;
      }
    }
  }

  list.appendChild(item);
  item.selected = false;
};

/**
 * select a user item
 */
FriendSelector.prototype.select = function(item) {
  if (item.style.color == 'rgb(204, 204, 204)' /* FF */
      || item.style.color == '#cccccc' /* IE */) {
    item.selected = false;
    return;
  }

  var itemCopy = document.createElement("option");
  itemCopy.value = item.value;
  itemCopy.appendChild(document.createTextNode(item.text));

  var list = this.selectedList;
  var listLen = list.options.length;
  for(var i=0; i<listLen;i++){
	if(list.options[i].value == item.value){
		item.selected = false;
		item.style.color = '#cccccc';
        return;
	}	  
  }
 
  if (listLen > 0) {
    for (var i = 0; i < listLen; i++) {
    if (item.text.toLowerCase() <= list.options[i].text.toLowerCase()) {
		list.insertBefore(itemCopy, list.options[i]);
        item.selected = false;
        itemCopy.selected = false;
        item.style.color = '#cccccc';
        return;
      }
    }
  }

  list.appendChild(itemCopy);
  itemCopy.selected = false;
  item.selected = false;
  item.style.color = '#cccccc';
};

/**
 * deselect a user item
 */
FriendSelector.prototype.deselect = function(itemIndex) {
  var list = this.friendList;
  var listLen = list.options.length;
  var remove =0;	
  var selectedItem = this.selectedList.options[itemIndex];

  for (var i = 0; i < listLen; i++) {
    if (selectedItem.value == list.options[i].value) {
      list.options[i].style.color = 'black';
      this.selectedList.options[itemIndex] = null;
	  remove=1;      
    }
  }
  if (remove!=1){
	this.selectedList.options[itemIndex] = null;
    remove=1;      	  
  }
  return;
};

/**
 * move all items from friendList to selectedList
 */
FriendSelector.prototype.addAllFriends = function() {
  var listLen = this.friendList.options.length;

  for (var i = listLen - 1; i > -1; i--) {
    this.select(this.friendList.options[i]);
  }
};

/**
 * move the selected items from friendList to selectedList
 */
FriendSelector.prototype.addFriends = function() {
  var listLen = this.friendList.options.length;

  for (var i = listLen - 1; i > -1; i--) {
    if (this.friendList.options[i].selected) {
      this.select(this.friendList.options[i]);
    }
  }
};

/**
 * remove the selected items from selectedList to friendList
 */
FriendSelector.prototype.unselect = function() {
  var listLen = this.selectedList.options.length;

  for (var i = listLen - 1; i > -1; i--) {
    if (this.selectedList.options[i].selected) {
      this.deselect(i);
    }
  }
};

/**
 * remove all items from selectedList to friendList
 */
FriendSelector.prototype.unselectAll = function() {
  var listLen = this.selectedList.options.length;

  for (var i = listLen - 1; i > -1; i--) {
    this.deselect(i);
  }
};

/**
 * select a particular user
 */
FriendSelector.prototype.selectUser = function(userId) {
  var listLen = this.friendList.options.length;

  for (var i = listLen - 1; i > -1; i--) {
    if (this.friendList.options[i].value == userId) {
      this.select(this.friendList.options[i]);
    }
  }
};
