
// Tabbed Navigation JScript source code


var navLastSelectedTab = null;
var mouseIsOverThisTab = null;
var tabsArray = new Array();
var tabCount = 0;
var lastFocussesEl = null;
var lastFocussedButton = null;

var keepHover = false;
var isInOnClickEvent = false;

var accelsActive = false;
var focusRectActive = false;

function calcAbsoluteLeft(el){
    if (el)
      return el.offsetLeft + calcAbsoluteLeft(el.offsetParent);
    else
      return 0;
}

function calcAbsoluteTop(el){
    if (el){
      y = el.offsetTop + calcAbsoluteTop(el.offsetParent);
      return y;
    }
    else
      return 0;
}        
function navSetTabState(TabElementId, NewState){
    id_l = TabElementId + '.l';
    id_m = TabElementId + '.m';
    id_r = TabElementId + '.r';
    id_caption = TabElementId + '.Caption';

    document.getElementById(id_l).className = 'nav_tab_' + NewState + '_l';
    document.getElementById(id_m).className = 'nav_tab_' + NewState + '_m';
    document.getElementById(id_r).className = 'nav_tab_' + NewState + '_r';
    document.getElementById(id_caption).className = 'nav_caption_' + NewState;


    document.getElementById(TabElementId).className = 'nav_tab_' + NewState;

}

function navOnMouseEnterHandler(el){
    mouseIsOverThisTab = el;
    id = el.getAttribute('id');
    active = el.getAttribute('active');
    enabled = el.getAttribute('enabled');
    Tabs.setAttribute('hoveredTabId', id);
    if ((active == 'true') || (enabled == false || enabled == 'false' || enabled == 'False'))
      return;
    navSetTabState(id, 'hover');
}

function navOnMouseLeaveHandler(el){
    mouseIsOverThisTab = false;
    id = el.getAttribute('id');
    active = el.getAttribute('active');
    enabled = el.getAttribute('enabled');
    if ((active == 'true') || (enabled == false || enabled == 'false' || enabled == 'False'))
      return;
    navSetTabState(id, 'normal');
}

function navOnMouseClickHandler(el){
    id = el.getAttribute('id');
    active = el.getAttribute('active');
    enabled = el.getAttribute('enabled');
    if ((active == 'true') || (enabled == false || enabled == 'false' || enabled == 'False'))
      return;

    res = triggerHostBeforeTabChange(navLastSelectedTab, el);
    if (res == 'false')
      return;

    navSetTabState(id, 'selected');
    el.setAttribute('active','true');

    Tabs.setAttribute('activeTabId', id);
    
    document.getElementById(id+'.CaptionCont').focus();


    if (navLastSelectedTab) {
      id = navLastSelectedTab.getAttribute('id');
      navSetTabState(id, 'normal');
      navLastSelectedTab.setAttribute('active','false');
    }
    triggerHostAfterTabChange(navLastSelectedTab, el) ;
    navLastSelectedTab = el;
}


function navMouseDownHandler(el){
    

}


function setActiveTab(activeTabId){

    triggerHostBeforeTabChange(navLastSelectedTab, document.getElementById(activeTabId)) ;

    navSetTabState(activeTabId,'selected');
    document.getElementById(activeTabId).setAttribute('active','true');

    Tabs.setAttribute('activeTabId', activeTabId);

    if (navLastSelectedTab) {
      id = navLastSelectedTab.getAttribute('id');
      navSetTabState(id, 'normal');
      navLastSelectedTab.setAttribute('active','false');
    }

    navLastSelectedTab = document.getElementById(activeTabId);        

    triggerHostAfterTabChange(navLastSelectedTab, document.getElementById(activeTabId)) ;

    navLastSelectedTab = document.getElementById(activeTabId);
}

