﻿var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

function InitializeRequest(sender, args)
{
  enableUrlPoll = false;
  
  disableControls();
  
  for(var i = 0; i < isTabLoaded.length; i++)
  { OQP_Text.childNodes[hack_offset + i*(1+hack_offset)].childNodes[hack_offset].style.color = "#888888"; }
  oqp_loader.style.visibility = 'visible';
}

function EndRequest(sender, args)
{
  var i = parseInt(HF_TabIndex.value);
  if(tabWasTrigger)
  {
    isTabLoaded[i] = 1;
    OQP_Text.childNodes[hack_offset + i*(1+hack_offset)].style.visibility = "visible";
    
    if(OQP_Text_scrollbar)
    {
      var tmp = new Array();
      for(var j = 0; j < 3; j++)
      {
        tmp[j] = OQP_Text.childNodes[j + (hack_offset*(j+1))];
        if(j == i)
        { tmp[j].y = -OQP_Text_scroller._y; }
      }
      for(var j = 0; j < 3; j++)
      {
        if(j == i)
        {
          j = i;
          OQP_Text_scrollbar.swapContent(tmp[j]);
          break;
        }
      }
    }
    
    tabWasTrigger = false;
  }
  else
  {
    for(var j = 0; j < t_OQP_Tabs.rows[0].cells.length-2; j+=2 )
    {
      if(j != i*2 && j != 4)
      {
        if( isTabLoaded[j/2] != 2 )
        { isTabLoaded[j/2] = 0; }
      }
    }
  }
  oqp_commentaries = document.getElementById('oqp_commentaries' + HF_TabIndex.value);
  t_oqp_cTabs = document.getElementById('t_oqp_cTabs' + HF_TabIndex.value);
  HF_CommHighlightState.value = "";
  HF_CommHighlightState2.value = "";
  
  pageWasTrigger = false;
  
  OQP_Text_scroller.resizeY();
  OQP_Text_scrollbar.resizeY();
  
  if( OQP_Text.childNodes[hack_offset].childNodes[hack_offset].childNodes[hack_offset].childNodes.length != hack_offset )
  { highlightAya(); }
  
  if( firsttime
   && ( navigator.userAgent.indexOf("Chrome") != -1
     || navigator.userAgent.indexOf("Opera") != -1
     || navigator.userAgent.indexOf("Safari") != -1 ) )
  {
    var tabindex = parseInt(HF_TabIndex.value);
    var textPanelHeight = OQP_Text.childNodes[hack_offset + tabindex*(1+hack_offset)].childNodes[hack_offset].childNodes[hack_offset].offsetHeight;
    setTimeout( "handleDelayedFont(" + tabindex + "," + textPanelHeight + "," + textPanelHeight + ");", 410);
    forceScrollbarUpdate = false;
    setTimeout( "forceScrollbarUpdate = true;",5000);
  }
  else
  { setTimeout( "enableControls();", 410); }
  
  if( HF_SearchTarget.value != "" )
  {
    var tmp = document.getElementById('ctl00_ctl150_t_OQP_Ctrlp_SearchTargets').getElementsByTagName('input');
    RBL_SearchTarget0 = tmp[0];
    RBL_SearchTarget1 = tmp[1];
    RBL_SearchTarget2 = tmp[2];
    RBL_SearchTarget3 = tmp[3];
    RBL_SearchTarget4 = tmp[4];
    RBL_SearchTarget5 = tmp[5];
    setTimeout("RBL_SearchTarget" + HF_SearchTarget.value + ".checked=true;",0);
  }
  
  updateTitle();
  
  searchResIsUthma = false;
  enableUrlPoll = true;
}

function disableControls()
{
  oqp_ctrlp_bottom.childNodes[hack_offset].rows[0].cells[0].style.color = "#999999";
  
  blockOQP(true);
  
  TB_OQP_Search.disabled = true;
  RBL_SearchTarget0.disabled = true;
  RBL_SearchTarget1.disabled = true;
  RBL_SearchTarget2.disabled = true;
  RBL_SearchTarget3.disabled = true;
  RBL_SearchTarget4.disabled = true;
  RBL_SearchTarget5.disabled = true;
  disableNonSearchControls(true);
}

function handleDelayedFont(tabindex,prevheight,nextheight)
{
  if( prevheight == nextheight && !forceScrollbarUpdate )
  {
    prevheight = nextheight;
    nextheight = OQP_Text.childNodes[hack_offset + tabindex*(1+hack_offset)].childNodes[hack_offset].childNodes[hack_offset].offsetHeight;
    setTimeout( "handleDelayedFont(" + tabindex + "," + prevheight + "," + nextheight + ");",200);
  }
  else
  {
    forceScrollbarUpdate = false;
    
    OQP_Text_scroller.resizeY();
    OQP_Text_scrollbar.resizeY();
    
    highlightAya();
    
    enableControls();
    firsttime = false;
  }
}
function enableControls()
{
  oqp_ctrlp_bottom.childNodes[hack_offset].rows[0].cells[0].style.cssText = '';
  
  TB_OQP_Search.disabled = false;
  RBL_SearchTarget0.disabled = false;
  RBL_SearchTarget1.disabled = false;
  RBL_SearchTarget2.disabled = false;
  RBL_SearchTarget3.disabled = false;
  RBL_SearchTarget4.disabled = false;
  RBL_SearchTarget5.disabled = false;
  if( !searchtabIsOpen )
  { disableNonSearchControls(searchtabIsOpen); }
  
  DDL_ShowArabic.disabled = !( CBL_Show2.checked && !searchtabIsOpen );
  for(var i = 0; i < isTabLoaded.length; i++)
  { OQP_Text.childNodes[hack_offset + i*(1+hack_offset)].childNodes[hack_offset].style.color = "#181818"; }
  oqp_loader.style.visibility = 'hidden';
  
  blockOQP(false);
}

function disableNonSearchControls(bool)
{
  if(bool)
  {
    oqp_ctrlp_bottom.childNodes[hack_offset].rows[2].cells[0].style.color = "#999999";
    oqp_ctrlp_bottom.childNodes[hack_offset].rows[4].cells[0].style.color = "#999999";
    oqp_ctrlp_bottom.childNodes[hack_offset].rows[6].cells[0].style.color = "#999999";
  }
  else
  {
    oqp_ctrlp_bottom.childNodes[hack_offset].rows[2].cells[0].style.cssText = '';
    oqp_ctrlp_bottom.childNodes[hack_offset].rows[4].cells[0].style.cssText = '';
    oqp_ctrlp_bottom.childNodes[hack_offset].rows[6].cells[0].style.cssText = '';
  }
  
  b_oqp_sura.disabled = bool;
  DDL_OQP_Aya.disabled = bool;
  DDL_OQP_Aya2.disabled = bool;
  TB_OQP_PageSelect.disabled = bool;
  if(!bool)
  {
    b_oqp_nextpage.disabled = (HF_PageState.value == "604");
    b_oqp_prevpage.disabled = (HF_PageState.value == "1");
  }
  else
  {
    b_oqp_nextpage.disabled = bool;
    b_oqp_prevpage.disabled = bool;
  }
  
  b_oqp_trans.disabled = bool;
  CBL_Show1.disabled = bool; CBL_Show1.parentNode.disabled = bool;
  CBL_Show2.disabled = bool; CBL_Show2.parentNode.disabled = bool;
  DDL_ShowArabic.disabled = bool;
}
