var currId = -1;
var mouseY; // Mouse Y position onclick
var mouseX; // Mouse X position onclick
var minDragWidth = 30;
var drag_x=null;
var drag_o=null;
var drag_w=0;
var drag_curWidth=0;
function mouse_down(e) {
  var target = Event.element(e);
  if (!browser.isIE) target = e.target;
  var pointer = [Event.pointerX(e), Event.pointerY(e)];
  drag_o = target;
  drag_x=pointer[0];
  drag_w=$('leftOuter').offsetWidth;
  return false;
}
function mouse_move(e) {
  if (drag_o!=null) {
    var pointer = [Event.pointerX(e), Event.pointerY(e)];
    drag_curWidth = (pointer[0] - drag_x +drag_w > minDragWidth) ? pointer[0] - drag_x + drag_w : minDragWidth;
    drag_curWidth = drag_curWidth<150 ? 150 :drag_curWidth ;  
    $('leftOuter').style.width=drag_curWidth+'px';
    resizeWindow();
  }
  return false;
}
function mouse_select() {
  return !drag_o;
}
function mouse_up() {
  if (drag_o!=null) {
   resizeWindow();
  }
  drag_o=null;
  return false;
}
var currNav = 'left';
function openNav(id) {
  mhide(currNav);
  $(currNav+'Tab').id=id+'Tab';
  currNav=id;
  mshow(currNav);
  $('kw').value='';
  if (currNav=='leftSearch')
  $('kw').focus();
}
function doSearch() {
  var url="searchHelp.htm?kw="+$('kw').value;
  new Ajax.Updater('searchResult',url,{asynchronous:false, evalScripts:true,onComplete: function(){
  }});
  return false;
}
function helpDetail(id,isSearch) {
  if (isSearch==null) isSearch=false;
  var url="ajaxHelp.htm?helpId="+id;
  new Ajax.Updater('dummy',url,{asynchronous:false, evalScripts:true,onComplete: function(){
    $('title').innerHTML=$('hTitle').innerHTML;
    $('details').innerHTML=$('hContent').innerHTML;
    mhide('closeLink');
    var liId = isSearch?'lis':'li';
    if ($(liId+currId)) {
      $(liId+currId).style.background="white";
      $(liId+currId).firstChild.style.color="#065587";
    }
    currId=id;
    $(liId+currId).style.background="lightyellow";
    $(liId+currId).firstChild.style.color="red";
  }});
}
function resizeWindow() {
  $('leftOuter').style.height=(getWindowHeight()-$('header').offsetHeight-$('footer').offsetHeight-33)+'px';
  $('left').style.height=($('leftOuter').offsetHeight-34)+'px';
  $('leftSearch').style.height=($('leftOuter').offsetHeight-34)+'px';
  $('divider').style.height=$('leftOuter').offsetHeight+'px';
  $('helpMain').style.height=$('leftOuter').offsetHeight+'px';
  $('footer').style.marginTop=0;
  $('helpMain').style.width=((getWindowWidth()<1000?1000:getWindowWidth())-$('leftOuter').offsetWidth-30)+'px';
  $('left').style.width=($('leftOuter').offsetWidth)+'px';
  $('leftSearch').style.width=($('leftOuter').offsetWidth)+'px';
}
function eventStart() {
  var target=$('divider');
  Event.observe(document,'mousemove',mouse_move.bindAsEventListener());
  Event.observe(target,'mousedown',mouse_down.bindAsEventListener());
  Event.observe(document,'mouseup',mouse_up.bindAsEventListener());
  Event.observe(target,'select',mouse_select.bindAsEventListener());
}
function pop_mouse_out() {
  mhide('filter'+currRelatedId);
}
function pop_mouse_over() {
  mshow('filter'+currRelatedId);
}
var currRelatedId = 0;
function relatedInputChange(id) {
  currRelatedId=id;
  Event.observe('filter'+id,'mouseout',pop_mouse_out.bindAsEventListener());
  Event.observe('filter'+id,'mouseover',pop_mouse_over.bindAsEventListener());
  var input = $('keyword'+id).value;
  var ret0 = new Array();
  var ret1 = new Array();
  for (var i=0; i<allHelps.length;i++) {
    var helpObj = allHelps[i];
    var idx = helpObj.name.toLowerCase().indexOf(input.toLowerCase());
    if (idx>-1 && helpObj.value!=id) {
      if (idx==0) ret0[ret0.length]=helpObj;
      else ret1[ret1.length]=helpObj;
    }
  }
  var filter = ret0.concat(ret1);
  var s='';
  for (var i=0;i<filter.length;i++) {
    var helpObj = filter[i];
    s+='<a href="javascript:addRelated('+id+','+helpObj.value+',\''+replaceStr(helpObj.name,'\'','||')+'\')" style="display:block;">'+helpObj.name+'</a>';
  }
  if (!isEmptyString(s)) {
    $('filter'+id).innerHTML=s;
    mshow('filter'+id);
  }
  else
    mhide('filter'+id);
  return true;
}
function addRelated(id,value,name) {
  var existed = false;
  for (var i=0;i<$('relatedIds_'+id).length;i++) {
    if ($('relatedIds_'+id).options[i].value==value) existed=true;
  }
  if (!existed)
    $('relatedIds_'+id).options[$('relatedIds_'+id).length]= new Option(replaceStr(name,'||','\''), value, false, false);
}
function delRelated(id) {
  $('relatedIds_'+id).options[$('relatedIds_'+id).selectedIndex]= null;
}
function preview(id) {
  mhide('list');
  mshow('dummy');
  $('pParent').innerHTML = $('parent_'+id).options[$('parent_'+id).selectedIndex].text;
  $('pId').innerHTML = $('id_'+id).value;
  $('pTitle').innerHTML = $('title_'+id).value;
  $('pContent').innerHTML = $('content_'+id).value;
  var line = '';
  for (var i=0;i<$('relatedIds_'+id).length;i++) {
    line+=$('relatedIds_'+id).options[i].text+', ';
  }
  $('pRelated').innerHTML = line;
  previewId = id;
  if (location.href.indexOf('#')==-1)
   location.href=location.href+'#top';
  else
    location.href=location.href.substr(0,location.href.indexOf('#'))+'#top';

}
function closePreview(id) {
  mshow('list');
  mhide('dummy');
  if (location.href.indexOf('#')==-1)
   location.href=location.href+'#_a'+previewId;
  else
    location.href=location.href.substr(0,location.href.indexOf('#'))+'#_a'+previewId;
}