function alignButtonsBar(){
    buttons_Bar = document.getElementById('buttonsBar');

    if (buttons_Bar){
      buttonsBarWidth  = buttons_Bar.offsetWidth;
      buttonsBarHeight = buttons_Bar.offsetHeight;
      
      //alert(buttonsBarWidth);
      main_Window = document.getElementById('mainWindow');
      windowWidth  = main_Window.offsetWidth;
      windowHeight = main_Window.offsetHeight;
      pixelLeft = (windowWidth - buttonsBarWidth) - 20;
      
      buttonsBar.style.pixelLeft = pixelLeft;
      
    }     
    if (lastFocussedButton != null){
      el = lastFocussedButton;
      ALeft = el.offsetLeft + el.offsetParent.offsetLeft + pixelLeft;
      ATop = el.offsetTop  + el.offsetParent.offsetTop + buttonsBar.offsetTop;
     
      ButtonHoverRoundRect.style.pixelLeft = ALeft;
      //ButtonHoverRoundRect.style.pixelTop = ATop;
                
      leftR = backgroundL.offsetWidth + backgroundM.offsetWidth + 1;
      
      //ButtonHoverRoundRect.style.backgroundPositionX = (leftR - ALeft);
      //ButtonHoverRoundRect.style.backgroundPositionY = (-1) * ATop;                    
    }
}
function triggerHostBeforeTabChange(oldTab, newTab){
  res = 'error';
  try{
       if (oldTab){
              oldTabName = oldTab.getAttribute('id');
	            oldTabAppData = oldTab.getAttribute('appData')+ '';
           }else{
              oldTabName = 'NA';
	            oldTabAppData = 'NA';
           }
	     
           if (newTab){
              newTabName = newTab.getAttribute('id');
	            newTabAppData = newTab.getAttribute('appData')+ '';
           }else{
              newTabName = 'NA';
	            newTabAppData = 'NA';
           }	
       
       res = window.external.HandleBeforeTabChange(oldTabName, newTabName , oldTabAppData , newTabAppData);
       return res;
       
  }
  catch(e){
    return true;
  }
}
function triggerHostAfterTabChange(oldTab, newTab){
 try{
     if (oldTab){
          oldTabName = oldTab.getAttribute('id');
            oldTabAppData = oldTab.getAttribute('appData');
       }else{
          oldTabName = '';
            oldTabAppData = '';
       }
     
       if (newTab){
          newTabName = newTab.getAttribute('id');
            newTabAppData = newTab.getAttribute('appData');
       }else{
          newTabName = '';
            newTabAppData = '';
       }
     
     window.external.HandleAfterTabChange(oldTabName , newTabName , oldTabAppData , newTabAppData);
     
 }
 catch(e){
   //alert(e);
 }
}     

     
function initNav(activeTabId){
    try{
      window.external.OnDocumentLoaded();
    }
    catch(e){
        //alert(e);
     }        

    backgroundL.style.pixelWidth=backgroundImgL.offsetWidth;
    backgroundR.style.pixelWidth=backgroundImgR.offsetWidth;

    mainWindow.style.pixelHeight = backgroundImgL.offsetHeight;
    setActiveTab(activeTabId);

    alignButtonsBar();
    //updateNavTab('NavTab4', 'NavTab4', true, true);

}

