Banner = function(id, code) { var _id = id; var _code = code; var _index = -1; return { getId: getId, getCode: getCode, setIndex: setIndex, getIndex: getIndex } function getId() { return _id; } function getCode() { return _code; } function setIndex(index) { _index = index; } function getIndex() { return _index; } } BannerManager = function () { var debug = false; var timeOut = 8000; var currentBanner = 0; var banners = []; var timeoutId = null; var bannersToRemove = []; return { start: start, changeBanner: changeBanner, addBanner: addBanner, removeBanner: removeBanner } function log(message) { if (debug) { console.log(message); } } function addBanner(id, code) { log('Adding Banner: ' + id); banners.push (new Banner(id, code)); } function removeBanner(id) { log("Removing Banner: " + id); for(var i = 0; i < banners.length; i ++) { if (banners[i].getId() == id) { var banner = getBanner(banners[i].getIndex()); var currentlyDisplayed = getCurrentlyDisplayedBanner(); // If we are displaying the banner that is being removed move to the // next banner in the rotation. if (banner.selector === currentlyDisplayed.selector) { log("Currently displayed banner is being removed!"); changeBanner(); } var removedElements = banners.splice(i, 1); bannersToRemove.push(banner); break; } } if (banners.length == 0) { var container = $('#banner_container'); if (timeoutId != null) { window.clearTimeout(timeoutId); } container.fadeOut(1000, function() { container.remove(); }); } } function start() { if (banners.length > 0) { loadBanners(); changeBanner(); } else { var container = $('#banner_container'); container.remove(); } } function loadBanners() { var container = $('#banner_container'); for (var i = 0; i < banners.length; i++) { var code = ''; container.append(code); banners[i].setIndex(i); } } function changeBanner() { if (timeoutId != null) { window.clearTimeout(timeoutId); } var banner = getCurrentlyDisplayedBanner(); changeBannerCounter(); var newBanner = getCurrentlyDisplayedBanner(); if ((banner.selector === newBanner.selector) && (banners.length <= 1)) { if (!banner.is(':visible')) { showBanner(); } return; } banner.fadeOut(1000, showBanner); if (banners.length > 1) { timeoutId = window.setTimeout('BannerManager.changeBanner()', timeOut); } } function changeBannerCounter() { currentBanner ++; if (currentBanner >= banners.length) { currentBanner = 0; } } function showBanner() { var banner = getCurrentlyDisplayedBanner(); banner.fadeIn(1000, checkRemoval); } function checkRemoval() { if (bannersToRemove.length > 0) { for(var i = 0; i < bannersToRemove.length; i ++) { var banner = bannersToRemove[i]; banner.remove(); } bannersToRemove = []; } } function getCurrentlyDisplayedBanner() { var index = banners[currentBanner].getIndex(); return getBanner(index); } function getBanner(index) { var banner = $('#banner' + index); return banner; } }(); // start the fun BannerManager.addBanner('no_resort_fee', 'No Resort Fee'); BannerManager.addBanner('holiday_events', 'Four Queens Banquet Facilities'); BannerManager.start();