﻿function init()
{
  document.onkeydown = handleOtherKeys;
  
  var tmp;
  document.getElementById("image_preloader").style.display = "none";
  
  hack_offset = 1;
  if(t_OQP_Tabs.childNodes[0].nodeType == 1)
  { hack_offset = 0; }
  
  t_OQP_SuraSelection_Local = OQP_Flyout_Sura_Content.childNodes[hack_offset].childNodes[hack_offset].childNodes[hack_offset];
  
  if(HF_Lang.value == "ar")
  {
    direction = -1;
    tmp = document.getElementById("oqp_designhack01");
    tmp.style.right = "auto";
    tmp.style.left = "-1px";
    tmp = tmp.childNodes[0];
    tmp.style.marginTop = "-110px";

    direction = -1;
    document.getElementById("ctl00_OQP_Body_Ctrlp").childNodes[hack_offset].childNodes[0].style.marginLeft = "-109px";
    oqp_ctrlp_bottom.childNodes[hack_offset].rows[2].cells[0].childNodes[hack_offset].childNodes[hack_offset+1].childNodes[hack_offset].childNodes[1+2*hack_offset].rows[0].cells[1].childNodes[0].childNodes[0].style.marginLeft = "-47px";
    tmp = document.getElementById("t_oqp_flyout_sura_bottom");
    tmp.rows[0].cells[0].childNodes[0].className = "oqp_flyout_bottomleft";
    tmp.rows[0].cells[0].childNodes[0].childNodes[0].className = "oqp_flyout_bottomleft_canvas";
    tmp.rows[0].cells[2].childNodes[0].className = "oqp_flyout_right";
    tmp.rows[0].cells[2].childNodes[0].childNodes[0].className = "";
  }
  else
  {
    direction = 1;
    oqp_ctrlp_bottom.childNodes[hack_offset].rows[2].cells[0].childNodes[hack_offset].childNodes[hack_offset+1].childNodes[hack_offset].childNodes[1+2*hack_offset].rows[0].cells[1].childNodes[0].childNodes[0].style.marginLeft = "-25px";
  }
  
  if( navigator.userAgent.indexOf("Chrome") != -1
   || navigator.userAgent.indexOf("Opera") != -1
   || navigator.userAgent.indexOf("Safari") != -1 )
  {
    flyoutOpSteps = 40;
    scrollbarDelay = 30;
  }
  if( navigator.userAgent.indexOf("Firefox") != -1 )
  { flyoutOpSteps = 80; }
  if( navigator.userAgent.indexOf("MSIE") != -1 )
  { noOpacity = true; }
  else
  {
    OQP_Flyout_Lang.style.filter = "alpha(opacity=0)";
    OQP_Flyout_Lang.style.opacity = "0.0";
    OQP_Flyout_Sura.style.filter = "alpha(opacity=0)";
    OQP_Flyout_Sura.style.opacity = "0.0";
    OQP_Flyout_Trans.style.filter = "alpha(opacity=0)";
    OQP_Flyout_Trans.style.opacity = "0.0";
  }
  
  TB_OQP_TransFilter = OQP_Flyout_Trans_Content.childNodes[hack_offset].rows[0].cells[0].childNodes[hack_offset];
  t_OQP_Flyout_Trans_Overview = OQP_Flyout_Trans_Content.childNodes[1 + 2*hack_offset].childNodes[hack_offset].childNodes[hack_offset];
  
  var scroller_div = OQP_Flyout_Sura_Content;
  var scrollbar_div = document.getElementById("SuraScrollbarContainer");
  var scroller2_div = OQP_Flyout_Trans_Content;
  var scrollbar2_div = document.getElementById("TransScrollbarContainer");
  var scroller3_div = OQP_Text;
  var scrollbar3_div = document.getElementById("ContentScrollbarContainer");
  
  if(direction == -1)
  {
    OQP_Flyout_Sura_Content.childNodes[0 + 1*hack_offset].childNodes[hack_offset].style.textAlign = "right";
    OQP_Flyout_Sura_Content.childNodes[1 + 2*hack_offset].childNodes[hack_offset].style.textAlign = "right";
    OQP_Flyout_Sura_Content.childNodes[2 + 3*hack_offset].childNodes[hack_offset].style.textAlign = "right";
    scrollbar_div.childNodes[2 + 3*hack_offset].style.left = "0px";
    scrollbar_div.childNodes[2 + 3*hack_offset].style.right = "auto";
    OQP_Flyout_Trans_Content.childNodes[1 + 2*hack_offset].childNodes[hack_offset].style.textAlign = "right";
    scrollbar2_div.childNodes[2 + 3*hack_offset].style.left = "0px";
    scrollbar2_div.childNodes[2 + 3*hack_offset].style.right = "auto";
    scrollbar3_div.childNodes[2 + 3*hack_offset].style.left = "3px";
    scrollbar3_div.childNodes[2 + 3*hack_offset].style.right = "auto";
  }
  
  tmp = document.getElementById("ctl00_t_OQP_Flyout_Sura_Tabs");
  if(direction == -1)
  { tmp.rows[0].cells[4].style.display = "none"; }
  
  var row; var cell; var index; var textAlign;
  var tmp2 = document.getElementById('ctl00_HF_SuraFlyout_Local');
  var suralist = tmp2.value.split('$');
  tmp2.value = "";
  var tmp3;
  for(var i = 37; i >= 0; i--)
  {
    tmp3 = scroller_div.childNodes[0 + 1*hack_offset].childNodes[hack_offset].childNodes[hack_offset];
    row = tmp3.insertRow(0);
    for(var j = 0; j < 3; j++)
    {
      index = (j*38) + i
      cell = row.insertCell(-1);
      cell.innerHTML = suralist[index];
      cell.style.cursor = "pointer";
      cell.setAttribute("onclick","Sura_Change(" + (index+1) + ",this);");
      cell.setAttribute("onmouseover","this.style.backgroundColor = '#555555';");
      cell.setAttribute("onmouseout","this.style.backgroundColor = '#363636';");
    }
  }
  
  tmp2 = document.getElementById('ctl00_HF_SuraFlyout_Translit');
  suralist = tmp2.value.split('$');
  tmp2.value = "";
  for(var i = 37; i >= 0; i--)
  {
    tmp3 = scroller_div.childNodes[1 + 2*hack_offset].childNodes[hack_offset].childNodes[hack_offset];
    row = tmp3.insertRow(0);
    for(var j = 0; j < 3; j++)
    {
      index = (j*38) + i
      cell = row.insertCell(-1);
      cell.innerHTML = suralist[index];
      cell.style.cursor = "pointer";
      cell.setAttribute("onclick","Sura_Change(" + (index+1) + ",this);");
      cell.setAttribute("onmouseover","this.style.backgroundColor = '#555555';");
      cell.setAttribute("onmouseout","this.style.backgroundColor = '#363636';");
    }
  }
  
  tmp2 = document.getElementById('ctl00_HF_SuraFlyout_Arabic');
  suralist = tmp2.value.split('$');
  tmp2.value = "";
  for(var i = 37; i >= 0; i--)
  {
    tmp3 = scroller_div.childNodes[2 + 3*hack_offset].childNodes[hack_offset].childNodes[hack_offset];
    row = tmp3.insertRow(0);
    for(var j = 0; j < 3; j++)
    {
      index = (j*38) + i
      cell = row.insertCell(-1);
      cell.innerHTML = suralist[index];
      cell.style.cursor = "pointer";
      cell.setAttribute("onclick","Sura_Change(" + (index+1) + ",this);");
      cell.setAttribute("onmouseover","this.style.backgroundColor = '#555555';");
      cell.setAttribute("onmouseout","this.style.backgroundColor = '#363636';");
    }
  }
  
  
  OQP_Flyout_Sura_scroller = new jsScroller(scroller_div.childNodes[hack_offset], 529, scroller_div.offsetHeight, false);
  OQP_Flyout_Sura_scrollbar = new jsScrollbar(scrollbar_div, OQP_Flyout_Sura_scroller, true, scrollbarEvent);
  setSuraFlyoutContent(0,tmp.rows[0],false);
  
  OQP_Flyout_Trans_scroller = new jsScroller(scroller2_div.childNodes[1 + 2*hack_offset], 529, scroller2_div.offsetHeight-50-38, false);
  OQP_Flyout_Trans_scrollbar = new jsScrollbar(scrollbar2_div, OQP_Flyout_Trans_scroller, true, scrollbarEvent);
  
  OQP_Text_scroller = new jsScroller(scroller3_div.childNodes[hack_offset], 529, scroller3_div.offsetHeight, true);
  OQP_Text_scrollbar = new jsScrollbar(scrollbar3_div, OQP_Text_scroller, true, scrollbarEvent);
  
  initTransFlyout();
  activateFilter(true);
  setCtrlpMode(1);
  
  suppressTabLoad = true;
  setContentTab(0);
  suppressTabLoad = false;
  getContent();
}
function updateTitle()
{
  var tmp = "";
  if(HF_AyaState.value != "0")
  { tmp = ", Aya: " + HF_AyaState.value; }
  var tmp2 = "";
  var translist = HF_TransState.value.split(',');
  for (var i = 0; i < translist.length-1; i++)
  { tmp2 += translist[i].split('%')[0].split(': ')[1] + ", "; }
  tmp2 = tmp2.substring(0, tmp2.length-2);
  document.title = "OQP - "+ tmp2 + " - Sura: " + HF_SuraState.value + tmp;
}
function resize()
{
  OQP_Flyout_Sura_scroller.resizeY();
  OQP_Flyout_Sura_scrollbar.resizeY();
  OQP_Flyout_Trans_scroller.resizeY();
  OQP_Flyout_Trans_scrollbar.resizeY();
  OQP_Text_scroller.resizeY();
  OQP_Text_scrollbar.resizeY();
}