function recalcButtonHoverEffectPosition(el){
    ALeft = calcAbsoluteLeft(el);
    ATop  = calcAbsoluteTop (el)-1;
    //ButtonHoverRoundRect.style.backgroundImage = 'url('+backgroundImgR.src+')';
    ButtonHoverRoundRect.style.pixelLeft = ALeft;
    ButtonHoverRoundRect.style.pixelTop = ATop;
    ButtonHoverRoundRect.style.pixelWidth = el.offsetWidth;
    ButtonHoverRoundRect.style.pixelHeight = el.offsetHeight + 1;
    ButtonHoverRoundRect.coordsize = el.offsetWidth + ' ' + el.offsetHeight;
    /*
    ButtonHoverRoundRect_Shape1.style.pixelWidth = el.offsetWidth;
    ButtonHoverRoundRect_Shape1.style.pixelHeight = el.offsetHeight;
    ButtonHoverRoundRect_Shape1.coordsize = el.offsetWidth + ' ' + el.offsetHeight;

    ButtonHoverRoundRect_Shape2.style.pixelWidth = el.offsetWidth-2;
    ButtonHoverRoundRect_Shape2.style.pixelHeight = el.offsetHeight-2;
    ButtonHoverRoundRect_Shape2.coordsize = (el.offsetWidth-2) + ' ' + (el.offsetHeight-2);
    */
    leftR = backgroundL.offsetWidth + backgroundM.offsetWidth + 1;

    ButtonHoverRoundRect.style.backgroundPositionX = (leftR - ALeft);
    ButtonHoverRoundRect.style.backgroundPositionY = (-1) * ATop;

}
var avoidMouseOver = false;
function handleButtonOnMouseEnter(el){

    lastFocussedButton = el;

    if (avoidMouseOver)
      return false;

    recalcButtonHoverEffectPosition(el);
    

    id = el.getAttribute('id');
    document.getElementById(id+'.Caption').className = 'button_hover';

    if (ButtonHoverRoundRect.filters[0])
      ButtonHoverRoundRect.filters[0].Apply(); 
    ButtonHoverRoundRect.style.visibility = 'visible';  
    if (ButtonHoverRoundRect.filters[0])      
      ButtonHoverRoundRect.filters[0].Play();
    return false;
}
function handleButtonOnMouseLeave(el){


    if (keepHover) 
      return false;
     
    lastFocussedButton = null;    
    
    if (ButtonHoverRoundRect.filters[0]){
      ButtonHoverRoundRect.filters[0].Stop();
      ButtonHoverRoundRect.filters[0].Apply();        
    }
    ButtonHoverRoundRect.style.visibility = 'hidden'; 
    if (ButtonHoverRoundRect.filters[0]){
      ButtonHoverRoundRect.filters[0].Play();
    }
  
    id = el.getAttribute('id');
    document.getElementById(id+'.Caption').className = 'button_normal';
    return false;
    
}

var
fcolor1 = '';
fcolor2 = '';

function handleButtonOnMouseDown(el){

    //fcolor1 = ButtonHoverRoundRect_fill.color + '';
    //fcolor2 = ButtonHoverRoundRect_fill.color2 + '';
    //ButtonHoverRoundRect_fill.color  = fcolor2;
    //ButtonHoverRoundRect_fill.color2 = fcolor1;       
}
function handleButtonOnMouseUp(el){

    //idCaption = button.id + '.Caption';
    //document.getElementById(idCaption).blur();       
    //ButtonHoverRoundRect_fill.color  = fcolor1;
    //ButtonHoverRoundRect_fill.color2 = fcolor2;
}

function triggerHostButtonClick(button){
     event.cancelBubble = true;
       
     if (event.srcElement.id.indexOf('.label') > 0)
       return true;
     
     
     keepHover = true; 
     
     isInOnClickEvent = true;
     
     try{
         idCaption = button.id + '.Caption';
         
         document.getElementById(idCaption).focus();
         if (button){
              buttonName = button.getAttribute('id');                  
              action     = button.getAttribute('action');
         }else{
              buttonName = '';
              action     = '';
         }
         
         
         window.external.HandleButtonClick(buttonName, action);
         
     }
     catch(e){
        alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message);
     }
     isInOnClickEvent = false;
     keepHover = false;
     handleButtonOnMouseLeave(button);
     avoidMouseOver = true;
     setTimeout("avoidMouseOver = false;",100);
     return false;
     
}    

function handleButtonOnKeyPress(el){
    
    if (event.keyCode == 13 || event.keyCode == 32) 
      if (!isInOnClickEvent){
        triggerHostButtonClick(el);
       }
}
function handleButtonCaptionOnFocus(captionEl, buttonEl){
    //window.status='focus:'+event.srcElement.id;
    if (focusRectActive){
     if (!isInOnClickEvent){
        handleButtonOnMouseEnter(buttonEl);
     }
    }
    else{
     if (captionEl.blur) 
        captionEl.blur();
    }
}

