﻿
// OemSchPicker.js

// Calc the position to scroll the schematic box after selection.
// If collapsed, save and restore the current position.
// If expanded (and now collapsing), put the selected thumbnail at the top.
//   Also scroll the page to the top, else it stays way down below the content.
// The difference between the selected thumbnail and the first thumbnail is the distance to scroll.
//   Because the expanded box is collapsed, can't just save the scroll position.
//   Because the search varies the height of each row, can't just calc based on row number.
//   Because the browsers' position properties vary, can't just use page position.

// Globals in calling page, to pass values from begin to end request, and calc IDs.
//  elSch               - Postback element
//  scrollTop           - Calculated scroll position result.
//  isExpanded          - Current state at time of selection.
//  divSchPickBoxID     - divSchPickBox client id, to calc server-side.
//  btnExpandTopID      - btnExpandTop link button client id, to calc server-side.

function beginRequestOemSchPicker(sender, args)
{
  elSch = args.get_postBackElement();
  scrollTop = 0;        // Reset for other postbacks.
  
  var schBox = $get(divSchPickBoxID);
  if (elSch && schBox)
  {
    // Check if a schematic selection.
    var elID = elSch.id;
    if ((elID.indexOf("btnThumb") > 0) || (elID.indexOf("btnSch") > 0))
    {
      // Check if expanded or collapsed.
      //  Check the inner html of the link button, just for one way.
      //  Wasn't sure how to get the server code to set isExpanded; those vars don't update on the postback.
      var btnExpandTop = $get(btnExpandTopID);
      if (btnExpandTop)     // Just in case.
      {
        isExpanded = (btnExpandTop.innerHTML != "Expand");    // Current state is opposite the label.
      }
      
      if (!isExpanded)
      {
        // If collapsed, save and restore the current position.
        scrollTop = schBox.scrollTop;
      }
      else
      {
        // If expanded (and now collapsing), put the selected thumbnail at the top.
        // Parse the index number from ctl00_cphMain_ucOemSchPicker_dlSch_ctl00_btnThumb
        //  or ctl00_cphMain_ucOemSchPicker_dlSch_ctl00_btnSch
        // The last ctlNN[N] has the index number.
        // Get the thumbnail (in case it was the text that was clicked).
        // Get the first thumbnail's position to know where to scroll to.
        var pos1 = elID.lastIndexOf("_ctl");
        var pos2 = elID.lastIndexOf("_");
        
        if ((pos1 > 0) && (pos2 > pos1))
        {
          // Start past _ctl, end before next underscore; parseInt to avoid string concat; Base 10 to avoid octal with leading 0. 
          //  substring acts like second param does -1 (it goes to the char before the value)
          var thumbPrefix = elID.substring(0, pos1) + "_ctl";
          var thumbIndex = parseInt(elID.substring(pos1 + 4, pos2), 10);
          
          // Get the selected thumbnail and the first thumbnail positions.
          var elThumb = $get(elID.substring(0, pos2) + "_btnThumb");
          var elFirst = $get(thumbPrefix + "00_btnThumb");
          
          var thumbPosY = findPos(elThumb)[1];
          var firstPosY = findPos(elFirst)[1];
          
          // Scroll the difference.
          scrollTop = thumbPosY - firstPosY;
        }
      }
    }
  }
}

function endRequestOemSchPicker(sender, args)
{
  var postBackElement = sender._postBackSettings.sourceElement.id;
  var iLen = postBackElement.length;
  
  if (iLen > 8) {
    if (postBackElement.substring(iLen, iLen-9) == 'btnSearch')   // fire only the click-event of the search button 
    {
      // get the machinr id in the url
      var match = window.location.href.match("\/m(\.*)$");  
      var machineId = match[1];
      
      var resultCountVal = '1';
      if (document.getElementById(lblResultsID).innerText.substring(0,1) == '0')    // check the result count
      {
        resultCountVal = '0' 
      }
      
      // WT tagging for onsite search
      var searchTermValue = 'oemsch:' + machineId + ':' + document.getElementById(txtSearchID).value;
      dcsSearchExtCall(sender,searchTermValue,resultCountVal);
    }
  }
  
  // Load the selected schematic's Ask & Answer content if bvLoadQA() is defined (i.e. if Ask & Answer is visible in the page).
  if (typeof bvLoadQA == 'function')
  {
    var askAndAnswerContentUrl = document.getElementById('askAndAnswerContentUrl').innerHTML;
    bvLoadQA(askAndAnswerBaseUrl, askAndAnswerContentUrl);
  }
  
  var schBox = $get(divSchPickBoxID);
  if (elSch && schBox)   // Just to be consistent with above.
  {
    // If expanded (and now collapsing), scroll the page to the top.
    if (isExpanded)
    {
      scroll(0, 0);
    }

    // Scroll the schematic box.
    schBox.scrollTop = scrollTop;
  }
}

// These are probably helping to preload into the browser.
//  The default head scripts have the changeimage routines;
//  this is just to get the browser to load them before the thumbnails start loading.
function preloadImagesOemSchPicker()
{
	if (document.images)
	{
	  assytab_on = newImage("/img/tabs/assytab_on.gif");
	  assytab_off = newImage("/img/tabs/assytab_off.gif");
	  assytab_hover = newImage("/img/tabs/assytab_hover.gif");
	}
}


