//// Version 2.13.100 reco
(function () {
    'use strict';
    var DeqwasAgent = function () {
        this.initialize.apply(this, arguments);
    };

    DeqwasAgent.prototype = {
        codeVersion: 'xxx',
        collectionType: { normal: 'i', cart: 'c', shipping: 's' },
        collectionElementId: 'deqwas-collection',
        screenType: { normal: 'r', cart: 'k', shipping: 'h' },
        screenElementId: 'deqwas-screen',
        visibleFunctionIds: { r: true, k: true, h: true },
        urlLimitation: 2083,

        initialize: function (site, role, deqwasObject, scriptType) {
            this.site = site;
            this.role = role;
            this.objectId = deqwasObject.iid || deqwasObject.id;// TODO remove deqwasObject.id;
            this.scriptType = scriptType;
            if (deqwasObject.option == undefined) {
                deqwasObject.option = new Object();
            }

            this.parameters = {
                ref: document.referrer,
                url: document.location.href,
                l: deqwasObject.location || this._getCanonicalUrl(),
                fc: "",
                cid: this.site,
                iid: deqwasObject.iid || deqwasObject.id,// TODO remove deqwasObject.id,
                uid: deqwasObject.uid || deqwasObject.viewer_id,
                title: deqwasObject.iname || deqwasObject.name,
                img: deqwasObject.img || deqwasObject.image,
                category: deqwasObject.category,
                caption: deqwasObject.caption,
                info: deqwasObject.information,
                role: '',
                logic: '',
                place: ''
            };
            if (this.parameters.url == this.parameters.l) {
                this.parameters.l = null;
            }
            this.priorParameters = ['cid', 'fc', 'iid'];
            this.inferiorParameters = ['caption', 'keywords', 'description'];

            this.logic = '';
            this.exclusion = deqwasObject.option.exclusion;

            this.noscreen = deqwasObject.option.noscreen;
            if (deqwasObject.option.noscreen || deqwasObject.option.solitude) {
                this.expression = deqwasObject.option.noscreen ? 'F' : 'X';
                this.expression += deqwasObject.option.solitude ? 'T' : 'X';
            }

            this.targetUrl = '';
            this.extension = '';
            this.baseXmlUrl = '';

            this.xslName = '';
            this.xslDirectory = '';

            this.candidates = [];

            this.iframeStyle = {
                width: '0px',
                height: '0px',
                display: 'none'
            };
            this.iframeOptions = {
                frameBorder: "0",
                scrolling: "no"
            };
        },

        getSafeProperty: function (property) {
            return Object.prototype.hasOwnProperty.call(window, property)
          ? window[property] : "";
        },

        setCookie: function (key, val, path, expires, subdomain_flag) {
            var cookie_params = [key + '=' + encodeURIComponent(val),
                       'path' + '=' + path,
                       'expires' + '=' + expires
                     ];
            if (subdomain_flag) {
                var hostname = location.hostname;
                var dotIndex = hostname.indexOf(".");
                if (dotIndex > -1) { 
                    cookie_params.push('domain' + '=' + hostname.substring(dotIndex));
                }
            } 
            document.cookie = cookie_params.join(";");
        },

        getCookie: function (key) {
            var cookieKey = key + "=";
            var val = null;
            var cookie = document.cookie + ";";
            var index = cookie.indexOf(cookieKey);
            if (index != -1) {
                var endIndex = cookie.indexOf(";", index);
                val = decodeURIComponent(cookie.substring(index + cookieKey.length, endIndex));
            }
            return val;
        },

        deleteCookie: function (key, path) {
            var commingTime = new Date();
            commingTime.setFullYear(commingTime.getFullYear() - 1);
            var expires = commingTime.toUTCString() + ";";
            this.setCookie(key, '', path, expires);
        },

        _generateUUID: function () {
            var d = new Date().getTime();
            var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                var r = (d + Math.random() * 16) % 16 | 0;
                d = Math.floor(d / 16);
                return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
            });
            return uuid;
        },

        _createRandomSession: function () {
            var value = '';
            var characters = 'abcdefghijklmnopqrstuvwxyz012345678';
            for (var i = 0; i < 8; i++) {
                value += characters.charAt(Math.floor(Math.random() * characters.length));
            }
            return value;
        },

        setViewerCookie: function (name, subdomain_flag) {
            var cookieValue = this.getCookie(name) || this._generateUUID();

            var time = new Date();
            time.setTime(time.getTime() + (1000 * 60 * 60 * 24 * 90));
            var expires = time.toUTCString();

            this.setCookie(name, cookieValue, '/', expires, subdomain_flag);
            this.parameters["mark"] = cookieValue;
        },

        setSessionCookie: function () {
            var sessionCookieKey = 'deqwasSession';
            var sessionParameterKey = 'session';

            var sessionValue = this.getCookie(sessionCookieKey) || this._createRandomSession();

            var time = new Date();
            time.setTime(time.getTime() + (1000 * 60 * 30));
            var expires = time.toUTCString();

            this.setCookie(sessionCookieKey, sessionValue, '/', expires);
            this.parameters[sessionParameterKey] = sessionValue;
        },

        _createUrlWithLimit: function (parameters, targetUrl) {
            var parameterArray = [];
            var dummies = new Object(); // for filtering entries coming from Object.prototype

            var parameterKeys = this._concatKeysWithPriority(parameters, this.priorParameters, this.inferiorParameters);

            var url = '' + targetUrl;
            var length = url.length;
            for (var i = 0; i < parameterKeys.length; i++) {
                var key = parameterKeys[i];
                if (parameters[key] && typeof parameters[key] != 'function' && typeof dummies[key] == 'undefined') {
                    var value = (typeof parameters[key] == 'string') ? parameters[key].replace(/[\f\n\r\t\v]/g, '') : parameters[key];
                    var parameter = key + "=" + encodeURIComponent(value);
                    length += 1 + parameter.length;
                    if (length > this.urlLimitation) {
                        break;
                    }
                    parameterArray.push(parameter);
                }
            }
            url += '?' + parameterArray.join("&");
            return url;
        },

        _arrayContains: function (array, obj) {
            for (var i = 0; i < array.length; i++) {
                if (array[i] === obj) {
                    return true;
                }
            }
            return false;
        },

        _concatKeysWithPriority: function (keys, priorKeys, inferiorKeys) {
            var array = [].concat(priorKeys);
            for (var key in keys) {
                if (!this._arrayContains(array, key) && !this._arrayContains(inferiorKeys, key)) {
                    array.push(key);
                }
            }
            array = array.concat(inferiorKeys);
            return array;
        },

        _createSourceParameter: function (baseXmlUrl, site, role, objectId, extension) {
            return [baseXmlUrl, site, role, objectId + extension].join('/');
        },

        _flatten: function (hash, assignment, terminator) {
            var array = [];
            for (var key in hash) {
                array.push([key, assignment, hash[key], terminator].join(''));
            }
            return array.join('');
        },

        _createIframeAsHtml: function (src, name, style, options) {
            return ['<iframe ',
           'name="', name, '" ',
            'src="', src, '" ',
          'style="', this._flatten(style, ':', ';'), '" ',
          this._flatten(options, '="', '" '),
          '></iframe>'].join('');
        },

        setTargetUrl: function (targetUrl) {
            this.targetUrl = targetUrl;
        },

        setAdditionalParameters: function (additionalParameters) {
            for (var key in additionalParameters) {
                this.parameters[key] = additionalParameters[key];
            }
        },

        setRole: function (role) {
            this.role = role;
        },

        setLogic: function (logic) {
            this.logic = logic;
        },

        setNextCandidateId: function (objectId) {
            this.candidates.push(this._createSourceParameter(
                          this.baseXmlUrl
                          , this.site
                          , this.role
                          , objectId
                          , this.extension));
        },

        setNextCandidateParameter: function (parameter) {
            this.candidates.push(parameter);
        },

        setExtension: function (extension) {
            this.extension = extension;
        },

        setBaseXmlUrl: function (baseXmlUrl) {
            this.baseXmlUrl = baseXmlUrl;
        },

        setXslName: function (xslName) {
            this.xslName = xslName;
        },

        setXslDirectory: function (xslDirectory) {
            this.xslDirectory = xslDirectory;
        },

        setScreenName: function (screenName) {
            this.screenElementId = [this.screenElementId, screenName].join('-');
        },

        setCollectionElementId: function (elementId) {
            this.collectionElementId = elementId;
        },

        setScreenElementId: function (elementId) {
            this.screenElementId = elementId;
        },

        setIframeStyle: function (width, height, styleObject) {
            if (!width || !height) return;
            styleObject = styleObject || {};

            this.iframeStyle.width = width;
            this.iframeStyle.height = height;
            for (var key in styleObject) {
                this.iframeStyle[key] = styleObject[key];
            }
        },

        setIframeOptions: function (iframeOptions) {
            if (!iframeOptions) return;
            for (var key in iframeOptions) {
                this.iframeOptions[key] = iframeOptions[key];
            }
        },

        setWidthAndHeight: function (width, height, styleObject) {
            if (!width || !height) return;
            this.setIframeStyle(width + 'px', height + 'px', styleObject);
            this.setIframeOptions({
                width: width,
                height: height
            });
            this.setAdditionalParameters({ area: width + "x" + height });
        },

        insertContainer: function (elementId) {
            var container = document.createElement('div');
            container.id = elementId;
            container.style.cssText = 'width:0;height:0;margin:0;padding:0;border:none';
            document.body.insertBefore(container, document.body.firstChild);
            return container;
        },

        getFunctionId: function (functionId) {
            if (!functionId) {
                if (document.getElementById(this.screenElementId)) {
                    functionId = this.screenType[this.scriptType];
                } else {
                    functionId = this.collectionType[this.scriptType];
                }
            }

            if (this.noscreen) {
                functionId = this.collectionType[this.scriptType];
            }

            if (this.exclusion) {
                functionId = 'x';
            }

            return functionId;
        },

        createUrl: function (functionId) {
            functionId = this.getFunctionId(functionId);
            if (this.visibleFunctionIds[functionId]) {
                this.parameters.source = this._createSourceParameter(
                                     this.baseXmlUrl
                                     , this.site
                                     , this.role
                                     , this.objectId
                                     , this.extension);
                for (var i = 0; i < this.candidates.length; i++) {
                    this.parameters['source' + (i + 2)] = this.candidates[i];
                }
                this.parameters.type = [this.xslDirectory
                                  , this.site
                                  , this.xslName].join('/');
            }

            this.parameters.fc = functionId;
            this.parameters.role = this.role;
            this.parameters.logic = this.logic;
            this.parameters.expression = this.expression;

            return this._createUrlWithLimit(this.parameters, this.targetUrl);
        },

        appendIframeToElement: function (functionId) {
            var containerDiv;
            var elementId;
            functionId = this.getFunctionId(functionId);
            if (this.visibleFunctionIds[functionId]) {
                elementId = this.screenElementId;
                containerDiv = document.getElementById(elementId);
            }
            else {
                elementId = this.collectionElementId;
                containerDiv = document.getElementById(elementId);
                if (!containerDiv) {
                    containerDiv = this.insertContainer(elementId);
                }
            }
            if (!containerDiv) {
                return false;
            }

            var deqwasName = "deqwas_" + functionId;
            var iframe = document.createElement('iframe');
            iframe.src = this.createUrl(functionId);
            iframe.style.cssText = this._flatten(this.iframeStyle, ':', ';');
            for (var attr in this.iframeOptions) {
                iframe[attr] = this.iframeOptions[attr];
            }
            iframe.name = deqwasName;
            containerDiv.appendChild(iframe);
            return deqwasName;
        },

        _getKeywords: function () {
            var metas = document.getElementsByTagName("meta");
            for (var i = 0; i < metas.length; i++) {
                var meta = metas[i];
                var name = meta.name;

                if (name.match(/keywords/i)) {
                    return this._sanitize(meta.content);
                }
            }
            return null;
        },

        _getDescription: function () {
            var metas = document.getElementsByTagName("meta");
            for (var i = 0; i < metas.length; i++) {
                var meta = metas[i];
                var name = meta.name;

                if (name.match(/description/i)) {
                    return this._sanitize(meta.content);
                }
            }
            return null;
        },

        _getCanonicalUrl: function () {
            var i;
            var links = document.getElementsByTagName("link");
            for (i = 0; i < links.length; i++) {
                if (links[i].rel == "canonical") {
                    return links[i].href;
                }
            }

            var metas = document.getElementsByTagName("meta");
            for (i = 0; i < metas.length; i++) {
                if (metas[i].attributes["property"] && metas[i].attributes["property"].value == "og:url") {
                    return metas[i].content;
                }
            }
            return null;
        },

        _sanitize: function (unsafeString) {
            return unsafeString.replace(/["'<>]/g, '');
        },

        _getUrlParams: function () {
            this.urlParams = {};
            var pairs = location.search.substring(1).split("&");
            for (var i = 0; i < pairs.length; i++) {
                var keyvalues = pairs[i].split("=");
                if (keyvalues.length != 2)
                    continue;
                this.urlParams[keyvalues[0]] = keyvalues[1];
            }
        },

        getUrlParam: function (key) {
            if (!this.urlParams) {
                this._getUrlParams();
            }
            if (!(key in this.urlParams)) {
                return null;
            }
            try {
                return decodeURIComponent(this.urlParams[key]);
            }
            catch (e) {
                return null;
            }
        },

        isMobileDevice: function () {
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.indexOf("android") >= 0 || ua.indexOf("iphone") >= 0 || ua.indexOf("ipad") >= 0 || ua.indexOf("ipod") >= 0) {
                return true;
            }
            return false;
        },

        isIos: function () {
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.indexOf("iphone") >= 0 || ua.indexOf("ipad") >= 0 || ua.indexOf("ipod") >= 0) {
                return true;
            }
            return false;
        },

        isAndroid: function () {
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.indexOf("android") >= 0) {
                return true;
            }
            return false;
        },

        isSafari: function () {
            return window.navigator.vendor == "Apple Computer, Inc." && window.navigator.userAgent.indexOf("CriOS") < 0;
        },

        getAParent: function (element) {
            return "a" === element.localName ? element : element.parentElement ? this.getAParent(element.parentElement) : null;
        },

        sendBeacon: function (url) {
            var img = document.createElement('img');
            img.src = (url || this.createUrl()) + '&noCache=' + (new Date()).getTime();
            img.width = 1;
            img.height = 1;
            img.style.display = "none";
            var containerDiv = document.getElementById(this.collectionElementId);
            if (!containerDiv) {
                containerDiv = this.insertContainer(this.collectionElementId);
            }
            containerDiv.appendChild(img);
        }
    };

    ////////////////////////////////////////////////////////////////////////////////application
    (function (deqwas) {
        var item_id = deqwas.iid;
        var deqwasAgent = new DeqwasAgent("dlsitecom", "", deqwas, "normal");
        deqwasAgent.setViewerCookie('deqwas_vc',true);
        deqwasAgent.setTargetUrl(('https:' == document.location.protocol ? 'https:' : 'http:') + '//dex50.deteql.net/common/collectionx');
        var params = {
            //alt: item_id,
            //keywords: deqwasAgent._getKeywords(),
            //description: deqwasAgent._getDescription()
        };
        var inflow =deqwasAgent.getUrlParam("deqwas_inflow");
        var tg = deqwasAgent.getUrlParam("deqwas_tg");
        var rpos = deqwasAgent.getUrlParam("deqwas_rpos");
        if (inflow && tg) {
            params.inflow = inflow;
            params.tg = tg;
            params.rpos = rpos;
        } else {
            params.inflow2 = inflow;
        }
        for (var key in deqwas) {
            if (typeof deqwas[key] === 'string' || typeof deqwas[key] === 'number') {
                params[key] = deqwas[key];
            }
        }
        deqwasAgent.setAdditionalParameters(params);
        deqwasAgent.appendIframeToElement();
    })(typeof deteql !== 'undefined' ? deteql : {});

})();