feiferry / Tokopedia shop search

// ==UserScript==
// @name        Tokopedia shop search
// @namespace   https://www.tokopedia.com/advanceSearchMerchant
// @include     https://www.tokopedia.com/*
// @grant       none
// @author      FerryPra
// @version     0.3
// @description Search shop that sell items in one store to reduce shipping cost
// ==/UserScript==


/* jshint -W097 */
(function()  {
    'use strict';

    function exec(fn) {
        var script = document.createElement('script');
        script.setAttribute("type", "application/javascript");
        script.textContent = '(' + fn + ')();';
        document.body.appendChild(script);
        document.body.removeChild(script);
    }

    function addFunction(fn, exec) {
        var script = document.createElement("script");
        script.setAttribute("type", "application/javascript");
        script.textContent =  fn ;
        (document.body || document.head || document.documentElement).appendChild(script);
    }

    function addGlobalStyle(css) {
        var head, style;
        head = document.getElementsByTagName('head')[0];
        if (!head) { return; }
        style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = css;
        head.appendChild(style);
    }

    function productListToMerchantList(productList, keyword) {
        var dataProductsArray = productList.data;
        //window.dataProductArray = dataProductsArray;
        for (var i in dataProductsArray) {
            var product = dataProductsArray[i];
            if (window.dataMerchantMap === undefined) {
                window.dataMerchantMap = {};
            }
            if (window.dataMerchantMap[keyword] === undefined ) {
                window.dataMerchantMap[keyword] = {};
            }
            if (window.dataMerchantMap[keyword][product.shop.id] === undefined) { 
                window.dataMerchantMap[keyword][product.shop.id] = [product];
            } else {
                window.dataMerchantMap[keyword][product.shop.id].push(product);
            }
        }
    }

    function showLoading() {
        $('#mainSearch').find('input').prop('disabled', true);
        $('#mainSearch').find('select').prop('disabled', true);
        $('#loadingAnim').css('display','block');
    }

    function hideLoading() {
        $('#mainSearch').find('input').prop('disabled', false);
        $('#mainSearch').find('select').prop('disabled', false);
        $('#loadingAnim').css('display','none');
    }

    function ajaxProductsByKeyword(keyword, categories, location, minPrice, maxPrice, grosir, goldMerch, page) {
        $.ajax({
            url: "https://ace.tokopedia.com/search/v1/product",
            data: {
                q: keyword,
                st: "product",
                correction: false,
                page: page,
                fshop: 1,
                // ob: 9,
                sc: categories,
                floc: location,
                pmin: minPrice,
                pmax: maxPrice,
                wholesale: grosir,
                rows: 100,
                start: (page - 1) * 100,
                full_domain: "www.tokopedia.com"
            }
        }).done(function(data, textStatus, jqXHR ) {
            productListToMerchantList(data, keyword);
            window.queueDone = window.queueDone + 1;
            if (window.queueDone === window.queryArray.length * 10) {
                hideLoading();
                doMapMerchant();
                updateMerchantList();
                showMerchantResult();
            }
        });
    }

    function ajaxProdCountSync(keyword, categories, location, minPrice, maxPrice, grosir, goldMerch, page) {
        var prodCount = 0;
        $.ajax({
            url: "https://ace.tokopedia.com/search/v1/product",
            async : false,
            data: {
                q: keyword,
                st: "product",
                correction: false,
                page: page,
                fshop: 1,
                // ob: 9,
                sc: categories,
                floc: location,
                pmin: minPrice,
                pmax: maxPrice,
                wholesale: grosir,
                rows: 100,
                start: (page - 1) * 100,
                full_domain: "www.tokopedia.com"
            }
        }).done(function(data, textStatus, jqXHR ) {
            prodCount = data.header.total_data;
        });
        return prodCount;
    }

    function doSearch() {
        var queryText = $('#search-keyword-1').val();
        var categories = $('#categroies-1').val();
        var location = $('#location-1').val();
        var minPrice = $('#pr-min-1').val();
        var maxPrice = $('#pr-max-1').val();
        var grosirPrice = $('#pr-grosir-1').prop('checked');
        var goldMerch = $('#gold-merch-1').prop('checked');

        var splittedQuery = queryText.split(',');
        window.queryArray = [];
        window.queueDone = 0;

        showLoading();
        for (var i = 0; i < splittedQuery.length ; i++) {
            var querystr = splittedQuery[i].trim();
            if (querystr.length !== 0) {
                window.queryArray.push(querystr);

                // find search result count
                // var countQuery = ajaxProdCountSync(querystr, categories, location, minPrice, maxPrice, grosirPrice, goldMerch, 1);
                // var maxPage = countQuery / 100;

                for (var p = 1; p < 11; p++) {
                    var resRow = ajaxProductsByKeyword(querystr, categories, location, minPrice, maxPrice, grosirPrice, goldMerch, p);

                }
            }
        }
    }

    function doMapMerchant() {
        if (window.dataMerchantMap !== undefined) {
            for (var i in window.dataMerchantMap) {
                var keyword = i;
                for (var j in window.dataMerchantMap[keyword]) {
                    var merchantID = j;
                    if (window.dataResultMap === undefined) {
                        window.dataResultMap = {};
                    }
                    if (window.dataResultMap[merchantID] === undefined ) {
                        window.dataResultMap[merchantID] = {};
                    }

                    for (var productObj1 = 0;  productObj1 < window.dataMerchantMap[keyword][merchantID].length; productObj1++) {
                        var prod = window.dataMerchantMap[keyword][merchantID][productObj1];
                        if (window.dataResultMap[merchantID][keyword] === undefined) { 
                            window.dataResultMap[merchantID][keyword] = [prod];
                        } else {
                            window.dataResultMap[merchantID][keyword].push(prod);
                        }
                        if (window.dataMerchantDataMap === undefined) {
                            window.dataMerchantDataMap = {};
                        }
                        window.dataMerchantDataMap[merchantID] = prod.shop;
                    }
                }
            }
        }
    }

    function createSearchDialogContent() {
        var dialogContent = $('<div></div>');
        dialogContent.attr('id', 'mainSearch');
        dialogContent.css('display', 'block');
        dialogContent.css('width', '95%');
        dialogContent.css('margin-left', '15px');
        var searchInputText = createInputSearch();
        var categoriesDropdown = createCategoriesDropdownClone();
        var locationDropDown = createLocationDropDown();
        var priceRangeInput = createPriceRange();
        var goldMerchantcb = createGoldmerchantCheckbox();

        var inputAndDropDown1 = $('<div />').append(searchInputText).append(categoriesDropdown);
        inputAndDropDown1.css('display','block');

        var inputAndDropDown2 = $('<div />').append(locationDropDown).append(priceRangeInput).append(goldMerchantcb);
        inputAndDropDown2.css('display','block');
        
        var searchBtn = $('<div />').append(createSearchButton());
        searchBtn.css('right', '20px');
        searchBtn.css('padding-right', '20px');

        var dialogContentForm = $('<form></form>').append(inputAndDropDown1).append(inputAndDropDown2).append(searchBtn);
        dialogContent.append(dialogContentForm);
        return dialogContent;
    }

    function createInputSearch() {
        var searchInputText = $('<input name="q" class="input-wrapper query-text" id="search-keyword-1" type="text" placeholder="Cari produk" autocomplete="off" value="">');
        searchInputText.css('width', '75%');
        return searchInputText;
    }

    function createCategoriesDropdownClone() {
        var dropDownCategories = $('#cat-select').clone(false);
        dropDownCategories.css('width', '20%');
        dropDownCategories.css('margin', '0px 0px 10px 0px');
        dropDownCategories.css('padding', '7px 12px 7px 12px');
        dropDownCategories.removeClass();
        dropDownCategories.attr('id', "categroies-1");
        return dropDownCategories;
    }

    function showMerchantResult() {
        $('#mainSearch').css('display', 'none');
        $('#back-to-merch-result').css('display', 'none');
        $('#product-list').css('display', 'none');

        $('#merchant-list').css('display', 'block');
        $('#back-to-search').css('display', 'block');
    }

    function showProductResult() {
        $('#mainSearch').css('display', 'none');
        $('#back-to-search').css('display', 'none');
        $('#merchant-list').css('display', 'none');

        $('#product-list').css('display', 'block');
        $('#back-to-merch-result').css('display', 'block');
    }

    function showMainmenu() {
        $('#mainSearch').css('display', 'block');
        $('#back-to-search').css('display', 'none');
        $('#merchant-list').css('display', 'none');
        $('#product-list').css('display', 'none');
        $('#back-to-merch-result').css('display', 'none');
    }

    function createDialogSearch() {
        var dialogHandler = $('<div/>');
        dialogHandler.attr('id', 'searchDialog');

        //var headerHandler = $('<div />');

        var title = $('<span>Find merchant by products</span>');
        title.css('font-size', '25px');
        title.css('color', 'green');
        title.css('display', 'block');
        title.css('font-weigh', '700');
        title.css('padding', '15px 5px 2px 5px');
        dialogHandler.append(title);

        var desc = $('<p>Search shop that sell items in one store to reduce shipping cost</p>');
        desc.css('font-size', '13px');
        desc.css('color', '#999');
        desc.css('display', 'block');
        desc.css('font-weigh', 'normal');
        desc.css('padding', '2px 15px 5px 5px');
        dialogHandler.append(desc);

        dialogHandler.css('display', 'none');
        dialogHandler.css('width', '960px');
        dialogHandler.css('overflow', 'hidden');
        dialogHandler.css('margin-right','auto');
        dialogHandler.css('margin-left','auto');

        dialogHandler.css('height','90%');
        dialogHandler.css('position','fixed');
        dialogHandler.css('z-index','999999');
        dialogHandler.css('margin', 'auto');
        dialogHandler.css('left', '0px');
        dialogHandler.css('right', '0px');
        dialogHandler.css('top','100');
        dialogHandler.css('border','solid 2px');
        dialogHandler.css('background', '#f7f7f7');

        var closeButton = $('<span><a>x</a></span>');
        closeButton.css('float', 'right');
        closeButton.css('margin-top', '20px');
        closeButton.css('margin-right', '20px');
        closeButton.css('cursor', 'pointer');
        closeButton.css('color', '#fff');
        closeButton.css('background', '#605F61');
        closeButton.css('font-size', '31px');
        closeButton.css('font-weight', 'bold');
        closeButton.css('display', 'inline-block');
        closeButton.css('line-height', '0px');
        dialogHandler.prepend(closeButton);
        closeButton.click(function() {
            toogleDialog();
        });

        var backtoSearch = $('<div><a>&lt; back to main menu</a></div>');
        backtoSearch.css('padding', '0px 15px 15px 5px');
        backtoSearch.css('display' , 'none');
        backtoSearch.attr('id', 'back-to-search');
        backtoSearch.click(function() {
            showMainmenu();
        });
        var backtoMerchResult = $('<div><a>&lt; back to search result</a></div>');
        backtoMerchResult.css('padding', '0px 15px 15px 5px');
        backtoMerchResult.css('display' , 'none');
        backtoMerchResult.attr('id', 'back-to-merch-result');
        backtoMerchResult.click(function() {
            showMerchantResult();
        });

        dialogHandler.append(backtoSearch);
        dialogHandler.append(backtoMerchResult);

        $('#content-container').prepend(dialogHandler);
        var searchDialogMain = createSearchDialogContent();
        var searchResultMerch = createMerchantList();
        var searchResultProduct = createProductList();
        dialogHandler.append(searchDialogMain);
        dialogHandler.append(searchResultMerch);
        dialogHandler.append(searchResultProduct);

        var loadingAnim = $('<div> Memuat... </div>');
        loadingAnim.css('background', 'url(https://www.tokopedia.com/img/newtkpd/ajax-loader.gif) center 25px no-repeat #fff');
        loadingAnim.css('position','fixed');
        loadingAnim.css('z-index','999999');
        loadingAnim.css('border-left','1px solid #ccc');
        loadingAnim.css('border-right','1px solid #ccc');
        loadingAnim.css('border-bottom','1px solid #ccc');
        loadingAnim.css('text-align','center');
        loadingAnim.css('foont-size','10px');
        loadingAnim.css('left','46%');
        loadingAnim.css('top','51px');
        loadingAnim.css('height','50px');
        loadingAnim.css('width','175px');
        loadingAnim.css('display','none');
        loadingAnim.attr('id', 'loadingAnim');
        dialogHandler.append(loadingAnim);

        //dialogHandler.append(headerHandler);

        return dialogHandler;
    }

    function createLocationDropDown() {
        /*jshint multistr: true */
        var locationDropdown = $("<select id=\"location-1\" name=\"location\"><option value=\"\">Pilih Lokasi</option><option value=\"2210,2228,5573,1940,1640,2197\">Jabodetabek</option><option value=\"5569\">Abepura</option><option value=\"339\">Aek Kuo</option><option value=\"5365\">Aimas</option><option value=\"5130\">Airmadidi</option><option value=\"5281\">Ambon</option><option value=\"5036\">Ampana Kota</option><option value=\"4215\">Amuntai</option><option value=\"4216\">Amuntai Selatan</option><option value=\"4217\">Amuntai Tengah</option><option value=\"4218\">Amuntai Utara</option><option value=\"4902\">Andolo/Wanggodo</option><option value=\"631\">Arga Makmur</option><option value=\"3793\">Atambua</option><option value=\"3912\">Baa</option><option value=\"879\">Bagan Siapi-api</option><option value=\"880\">Bagan Sinembah</option><option value=\"3904\">Bajawa</option><option value=\"557\">Balige</option><option value=\"4501\">Balikpapan</option><option value=\"4990\">Banawa</option><option value=\"231\">Banda Aceh</option><option value=\"467\">Bandar</option><option value=\"1403\">Bandar Lampung</option><option value=\"2170\">Bandung</option><option value=\"5661\">Bandungan</option><option value=\"4970\">Banggai</option><option value=\"3283\">Bangil</option><option value=\"3444\">Bangilan</option><option value=\"2880\">Bangkalan</option><option value=\"817\">Bangkinang</option><option value=\"730\">Bangko</option><option value=\"3632\">Bangli</option><option value=\"2193\">Banjar</option><option value=\"4279\">Banjar Baru</option><option value=\"4282\">Banjarmasin</option><option value=\"2303\">Banjarnegara</option><option value=\"4565\">Bantaeng</option><option value=\"3546\">Bantul</option><option value=\"1090\">Banyuasin I</option><option value=\"2381\">Banyudono</option><option value=\"2898\">Banyuwangi</option><option value=\"4205\">Barabai</option><option value=\"1353\">Baradatu</option><option value=\"4411\">Barong Tongkok</option><option value=\"2236\">Baros</option><option value=\"4572\">Barru</option><option value=\"1500\">Batam</option><option value=\"2350\">Batang</option><option value=\"1313\">Batanghari</option><option value=\"505\">Batang Toru</option><option value=\"4239\">Bati Bati</option><option value=\"3482\">Batu</option><option value=\"4962\">Batui</option><option value=\"4260\">Batu Licin</option><option value=\"1189\">Baturaja</option><option value=\"1044\">Batu Sangkar</option><option value=\"4489\">Batu Sopang</option><option value=\"4947\">Bau - Bau</option><option value=\"1145\">Bayung Lencir</option><option value=\"2197\">Bekasi</option><option value=\"1429\">Belinyu</option><option value=\"1199\">Belitang</option><option value=\"4652\">Belopa</option><option value=\"4447\">Bengalon</option><option value=\"783\">Bengkalis</option><option value=\"3997\">Bengkayang</option><option value=\"692\">Bengkulu</option><option value=\"4722\">Benteng</option><option value=\"5103\">Beo</option><option value=\"2712\">Bergas</option><option value=\"1093\">Betung</option><option value=\"5419\">Biak Kota</option><option value=\"3778\">Bima</option><option value=\"365\">Binjai</option><option value=\"572\">Binjai</option><option value=\"4267\">Binuang</option><option value=\"157\">Bireuen</option><option value=\"5154\">Bitung</option><option value=\"175\">Blang Kejeren</option><option value=\"13\">Blangpidie</option><option value=\"2923\">Blitar</option><option value=\"2364\">Blora</option><option value=\"2210\">Bogor</option><option value=\"2530\">Boja</option><option value=\"2946\">Bojonegoro</option><option value=\"1718\">Bojonggede</option><option value=\"2972\">Bondowoso</option><option value=\"982\">Bonjol</option><option value=\"4507\">Bontang</option><option value=\"5647\">Boyolali</option><option value=\"2382\">Boyolali Penggung</option><option value=\"3465\">Boyolangu</option><option value=\"2400\">Brebes</option><option value=\"3089\">Brondong</option><option value=\"5670\">Buahbatu</option><option value=\"2100\">Buahdua</option><option value=\"1058\">Bukit Tinggi</option><option value=\"3637\">Buleleng</option><option value=\"4608\">Bulukumba</option><option value=\"2402\">Bumiayu</option><option value=\"5004\">Bungku</option><option value=\"4288\">Buntok</option><option value=\"4982\">Buol</option><option value=\"3136\">Caruban</option><option value=\"2366\">Cepu</option><option value=\"1759\">Ciamis</option><option value=\"1796\">Cianjur</option><option value=\"1722\">Ciawi</option><option value=\"1723\">Cibinong</option><option value=\"2061\">Cicurug</option><option value=\"1966\">Cigugur</option><option value=\"1573\">Cikande</option><option value=\"1698\">Cikarang</option><option value=\"1595\">Cikupa</option><option value=\"2418\">Cilacap</option><option value=\"1631\">Ciledug</option><option value=\"1826\">Ciledug</option><option value=\"1621\">Cilegon</option><option value=\"1728\">Cileungsi</option><option value=\"1681\">Cililin</option><option value=\"2217\">Cimahi</option><option value=\"1688\">Cimareme</option><option value=\"1575\">Cinangka</option><option value=\"5650\">Cinere</option><option value=\"1804\">Cipanas</option><option value=\"5648\">Cipayung</option><option value=\"1596\">Ciputat</option><option value=\"5582\">Ciputat Timur</option><option value=\"2069\">Ciracap</option><option value=\"2221\">Cirebon</option><option value=\"1599\">Curug</option><option value=\"672\">Curup</option><option value=\"1477\">Daik</option><option value=\"722\">Danau Kerinci</option><option value=\"1660\">Dayeuhkolot</option><option value=\"2441\">Demak</option><option value=\"3681\">Denpasar</option><option value=\"2228\">Depok</option><option value=\"3593\">Depok</option><option value=\"130\">Dewantara</option><option value=\"3203\">Dlanggu</option><option value=\"5231\">Dobo</option><option value=\"317\">Dolok Sanggul</option><option value=\"3699\">Dompu</option><option value=\"905\">Dumai</option><option value=\"785\">Duri-Mandau</option><option value=\"4289\">Dusun Hilir</option><option value=\"4290\">Dusun Selatan</option><option value=\"4297\">Dusun Tengah</option><option value=\"3811\">Ende</option><option value=\"4623\">Enrekang</option><option value=\"5334\">Fakfak</option><option value=\"4169\">Gambut</option><option value=\"1451\">Gantung</option><option value=\"1876\">Garut</option><option value=\"1877\">Garut Kota</option><option value=\"1396\">Gedong Tataan</option><option value=\"3285\">Gempol</option><option value=\"2857\">Genuk</option><option value=\"3647\">Gianyar</option><option value=\"2511\">Gombong</option><option value=\"4560\">Gorontalo</option><option value=\"2998\">Gresik</option><option value=\"1734\">Gunung Putri</option><option value=\"403\">Gunungsitoli</option><option value=\"1293\">Gunung Sugih</option><option value=\"433\">Gunung Tua</option><option value=\"4223\">Hampang</option><option value=\"110\">Idi Rayeuk</option><option value=\"1238\">Ilir Barat I</option><option value=\"1239\">Ilir Barat II</option><option value=\"1240\">Ilir Timur I</option><option value=\"1241\">Ilir Timur II</option><option value=\"1171\">Indralaya</option><option value=\"1912\">Indramayu</option><option value=\"1000\">IV Jurai</option><option value=\"5288\">Jailolo</option><option value=\"5573\">Jakarta</option><option value=\"773\">Jambi</option><option value=\"2076\">Jampang Kulon</option><option value=\"2325\">Jatilawang</option><option value=\"3446\">Jatirogo</option><option value=\"5437\">Jayapura</option><option value=\"1435\">Jebus</option><option value=\"3016\">Jember</option><option value=\"4646\">Jeneponto</option><option value=\"2475\">Jepara</option><option value=\"3046\">Jombang</option><option value=\"1737\">Jonggol</option><option value=\"4241\">Jorong</option><option value=\"3484\">Junrejo</option><option value=\"2614\">Juwana</option><option value=\"329\">Kabanjahe</option><option value=\"4530\">Kabila</option><option value=\"3508\">Kademangan</option><option value=\"3789\">Kalabahi</option><option value=\"1274\">Kalianda</option><option value=\"4199\">Kandangan</option><option value=\"895\">Kandis</option><option value=\"2495\">Karanganyar</option><option value=\"3661\">Karangasem</option><option value=\"1940\">Karawang</option><option value=\"4329\">Kasongan</option><option value=\"647\">Kaur</option><option value=\"2515\">Kebumen</option><option value=\"3069\">Kediri</option><option value=\"3488\">Kediri Kota</option><option value=\"3988\">Kefamenanu</option><option value=\"1431\">Kelapa</option><option value=\"1453\">Kelapa Kampit</option><option value=\"4252\">Kelua</option><option value=\"2536\">Kendal</option><option value=\"4956\">Kendari</option><option value=\"656\">Kepahiang</option><option value=\"3180\">Kepanjen</option><option value=\"3485\">Kepanjenkidul</option><option value=\"637\">Ketahun</option><option value=\"4036\">Ketapang</option><option value=\"266\">Kisaran</option><option value=\"1840\">Klangenan</option><option value=\"2563\">Klaten</option><option value=\"3669\">Klungkung</option><option value=\"1436\">Koba</option><option value=\"4867\">Kolaka</option><option value=\"1367\">Kota Agung</option><option value=\"4228\">Kota Baru</option><option value=\"1344\">Kotabumi</option><option value=\"1180\">Kota Kayu Agung</option><option value=\"624\">Kota Manna</option><option value=\"5237\">Kota Masohi</option><option value=\"5068\">Kotamobagu</option><option value=\"345\">Kota Pinang</option><option value=\"4763\">Kota Takalar</option><option value=\"3337\">Kraksaan</option><option value=\"41\">Krueng Sabee/Calang</option><option value=\"4323\">Kuala Kapuas</option><option value=\"4380\">Kuala Pembuang</option><option value=\"74\">Kuala Simpang</option><option value=\"758\">Kualatungkal</option><option value=\"2582\">Kudus</option><option value=\"1462\">Kundur</option><option value=\"1982\">Kuningan</option><option value=\"3994\">Kupang</option><option value=\"4310\">Kurun</option><option value=\"4261\">Kusan Hilir</option><option value=\"101\">Kutacane</option><option value=\"3885\">Labuan Bajo</option><option value=\"5298\">Labuha</option><option value=\"1115\">Lahat</option><option value=\"3097\">Lamongan</option><option value=\"2194\">Langensari</option><option value=\"241\">Langsa</option><option value=\"3829\">Larantuka</option><option value=\"4890\">Lasolo</option><option value=\"4882\">Lasusua</option><option value=\"3182\">Lawang</option><option value=\"1124\">Lawang Kidul</option><option value=\"659\">Lebong</option><option value=\"1687\">Lembang</option><option value=\"3867\">Lembata</option><option value=\"1740\">Leuwiliang</option><option value=\"249\">Lhokseumawe</option><option value=\"133\">Lhoksukon</option><option value=\"1262\">Liwa</option><option value=\"4433\">Loa Kulu</option><option value=\"971\">Lubuk Alung</option><option value=\"930\">Lubuk Basung</option><option value=\"1219\">Lubuk Linggau</option><option value=\"304\">Lubuk Pakam</option><option value=\"983\">Lubuk Sikaping</option><option value=\"3120\">Lumajang</option><option value=\"4967\">Luwuk</option><option value=\"5305\">Maba</option><option value=\"3492\">Madiun</option><option value=\"2837\">Magelang</option><option value=\"3158\">Magetan</option><option value=\"2008\">Majalengka</option>\
<option value=\"5675\">Majasari</option><option value=\"5177\">Majene</option><option value=\"4771\">Makale</option><option value=\"4824\">Makassar/U. Pandang</option><option value=\"3499\">Malang</option><option value=\"4665\">Malili</option><option value=\"4469\">Malinau</option><option value=\"5606\">Malinau Kota</option><option value=\"5197\">Mamuju</option><option value=\"5162\">Manado</option><option value=\"1452\">Manggar</option><option value=\"625\">Manna</option><option value=\"5345\">Manokwari</option><option value=\"4187\">Marabahan</option><option value=\"4553\">Marisa</option><option value=\"4688\">Maros</option><option value=\"1207\">Martapura</option><option value=\"4172\">Martapura</option><option value=\"4677\">Masamba</option><option value=\"3776\">Mataram</option><option value=\"3923\">Maumere</option><option value=\"3895\">Mbay</option><option value=\"576\">Medan</option><option value=\"581\">Medan Belawan</option><option value=\"4422\">Melak</option><option value=\"4075\">Mempawah</option><option value=\"4058\">Mempawah Hulu</option><option value=\"1385\">Menggala</option><option value=\"3630\">Mengwi</option><option value=\"1433\">Mentok</option><option value=\"5479\">Merauke</option><option value=\"1424\">Merawang</option><option value=\"1417\">Metro</option><option value=\"6\">Meulaboh</option><option value=\"214\">Meureudu</option><option value=\"3211\">Mojokerto</option><option value=\"1160\">Muara Beliti Baru</option><option value=\"706\">Muara Bulian</option><option value=\"712\">Muara Bungo</option><option value=\"1215\">Muaradua</option><option value=\"1127\">Muara Enim</option><option value=\"4436\">Muara Jawa</option><option value=\"4483\">Muara Komam</option><option value=\"1162\">Muara Lakitan</option><option value=\"753\">Muarasabak</option><option value=\"763\">Muara Tebo</option><option value=\"707\">Muara Tembesi</option><option value=\"4306\">Muara Teweh</option><option value=\"665\">Muko</option><option value=\"2594\">Muntilan</option><option value=\"5228\">Namlea</option><option value=\"4069\">Nanga Pinoh</option><option value=\"1278\">Natar</option><option value=\"3656\">Negara</option><option value=\"4062\">Ngabang</option><option value=\"3228\">Nganjuk</option><option value=\"5660\">Ngasem</option><option value=\"3250\">Ngawi</option><option value=\"2390\">Ngemplak</option><option value=\"3601\">Ngemplak</option><option value=\"4479\">Nunukan</option><option value=\"3629\">Nusa Dua</option><option value=\"1845\">Pabuaran</option><option value=\"2046\">Pabuaran</option><option value=\"3264\">Pacitan</option><option value=\"1689\">Padalarang</option><option value=\"1061\">Padang</option><option value=\"1073\">Padang Panjang</option><option value=\"1074\">Padang Panjang Barat</option><option value=\"5642\">Padang Sidempuan</option><option value=\"515\">Padang Sidempuan Barat</option><option value=\"516\">Padang Sidempuan Timur</option><option value=\"1228\">Pagar Alam</option><option value=\"4526\">Paguyaman</option><option value=\"1009\">Painan</option><option value=\"3188\">Pakisaji</option><option value=\"4387\">Palangkaraya</option><option value=\"1234\">Palembang</option><option value=\"1846\">Palimanan</option><option value=\"4835\">Palopo</option><option value=\"5054\">Palu</option><option value=\"3277\">Pamekasan</option><option value=\"5031\">Pamona Selatan</option><option value=\"1610\">Pamulang</option><option value=\"307\">Pancur Batu</option><option value=\"3295\">Pandaan</option><option value=\"1561\">Pandeglang</option><option value=\"4706\">Pangkajene</option><option value=\"1097\">Pangkalan Balai</option><option value=\"4344\">Pangkalan Bun</option><option value=\"860\">Pangkalan Kerinci</option><option value=\"1457\">Pangkal Pinang</option><option value=\"451\">Pangururan</option><option value=\"5578\">Panyabungan</option><option value=\"3078\">Pare</option><option value=\"4355\">Parenggean</option><option value=\"4841\">Pare - Pare</option><option value=\"974\">Pariaman</option><option value=\"5021\">Parigi</option><option value=\"4165\">Paringin</option><option value=\"5209\">Pasangkayu</option><option value=\"870\">Pasir Pangaraian</option><option value=\"812\">Pasir Penyu</option><option value=\"3505\">Pasuruan</option><option value=\"2195\">Pataruman</option><option value=\"2618\">Pati</option><option value=\"4766\">Pattallassang</option><option value=\"952\">Payakumbuh</option><option value=\"5644\">Payakumbuh</option><option value=\"2484\">Pecangaan</option><option value=\"1326\">Pekalongan</option><option value=\"2869\">Pekalongan</option><option value=\"913\">Pekanbaru</option><option value=\"2089\">Pelabuhan Ratu</option><option value=\"4245\">Pelaihari</option><option value=\"2652\">Pemalang</option><option value=\"603\">Pematang Siantar</option><option value=\"4493\">Penajam</option><option value=\"461\">Perbaungan</option><option value=\"1264\">Pesisir Tengah / Krui</option><option value=\"4718\">Pinrang</option><option value=\"4814\">Pitumpanua</option><option value=\"3058\">Ploso</option><option value=\"5221\">Polewali</option><option value=\"1613\">Pondok Aren/ Jurang Mangu</option><option value=\"3320\">Ponorogo</option><option value=\"4147\">Pontianak</option><option value=\"4556\">Popayato</option><option value=\"5034\">Poso Kota</option><option value=\"1251\">Prabumulih</option><option value=\"5349\">Prafi</option><option value=\"3731\">Praya</option><option value=\"2524\">Prembun</option><option value=\"1371\">Pringsewu</option><option value=\"3347\">Probolinggo</option><option value=\"3124\">Pronojiwo</option><option value=\"3280\">Proppo</option><option value=\"4402\">Pulau Bunyu</option><option value=\"801\">Pulau Burung</option><option value=\"939\">Pulau Punjung</option><option value=\"2676\">Purbalingga</option><option value=\"4365\">Puruk Cahu</option><option value=\"2196\">Purwaharja</option><option value=\"2028\">Purwakarta</option><option value=\"2467\">Purwodadi</option><option value=\"2911\">Purwoharjo</option><option value=\"2336\">Purwokerto</option><option value=\"2693\">Purworejo</option><option value=\"644\">Putri Hijau</option><option value=\"4023\">Putussibau</option><option value=\"4934\">Raha</option><option value=\"1492\">Ranai</option><option value=\"1674\">Rancaekek</option><option value=\"2114\">Rancakalong</option><option value=\"1534\">Rangkasbitung</option><option value=\"4277\">Rantau</option><option value=\"356\">Rantau Prapat</option><option value=\"4797\">Rantepao</option><option value=\"2677\">Rembang</option><option value=\"2701\">Rembang</option><option value=\"1131\">Rembang Dangku</option><option value=\"814\">Rengat</option><option value=\"802\">Reteh</option>\
<option value=\"764\">Rimbo Bujang</option><option value=\"917\">Rumbai</option><option value=\"4850\">Rumbia</option><option value=\"3882\">Ruteng</option><option value=\"251\">Sabang</option><option value=\"1246\">Sako</option><option value=\"2847\">Salatiga</option><option value=\"4512\">Samarinda</option><option value=\"4091\">Sambas</option><option value=\"3362\">Sampang</option><option value=\"4357\">Sampit</option><option value=\"5321\">Sanana</option><option value=\"4459\">Sangatta</option><option value=\"4112\">Sanggau</option><option value=\"750\">Sarolangun</option><option value=\"4263\">Satui</option><option value=\"5262\">Saumlaki</option><option value=\"1084\">Sawahlunto</option><option value=\"4480\">Sebatik</option><option value=\"1247\">Seberang Ulu I</option><option value=\"1248\">Seberang Ulu II</option><option value=\"2601\">Secang</option><option value=\"462\">Sei Rampah</option><option value=\"276\">Sei Suka</option><option value=\"4122\">Sekadau Hilir</option><option value=\"1150\">Sekayu</option><option value=\"840\">Selat Panjang</option><option value=\"3746\">Selong</option><option value=\"2852\">Semarang</option><option value=\"5676\">Semplak</option><option value=\"4817\">Sengkang</option><option value=\"5443\">Sentani</option><option value=\"1644\">Serang</option><option value=\"1616\">Serpong</option><option value=\"5581\">Serpong Utara</option><option value=\"5556\">Serui</option><option value=\"189\">Seunagan</option><option value=\"901\">Siak</option><option value=\"533\">Sibolga</option><option value=\"610\">Sibolga Kota</option><option value=\"3005\">Sidayu</option><option value=\"4749\">Sidenreng</option><option value=\"286\">Sidikalang</option><option value=\"3377\">Sidoarjo</option><option value=\"200\">Sigli</option><option value=\"1018\">Sijunjung/ Muaro Sijunjung</option><option value=\"454\">Simanindo</option><option value=\"996\">Simpang Empat</option><option value=\"5634\">Simpang Empat</option><option value=\"153\">Simpang Tiga Redelon</option><option value=\"5663\">Sindang Jaya</option><option value=\"2157\">Singaparna</option><option value=\"3643\">Singaraja</option><option value=\"4154\">Singkawang</option><option value=\"3192\">Singosari</option><option value=\"4733\">Sinjai</option><option value=\"4134\">Sintang</option><option value=\"534\">Sirandorung</option><option value=\"3399\">Situbondo</option><option value=\"2772\">Slawi</option><option value=\"3605\">Sleman</option><option value=\"3973\">Soe</option><option value=\"2841\">Solo</option><option value=\"1087\">Solok</option><option value=\"1676\">Soreang</option><option value=\"5376\">Sorong</option><option value=\"2742\">Sragen</option><option value=\"379\">Stabat</option><option value=\"2053\">Subang</option><option value=\"254\">Subulussalam</option><option value=\"2093\">Sukabumi</option><option value=\"1331\">Sukadana</option><option value=\"4681\">Sukamaju</option><option value=\"191\">Suka Makmue</option><option value=\"4377\">Sukamara</option><option value=\"1375\">Sukoharjo</option><option value=\"2756\">Sukoharjo</option><option value=\"2547\">Sukorejo</option><option value=\"3766\">Sumbawa Besar</option><option value=\"1853\">Sumber</option><option value=\"2117\">Sumedang</option><option value=\"3417\">Sumenep</option><option value=\"2345\">Sumpyuh /Sumpiuh</option><option value=\"744\">Sungai Bahar</option><option value=\"1426\">Sungailiat</option><option value=\"1152\">Sungai Lilin</option><option value=\"4264\">Sungai Loban</option><option value=\"729\">Sungai Penuh</option><option value=\"4177\">Sungai Tabuk</option><option value=\"4636\">Sungguminasa</option><option value=\"3536\">Surabaya</option><option value=\"3680\">Tabanan</option><option value=\"5100\">Tahuna</option><option value=\"688\">Tais</option><option value=\"93\">Takengon</option><option value=\"1136\">Talang Ubi</option><option value=\"3769\">Taliwang</option><option value=\"3937\">Tambolaka</option><option value=\"1713\">Tambun Selatan</option><option value=\"4301\">Tamiang Layang</option><option value=\"4487\">Tanah Grogot</option><option value=\"891\">Tanah Putih</option><option value=\"4500\">Tanah Tidung</option><option value=\"1640\">Tangerang</option><option value=\"1820\">Tanggeung</option><option value=\"4257\">Tanjung</option><option value=\"5577\">Tanjung Balai</option><option value=\"1459\">Tanjung Balai Karimun</option><option value=\"1284\">Tanjung Bintang</option><option value=\"315\">Tanjung Morawa</option><option value=\"1446\">Tanjung Pandan</option><option value=\"1509\">Tanjung Pinang</option><option value=\"4398\">Tanjung Redeb</option><option value=\"2123\">Tanjungsari</option><option value=\"4409\">Tanjung Selor</option><option value=\"59\">Tapak Tuan</option><option value=\"5632\">Tapos</option><option value=\"832\">Tapung</option><option value=\"4518\">Tarakan</option><option value=\"555\">Tarutung</option><option value=\"2250\">Tasikmalaya</option><option value=\"622\">Tebing Tinggi</option><option value=\"1107\">Tebing Tinggi</option><option value=\"2874\">Tegal</option><option value=\"2603\">Tegalrejo</option><option value=\"781\">Telanaipura</option><option value=\"843\">Teluk Kuantan</option><option value=\"2792\">Temanggung</option><option value=\"805\">Tembilahan</option><option value=\"5389\">Teminabuan</option><option value=\"1432\">Tempilang</option><option value=\"4444\">Tenggarong</option><option value=\"5326\">Ternate</option><option value=\"337\">Tiga Binanga</option><option value=\"1619\">Tigaraksa</option><option value=\"4527\">Tilamuta</option><option value=\"5497\">Timika</option><option value=\"5223\">Tinambung</option><option value=\"5316\">Tobelo</option><option value=\"1442\">Toboali</option><option value=\"4969\">Toili</option><option value=\"5053\">Toli - Toli</option><option value=\"5170\">Tomohon</option><option value=\"5125\">Tondano</option><option value=\"5128\">Tondano Timur</option><option value=\"5129\">Tondano Utara</option><option value=\"3440\">Trenggalek</option><option value=\"5251\">Tual</option><option value=\"904\">Tualang</option><option value=\"961\">Tuapejat</option><option value=\"3461\">Tuban</option><option value=\"1169\">Tugumulyo</option><option value=\"3481\">Tulungagung</option><option value=\"1394\">Tumi Jajar</option><option value=\"3196\">Tumpang</option><option value=\"3197\">Turen</option><option value=\"3652\">Ubud</option><option value=\"878\">Ujung Batu</option><option value=\"2192\">Ujungberung</option><option value=\"865\">Ukui</option><option value=\"2724\">Ungaran</option><option value=\"2725\">Ungaran Barat</option><option value=\"3931\">Waikabubak</option><option value=\"3950\">Waingapu</option><option value=\"3199\">Wajak</option><option value=\"5461\">Wamena</option><option value=\"4945\">Wangi-Wangi</option><option value=\"4605\">Watampone</option><option value=\"4760\">Watansoppeng</option><option value=\"3590\">Wates</option><option value=\"1334\">Way Jepara</option><option value=\"5304\">Weda</option><option value=\"2472\">Wirosari</option><option value=\"3834\">Witihama</option><option value=\"2820\">Wonogiri</option><option value=\"3578\">Wonosari</option><option value=\"2836\">Wonosobo</option><option value=\"3621\">Yogyakarta</option></select>");
        locationDropdown.css('display', 'table-cell');
        locationDropdown.css('float', 'left');
        locationDropdown.css('padding-right', '10px');
        return locationDropdown;
    }

    function createPriceRange() {
        var priceRangeInput = $("<div class=\"mb-20\"><div><div class=\"price-filter clearfix\" style=\"float: left\"><span><label for=\"pr-min-1\" class=\"muted alert-gray\">Rp</label><input type=\"text\" id=\"pr-min-1\" name=\"pr_min\" class=\"input-small\" value=\"\" title=\"Harga Terendah\" placeholder=\"Min\" ></span><label class=\"connector\"  style=\"padding-left:5px;padding-right:5px\" >ke</label><span class=\"mb-0\"><label for=\"pr-max-1\" class=\"muted alert-gray\">Rp</label><input type=\"text\" id=\"pr-max-1\" name=\"pr_max\" class=\"input-small\" value=\"\" title=\"Harga Tertinggi\" placeholder=\"Maks\" ></span></div>\<ul style=\"display: table-cell;padding-top: 10px;padding-left: 10px;margin-top: 0px;\">\<li><label class=\"checkbox\" style=\"padding-left:5px;\"><input type=\"checkbox\" id=\"pr-grosir-1\" value=\"true\" class=\"ng-pristine ng-untouched ng-valid\" style=\"margin:5px 0px 0px 0px;\" > Grosir</label></li>\</ul>\</div>\</div>");
        priceRangeInput.css('display', 'table-cell');
        priceRangeInput.css('padding-left', '10px');
        return priceRangeInput;
    }

    function createGoldmerchantCheckbox() {
        var goldMerchantCb = $("<ul style=\"display: table-cell;padding-top: 10px;padding-left: 10px;margin-top: 0px;\">\<li><label class=\"checkbox\" style=\"padding-left:5px;\"><input id=\"gold-merch-1\" type=\"checkbox\" value=\"true\" class=\"ng-pristine ng-untouched ng-valid\" style=\"margin:5px 0px 0px 0px;\" > Gold Merchant</label></li>\</ul>\</div>\</div>");
        goldMerchantCb.css('display', 'table-cell');
        goldMerchantCb.css('padding-left', '10px');
        return goldMerchantCb;
    }

    function updateProductList(merchantID) {
        var prodListDiv = $('#product-list').empty();

        // create tab
        var tabContainer = $('<div />'); 
        tabContainer.css('width', '100%');
        tabContainer.css('display', 'block');
        tabContainer.css('padding-top', '20px');

        var ulContainer = $('<ul class="horizontal-tab" />');
        ulContainer.css('padding','0px');
        ulContainer.css('margin','0px');
        ulContainer.css('margin-bottom','10px');
        ulContainer.css('border-bottom','1px solid #ddd');
        ulContainer.css('list-style','none');
        ulContainer.css('min-height','29px');

        tabContainer.append(ulContainer);

        for (var i = 0; i < window.queryArray.length; i++) {
            var queryStr = window.queryArray[i];
            var qclass = "";
            if ( i === 0) {
                qclass = "active";
            }
            var listTab = $('<li id="" class="' + qclass + '"><a rel="nofollow">'  +  queryStr + '</a></li>');

            listTab.css('float','left');
            listTab.css('font-size','11px');
            listTab.css('diplay','inline-block');
            listTab.css('line-height','22px');

            listTab.find('a').css('background-color','#f7f7f7');
            listTab.find('a').css('padding','4px 16px');
            listTab.find('a').css('margin-right','10px');
            listTab.find('a').css('border-top','1px solid #eaeaea');
            listTab.find('a').css('border-left','1px solid #eaeaea');
            listTab.find('a').css('border-right','1px solid #eaeaea');
            listTab.find('a').css('color','#606060');
            listTab.find('a').css('display','block');
            listTab.find('a').css('height','20px');
            listTab.find('a').css('line-height','20px');
            listTab.find('a').css('border-radius','2px 2px 0 0');

            listTab.find('.active').css('color','#e55b36');
            listTab.find('.active').css('cursor','default');
            listTab.find('.active').css('background-color','#fff');
            listTab.find('.active').css('border-top','2px solid green');

            var clickFn = (function() {
                var merchID = merchantID;
                var keywd = queryStr;
                return function() {
                    var prodListDiv = $('#product-list');
                    var prdlistcont = prodListDiv.find('#prodlist-cont');
                    prdlistcont.empty();
                    prodListDiv.find('li').removeClass('active');
                    $(this).addClass('active');
                    updateProdListByMerchAndKeyword(merchID, keywd, prdlistcont);
                };
            })();
            listTab.click(clickFn);
            ulContainer.append(listTab);
        }

        var merchData = window.dataResultMap[merchantID];
        var firstKeyword = Object.keys(merchData)[0];
        var prdListCont = $('<div id="prodlist-cont" />');
        prdListCont.css('display', 'block');
        prdListCont.css('width', '100%');
        prdListCont.css('float', 'left');

        tabContainer.append(prdListCont);
        prodListDiv.append(tabContainer);
        updateProdListByMerchAndKeyword(merchantID, firstKeyword, prdListCont);
    }

    function updateProdListByMerchAndKeyword(merchantID,keyword, prodListDiv) {
        var prodList = window.dataResultMap[merchantID][keyword];
        for (var i in prodList) {
            var prod = prodList[i];
            var prodName = prod.name;
            var prodUri = prod.uri;
            var prodLocation = window.dataMerchantDataMap[merchantID].location;
            var prodPrice = prod.price;
            var prodimg = prod.image_uri;
            var shopName = window.dataMerchantDataMap[merchantID].name;

            var proddiv = createProductDiv(prodName,prodUri,prodLocation,prodPrice,prodimg,shopName);
            prodListDiv.append(proddiv);
        }
    }

    function updateMerchantList() {
        var merchListDiv = $('#merchant-list').empty();
        if (window.dataMerchantDataMap !== undefined) {
            for (var i in window.dataMerchantDataMap) {
                // show only merchant that has all search result 
                if (window.queryArray.length == Object.keys(window.dataResultMap[i]).length) {
                    var shopName = window.dataMerchantDataMap[i].name;
                    var shopUri = window.dataMerchantDataMap[i].uri;
                    var shopLoc = window.dataMerchantDataMap[i].location;
                    var resultCount = 0;
                    for (var j in  window.dataResultMap[i]) {
                        resultCount = resultCount + Object.keys(window.dataResultMap[i][j]).length;
                    }
                    // https://ecs7.tokopedia.net/img/repsys/bronze-1.gif
                    var reputationClass = "bronze";
                    if (window.dataMerchantDataMap[i].rating.reputation_set == 1) {
                        reputationClass = "bronze";
                    } else if (window.dataMerchantDataMap[i].rating.reputation_set == 2) {
                        reputationClass = "silver";
                    } else if (window.dataMerchantDataMap[i].rating.reputation_set == 3) {
                        reputationClass = "gold";
                    } else if (window.dataMerchantDataMap[i].rating.reputation_set == 4) {
                        reputationClass = "diamond";
                    }
                    var repLevel = window.dataMerchantDataMap[i].rating.reputation_level;
                    var repImg = "https://ecs7.tokopedia.net/img/repsys/" + reputationClass + "-" + repLevel + ".gif";
                    if (repLevel === 0) {
                        repImg = "https://ecs7.tokopedia.net/img/repsys/badges-off.jpg";
                    }

                    var divMerchant = createMerchandDiv(shopName, shopUri, shopLoc, resultCount, repImg, i);
                    merchListDiv.append(divMerchant);
                }
            }
        }   
    }

    function createProductList() {
        var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
        var divContainer = $('<div class="product-list" />');
        divContainer.css('display', 'none');
        divContainer.css('background-color', '#fff');
        divContainer.css('width', '890px');
        divContainer.css('height', (h * 0.9 - 90) + 'px' );
        divContainer.css('overflow', 'auto');
        divContainer.css('padding-left', '70px');
        divContainer.attr('id', 'product-list');
        return divContainer;
    }

    function createMerchantList() {
        var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
        var divContainer = $('<div class="product-list" />');
        divContainer.css('display', 'none');
        divContainer.css('background-color', '#fff');
        divContainer.css('width', '890px');
        divContainer.css('height', (h * 0.9 - 90) + 'px');
        divContainer.css('overflow', 'auto');
        divContainer.css('padding-left', '70px');
        divContainer.attr('id', 'merchant-list');
        return divContainer;
    }

    function createMerchandDiv(shopName,shopURI,shopLocation,resultCount,reputationImg,merchID) {
        /*jshint multistr: true */
        var divMerchant = $("<div class=\"product-card\" >\
<a href=\"" +  shopURI + "\">\
<div class=\"image\"><img alt=\"" +  shopName + "\" src=\"https://ecs12.tokopedia.net/newimg/cache/215-square/default_v3-shopnophoto.png\"></div><div class=\"detail\">\
<span class=\"detail__name\">" +  shopName + "</span><div class=\"row-fluid\" ><div class=\"span7\">\
<span class=\"detail__price\">" +  resultCount + "</span></div><div class=\"span5 text-right ellipsis\"><small class=\"muted info-tooltip\">\
<i class=\"icon-pin-alt\"></i>" +  shopLocation + "</small></div></div></div></a><div class=\"shop-detail\"><div class=\"row-fluid\"><div class=\"span8\">\
<div class=\"shop-detail__name ellipsis \"><img title=\"\" class=\"inline-block\"  src=\"" +  reputationImg + "\"></div></div></div></div></div>");
        divMerchant.css('width', '180px');
        divMerchant.css('margin', '5px 15px 5px 5px');
        divMerchant.css('min-height', '100px');
        divMerchant.css('float', 'left');

        divMerchant.find('a').css('display','block');
        divMerchant.find('a').css('position','relative');
        divMerchant.find('a').css('border-width','1px 1px 0px');
        divMerchant.find('a').css('border-color','#ddd');
        divMerchant.find('a').css('border-style','solid');

        divMerchant.find('a').css('color','green');
        divMerchant.find('a').css('text-decoration','none');

        divMerchant.find('a>.image').css('height','178px');
        divMerchant.find('a>.image').css('width','178px');
        divMerchant.find('a>.image>img').css('height','178px');
        divMerchant.find('a>.image>img').css('width','178px');

        divMerchant.find('.detail').css('background-color','#fff');
        divMerchant.find('.detail').css('padding','5px');

        divMerchant.find('.detail__name').css('height','40px');
        divMerchant.find('.detail__name').css('font-size','12px');
        divMerchant.find('.detail__name').css('font-weight','700');
        divMerchant.find('.detail__name').css('line-height','18px');
        divMerchant.find('.detail__name').css('display','block');
        divMerchant.find('.detail__name').css('overflow','hidden');

        divMerchant.find('.row-fluid').css('width','100%');
        divMerchant.find('.shop-detail').css('background-color','#f7f7f7');
        divMerchant.find('.shop-detail').css('border','1px solid #ddd');
        divMerchant.find('.shop-detail').css('border-top-style','dashed');
        divMerchant.find('.shop-detail').css('border-radius','0 0 2px 2px');
        divMerchant.find('.shop-detail').css('padding','0 4px');
        divMerchant.find('.shop-detail').css('padding-left','5px');

        divMerchant.find('a').click( function(event) {
            event.preventDefault();
            updateProductList(merchID);
            showProductResult();
        });

        return divMerchant;
    }

    function createProductDiv(prodName,prodURI,prodLocation,prodPrice,prodimg,shopName) {
        /*jshint multistr: true */
        var divProd = $("<div class=\"product-card\" >\
<a href=\"" +  prodURI + "\">\
<div class=\"image\"><img alt=\"" +  prodName + "\" src=\""+ prodimg +"\"></div><div class=\"detail\">\
<span class=\"detail__name\">" +  prodName + "</span><div class=\"row-fluid\" ><div class=\"span7\">\
<span class=\"detail__price\">" +  prodPrice + "</span></div><div class=\"span5 text-right ellipsis\"><small class=\"muted info-tooltip\">\
<i class=\"icon-pin-alt\"></i>" +  prodLocation + "</small></div></div></div></a><div class=\"shop-detail\"><div class=\"row-fluid\"><div class=\"span8\">\
<div class=\"shop-detail__name ellipsis \">" + shopName + "</div></div></div></div></div>");
        divProd.css('width', '180px');
        divProd.css('margin', '20px 15px 0 0');
        divProd.css('min-height', '100px');
        divProd.css('float', 'left');

        divProd.find('a').css('display','block');
        divProd.find('a').css('position','relative');
        divProd.find('a').css('border-width','1px 1px 0px');
        divProd.find('a').css('border-color','#ddd');
        divProd.find('a').css('border-style','solid');

        divProd.find('a').css('color','green');
        divProd.find('a').css('text-decoration','none');

        divProd.find('a>.image').css('height','178px');
        divProd.find('a>.image').css('width','178px');
        divProd.find('a>.image>img').css('height','178px');
        divProd.find('a>.image>img').css('width','178px');

        divProd.find('.detail').css('background-color','#fff');
        divProd.find('.detail').css('padding','5px');

        divProd.find('.detail__name').css('height','40px');
        divProd.find('.detail__name').css('font-size','12px');
        divProd.find('.detail__name').css('font-weight','700');
        divProd.find('.detail__name').css('line-height','18px');
        divProd.find('.detail__name').css('display','block');
        divProd.find('.detail__name').css('overflow','hidden');

        divProd.find('.row-fluid').css('width','100%');
        divProd.find('.shop-detail').css('background-color','#f7f7f7');
        divProd.find('.shop-detail').css('border','1px solid #ddd');
        divProd.find('.shop-detail').css('border-top-style','dashed');
        divProd.find('.shop-detail').css('border-radius','0 0 2px 2px');
        divProd.find('.shop-detail').css('padding','0 4px');
        divProd.find('.shop-detail').css('padding-left','5px');

        divProd.find('a').attr('href', prodURI );
        divProd.find('a').attr('target', '_blank' );

        return divProd;
    }

    function toogleDialog() {
        var diag = $('#searchDialog');
        if (diag && diag.css('display') == 'none') {
            diag.css('display','block');
        } else {
            diag.css('display','none');
        }   
    }

    function createSearchButton() {
        var searchBtn = $('<a target="_blank" href="#" class="btn btn-action">Search</a>');
        searchBtn.css('float' ,'right');
        searchBtn.click( function(event) {
            event.preventDefault();
            window.dataMerchantMap = undefined;
            window.dataResultMap = undefined;
            window.dataMerchantDataMap = undefined;
            doSearch();
        });
        return searchBtn;
    }

    addFunction(showLoading, false);
    addFunction(hideLoading, false);
    addFunction(showMainmenu, false);

    addFunction(updateProdListByMerchAndKeyword, false);
    addFunction(updateProductList, false);
    addFunction(showProductResult, false);
    addFunction(createProductList, false);
    addFunction(createProductDiv, false);

    addFunction(updateMerchantList, false);
    addFunction(showMerchantResult, false);
    addFunction(createMerchantList, false);
    addFunction(createMerchandDiv, false);

    addFunction(doMapMerchant, false);
    addFunction(ajaxProductsByKeyword, false);
    addFunction(productListToMerchantList, false);
    addFunction(doSearch, false);
    addFunction(createSearchButton, false);
    addFunction(toogleDialog, false);
    addFunction(createGoldmerchantCheckbox, false);
    addFunction(createPriceRange, false);
    addFunction(createLocationDropDown, false);
    addFunction(createInputSearch, false);
    addFunction(createCategoriesDropdownClone, false);
    addFunction(createSearchDialogContent, false);
    addFunction(createDialogSearch, false);

    window.addEventListener("load", function() {
        exec(function() {
            $('.search-parent').append('<span class="btn-search-wrapper"><button class="btn btn-search" type="button" id="advancedSearchMerchantBtn"><i class="icon-search icon-large orange"></i></button></span>');
            createDialogSearch();

            $('#advancedSearchMerchantBtn').click(function() {
                toogleDialog();
            });
        });
    }, false);
})();