/*!
 * @preserve qwery - A selector engine
 * https://github.com/ded/zQ
 * (c) Dustin Diaz 2014 | License MIT
 */

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, '');
};

String.prototype.replaceAllValue = function (token, newToken, ignoreCase) {
    var _token;
    var str = this + "";
    var i = -1;

    if (typeof token === "string") {

        if (ignoreCase) {

            _token = token.toLowerCase();

            while ((
                i = str.toLowerCase().indexOf(
                    token, i >= 0 ? i + newToken.length : 0
                ) ) !== -1
                ) {
                str = str.substring(0, i) +
                newToken +
                str.substring(i + token.length);
            }

        } else {
            return this.split(token).join(newToken);
        }

    }
    return str;
};


(function (name, context, definition) {
    if (typeof module != 'undefined' && module.exports) module.exports = definition()
    else if (typeof define == 'function' && define.amd) define(definition)
    else context[name] = definition()
})('zQ', this, function () {
    var doc = document
        , html = doc.documentElement
        , byClass = 'getElementsByClassName'
        , byTag = 'getElementsByTagName'
        , qSA = 'querySelectorAll'
        , useNativeQSA = 'useNativeQSA'
        , tagName = 'tagName'
        , nodeType = 'nodeType'
        , select // main select() method, assign later

        , id = /#([\w\-]+)/
        , clas = /\.[\w\-]+/g
        , idOnly = /^#([\w\-]+)$/
        , classOnly = /^\.([\w\-]+)$/
        , tagOnly = /^([\w\-]+)$/
        , tagAndOrClass = /^([\w]+)?\.([\w\-]+)$/
        , splittable = /(^|,)\s*[>~+]/
        , normalizr = /^\s+|\s*([,\s\+\~>]|$)\s*/g
        , splitters = /[\s\>\+\~]/
        , splittersMore = /(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\]|[\s\w\+\-]*\))/
        , specialChars = /([.*+?\^=!:\$\{\}()|\[\]\/\\])/g
        , simple = /^(\*|[a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/
        , attr = /\[([\w\-]+)(?:([\|\^\$\*\~]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/
        , pseudo = /:([\w\-]+)(\(['"]?([^()]+)['"]?\))?/
        , easy = new RegExp(idOnly.source + '|' + tagOnly.source + '|' + classOnly.source)
        , dividers = new RegExp('(' + splitters.source + ')' + splittersMore.source, 'g')
        , tokenizr = new RegExp(splitters.source + splittersMore.source)
        , chunker = new RegExp(simple.source + '(' + attr.source + ')?' + '(' + pseudo.source + ')?')

    var walker = {
        ' ': function (node) {
            return node && node !== html && node.parentNode
        }, '>': function (node, contestant) {
            return node && node.parentNode == contestant.parentNode && node.parentNode
        }, '~': function (node) {
            return node && node.previousSibling
        }, '+': function (node, contestant, p1, p2) {
            if (!node) return false
            return (p1 = previous(node)) && (p2 = previous(contestant)) && p1 == p2 && p1
        }
    }

    function cache() {
        this.c = {}
    }

    cache.prototype = {
        g: function (k) {
            return this.c[k] || undefined
        }, s: function (k, v, r) {
            v = r ? new RegExp(v) : v
            return (this.c[k] = v)
        }
    }

    var classCache = new cache()
        , cleanCache = new cache()
        , attrCache = new cache()
        , tokenCache = new cache()

    function classRegex(c) {
        return classCache.g(c) || classCache.s(c, '(^|\\s+)' + c + '(\\s+|$)', 1)
    }

    // not quite as fast as inline loops in older browsers so don't use liberally
    function each(a, fn) {
        var i = 0, l = a.length
        for (; i < l; i++) fn(a[i])
    }

    function flatten(ar) {
        for (var r = [], i = 0, l = ar.length; i < l; ++i) arrayLike(ar[i]) ? (r = r.concat(ar[i])) : (r[r.length] = ar[i])
        return r
    }

    function arrayify(ar) {
        var i = 0, l = ar.length, r = []
        for (; i < l; i++) r[i] = ar[i]
        return r
    }

    function previous(n) {
        while (n = n.previousSibling) if (n[nodeType] == 1) break;
        return n
    }

    function q(query) {
        return query.match(chunker)
    }

    // called using `this` as element and arguments from regex group results.
    // given => div.hello[title="world"]:foo('bar')
    // div.hello[title="world"]:foo('bar'), div, .hello, [title="world"], title, =, world, :foo('bar'), foo, ('bar'), bar]
    function interpret(whole, tag, idsAndClasses, wholeAttribute, attribute, qualifier, value, wholePseudo, pseudo, wholePseudoVal, pseudoVal) {
        var i, m, k, o, classes
        if (this[nodeType] !== 1) return false
        if (tag && tag !== '*' && this[tagName] && this[tagName].toLowerCase() !== tag) return false
        if (idsAndClasses && (m = idsAndClasses.match(id)) && m[1] !== this.id) return false
        if (idsAndClasses && (classes = idsAndClasses.match(clas))) {
            for (i = classes.length; i--;) if (!classRegex(classes[i].slice(1)).test(this.className)) return false
        }
        if (pseudo && zQ.pseudos[pseudo] && !zQ.pseudos[pseudo](this, pseudoVal)) return false
        if (wholeAttribute && !value) { // select is just for existance of attrib
            o = this.attributes
            for (k in o) {
                if (Object.prototype.hasOwnProperty.call(o, k) && (o[k].name || k) == attribute) {
                    return this
                }
            }
        }
        if (wholeAttribute && !checkAttr(qualifier, getAttr(this, attribute) || '', value)) {
            // select is for attrib equality
            return false
        }
        return this
    }

    function clean(s) {
        return cleanCache.g(s) || cleanCache.s(s, s.replace(specialChars, '\\$1'))
    }

    function checkAttr(qualify, actual, val) {
        switch (qualify) {
            case '=':
                return actual == val
            case '^=':
                return actual.match(attrCache.g('^=' + val) || attrCache.s('^=' + val, '^' + clean(val), 1))
            case '$=':
                return actual.match(attrCache.g('$=' + val) || attrCache.s('$=' + val, clean(val) + '$', 1))
            case '*=':
                return actual.match(attrCache.g(val) || attrCache.s(val, clean(val), 1))
            case '~=':
                return actual.match(attrCache.g('~=' + val) || attrCache.s('~=' + val, '(?:^|\\s+)' + clean(val) + '(?:\\s+|$)', 1))
            case '|=':
                return actual.match(attrCache.g('|=' + val) || attrCache.s('|=' + val, '^' + clean(val) + '(-|$)', 1))
        }
        return 0
    }

    // given a selector, first check for simple cases then collect all base candidate matches and filter
    function _zQ(selector, _root) {
        var r = [], ret = [], i, l, m, token, tag, els, intr, item, root = _root
            , tokens = tokenCache.g(selector) || tokenCache.s(selector, selector.split(tokenizr))
            , dividedTokens = selector.match(dividers)

        if (!tokens.length) return r

        token = (tokens = tokens.slice(0)).pop() // copy cached tokens, take the last one
        if (tokens.length && (m = tokens[tokens.length - 1].match(idOnly))) root = byId(_root, m[1])
        if (!root) return r

        intr = q(token)
        // collect base candidates to filter
        els = root !== _root && root[nodeType] !== 9 && dividedTokens && /^[+~]$/.test(dividedTokens[dividedTokens.length - 1]) ?
            function (r) {
                while (root = root.nextSibling) {
                    root[nodeType] == 1 && (intr[1] ? intr[1] == root[tagName].toLowerCase() : 1) && (r[r.length] = root)
                }
                return r
            }([]) :
            root[byTag](intr[1] || '*')
        // filter elements according to the right-most part of the selector
        for (i = 0, l = els.length; i < l; i++) {
            if (item = interpret.apply(els[i], intr)) r[r.length] = item
        }
        if (!tokens.length) return r

        // filter further according to the rest of the selector (the left side)
        each(r, function (e) {
            if (ancestorMatch(e, tokens, dividedTokens)) ret[ret.length] = e
        })
        return ret
    }

    // compare element to a selector
    function is(el, selector, root) {
        if (isNode(selector)) return el == selector
        if (arrayLike(selector)) return !!~flatten(selector).indexOf(el) // if selector is an array, is el a member?

        var selectors = selector.split(','), tokens, dividedTokens
        while (selector = selectors.pop()) {
            tokens = tokenCache.g(selector) || tokenCache.s(selector, selector.split(tokenizr))
            dividedTokens = selector.match(dividers)
            tokens = tokens.slice(0) // copy array
            if (interpret.apply(el, q(tokens.pop())) && (!tokens.length || ancestorMatch(el, tokens, dividedTokens, root))) {
                return true
            }
        }
        return false
    }

    // given elements matching the right-most part of a selector, filter out any that don't match the rest
    function ancestorMatch(el, tokens, dividedTokens, root) {
        var cand
        // recursively work backwards through the tokens and up the dom, covering all options
        function crawl(e, i, p) {
            while (p = walker[dividedTokens[i]](p, e)) {
                if (isNode(p) && (interpret.apply(p, q(tokens[i])))) {
                    if (i) {
                        if (cand = crawl(p, i - 1, p)) return cand
                    } else return p
                }
            }
        }

        return (cand = crawl(el, tokens.length - 1, el)) && (!root || isAncestor(cand, root))
    }

    function isNode(el, t) {
        return el && typeof el === 'object' && (t = el[nodeType]) && (t == 1 || t == 9)
    }

    function uniq(ar) {
        var a = [], i, j;
        o:
            for (i = 0; i < ar.length; ++i) {
                for (j = 0; j < a.length; ++j) if (a[j] == ar[i]) continue o
                a[a.length] = ar[i]
            }
        return a
    }

    function arrayLike(o) {
        return (typeof o === 'object' && isFinite(o.length))
    }

    function normalizeRoot(root) {
        if (!root) return doc
        if (typeof root == 'string') return zQ(root)[0]
        if (!root[nodeType] && arrayLike(root)) return root[0]
        return root
    }

    function byId(root, id, el) {
        // if doc, query on it, else query the parent doc or if a detached fragment rewrite the query and run on the fragment
        return root[nodeType] === 9 ? root.getElementById(id) :
        root.ownerDocument &&
        (((el = root.ownerDocument.getElementById(id)) && isAncestor(el, root) && el) ||
        (!isAncestor(root, root.ownerDocument) && select('[id="' + id + '"]', root)[0]))
    }

    function zQ(selector, _root) {
        var m, el, root = normalizeRoot(_root)

        // easy, fast cases that we can dispatch with simple DOM calls
        if (!root || !selector) return []
        if (selector === window || isNode(selector)) {
            return !_root || (selector !== window && isNode(root) && isAncestor(selector, root)) ? [selector] : []
        }
        if (selector && arrayLike(selector)) return flatten(selector)
        if (m = selector.match(easy)) {
            if (m[1]) return (el = byId(root, m[1])) ? [el] : []
            if (m[2]) return arrayify(root[byTag](m[2]))
            if (hasByClass && m[3]) return arrayify(root[byClass](m[3]))
        }

        return select(selector, root)
    }

    // where the root is not document and a relationship selector is first we have to
    // do some awkward adjustments to get it to work, even with qSA
    function collectSelector(root, collector) {
        return function (s) {
            var oid, nid
            if (splittable.test(s)) {
                if (root[nodeType] !== 9) {
                    // make sure the el has an id, rewrite the query, set root to doc and run it
                    if (!(nid = oid = root.getAttribute('id'))) root.setAttribute('id', nid = '__zQmeupscotty')
                    s = '[id="' + nid + '"]' + s // avoid byId and allow us to match context element
                    collector(root.parentNode || root, s, true)
                    oid || root.removeAttribute('id')
                }
                return;
            }
            s.length && collector(root, s, false)
        }
    }

    var isAncestor = 'compareDocumentPosition' in html ?
            function (element, container) {
                return (container.compareDocumentPosition(element) & 16) == 16
            } : 'contains' in html ?
            function (element, container) {
                container = container[nodeType] === 9 || container == window ? html : container
                return container !== element && container.contains(element)
            } :
            function (element, container) {
                while (element = element.parentNode) if (element === container) return 1
                return 0
            }
        , getAttr = function () {
            // detect buggy IE src/href getAttribute() call
            var e = doc.createElement('p')
            return ((e.innerHTML = '<a href="#x">x</a>') && e.firstChild.getAttribute('href') != '#x') ?
                function (e, a) {
                    return a === 'class' ? e.className : (a === 'href' || a === 'src') ?
                        e.getAttribute(a, 2) : e.getAttribute(a)
                } :
                function (e, a) {
                    return e.getAttribute(a)
                }
        }()
        , hasByClass = !!doc[byClass]
    // has native qSA support
        , hasQSA = doc.querySelector && doc[qSA]
    // use native qSA
        , selectQSA = function (selector, root) {
            var result = [], ss, e
            try {
                if (root[nodeType] === 9 || !splittable.test(selector)) {
                    // most work is done right here, defer to qSA
                    return arrayify(root[qSA](selector))
                }
                // special case where we need the services of `collectSelector()`
                each(ss = selector.split(','), collectSelector(root, function (ctx, s) {
                    e = ctx[qSA](s)
                    if (e.length == 1) result[result.length] = e.item(0)
                    else if (e.length) result = result.concat(arrayify(e))
                }))
                return ss.length > 1 && result.length > 1 ? uniq(result) : result
            } catch (ex) {
            }
            return selectNonNative(selector, root)
        }
    // no native selector support
        , selectNonNative = function (selector, root) {
            var result = [], items, m, i, l, r, ss
            selector = selector.replace(normalizr, '$1')
            if (m = selector.match(tagAndOrClass)) {
                r = classRegex(m[2])
                items = root[byTag](m[1] || '*')
                for (i = 0, l = items.length; i < l; i++) {
                    if (r.test(items[i].className)) result[result.length] = items[i]
                }
                return result
            }
            // more complex selector, get `_zQ()` to do the work for us
            each(ss = selector.split(','), collectSelector(root, function (ctx, s, rewrite) {
                r = _zQ(s, ctx)
                for (i = 0, l = r.length; i < l; i++) {
                    if (ctx[nodeType] === 9 || rewrite || isAncestor(r[i], root)) result[result.length] = r[i]
                }
            }))
            return ss.length > 1 && result.length > 1 ? uniq(result) : result
        }
        , configure = function (options) {
            // configNativeQSA: use fully-internal selector or native qSA where present
            if (typeof options[useNativeQSA] !== 'undefined')
                select = !options[useNativeQSA] ? selectNonNative : hasQSA ? selectQSA : selectNonNative
        }

    configure({useNativeQSA: true})

    zQ.configure = configure
    zQ.uniq = uniq
    zQ.is = is
    zQ.pseudos = {}

    return zQ
});

var zie = {
    log: function () {
        if (window.console) {
            console.log.apply(console, arguments);
        }
    },
    createCookie: function (name, value) {
        var expires = "; expires=1"
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    getUrlParam: function (location, variableName) {
        return decodeURIComponent((new RegExp('[?|&]' + variableName + '=' + '([^&;]+?)(&|#|;|$)').exec(location) || [, ""])[1].replace(/\+/g, '%20')) || null
    },
    getCookie: function (cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i].trim();
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    },
    getWidgetBaseUrl: function (script) {
        return script.getAttribute("src").split("?")['0'].split("/sourceId")[0] || {};
    },
    documentKeywords: function (tagName) {

        var keywords;
        var metas = document.getElementsByTagName('meta');
        for (var x = 0, y = metas.length; x < y; x++) {
            if (metas[x].name.toLowerCase() == tagName) {
                keywords = metas[x];
            }
        }
        if (keywords != undefined)
            return keywords.content;

    },
    getSE: function () {
        var referURL = document.referrer;
        var searchEngine = '';
        if (referURL) {
            referURL = referURL.toString().toLowerCase()
            if (referURL.indexOf("google.com") != -1 || referURL.indexOf("google.co") != -1) {
                searchEngine = "Google";
            }
            else if (referURL.indexOf("bing.com") != -1) {
                searchEngine = "Bing";
            }

            else if (referURL.indexOf("ask.com") != -1) {
                searchEngine = "Ask";
            }

            else if (referURL.indexOf("yahoo.com") != -1 || referURL.indexOf("yahoo.co") != -1) {
                searchEngine = "Yahoo";
            }
        }
        return searchEngine;
    },
    getSETerm: function () {
        var referURL = document.referrer;
        var searchTerm = '';
        if (referURL) {
            referURL = referURL.toString().toLowerCase()
            if (referURL.indexOf("bing.com") != -1 || referURL.indexOf("ask.com") != -1) {
                if (referURL.indexOf("?q=") != -1) {
                    searchTerm = zie.getUrlParam(referURL, "q")
                }

            }
            else if (referURL.indexOf("yahoo.com") != -1 || referURL.indexOf("yahoo.co") != -1) {
                if (referURL.indexOf("?p=") != -1) {
                    searchTerm = zie.getUrlParam(referURL, "p")
                }
            }
        }
        return searchTerm;
    },
    getURLParametersValue: function (location) {
        var queryValue = '';
        var SearchString = location.substring(1);
        var VariableArray = SearchString.split('&');
        for (var i = 0; i < VariableArray.length; i++) {
            var KeyValuePair = VariableArray[i].split('=');
            queryValue = queryValue + " " + KeyValuePair[1];
        }
        return queryValue.trim() || "";
    },
    getTagContent: function (tagName) {

        var zQelemList = zQ(tagName),
            data = "";
        for (var idx in zQelemList) {
            if (!zQelemList.hasOwnProperty(idx)) continue;
            data += "," + zQelemList[idx].innerHTML.replace(/<\/?(a|span|div|p...)\b[^<>]*>/g, "").trim();
        }
        return data;
    }
}


function generateSearchQueries() {
    /**
     * Get Content From webpage
     */
    var scriptSrc = document.currentScript || (function () {
            var scripts = document.getElementsByTagName('script');
            return scripts[scripts.length - 1];
        })();

    var widgetAppParams = {},
        $currentScript = zQ(scriptSrc)[0],
        baseUrl = zie.getWidgetBaseUrl($currentScript),
        refererURL = window.location.href,
        title = document.title || "",
        h1Data = zie.getTagContent('h1'),
        h2Data = zie.getTagContent('h2'),
        h3Data = zie.getTagContent('h3');
    widgetAppParams.sourceId = "6201002311105768274";
    widgetAppParams.clientId = "audiovideo";
    widgetAppParams.sourceData = $currentScript.getAttribute('sourceData');
    var customTitle = "title";
    var customCategory = ".headhome600 > h1 , .headhome600 > h1 > a";
    var clientId = "audiovideo";
    var customSkuTitle = ""
    jQuery(".view-content .views-field-title .field-content a").each(function () {
        if (customSkuTitle.length > 0)
            customSkuTitle = customSkuTitle + ","
        customSkuTitle = customSkuTitle + jQuery(this).text()
    })
    widgetAppParams.customTitle = zie.getTagContent(customTitle);
    widgetAppParams.customCategory = zie.getTagContent(customCategory);
    widgetAppParams.cookieExist = false;
    widgetAppParams.cookieValue = widgetAppParams.sourceId;

    if (refererURL) {
        //[TODO check] widgetAppParams.cookieValue += "##" + zie.removeHTTPWWWFROmURL(refererURL);
        widgetAppParams.cookieValue += "##" + window.location.host;
    }

    (zie.getCookie('cws') === widgetAppParams.cookieValue) ? widgetAppParams.cookieExist = false : zie.createCookie('cws', widgetAppParams.cookieValue);


    widgetAppParams.imageSource = $currentScript.getAttribute('imageSource');
    widgetAppParams.keywords = zie.documentKeywords("keywords") || "";
    widgetAppParams.searchTerm = zie.getSETerm() || "";
    widgetAppParams.searchEngine = zie.getSE() || "";


    var urlParams = zie.getURLParametersValue(window.location.href);
    var urlValue = '';
    var pathValue = window.location.pathname;
    if (pathValue != undefined) {
        pathValue = pathValue.replaceAllValue("\\", " ").replaceAllValue("/", " ");
    } else
        pathValue = "";

    var urlAndTitle = pathValue;

    if (urlParams != undefined)
        urlAndTitle = urlAndTitle + " " + urlParams;

    if (urlAndTitle && urlAndTitle.trim() !== "undefined") {
        widgetAppParams.keywords += " " + urlAndTitle;
    }
    if (title && title.trim() !== "undefined") {
        widgetAppParams.keywords += " " + title;
    }
    if (h1Data && h1Data.trim() !== "undefined") {
        widgetAppParams.keywords += " " + h1Data;
    }
    if (h2Data && h3Data.trim() !== "undefined") {
        widgetAppParams.keywords += " " + h2Data;
    }
    if (h3Data && h3Data.trim() !== "undefined") {
        widgetAppParams.keywords += " " + h3Data;
    }
    baseUrl = baseUrl.substring(0, baseUrl.lastIndexOf("widget"));

    baseUrl += "widget/contextual?widgetKey=" + widgetAppParams.sourceId + "&sourceId=" + widgetAppParams.sourceId + "&keywords=" + encodeURIComponent(widgetAppParams.keywords) + "&urlTitle=" + encodeURIComponent(urlAndTitle);
    baseUrl += "&customTitle=" + encodeURIComponent(widgetAppParams.customTitle) + "&customCategory=" + encodeURIComponent(widgetAppParams.customCategory) + "&clientId=" + widgetAppParams.clientId;

    baseUrl += "&searchTerm=" + widgetAppParams.searchTerm + "&searchEngine=" + widgetAppParams.searchEngine + "&referPageURL=" + refererURL + "&cookieExist=" + widgetAppParams.cookieExist+"&customSkuTitle="+customSkuTitle;
    //zie.log( baseUrl, widgetAppParams );
    //baseUrl = "http://localhost:8000/widget/renderContextualWidget"; //Todo Remove
    return baseUrl;

}


function initialize() {
    var targetUrl = generateSearchQueries();
    var scriptSrc = document.currentScript || (function () {
            var scripts = document.getElementsByTagName('script');
            return scripts[scripts.length - 1];
        })();

    var forAllTraffic = true;
    if (forAllTraffic) {
        if (targetUrl) {
            zQ('#listProductWidgetData')[0].innerHTML = "<iframe onload='adLoaded()' width='100%' height='400px' style='border:0;width:100%'  id='stylz-ad-block' src='" + targetUrl + "'></iframe>";
        }
    }
    else {
        if (targetUrl && zie.getSE()) {
            zQ('#listProductWidgetData')[0].innerHTML = "<iframe onload='adLoaded()' width='100%' height='400px' style='border:0;width:100%'  id='stylz-ad-block' src='" + targetUrl + "'></iframe>";
        }
    }
}
function adLoaded() {
}

initialize();