function scrollbarEvent (o, type)
{
	if (type == "mousedown") {
		if (o.className == "Scrollbar-Track") {}
		else {}
	}
	else
	{
		if (o.className == "Scrollbar-Track") {}
		else {}
	}
}

function enableOQP(bool)
{
  if(bool)
  {
    oqp_flyout_badurl.style.visibility = "hidden";
    oqp_disabler.style.display = "none";
  }
  else
  {
    oqp_disabler.style.display = "block";
    oqp_flyout_badurl.style.visibility = "visible";
    document.getElementById('urlred').scrollIntoView(true);
  }
}
function blockOQP(bool)
{
  if(bool)
  { isOQPblocked = bool; oqp_blocker.style.display = "block"; oqp_blocker2.style.display = "block"; }
  else
  { oqp_blocker.style.display = "none"; oqp_blocker2.style.display = "none"; isOQPblocked = bool; }
}

function setGlobalLang(lang,text)
{
  if( lang != HF_Lang.value )
  {
    HF_Lang.value = lang;
    document.getElementById('ctl00_HF_LangText').value = text;
    
    HF_UpdateTrigger.value = "lang";
    HF_ContentUpdate.value = "update";
    HF_ContentType.value = "trans";
    
    enableUrlPoll = false;
    
    HF_LastState.value = location.href.replace("%23","#").split('#&&')[1];
    
    setTimeout('__doPostBack(\'\',\'\')', 0);
  }
}

