//Bookmark Panel
BookmarkPanel = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapId)
{
  if (arguments.length > 0)
    this.init(newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapId);
};

BookmarkPanel.prototype = new GuiWidget();
BookmarkPanel.constructor = BookmarkPanel;
BookmarkPanel.superclass = GuiWidget.prototype;

BookmarkPanel.prototype.init = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapId)
{
  BookmarkPanel.superclass.init.call(this, newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, "BookmarkPanel");

  this.id = newId;
  if (newMapId!=null)
  {
    this.mapObject = OBJECT_MANAGER.getControl(newMapId);
    this.mapId = newMapId;
  }
  else
  {
    this.mapObject = newMapObject;
    this.mapId = newMapObject.id;
  }
  this.mapObject.bookmarkControl = this;
  this.bookmarks = new Array();
  
  this.addForm  = new GuiWidget(this.id+"addForm", this.element, 0, 0, 4, this.width(), 65, true, "QueryPanel");
    this.bookmarkCaptionElement = new GuiWidget(this.id+"bookmarkCaptionElement", this.addForm.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
    this.bookmarkCaptionElement.element.cbe.innerHtml("Add New Bookmark");
    this.bookmarkAddNameElement = new GuiWidget(this.id+"bookmarkAddNameElement",this.addForm.element, 0, 20, 0, this.width(), 40, true, "loginPanelForm");
    this.bookmarkAddNameElement.element.cbe.innerHtml('<input type="text" id="'+this.id+'bookmarkFormAddInputBox"  onKeyDown="document.getWidgetById(\''+this.id+'\').onKeyPress(this,event);" size="30" value="">');
    this.bookmarkButtonElement = new GuiWidget(this.id+"bookmarkButtonElement",this.addForm.element, 0, 40, 0, this.width(), 25, true, "queryObjectBottom");
    this.bookmarkButton = new ImageButton(this.id+"bookmarkAddButton", this.bookmarkButtonElement.element, 10, 0, 0, 100,20,true,'bookmarkButton','');
    this.bookmarkButton.bookmarkPanel = this;
    this.bookmarkButton.mapObject = this.mapObject;
  this.bookmarkAddBox = document.getElementById(this.id+'bookmarkFormAddInputBox');
    this.bookmarkButton.bookmarkAddBox = this.bookmarkAddBox;
    this.bookmarkButton.clickEvent = function(e)
    {
      this.mapObject.addBookmark(this.bookmarkAddBox.value.strtrim());
    };
  this.displayForm = new GuiWidget(this.id+"displayForm", this.element, 0, this.addForm.height(), 4, this.width(), 20, true,"");
    this.displayCaptionElement = new GuiWidget(this.id+"displayCaptionElement", this.displayForm.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
    this.displayCaptionElement.element.cbe.innerHtml("Bookmarks");
    this.bookmarkListElement = new GuiWidget(this.id+"bookmarkListElement", this.displayForm.element, 0, 20, 0, this.width(), 0, true, "");
  this.height(100);
};

BookmarkPanel.prototype.gotoBookmark = function(bookmarkName)
{
  this.mapObject.gotoBookmark(bookmarkName);
};

BookmarkPanel.prototype.removeBookmark = function(bookmarkName)
{
  this.mapObject.removeBookmark(bookmarkName); 
};

BookmarkPanel.prototype.onKeyPress = function(textField,evt)
{
  //check for enter key and run query if pressed.
  if (!evt)
    evt = window.evt; 
  else
    if (!evt.keyCode)
      evt.keyCode = evt.which;
  if (evt.keyCode == 13)
  {
    this.mapObject.addBookmark(textField.value.strtrim());
    textField.value = '';
    textField.blur();
  }
};

BookmarkPanel.prototype.drawBookmarkList = function()
{
  var numBookmarks = 0;
  if (this.mapObject.bookmarks!=null)
    numBookmarks = this.mapObject.bookmarks.length;
  this.bookmarkListElement.height(100*numBookmarks);
  this.displayForm.height(this.bookmarkListElement.height()+this.bookmarkCaptionElement.height());
  var newHeight = 100 + this.displayForm.height();
  this.height(isNaN(newHeight)?100:newHeight);
  var html = '';
  if (numBookmarks>0)
  {
    html = '<table id="'+this.id+'BookmarkTable" class="bookmarkList" cellpadding="0" cellspacing="0" width="100%" style="margin-right: 10px">';
    for (i=0; i<numBookmarks; i++)
    {
      var bookmarkHTML = escapeHTML(this.mapObject.bookmarks[i]);
      html += '<tr>'
      +'<td class="bookmarkKillButton"><span class="kill" onclick="document.getWidgetById(\''+this.id+'\').removeBookmark('+i+')"><img src="'+GuiWidget.THEME_PATH+GuiWidget.THEME+'/images/deleteBookmark.png"></span></td>'
      +'<td class="bookmarkTitle"><span class="pseudolink" style="overflow: clip" onclick="document.getWidgetById(\'' + this.id + '\').gotoBookmark('+i+')">' + escapeHTML(this.mapObject.bookmarks[i]) + '</span></td>'
      +'</tr>';
    }
    html+='</table>';
  }
  this.bookmarkListElement.element.cbe.innerHtml(html);
};

BookmarkPanel.prototype.callback = function(data, requestType)
{
  var i = 0;
  var html = '';
  var numBookmarks = 0;
  switch (requestType)
  {
    case 'getBookmarkList':
      this.mapObject.bookmarks = data;
      if (this.mapObject.bookmarks!=null)
        for (var i=0;i<this.mapObject.bookmarks.length;i++)
          this.mapObject.bookmarks[i] = unescapeHTML(this.mapObject.bookmarks[i]);
      this.drawBookmarkList();
      break;
    case 'addBookmark':
    case 'removeBookmark':
      break;
    default:
      alert(this.id+'.callback error:\nUnknown request type "'+requestType+'"');
      break;
  }
};

BookmarkPanel.prototype.resize = function(newWidth, newHeight)
{
  this.element.cbe.resizeTo(newWidth, this.height());  
  this.addForm.resize(newWidth,this.addForm.height());
  this.bookmarkCaptionElement.resize(newWidth,this.bookmarkCaptionElement.height());
  this.bookmarkAddNameElement.resize(newWidth,this.bookmarkAddNameElement.height());
  this.bookmarkButtonElement.resize(newWidth,this.bookmarkButtonElement.height());
  this.displayForm.resize(newWidth,this.displayForm.height());
  this.displayCaptionElement.resize(newWidth,this.displayCaptionElement.height());
  this.bookmarkListElement.resize(newWidth,this.bookmarkListElement.height());
};


