var fstTime=true;

function FixFooter(blk) {
  var maxht=GetDivHeight(blk);
  var pixel=maxht+'px';
  document.getElementById(blk).style.height=pixel;  
 var mainheight=GetTotalHeight('main-container');
  var id=document.getElementById('main-container');
  id.style.height=mainheight+'px';
  document.body.className='BPL_BackGround';
  if (typeof LBoxInit == 'function')  LBoxInit();
  if (fstTime) SetSearch();
}

function SetSearch() {
  var frms=document.getElementsByTagName('form');
  var i;
  for (i=0; i<frms.length;i++) {
    frms[i].onsubmit=PAL_DoSearch;
  }
  var frms=document.getElementsByTagName('a');
  var i;
  for (i=0; i<frms.length;i++) {
    if (frms[i].className='rss') {
      frms[i].href='/rss.xml';
      break;
    }
  }
  frms=document.getElementById('HomeLink_Link');
  var buf=getStyle(frms, "background-image");
  if (buf.indexOf('("')!=-1 )
    buf=buf.substr(5,buf.length-7);
  else
    buf=buf.substr(4,buf.length-5);
  frms.innerHTML="<img src='" + buf + "' alt='The offical website of The British Monarchy'/> ";   
  fstTime=false;
}

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function GetDivHeight(blk) {
  var id;
  id=document.getElementById(blk);
  var dvs=id.getElementsByTagName('div');
  var i, maxht=0, ht;
  for (i=0; i<dvs.length; i++){
    ht=dvs[i].offsetHeight;   
    if (ht>maxht) maxht=ht;
  }
  return maxht;
}

function GetTotalHeight(blk) {
  var id, node;
  id=document.getElementById(blk);
  var i, maxht=0;
  for (i=0; i<id.childNodes.length; i++){
    node=id.childNodes.item(i);
    if (node.tagName=='DIV') maxht+=node.offsetHeight;    
  }
  return maxht;
}


function PAL_MenuHover(id, on) {
  if (on) {
  id.style.backgroundColor='#F0F0F0';
 } else
  id.style.backgroundColor='#FFFFFF';
}

function PAL_ShowSelect(guid) {
  var id=document.getElementById('TREE_'+guid);
  if (id!=null) {
   id.style.backgroundColor='#e5e4dc';
   id.onmouseover=null;
   id.onmouseout=null;
  }
}

function PAL_DoSearch () {
  var id=document.getElementById('txtSearch');
  if (CFG_Target=='') {  
   window.open('Search.aspx?PAG_GUID=8bcc41b7-5554-4123-9be8-c31a3f444618&Search=' + escape(id.value),'_self' );
  } else {
    window.open(CFG_RootURL + 'Searching/Search.aspx?Search=' + escape(id.value),'_self' );
  }
  return false;
}

function TMPL_IFrame_SetSize(nam, sz) {
  var id=document.getElementById(nam+"_TmplInc");
  if (id!=null) {
    if (sz>parseInt(id.height,10)) id.height=sz;
    FixFooter('mainBlock');
  }
}
document.SetIFrameSize=TMPL_IFrame_SetSize;
document.setFrameSize=setFrameSize;

function setFrameSize(size){ 
    var iFrames=document.getElementsByTagName("iframe"); 
    var activeFrame;
    for(var frameIndex=0;frameIndex<iFrames.length;frameIndex++){
        activeFrame= iFrames[frameIndex];
        break;
    }
    if (activeFrame!=null){
        if (navigator.appName=="Microsoft Internet Explorer"){
         if(size==1)  
           activeFrame.style.height=679;
         else
            activeFrame.style.height=size+100;
        }else {            
            if(size==0)          
            activeFrame.height=900;
           else if(size==1)          
            activeFrame.height=679;
            else 
            activeFrame.height=size+100;  
      }
    }
   FixFooter('mainBlock');
 }

function DynamicSize() {
    var iFrames=document.getElementsByTagName("iframe"); 
    var activeFrame;
    for(var frameIndex=0;frameIndex<iFrames.length;frameIndex++){
        activeFrame= iFrames[frameIndex];
        break;
    }       
    activeFrame.setFrameSize=setFrameSize;    
   FixFooter('mainBlock');
 }
function FutureEngagement()
{ 
if(document.domain=="www.royal.gov.uk")
    {
        var path=location.href.split('?'); 
        if(path.length>1)
        {
            if(path[1].split('=')[0]=='MRF')
                {
                    var iFrames=document.getElementsByTagName("iframe"); 
                    var activeFrame;
                    for(var frameIndex=0;frameIndex<iFrames.length;frameIndex++)
                    {
                        activeFrame= iFrames[frameIndex];
                        break;
                    }
                    if(activeFrame!=null)
                        {  
	var newPath=activeFrame.src.replace(activeFrame.src.split('?')[1],path[1]);
	//alert(newPath);
	activeFrame.src= newPath;    
	activeFrame.scrolling='yes';                        
                        }
                }
          }
    }
}

   function DynamicImage()
        { 
        if(document.domain=="www.royal.gov.uk")
            {             
            var path=location.href.split('?'); 
                if(path.length>1)
                 {
                    if(path[1].split('=')[0]=='image')
                        {
                            var count=0;
                            var start;
                            var end; 
                            var largeImage=path[1].split('=')[1];
			    if(largeImage.indexOf("(")>0)
                               largeImage=largeImage.substring(0,largeImage.indexOf("("))+".jpg";
				for(var imgs=0;imgs<relImgs.length;imgs++)
				{
                                    if(imgs%12==0)
                                        count+=1;     
                                   if(relImgs[imgs].Large==largeImage)
					{           
                                         start=0;
                                         end=(count-1)*12; 	
 				         var imageNumber=imgs-end; 
					 GalShowPage(start,end);
					 GalleryShowImage(imageNumber);	
					}
				}
                        }
                  }
            }
        }