function handleMouseClick()
{
  if(!scrollbarfocus)
  {
    switch(mousepos)
    {
      case 0:
        setFocusCtrlp(false);
        handleFlyout(OQP_Flyout_Sura,1,0,95,0);
        handleFlyout(OQP_Flyout_Trans,2,0,95,0);
        break;
      case 1:
        setFocusCtrlp(true);
        handleFlyout(OQP_Flyout_Sura,1,0,95,0);
        handleFlyout(OQP_Flyout_Trans,2,0,95,0);
        break;
      case 2:
        handleFlyout(OQP_Flyout_Trans,2,0,95,0);
        break;
      case 3:
        handleFlyout(OQP_Flyout_Sura,1,0,95,0);
        break;
      case 4:
        setFocusCtrlp(true);
        handleFlyout(OQP_Flyout_Sura,1,0,95,0);
        handleFlyout(OQP_Flyout_Trans,2,0,95,0);
        break;
      case 5:
        
        break;
      default:
        
        break;
    }
  }
  
  scrollbarfocus = false;
  
  handleFlyout(OQP_Flyout_Lang,0,0,95,0);
}
function handleOtherKeys(e)
{
  if( !keyhold && !isOQPblocked )
  {
    keyhold = true;
    
    if (!e) {
      e = window.event;
    }
    var unicode=e.charCode? e.charCode : e.keyCode;
    var actualkey=String.fromCharCode(unicode);
    
    if( actualkey == '%' && focusedCtrl == 0 )
    {
      //Arrow left
      keyhold = false;
      Content_Nav(-1);
    }
    else if ( actualkey == '\'' && focusedCtrl == 0 )
    {
      //Arrow right
      keyhold = false;
      Content_Nav(1);
    }
    else if( actualkey == '&' )
    {
      //Arrow up
      if( mousepos==2 )
      { handleHoldKey("OQP_Flyout_Sura_scrollbar",-10); }
      else if( mousepos==3 )
      { handleHoldKey("OQP_Flyout_Trans_scrollbar",-10); }
      else
      { handleHoldKey("OQP_Text_scrollbar",-10); }
    }
    else if( actualkey == '(' )
    {
      //Arrow down
      if( mousepos==2 )
      { handleHoldKey("OQP_Flyout_Sura_scrollbar",10); }
      else if( mousepos==3 )
      { handleHoldKey("OQP_Flyout_Trans_scrollbar",10); }
      else
      { handleHoldKey("OQP_Text_scrollbar",10); }
    }
    else if( actualkey == '!' )
    {
      //Page up
      if( mousepos==2 )
      { handleHoldKey("OQP_Flyout_Sura_scrollbar",-30); }
      else if( mousepos==3 )
      { handleHoldKey("OQP_Flyout_Trans_scrollbar",-30); }
      else
      { handleHoldKey("OQP_Text_scrollbar",-30); }
    }
    else if( actualkey == '\"' )
    {
      //Page down
      if( mousepos==2 )
      { handleHoldKey("OQP_Flyout_Sura_scrollbar",30); }
      else if( mousepos==3 )
      { handleHoldKey("OQP_Flyout_Trans_scrollbar",30); }
      else
      { handleHoldKey("OQP_Text_scrollbar",30); }
    }
    else if( actualkey == '$' )
    {
      //Home
      if( mousepos==2 )
      { OQP_Flyout_Sura_scrollbar.scrollTo(0,0); }
      else if( mousepos==3 )
      { OQP_Flyout_Trans_scrollbar.scrollTo(0,0); }
      else
      { OQP_Text_scrollbar.scrollTo(0,0); }
    }
    else if( actualkey == '#' )
    {
      //End
      if( mousepos==2 )
      { OQP_Flyout_Sura_scrollbar.scrollTo(0,999999); }
      else if( mousepos==3 )
      { OQP_Flyout_Trans_scrollbar.scrollTo(0,999999); }
      else
      { OQP_Text_scrollbar.scrollTo(0,99999); }
    }
  }
}
function handleHoldKey(name,dist)
{
  setTimeout(name + ".scrollBy(0," + dist + ");",0);
  setTimeout("handleHoldKey2('" + name + "'," + dist + ");",300);
}
function handleHoldKey2(name,dist)
{
  if(keyhold)
  {
    setTimeout(name + ".scrollBy(0," + dist + ");",0);
    setTimeout("handleHoldKey2('" + name + "'," + dist + ");",20);
  }
}
function handleEnterButton()
{
  switch(focusedCtrl)
  {
    case 1:
      focusedCtrl = 0;
      TB_OQP_PageSelect.blur();
      Page_Change(0);
      break;
    case 2:
      focusedCtrl = 0;
      TB_OQP_TransFilter.blur();
      clearTimeout (oqp_transfilterTimer);
      ignoreFilter = true;
      activateFilter(true);
      ignoreFilter = false;
      break;
    case 3:
      focusedCtrl = 0;
      TB_OQP_Search.blur();
      B_Search();
      break;
    default:
      
      break;
  }
}

