addEvent('domready', function () {
    var body = document.getElement('body'),
        siteName = body.get('id').toLowerCase(),
        path = location.pathname.toLowerCase(),
        rectDivs = document.getElements('div.ad-300x250'),
        numRects = rectDivs.length,
        adCallSrc = 'http://crtl.aimatch.com/mwc/bserver/ball/site=' + siteName,
        adCallScript,
        adCallback;
    if (!numRects) {
        return;
    }
    switch (path) {
    // these paths should be the same accross all sites (but they're not)
    case '/readonly/communityinfo/events/events20.asp':
        adCallSrc += '/area=events';
        break;
    case '/readonly/communityinfo/communityinfo20.asp':
        adCallSrc += '/area=events';
        break;
    case '//readonly/communityinfo/events/events20.asp':
        adCallSrc += '/area=events';
        break;
    case '/readonly/communityinfo/births/birthdir20.asp':
        adCallSrc += '/area=births';
        break;
    case '/readonly/communityinfo/weddingsengagements/wedengdir20.asp':
        adCallSrc += '/area=weddings';
        break;
    case '/readonly/communityinfo/obituaries/obituarydir20.asp':
        adCallSrc += '/area=obits';
        break;
    case '/readonly/communityinfo/obituaries/obituarydetails.asp':
        adCallSrc += '/area=obits';
        break;
    // these paths are site-specific and may not have all cases covered
    case '/stationfiles/concerts/concerts.asp':
        adCallSrc += '/area=concerts';
        break;
    case '/stationfiles/events/events2.asp':
        adCallSrc += '/area=concerts';
        break;
    case '/stationfiles/concerts/concerts20.asp':
        adCallSrc += '/area=concerts';
        break;
    case '/stationfiles/concerts/index20.asp':
        adCallSrc += '/area=concerts';
        break;
    case '/music/concerts.asp':
        adCallSrc += '/area=concerts';
        break;
    case '/concerts/':
        adCallSrc += '/area=concerts';
        break;
    case '/stationfiles/liveonloc/liveonloc.asp':
        adCallSrc += '/area=station_events';
        break;
    case '/stationfiles/events/index.asp':
        adCallSrc += '/area=station_events';
        break;
    case '/stationfiles/events/events.asp':
        adCallSrc += '/area=station_events';
        break;
    case '/stationfiles/events/':
        adCallSrc += '/area=station_events';
        break;
    case '/stationfiles/wiredin.asp':
        adCallSrc += '/area=station_events';
        break;
    case '/stationfiles/vanstops.asp':
        adCallSrc += '/area=station_events';
        break;
    case '/stationfiles/station_events/index20.asp':
        adCallSrc += '/area=station_events';
        break;
    case '/station_events.asp':
        adCallSrc += '/area=station_events';
        break;
    case '/events/station_events.asp':
        adCallSrc += '/area=station_events';
        break;
    case '/stationfiles/live-broadcast-schedule/':
        adCallSrc += '/area=station_events';
        break;
    }
    adCallSrc += '/viewid=' + String.from(Math.floor(Math.random() * 100000000));
    numRects.times(function (adIdx) {
        var adNum = String.from(adIdx + 1);
        adCallSrc += '/b' + adNum + '/size=medium_rectangle/pos=' + adNum;
    });
    adCallScript = new Element('script', {src: adCallSrc});
    adCallback = function () {
        rectDivs.each(function (adDiv, adIdx) {
            var adVar = 'b' + String.from(adIdx + 1),
                adScripts;
            adDiv.set('html', window[adVar]);
            adScripts = adDiv.getElements('script');
            adScripts.each(function (adScript) {
                var newScript = new Element('script', {'type': 'text/javascript'});
                newScript.text = adScript.text;
                newScript.replaces(adScript);
            });
        });
    };
    if (adCallScript.addEventListener) {
        adCallScript.addEvent('load', adCallback);
    } else {
        adCallScript.onreadystatechange = function () {
            if (this.readyState === 'loaded') {
                adCallback();
            }
        };
    }
    body.grab(adCallScript);
});

