var TOOLBAR, CONTENTS, NEXT, PREVIOUS, TOC, APPLET, CONTENTS_TOPIC, DISPLAY, TOP, META_WINDOW,
    IMAGE_FRAME, IMAGE_FRAME_HEIGHT, IMAGE_FRAME_STATUS=0, IMAGE_WINDOW, IMAGE_DISPLAY_STATUS='inline',
    IS_LOAD = false, KB_ROOT_URL = "", KB_WIN = window.top;


function openFactLink(facts)
{
  if (window.top.STANDALONE)
    return;

  _execRequest ('openFactLink:' + facts.join(';'));
}

function openResourceLink(resourceLink)
{
  if (window.top.STANDALONE)
    return;

  _execRequest ("openResourceLink " + resourceLink);
}

function getTocTreeFrame()
{
  ensureLoad();
    
  var tocTree = TOC.frames['toc_tree'];
  if (!tocTree)
    tocTree = TOC.frames[0].frames['toc_tree'];
  
  return tocTree;
}

function ensureLoad()
{
  if (!IS_LOAD) {
    var win  = KB_WIN;
    TOP      = win.frames['kbtop'];
    var view = win.frames['view'];
    DISPLAY  = view.frames['display'];
    CONTROL  = view.frames['control'];
    TOC      = CONTROL.frames['toc'];
    TOOLBAR  = DISPLAY.frames['toolbar'].frames['toolbar_'];
    CONTENTS = DISPLAY.frames['contents'];
    TOPIC    = DISPLAY.frames['topic'];
    NEXT     = new topicobj('next');
    PREVIOUS = new topicobj('previous');

    IMAGE_FRAME        = DISPLAY.frames['image_frame'];
    APPLET = function() { 
      var win = getTocTreeFrame();
      if (win) 
        return win.document.applets[0]; 
    };

    IMAGE_FRAME_HEIGHT = 180;
    win.related_flag   = 1;
    IS_LOAD = true;
  }

  if (window.top.STANDALONE)
    RELATED = CONTROL.frames['related'];
  else
    RELATED = CONTROL.frames[CONTROL.frames.length-1].frames['related'];
}


/* ----------------------------------- */

function TestingHelper_appletCall ()
{
  ensureLoad();
  var applet = APPLET();

  var methodName = arguments[0];

  var call = "applet['" + methodName + "'](";

  for (var i = 1; i < arguments.length; i++) {
    if (i!=1)
      call += ", ";
    call += "'" + arguments[i] + "'";
  }

  call += ")";
 
  var result = eval (call);
  
  getTocTreeFrame().document.getElementById ("__js_result").setAttribute ("value", result);
}

/* ----------------------------------- */



function syncTopic()
{
  var regex = /[\&\?]topic=([^\Z\&]*)/i;
  var topic = new String (location);
  var array = topic.match (regex);
  if (array && array.length >= 2)
    CONTENTS.location.href = array[1];
}

function syncTocAuto()
{
  ensureLoad();
  if (CONTENTS_TOPIC)
    syncApplet (CONTENTS_TOPIC);
  else
    syncTopic();
}

function syncToc()
{
  if (CONTENTS && CONTENTS.CURRENT_TOPIC_URL)
    syncApplet (CONTENTS.CURRENT_TOPIC_URL, true);
}

function syncTreeOnly ()
{
  if (CONTENTS && CONTENTS.CURRENT_TOPIC_URL)
    syncApplet (CONTENTS.CURRENT_TOPIC_URL, false);
}

function syncApplet (url, showDocument)
{
  ensureLoad();
  var applet = APPLET();
  if (applet)
    applet.syncronizeUrl (url, showDocument);
}

function setActivePane (paneId)
{
  ensureLoad();
  var applet = APPLET();
  if (applet)
    applet.setActivePane (paneId);
}

function hasContent (paneId)
{
  ensureLoad();
  var applet = APPLET();
  if (applet)
    applet.hasContent (paneId);
}

function _fireTopicLoad (topicUrl)
{
  if (window.top.STANDALONE)
    return;

  window.top.__properties__["loadedTopicUrl"] = topicUrl;
  window.top._execRequest ("topicLoaded");
}

function topicobj (name)
{
  this.topic=null;
  this.disabled=false;
  this.name=name;
//  this.setDisabled=topicobj_setDisabled;
  return this;
}

function setDisabledNext (param)
{
  if (NEXT.disabled==param)
    return;

  NEXT.disabled=param;
  if (param) {
    obj = TOOLBAR.document.all['next'];
  if (obj)
      obj._setIcons("kb-buttons/next/active.gif", "kb-buttons/next/active.gif", "kb-buttons/next/active.gif");
  } else {
    obj = TOOLBAR.document.all['next'];
  if (obj)
      obj._setIcons("kb-buttons/next/default.gif", "kb-buttons/next/hilite.gif", "kb-buttons/next/active.gif");
  }
}

function setDisabledPrevious(param)
{
  if (PREVIOUS.disabled==param)
    return;

  PREVIOUS.disabled=param;
  if (param) {
    TOOLBAR.document.all['previous']._setIcons("kb-buttons/previous/active.gif", "kb-buttons/previous/active.gif", "kb-buttons/previous/active.gif");
  } else {
    TOOLBAR.document.all['previous']._setIcons("kb-buttons/previous/default.gif", "kb-buttons/previous/hilite.gif", "kb-buttons/previous/active.gif");
  }
}

