/* 
   Script to stretch the content columns and the bbcpageservices box down to the same level 
   This has to be done after loading as Safari doesn't seem to have access to the stylesheets otherwise. 
   
   There needs to be a div id="lhn_bottom" under the barley graphic (this may already be there) and there
   nees to be div id="cols_bottom" - this should be positioned at the lowest point of main content in the 
   page. 
   
   The /languages/includes/styles/column_heights.css needs to be included (NOT using @import). Switching 
   on the red border on the id's #cols_bottom and #lhn_bottom in this stylesheet should show up two red dots
   in the page which are the id's mentioned above. The script works by calculating the height difference 
   between these dots and stretching the barley td.bbcpageservices element to this height difference so it 
   matches up with the bottom of the page content.
   
   There is also some script here for the homepages as it also has to match up the height of the columns
   and also the filler block at the bottom of the right nav.
   
 */
   
window.onload=function(){

    if (! document.getElementById){ return; }
    
    // refs to the main content columns in the page
    var col_1 = (document.getElementById('col#1')) ? document.getElementById('col#1') : "";
    var col_2 = (document.getElementById('col#2')) ? document.getElementById('col#2') : "";
    var col_languageSubIndex = (document.getElementById('languageSubIndex')) ? document.getElementById('languageSubIndex') : "";
    // ref to filler space bottom right
    var fillerBlock = (document.getElementById('fillerBlock')) ? document.getElementById('fillerBlock') : "";
    // ref to anchor points in bottom left nav and at bottom of content columns
    var lhn_bottom = (document.getElementById('lhn_bottom')) ? document.getElementById('lhn_bottom') : "";
    var cols_bottom = (document.getElementById('cols_bottom')) ? document.getElementById('cols_bottom') : "";
    
    // to prevent errors dues to missing ID's and older browsers    
    if (! lhn_bottom.style || ! lhn_bottom.offsetTop || ! cols_bottom.style){ return; }
    
    
    //following stuff for homepages
  
    function findLargest(arr_num){
    var count=0;
    var largest = arr_num[0];   
        while (count < arr_num.length){         
            (arr_num[count] > largest) ? largest = arr_num[count] : "";
            count++;       
        }
    return largest;   
    }   
    
    //get tallest column
    var col_largest = findLargest([col_1.clientHeight, col_2.clientHeight, col_languageSubIndex.clientHeight]);
    
    //set left and centre columns to height of tallest
    if (col_1.style){ col_1.style.height = col_largest;}
    if (col_2.style){ col_2.style.height = col_largest;}
    if (col_languageSubIndex.style){
        var filler_height = (col_largest - col_languageSubIndex.clientHeight);
        }else{
        var filler_height = 0;
    }  
    
    //adjust if neccessary filler in right column
    if (filler_height > 0){    
        fillerBlock.style.height = filler_height;
    }
   
    //end stuff for homepages
    
    
    
    // This bit sets the height of the bbcpageservices table cell, done this way because it doesn't have an ID set on it.  
       
    var obj = document.styleSheets;
    
    // go thorugh document stylesheets
    for (var i = 0; i < obj.length; i++){
    
        var obj2 = obj[i];        
         
         // cross-browser ref to css rules  
         if (obj2.cssRules){         	
        	var obj_rules = obj2.cssRules;
         }else if (obj2.rules){        	
        	var obj_rules = obj2.rules;
         }else{
        	var obj_rules = "";
        }     
        	
        // look for the td.bbcpageservices rule and set it using the anchor points defined above
        for (var j=0; j < obj_rules.length; j++){	        	
        	
            if (obj_rules[j].selectorText && obj_rules[j].selectorText.toLowerCase() == "td.bbcpageservices"){                
               obj_rules[j].style.height = (cols_bottom.offsetTop - lhn_bottom.offsetTop);
            }
        }       
     }
     
     
     
}