﻿///<reference name="MicrosoftAjax.js"/>
/**************/
var menuWidth = 250; //Width of the Left menu column
var widthOffset = 4; //sum of horizontal padding across widths.
var isMenuVisible = true;
var NewIfrHeight = 300;
var NewIfrWidth = 300;
//--------------Function to resize the Iframe after Menu is hide/shown-------------------
function toggleVisibility()
{
  $get(imgToggleID).disabled = true; //ensures the user does not click the button too quickly.
  isMenuVisible = !isMenuVisible;
  if(isMenuVisible)
     toggleVisibilityAfterDelay(); //making menu visible is instant
  else
     setTimeout("toggleVisibilityAfterDelay();",500);//hiding is not instant, it is time delay so that it is slowly hidden.
}
function toggleVisibilityAfterDelay()
{  
  var x = $get("tdIframe");
  var y = x.clientWidth;
  if(isMenuVisible)
    x.style.width = y - menuWidth + "px";
  else
      x.style.width = y + menuWidth + "px"
  $get(imgToggleID).disabled = false;   //enable the toggle button.   
}
//----------------------END Function resize-------------------------------------------------
//----------------Start - End waiting labels-------------------------
var WaitDelay = 100; //How much delay in ms need to set before waiting label is displayed. 
var ShowLabel;
function ShowWaitingLabel()
{
   ShowLabel = true;
   setInterval("ShowWaitingLabelActual()", WaitDelay); 
}
//Note this function may or may not be called. See comments under HideWaitingLabel()
function ShowWaitingLabelActual()
{  
   if(ShowLabel){
   $get("divWait").style.display = "";
   $get("divRightInner").style.display = "none";}
}

function HideWaitingLabel()
{
   //Since there is a time delay, iframe can be loaded before the WaitDelay time is over. So in that case,
   //ShowLabel function will be called after HideLabel function!! To address this, set the showlabel variable - false
   ShowLabel = false;  
   $get("divRightInner").style.display = "";
   $get("divWait").style.display = "none";
}
//-------------------END Show Hide functions-------------------------
function UpdateLayout()
{   
  ///Set the Height
  var bodyHt = BodyHeight(); 
  var headerHt = $get("tdHeader").clientHeight;
  var footerHt = $get("tdFooter").clientHeight;
  //Adjust with header and footer height YOu may have to update the offset based upon your design padding/margin
  //in your specific layout design.
  var minHeight = bodyHt - headerHt - footerHt - 30;
  if(Sys.Browser.agent == Sys.Browser.Safari ||Sys.Browser.agent == Sys.Browser.Opera)
      minHeight  -= 60;

  if(NewIfrHeight < minHeight)
     NewIfrHeight = minHeight;
  $get("divRight").style.height = NewIfrHeight + 20 + 'px';  
  $get("tdIframe").style.height = NewIfrHeight + "px";    
  $get("ifr1").height = "100%";     
  
  //***********************Now Set the width.********
  var tblMain = $get("main"); //for width
  var bodyWd = BodyWidth();  
  var SetDefaultWidth = false;    
  if(-1 == NewIfrWidth)
  {
    SetDefaultWidth = true;
  }
  else
  {   var newWidth = menuWidth + NewIfrWidth + widthOffset+50;
      if(newWidth < bodyWd)
         SetDefaultWidth = true;
      else
      {  tblMain.width = newWidth+ "px";
         $get("tdIframe").style.width = NewIfrWidth;    
         if(Sys.Browser.agent == Sys.Browser.Firefox || Sys.Browser.agent == Sys.Browser.Safari)
           $get("ifr1").width = NewIfrWidth;
         else
           $get("ifr1").width = "100%";            
      } 
  } 
  if(SetDefaultWidth) //set if the current iframe width is too small for the src or iframe src is not set.
  {
    //You may have to update the offset based upon your design padding/margin
    //in your specific layout design. 
  
     //safari needs some extra offsets,
     if(Sys.Browser.agent == Sys.Browser.Safari ||Sys.Browser.agent == Sys.Browser.Opera)
     {
        tblMain.width = bodyWd - widthOffset - 10 + "px"; 
         $get("tdIframe").style.width = (bodyWd - menuWidth - 50) + "px";
         $get("ifr1").width = "100%";
     }
     else
     {
         tblMain.width = bodyWd - widthOffset + "px"; 
         $get("tdIframe").style.width = (bodyWd - menuWidth - 40) + "px";
         $get("ifr1").width = "100%";     
     }
  }  
}
/*******Funtion to find the width and the height of client browser window.*****/
function BodyHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myHeight;  
}
function BodyWidth(){
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth; 
}
/****************Iframe related javascript*************************************/
function LoadIframeClient(iframeID, url, lnkID)
{
   //Disable the current selected page's css class
   $get(CurrPageLnkID).className = "UnSelectedPage"; //unselect the current page link in the menu
   CurrPageLnkID = lnkID;
   
   var lnk = $get(lnkID);
   lnk.className = "SelectedPage"; //reselect the new menu link
   ShowWaitingLabel();
  
   var WebSiteUrl = "/";
   var Ifr = $get(iframeID);    
    Ifr.width = "";
    Ifr.height = "";
    Ifr.src= WebSiteUrl+ url;
}

function ResizeIframe(ifr, SafariKickStart)
{ 
   //For Safari, you need a kickstart as sometimes these browsers fire onload event too early.
   //Note even though the time is 100 ms, it actually does not matter as clock is started once
   //the content is fully loaded.
   if(SafariKickStart)
   {   if(Sys.Browser.agent == Sys.Browser.Safari || Sys.Browser.agent == Sys.Browser.Opera)
                setTimeout("ResizeIframe(ifr, false);", 100);
   }   
   HideWaitingLabel(); //Hide the wait label.
   if(ifr.src == "")
   {  NewIfrWidth = -1;
   }
   else
   {   var Obody = ifr.contentWindow.document.body;  
       NewIfrHeight = Obody.scrollHeight + 5;
	   NewIfrWidth = Obody.scrollWidth + 5;	   
	   //The Firefox has a bug for the clientwidth, so get it using a quark method.
	   if(Sys.Browser.agent == Sys.Browser.Firefox)
	       NewIfrWidth = GetFireFoxWidth(ifr);
   }
   UpdateLayout()
 }
  //Get the firefox width. For other browsers, scrollWidth is used. But for the firefox; scrollWidth
  //gives a wrong output. So an alternate approach is used to find the width of iframe.
  //Basic idea is, use a very small temp width of the iframe, then turn on the scorll bar, then move the scroll bar by
  //a very big amount, idea is that scroll bar goes to the end. then the width of the iframe is small width + the
  //width travelled by the horizontal scroll bar.
  function GetFireFoxWidth(ifr)
  {  
       var OrigWidth = ifr.width; //store the current width. this needs to be restored at the end of function.                  
       var Obody =  ifr.contentWindow; 
       var tempWidth = 5;    // a small number   
       ifr.width = tempWidth;
       ifr.scrolling = "yes";                
       Obody.scrollBy(5000, 0); // a very big number.        
       var RequiredWidth = tempWidth +  Obody.pageXOffset ;
       ifr.scrolling = "no";             
       ifr.width = OrigWidth; //Note this is important to restore as UpdateLayout function has certain dimentions derived from it.       
       return RequiredWidth;
  }