/**
 *
 * these scripts are used for accessibility: modifying the text size on the web page
 * author: rsharp, ashrivastava
 * client: esfrs
 * date created: 24/10/09
 * version: 1.0.0
 * since: 1.0.0
 * 1.0.0 - 24/10/09 - version 1 created
 * 1.0.1 - 11/11/09 - From elements are now resizable.
 **/


/**
 * Following is a script level call to setActiveFontSize() function which sets the chosen font size before
 * other components are rendered, this avoids any flickering at page load.
 */
setActiveFontSize();


/**
 * sets the font size
 * @param size set by user
 */
function setFontSize(size){
    //if(size==undefined) size="0.8";
    var inputSize = setInputSize(size);
    var i=0;
    var ifraTags=null;
    document.body.style.fontSize=size+'em';
    for(i=0;$$('input').length>i;i++){
        $$('input')[i].style.fontSize=inputSize+'pt';
    }
    for(i=0;$$('textarea').length>i;i++){
        $$('textarea')[i].style.fontSize=inputSize+'pt';
    }
    setCookie('page_size', size, 30);
    if(!window.frameElement){ //making sure that only container/shtml will execute the following code NOT the embedded iframe/object.
        if($("centrePanelRowWrapper1")){  //checks if the page has rowWrapper-row(s) containing uneven blocks that need resizing.
            matchBlocksHeight();
        }
        //ifraObjs=window.frameElement;  // Array of actual embadded frame-object in current window.
        ifraTags = document.getElementsByTagName("iframe");  // Array of iframe-tags (as text-node) written on container page. Can not use $$("iframe") since Prototype 1.6 does NOT support iframes.
        if(ifraTags.length>0){
            for(i=0; i < ifraTags.length; i++) {
                if(ifraTags[i].contentWindow.document.getElementsByTagName('h1')[0] == undefined){//if "iframe.document.element" is available (instead of iframe.contentWindow) i.e. a plain server-generated output (e.g. 404 error) is sent rather than the actual generated content.
                    ifraTags[i].contentWindow.setFontSize(size);
                    resizeSystemFrame(ifraTags[i]);
                }
            }
        }
    }
}


/**
 * Resizes the frame to fit content
 * @param ifra - Tag that holds the frame/iframe whose height needs to be resized.
 */
function resizeSystemFrame(ifra){
    try{
        contentDoc = (ifra.contentDocument) ? ifra.contentDocument : ifra.contentWindow.document;
        ifra.style.height = contentDoc.body.offsetHeight + "px";
    }
    catch(err){
        window.status = err.message;
    }
}


/**
 * sets the input size
 * @param size set by user
 */
function setInputSize(size) {
    var inputSize;
    if(size=='0.8'){
        inputSize=(size*10)+1;
    }else if(size=='1.4'){
        inputSize=(size*10)-2;
    }else{
        inputSize=size*10;
    }
    return inputSize;
}


/**
 * checks the cookie and adjusts text if necasary then chains it to window.onload:
 */
function setActiveFontSize(){
    var inputSize = null;
    var size = getCookie("page_size");
    if(!size) size="0.8";
    inputSize = setInputSize(size);
    document.write('<style>');
    document.write('body{font-size:'+ size + 'em;}');
    document.write('input{font-size:'+ inputSize + 'pt;}');
    document.write('textarea{font-size:'+ inputSize + 'pt;}');
    document.write('</style>');
}