function handleButtonCaptionOnBlur(captionEl, buttonEl){
    //window.status= 'blur:'+event.srcElement.id;
    //if (!isInOnClickEvent)
    // handleButtonOnMouseLeave(buttonEl);
}

function changeTabCaption(tabId, NewCaption){
    tabCaptionElId = tabId+'.Caption';
    tabCaptionEl = document.getElementById(tabCaptionElId);
    var temp = new Array();
    temp = NewCaption.split('&');
    if (temp.length == 0)
      temp = NewCaption.split('&amp;');
    if (temp.length > 0){
      part1 = temp[0];
      accelChar = temp[1].substring(0,1);
      part2 = temp[1].substring(1,temp[1].length);
    }else{
      part1 = NewCaption;
      accelChar = '';
      part2 = '';
    }
    //alert(accelChar);
    tabCaptionEl.accessKey = accelChar;

    if (accelsActive) 
      textDecoration = 'underline';
    else
      textDecoration = 'none';

    if (tabCaptionEl)
     tabCaptionEl.innerHTML = part1 + '<span id="' + tabCaptionElId + '.accel" accelchar="true" style="text-decoration:'+ textDecoration + '">' + accelChar + '</span>' + part2;
    else
     alert('tab caption element not found. id:' + tabCaptionElId);
}
function updateNavTab(tabId, tabCaption, enabled, visible){

    changeTabCaption(tabId, tabCaption);
    tabEl = document.getElementById(tabId);
    tabCaptionEl = document.getElementById(tabId+'.CaptionCont');
    tabEl.setAttribute('enabled', enabled);
    if (enabled){
     tabCaptionEl.tabIndex = tabCaptionEl.getAttribute('tabIndexCache');
     navSetTabState(tabId,'normal');
    }else{
     tabCaptionEl.tabIndex = null;
     navSetTabState(tabId,'disabled');
    }
    if (mouseIsOverThisTab){
     navOnMouseEnterHandler(mouseIsOverThisTab);
    }
}
function doOnFocus(el){
    
    lastFocussesEl = el;
    if (el != null) 
       navOnMouseEnterHandler(el);
}
function doOnBlur(el){
    if (el != null) 
       navOnMouseLeaveHandler(el);
}
function doOnKeyPress(el){

    keyCodeSpace = 32;
    keyCodeEnter = 13;
    if ((event.keyCode == keyCodeSpace) || (event.keyCode == keyCodeEnter)){
      if (el != null)
        navOnMouseClickHandler(el);
    }else{
      //alert(event.keyCode);
    }

}
function doOnKeyDown(el){

    keyCodeLeft  = 37;
    keyCodeRight = 39;
    if  ( event.keyCode == keyCodeRight){
      activeTabId = Tabs.getAttribute('hoveredTabId');
      
      activeTab = document.getElementById(activeTabId);
      tabIdToActivate = activeTab.getAttribute('nextTabId');
      tabToActivate = document.getElementById(tabIdToActivate);
      while (tabToActivate.getAttribute('enabled') == 'false'){
        tabIdToActivate = tabToActivate.getAttribute('nextTabId');
        tabToActivate = document.getElementById(tabIdToActivate);
      }
      navOnMouseLeaveHandler(activeTab);
      navOnMouseEnterHandler(tabToActivate);
      id_caption = tabToActivate.id + '.CaptionCont';
      document.getElementById(id_caption).focus();  
      document.getElementById(id_caption).setActive();              
    }
    if  ( event.keyCode == keyCodeLeft){
      activeTab = document.getElementById(Tabs.getAttribute('hoveredTabId'));
      tabIdToActivate = activeTab.getAttribute('prevTabId');
      tabToActivate = document.getElementById(tabIdToActivate);
      while (tabToActivate.getAttribute('enabled') == 'false'){
        tabIdToActivate = tabToActivate.getAttribute('prevTabId');
        tabToActivate = document.getElementById(tabIdToActivate);
      }          
      navOnMouseLeaveHandler(activeTab);
      navOnMouseEnterHandler(tabToActivate);
      id_caption = tabToActivate.id + '.CaptionCont';
      document.getElementById(id_caption).focus();  
      document.getElementById(id_caption).setActive();              
      
    }        
}


