var sfgate_sizeclasses = {xs: {minus: null, plus: 'sm'},
                          sm: {minus: 'xs', plus: 'md'},
                          md: {minus: 'sm', plus: 'lg'},
                          lg: {minus: 'md', plus: 'xl'},
                          xl: {minus: 'lg', plus: null}}

var sfgate_bt_fonts = {georgia: 1,
                       verdana: 1,
                       times: 1,
                       arial: 1};

function sfgate_validate_fprefs(textclass) {

    var textclasses = textclass.split(' ');
    var fontclass   = textclasses[0];
    var sizeclass   = textclasses[1];

    if ( typeof fontclass == "undefined" ||
         typeof sizeclass == "undefined" ||
         typeof sfgate_sizeclasses[sizeclass] == "undefined" ||
         typeof sfgate_bt_fonts[fontclass] == "undefined" ) {
        return null;
    }
    return {fc: fontclass,
            sc: sizeclass};
}

function sfgate_chfont_mo(e) {
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    if (tg.id != 'fontpopup' ) return;
    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
    while (reltg != tg && reltg.nodeName != 'BODY')
    reltg= reltg.parentNode
    if (reltg== tg) return;
    // Otherwise, mouseout took place when mouse actually left layer
    // Handle event
    document.getElementById('fontpopup').style.display='none';
}

// returns an object that contains the
// bodytext element and the current fontclass and size class
// of that element. All error checking is done
// here.
function sfgate_getbodytext_obj () {
    var textel   = document.getElementById('bodytext');

    if ( typeof textel == "undefined" ||
         typeof textel.className == "undefined" ) {
        return null;
    }

    var c_obj = sfgate_validate_fprefs(textel.className);
    if ( c_obj == null ) {
        return null;
    }
    return {el: textel,
                    fc: c_obj.fc,
                    sc: c_obj.sc};
}

// change font family
function sfgate_chfont(newfont) {
    var c_obj = sfgate_getbodytext_obj();

    if ( c_obj == null ) {
        return;
    }

    var tc = newfont + ' ' + c_obj.sc;
    c_obj.el.className = tc;
    sfgate_set_fprefs(tc);
}

// change font size

function sfgate_chsize(direction) {
    var c_obj = sfgate_getbodytext_obj();

    if ( c_obj == null ||
         sfgate_sizeclasses[c_obj.sc][direction] == null ) {
        return;
    }

    var tc = c_obj.fc + ' ' +  sfgate_sizeclasses[c_obj.sc][direction];
    c_obj.el.className = tc;
    sfgate_set_fprefs(tc);

}

function sfgate_setsize(sizeclass) {
    var c_obj = sfgate_getbodytext_obj();

    if ( c_obj == null ||
         typeof sfgate_sizeclasses[sizeclass] == "undefined" ) {
        return;
    }

    c_obj.el.className = c_obj.fc + ' ' +  sizeclass;
}

// set cookie function, not yet in use

function sfgate_set_fprefs(fprefs) {
    var nextyear = new Date();
    nextyear.setDate(nextyear.getDate() + 365);
    document.cookie = 'fprefs=' + escape(fprefs) +
                '; expires=' + nextyear.toUTCString() +
                '; path=/' +
                '; domain=.sfgate.com';
}

function sfgate_get_fprefs() {
    var start = document.cookie.indexOf('fprefs')
    if (start == -1) {
        return;
    }
    start +=7 ;
    var cookieVal = document.cookie.substr(start);
    var end = cookieVal.indexOf(';');
    if ( end != -1 ) {
        cookieVal = cookieVal.substr(0,end);
    }
    if ( cookieVal.length == 0 ) {
        return;
    }
    cookieVal = unescape(cookieVal);

    var c_obj = sfgate_validate_fprefs(cookieVal);
    if ( c_obj == null ) {
        return;
    }

    var textel   = document.getElementById('bodytext');

    if ( typeof textel == "undefined" ) {
        return;
    }

    textel.className = cookieVal;

    var radiob = document.getElementById('font_radio_' + c_obj.fc);
    if ( typeof radiob == "undefined" ) {
        return;
    }

    radiob.checked = true;
}
