// ADSK_AD_ENGINE is a singleton for ACN pages to interact with the methods and variables for an ad-engine - in this case DoubleClick
// Allow for this js to be redundantly loaded without impacting the adEngine singleton

if (typeof ADSK_AD_ENGINE !== "object") {

    var ADSK_AD_ENGINE = (function () {

        // private variables
        var _propertyCode = ""; // this specifies the primary ID of the ad-engine account to use
        var _maxSlots = 0;
        var _isDebugMode = false; //  set to false before check-in for Production
        var _isLive = false;       // run in live/disconnected mode
        var _runOnceGSU = false;
        var _seek_serp_contact_slot =  new Array();

        function _setMaxSlots(maxSlots) {
            _maxSlots = maxSlots;
            for (var i = 0; i < maxSlots; i++) {
                _seek_serp_contact_slot[i] = "seek-serp-contact-list-" + i + "-right-custom";
            }
        }

        function _getMaxSlots() {
            return _maxSlots;
        }

        function _convertNonAllowCriteriaChars(c) {
            return c.replace(/[^a-zA-Z0-9_\-]/g, "-");
        }

        return {

            genericSetUp: function(){
                if (!_runOnceGSU) {      // execute this only once if multiple calls
                    if (_isLive && _propertyCode !== "") {
                        GS_googleAddAdSenseService(_propertyCode);
                        GS_googleEnableAllServices();
                    }
                    _runOnceGSU = true;
                }
            },

            prepareFetchAd: function(logicalAdUnit) {
                if (_isLive && _propertyCode !== "") {
                    if (logicalAdUnit instanceof Array) {
                        for (var i in logicalAdUnit) {
                            GA_googleAddSlot(_propertyCode, logicalAdUnit[i]);
                        }
                    } else {
                        GA_googleAddSlot(_propertyCode, logicalAdUnit);
                    }
	                GA_googleFetchAds();
                }
            },

            addCriterion: function(criterion){
                if (_isLive && _propertyCode !== "") {
                    if (criterion !== undefined) {
                        GA_googleAddAttr(criterion.key, _convertNonAllowCriteriaChars(criterion.value));
                    }
                }
            },

            genFillSlot: function(adUnit, criteria){
                if (_isLive && _propertyCode !== "") {
                    if (criteria !== undefined) {
                        if (criteria instanceof Array) {
                            for ( var i=0; i<criteria.length;  i++ ) {
                                GA_googleAddAttr(criteria[i].key, _convertNonAllowCriteriaChars(criteria[i].value));
                            }
                        } else {
                            GA_googleAddAttr(criteria.key, _convertNonAllowCriteriaChars(criteria.value));
                        }
                    }
                    GA_googleFillSlot(adUnit);
                }
            },

            setPropertyCode: function (propertyCode) {
                _propertyCode = propertyCode;
            },

            setMaxSlots: function(maxSlots) {
                _setMaxSlots(maxSlots);
            },

            getMaxSlots: function () {
                return _getMaxSlots();
            },

            activate: function (truefalse) {
              _isLive = truefalse;
            },

            isEnabled: function () {
                return _isLive === true;
            },

            CONSTANTS:{ AD_ENGINE_LATEST_VERSION: "4_4",
                CRITERIA_PAGE: "PAGE",
                CRITERIA_MFR: "MFR",
                CRITERIA_VERSION: "VERSION",    
                SEEK_ALL_ADSK_HEADER_SMALL_BANNER: "seek-all-adsk-header-right-small-banner",
                SEEK_HOME_ADSK_RIGHT_SMALL_BANNER: "seek-home-adsk-right-small-banner",
                SEEK_HOME_BPM_RIGHT_SMALL_BANNER: "seek-home-bpm-right-small-banner",
                SEEK_LOCALE_ADSK_FOOTER_SMALL_BANNER: "seek-locale-adsk-footer-right-small-banner",
                SEEK_PRODUCT_CONTACT_SLOT: "seek-product-contact-atf-center-custom",
                SEEK_SERP_CONTACT_SLOT: _seek_serp_contact_slot
            }
        };
    } )();
}

function Criteria(key, value) {
    this.key = key;
    this.value = value;
}

