// set onload to initialize active content
window.onload=initImages;

// set onunload to do nothing so onload runs on back button navigation
window.onunload = function() {};


function initImages() {
    // force preload of all the alternate images for big buttons
    var preloadImages = new Array('images/b_benefits2.jpg', 'images/b_search2.jpg',
                        'images/b_join2.jpg',       'images/b_renew2.jpg',
                        'images/b_class2.jpg',      'images/b_classpost2.jpg',
                        'images/b_events2.jpg',     'images/b_bfg2.jpg',
                        'images/b_affil2.jpg',      'images/b_faqs2.jpg',
                        'images/b_how2.jpg',
                        'images/b_accom2.jpg',      'images/b_dining2.jpg',
                        'images/b_group2.jpg',      'images/b_city_pf2.jpg',
                        'images/b_co_p2.jpg',       'images/b_shop2.jpg',
                        'images/b_rec2.jpg',        'images/b_points2.jpg',
                        'images/b_reg2.jpg',        'images/b_city_map2.jpg',
                        'images/b_city_p2.jpg',     'images/b_bike2.jpg',
                        'images/b_mh_map2.jpg',     'images/b_trans2.jpg',
                        'images/b_loc_info2.jpg',   'images/b_city_mhb1.jpg',
                        'images/b_city_mh2.jpg',    'images/b_comm_res2.jpg',
                        'images/b_edu2.jpg',        'images/b_history2.jpg',
                        'images/b_loc_leg2.jpg',    'images/b_loc_edu2.jpg',
                        'images/b_loc_lib2.jpg',    'images/b_loc_sen2.jpg',
                        'images/b_city_map2.jpg',   'images/b_city_p2.jpg',
                        '');

    var allTags = document.getElementsByTagName("a");
    var linkf = null;
    for (var ii = 0; ii < allTags.length; ii++) {
        linkf = allTags[ii];

        if (linkf.className == "bigbutton") {
            linkf.onmouseover = toggleimage;
            linkf.onmouseout = toggleimage;
        }
    }

    // load advert files
    initAdvert();

    // startup cycling of add in right column
    window.setInterval("cycleAdvert()",5000);

    // don't call init of validation if validation.js was not loaded
    if (typeof initValid != 'undefined') {
        initValid();
    }
}

var currentAdvert = 0;
var advertCount = 0;
var advertList = null;
var advertLink = null;

function cycleAdvert() {
    var bannerf = document.getElementById('adBanner');
    var linkf = document.getElementById('adLink');

    if (bannerf && linkf) {
        currentAdvert++;
        if (currentAdvert >= advertCount) {
            currentAdvert = 0;
        }

        bannerf.src=advertList[currentAdvert];
        bannerf.alt=advertLink[currentAdvert];
        linkf.href=advertLink[currentAdvert];
    }
}

function initAdvert() {
    var linkstr = null;
    var nlinkstr = null;
    var listf = document.getElementById("adList");
    var replacelist = new Array(    ["http-",   "http://"],
                                    ["www-",    "www."],
                                    ["mailto-", "mailto:"],
                                    ["-Q-",     "?"],
                                    ["-S-",     "/"],
                                    ["-com",    ".com"],
                                    ["-org",    ".org"],
                                    ["-html",   ".html"],
                                    ["-net",    ".net"],
                                    ["-php",    ".php"],
                                    ["-htm",    ".htm"]);

    if (listf) {
        var lista = listf.value.split(":");
        if (lista.length > 0) {
            advertCount = lista.length;
            advertList = new Array(advertCount);
            advertLink = new Array(advertCount);

            for (ii = 0; ii < advertCount; ii++) {
                advertList[ii] = lista[ii];
                fstr = lista[ii].split("/");
                 // last entry is file name and we don't want extension
                nlinkstr = ((fstr[fstr.length-1]).split("."))[0];
                for (jj = 0; jj < replacelist.length; jj++) {
                    do {
                        linkstr = nlinkstr;
                        nlinkstr = linkstr.replace(replacelist[jj][0], replacelist[jj][1]);
                    } while (nlinkstr != linkstr);
                }
                advertLink[ii] = linkstr;
            }
        }
    }
    // choose the second advert to display randomly
    currentAdvert = Math.floor(Math.random() * advertCount);
}


/*
 * toggleimage - action function called on a reference link <a>
 *      looks for an <img> child and if found looks at the file name
 *      if it ends in 1 then it is changed to 2. if 2 then changed to 1.
 */
function toggleimage(evt) {
    var targ = null;
    if (evt) {
        targ = evt.target;
    } else {
        targ = window.event.srcElement;
    }

    if (targ && (targ.tagName.toLowerCase() == "img")) {
        var extidx = targ.src.indexOf(".jpg");
        if (extidx < 2) {
            extidx = targ.src.indexOf(".png");
        } else if (extidx < 2) {
            extidx = targ.src.indexOf(".gif");
        }

        if (extidx > 2) {
            var newsrc = targ.src.slice(0,extidx-1);
            var oldnum = targ.src.slice(extidx-1, extidx);
            if (oldnum == "1") {
                newsrc += "2";
            } else {
                newsrc += "1";
            }
            newsrc += targ.src.slice(extidx);
            targ.src = newsrc;
        }
    }
}