function setContentTab(tabindex)
{
  if(tabindex == 2 && HF_TabIndex.value != "2")
  { HF_PrevTabIndex.value = HF_TabIndex.value; }
  HF_TabIndex.value = tabindex;
  
  var current;
  for(var i = 0; i < t_OQP_Tabs.rows[0].cells.length-2; i+=2)
  {
    current = t_OQP_Tabs.rows[0].cells[i].childNodes[hack_offset].childNodes[hack_offset];
    if(i == tabindex*2)
    {
      current.style.color = "#000000";
      current.style.marginTop = "0px";
      current.style.height = "30px";
      current.style.borderBottomColor = "Transparent";
      current = current.childNodes[0];
      current.style.marginTop = "-135px";
      current.style.paddingTop = "138px";
    }
    else
    {
      current.style.color = "";
      current.style.marginTop = "4px";
      current.style.height = "25px";
      current.style.borderBottomColor = "#C8C8C8";
      current = current.childNodes[0];
      current.style.marginTop = "-165px";
      current.style.paddingTop = "168px";
    }
  }
  
  if(tabindex == 0)
  {
    t_OQP_Contentnav.rows[0].cells[3].innerHTML = HF_PageState.value;
    t_OQP_Contentnav.style.display = "block";
    
  }
  else if(tabindex == 1)
  {
    t_OQP_Contentnav.rows[0].cells[3].innerHTML = HF_SuraState.value;
    t_OQP_Contentnav.style.display = "block";
    
  }
  else
  {
    t_OQP_Contentnav.style.display = "none";
  }
  
  var selectedTab;
  var doHighlight = false;
  for(var i = hack_offset; i < OQP_Text.childNodes.length; i = i + (1 + hack_offset))
  {
    if(i == hack_offset + tabindex*(1+hack_offset))
    {
      selectedTab = i;
      if( isTabLoaded[tabindex] == 0 )
      {
        if(tabindex != 2)
        {
          if(!suppressTabLoad)
          {
            forceLoad = true;
            suppressLoad = false;
          }
          
          tabWasTrigger = true;
        }
        else
        {
          tabWasTrigger = true;
          
          HF_ContentUpdate.value = "update";
          HF_ContentType.value = "search";
          HF_UpdateTrigger.value = "search";
          
          setTimeout('__doPostBack(\'ctl00$HF_ContentUpdate\',\'\')', 0);
        }
      }
      else
      {
        t_oqp_cTabs = document.getElementById('t_oqp_cTabs' + HF_TabIndex.value);
        oqp_commentaries = document.getElementById('oqp_commentaries' + HF_TabIndex.value);
        if(t_oqp_cTabs)
        { setCommentaryTab(parseInt(HF_CommTabIndex.value)); }
        
        var tmp1 = parseInt(HF_FontSize1.value);
        var tmp2 = parseInt(HF_FontSize2.value);
        var isOrig = (HF_UthmaIsOrig.value == "true");
        
        if(tabindex != 2)
        {
          if(!CBL_Show2.checked)
          { Content_UpdateZoom(0,HF_FontSize1,tmp1); }
          else
          {
            if( CBL_Show1.checked )
            {
              Content_UpdateZoom(0,HF_FontSize1,tmp1);
              if( isOrig )
              { Content_UpdateZoom(1,HF_FontSize2,tmp2); }
              else
              { Content_UpdateZoom(1,HF_FontSize1,tmp1); }
            }
            else
            {
              if( isOrig )
              { Content_UpdateZoom(0,HF_FontSize2,tmp2); }
              else
              { Content_UpdateZoom(0,HF_FontSize1,tmp1); }
            }
          }
        }
        else
        {
          if(searchResIsUthma)
          {
            if( isOrig )
            { Content_UpdateZoom(0,HF_FontSize2,tmp2); }
            else
            { Content_UpdateZoom(0,HF_FontSize1,tmp1); }
          }
          else
          { Content_UpdateZoom(0,HF_FontSize1,tmp1); }
        }
        
        doHighlight = true;
      }
      
      if(tabindex != 2)
      { searchtabIsOpen = false; }
      else
      { searchtabIsOpen = true; }
      
      disableNonSearchControls(searchtabIsOpen);
    }
    
    OQP_Text.childNodes[i].style.visibility = "hidden";
  }
  
  OQP_Text.childNodes[selectedTab].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 == tabindex)
      { tmp[j].y = -OQP_Text_scroller._y; }
    }
    for(var j = 0; j < 3; j++)
    {
      if(j == tabindex)
      {
        j = tabindex;
        OQP_Text_scrollbar.swapContent(tmp[j]);
        break;
      }
    }
  }
  
  if(doHighlight)
  {
    if( tabindex == 0 )
    {
      highlightAya();
      DDL_OQP_Aya.parentNode.style.display = "block";
      DDL_OQP_Aya2.parentNode.style.display = "none";
    }
    else if( tabindex == 1 )
    {
      DDL_OQP_Aya2.selectedIndex = parseInt(HF_AyaState.value);
      highlightAya();
      DDL_OQP_Aya.parentNode.style.display = "none";
      DDL_OQP_Aya2.parentNode.style.display = "block";
    }
  }
}

function tabHighlight(tab,bool)
{
  if(bool)
  {
    if( tab.parentNode.style.height == "25px")
    {
      tab.style.marginTop = '-135px';
      tab.style.paddingTop = '138px';
    }
  }
  else
  {
    if( tab.parentNode.style.height == "25px")
    {
      tab.style.marginTop = '-165px';
      tab.style.paddingTop = '168px';
    }
  }
}

function toggleAccordion(table)
{
  if( table.parentNode.parentNode.parentNode.childNodes[2 + hack_offset].style.display == "block" )
  { table.rows[0].cells[1].childNodes[0].childNodes[0].style.marginLeft = "-36px"; }
  else
  {
    if( direction == 1 )
    { table.rows[0].cells[1].childNodes[0].childNodes[0].style.marginLeft = "-25px"; }
    else
    { table.rows[0].cells[1].childNodes[0].childNodes[0].style.marginLeft = "-47px"; }
  }
}