var busy = false;



//************************************* Accel Char and Focus Handling **************************************

var tabPressed = false;

function setAccelCharStyle(style){
  for (i=0; i < document.all.length; i++){
    el = document.all(i);
    if (el.getAttribute('accelchar') == 'true'){
      el.style.textDecoration = style;
    }
  }
}
function showAccelChars(){
  accelsActive = true;
  setAccelCharStyle('underline');
}

function hideAccelChars(){
  accelsActive = false;
  setAccelCharStyle('none');
}

function setHideFocusProp(newState){
  for (i=0; i < document.all.length; i++){
    el = document.all(i);
    el.hideFocus = newState;
  }
}
function showFocusRect(){
  //alert('focus rectangles will be shown');
  focusRectActive = true;
  setHideFocusProp(false);
}

function hideFocusRect(){
  //alert('focus rectangles will be hidden');
  focusRectActive = false;
  setHideFocusProp(true);
}

function isVisible(el){
    if (el == null)
      return true;
    if (el.currentStyle.visibility == 'visible')
      return isVisible(el.parentElement);
    else if (el.currentStyle.visibility == 'hidden')
      return false;
    else if (el.currentStyle.visibility == 'inherit')
      return isVisible(el.parentElement)
    else
      return false;
}

function getFirstControlInTabOrder(){
  tabIndexMin = 255;
  res = null;               
  for (i = 0; i < document.all.length; i++){
    el = document.all[i];
    
    if (el.id == 'firstTabEventGenerator')
      continue;
      
    if (el.id == 'lastTabEventGenerator')
      continue;
    if (isVisible(el))      
    if (el.tabIndex  > 0)
      if (el.tabIndex < tabIndexMin){
        tabIndexMin = el.tabIndex;
        res =  el;
      }
  }
  return res;
}
function getLastControlInTabOrder(){
  tabIndexMax = -1;
  res = null;
  for (i = 0; i < document.all.length; i++){
    el = document.all[i];
    
    if (el.id == 'firstTabEventGenerator')
      continue;
      
    if (el.id == 'lastTabEventGenerator')
      continue;
    if (isVisible(el))         
    if (el.tabIndex >= tabIndexMax){
      tabIndexMax = el.tabIndex;
      res =  el;
    }
  }
  return res;
}
function selectPrevControl(){

    try{
      
      window.status = "selectPrevControl(): "+tabPressed;

      if (tabPressed){
        window.external.selectPrevControl();
      }else{
             firstEl = getFirstControlInTabOrder();
             firstEl.focus();       
      }
    }
    catch(e){
       window.status = "TabbedNav ScriptException. Error name: " + e.name + ". Error message: " + e.message;
    }
    
}
function selectNextControl(){
    try{
          
          window.status = "selectNextControl():"+tabPressed;
        
          if (tabPressed){
             window.external.selectNextControl();
          }else{
             firstEl = getFirstControlInTabOrder();
             firstEl.focus();      
          }
    }
    catch(e){
       window.status = "TabbedNav ScriptException. Error name: " + e.name + ". Error message: " + e.message;
    }  

    
}


//************************************* Document Handling **************************************
myTimeout = null;
function doOnDocKeyUp(){
  //window.status = event.keyCode;
  if (event.keyCode == 9){
    tabPressed = true;    
    if (myTimeout != null);
      clearTimeout(myTimeout);
    myTimeout = setTimeout("tabPressed = false;",500);
    window.status = "doOnDocKeyUp";
  }  
  return true;
}

function doOnDocMouseDown(){

}

function doOnWindowResize(){

  if (lastFocussedButton != null){
    recalcButtonHoverEffectPosition(lastFocussedButton);
  }
}
function doOnWindowBlur(){
}
function doWindowMouseLeave(){
}
window.onresize = doOnWindowResize;
window.onblur   = doOnWindowBlur;


document.onkeyup=doOnDocKeyUp;
//document.onmousedown=doOnDocMouseDown;