function setNext(url)
{
  setObj(NEXT, url);
  
  if (isMSIE()) {
  if (!url)
    setDisabledNext(true);
  else
    setDisabledNext(false);
}
}

function setPrevious(url)
{
  setObj(PREVIOUS, url);
  
  if (isMSIE()) {
  if (!url)
    setDisabledPrevious(true);
  else
    setDisabledPrevious(false);
}
}

function setObj(myobj, url)
{
  if (!myobj)
    return;

  myobj.topic=url;
}

function next()
{
  if (NEXT.topic)
    syncApplet(NEXT.topic, true);
}

function previous()
{
  if (PREVIOUS.topic)
    syncApplet(PREVIOUS.topic, true);
}

function setTopic(myobj)
{
  if (myobj.topic) {
    var topic = KB_ROOT_URL + myobj.topic;
    if (topic != CONTENTS.location.href) {
      CONTENTS.location.href = topic;
      syncApplet (myobj.topic);
    }
  }
}

function isMSIE()
{
  if ( navigator.userAgent.match(/MSIE/i) && !(navigator.userAgent.match(/Windows 95/i) || navigator.userAgent.match(/Win95/i) || navigator.userAgent.match(/opera/i)) ) {
    return true;
  } else {
    return false;
  }
}

function setTopicPath(param)
{
  if ((CONTENTS) && (CONTENTS.CURRENT_TOPIC_PATH!=null))
    TOPIC.document.forms[0].elements['topic_path'].value=CONTENTS.CURRENT_TOPIC_PATH;
}

function setTextFields()
{
  setTopicPath();
}


/* Image display stuff */

function ImageFrame_open()
{
  if (IMAGE_FRAME_STATUS == 0) {
    var fs = DISPLAY.document.getElementById ('frameset_display');
    fs.rows = '29, 24, *, ' + IMAGE_FRAME_HEIGHT;
    IMAGE_FRAME_STATUS = 1;
  }
}

function ImageFrame_close()
{
  if (IMAGE_FRAME_STATUS == 1) {
    var fs = DISPLAY.document.getElementById ('frameset_display');
    fs.rows =  '29, 24, *, 0';
    IMAGE_FRAME.location.href = 'image_frame.html';
    IMAGE_FRAME_STATUS = 0;
  }
}

function ImageFrame_show (url)
{
  ImageFrame_open();
  var imageContainer = IMAGE_FRAME.document.getElementById ('image_container');
  imageContainer.innerHTML = '<img border="0" hspace="0" vspace="0" src="' + url + '">';
}

function ImageWindow_show (url)
{
  if (!IMAGE_WINDOW || (IMAGE_WINDOW && IMAGE_WINDOW.closed)) {
    IMAGE_WINDOW = window.open (
      url,
      "IMAGE_WINDOW",
      "width=500,height=400,toolbar=no,status=no,location=no,resizable,scrollbars"
    );
  }
  else {
    IMAGE_WINDOW.location.href = url;
  }
  IMAGE_WINDOW.focus();
}

function handleImages (status)
{
  if (!CONTENTS || !CONTENTS.document || status == 'none')
    return;
  if (IMAGE_DISPLAY_STATUS == status)
    return;
  if (status)
    IMAGE_DISPLAY_STATUS = status;

  var getImage     = getInlineImage;
  var border_style = '0px solid white';
  var padding      = '0px';
  var background   = '';
  switch (IMAGE_DISPLAY_STATUS) {
    case 'inline':
      break;

    case 'frame':
      getImage     = getFrameImage;
      border_style = '1px solid Blue';
      padding      = '10px';
      background   = 'Orange';
      break;

    case 'window':
      getImage     = getWindowImage;
      border_style = '1px solid Blue';
      padding      = '10px';
      background   = 'Orange';
      break;

    case 'thumbnail':
      getImage     = getThumbnailImage;
      break;
  }

  var spans = CONTENTS.document.getElementsByTagName ('span');
  var length = spans.length;
  for (var i = 0; i < length; i++) {
    var span = spans[i];
    var className = span.className;
    if (className && className.match (/^pic/)) {
      span.innerHTML = getImage (
        span.id,
        className.replace (/^pic__sep__/, '')
      );
      span.style.border          = border_style;
      span.style.paddingLeft     = padding;
      span.style.paddingRight    = padding;
      span.style.backgroundColor = background;
    }
  }
}

function getInlineImage (url, alt)
{
  var name = url.replace(/^.*__sep__/, '');
  return '<IMG SRC="' + name + '" alt="' + alt + '">';
}

function getFrameImage (url, alt)
{
  return getTypeImage ("Frame", url, alt);
}

function getWindowImage (url, alt)
{
  return getTypeImage ("Window", url, alt);
}

function getTypeImage (type, url, alt)
{
  var name = url.replace (/^.*__sep__/, '');
  url = CONTENTS.location.href.replace (/[^\/]*$/,'') + name;
  if (alt != '')
    name = alt;
  else name = name.replace (/([^\/]*\/)*/, '');
  return '<a href="javascript: window.top.Image' + type + '_show(\'' + url + '\')">\
          <font style="color: blue; font-weight: normal">Image link: ' + name
       + '</font></a>';
}

function getThumbnailImage (url, alt)
{
  var name = url.replace(/^.*__sep__/, '');
  url = CONTENTS.location.href.replace (/[^\/]*$/,'') + name;
  return '<img class="thumbnail" src="' + name
   + '" alt="Click to view" onclick="window.top.ImageWindow_show(\''
   + url + '\')">';
}
