﻿function setViewBy(dd) {
    var myindex = dd.selectedIndex
    var SelValue = dd.options[myindex].value

    if (SelValue == "alpha") {
        window.location = "storesbyalpha.aspx?urlkey=store_directory";
    }
    if (SelValue == "category") {
        window.location = "storesbycategory.aspx?urlkey=store_directory";
    }
}

function goToByScroll(id) {

    //goto that anchor by setting the body scroll top to anchor top
    $('#store-listing-area').animate({ scrollTop: 0 }, 0, function() {
        var target_offset = $("#anchor" + id).offset();
        var target_top = target_offset.top - $("#store-listing-area").offset().top;    
        $('#store-listing-area').animate({ scrollTop: target_top }, 200); 
                });	    
	
}

function openStoreListingByCategory(dd) {
    var myindex = dd.selectedIndex
    var SelValue = dd.options[myindex].value

    if (window.location.toString().toLowerCase().indexOf('storesbycategory')>-1) {
        goToByScroll(SelValue);
    } else {
        window.location = "storesbycategory.aspx?urlkey=store_directory#anchor" + SelValue;
    }
}

function openStoreListingMobile(dd) {
    var myindex = dd.selectedIndex
    var SelValue = dd.options[myindex].value
    if (SelValue == 'alpha') {
        window.location = "storesbyalpha.aspx";
    } else if (SelValue != '') {    
      window.location = "storesbycategory.aspx#" + escape(SelValue);
    }
}

$(function() {
    //    function log(message) {
    //        $("<div/>").text(message).prependTo("#log");
    //        $("#log").scrollTop(0);
    //    }
    function openPage(item) {
        window.location = "storedetails.aspx?storeid=" + item + "&urlkey=store_directory";
    }

    $("#stores").autocomplete({
        source: "/ajaxstores.ashx",
        minLength: 2,
        highlight: function doTheHighlight(value, term) {
            // Escape any regexy type characters so they don't bugger up the other reg ex
            term = term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1");

            // Join the terms with a pipe as an 'OR'
            term = term.split(' ').join('|');

            return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
        },
        select: function(event, ui) {
            //            log(ui.item ?
            //			"Selected: " + ui.item.value + " store id=" + ui.item.id :
            //			"Nothing selected, input was " + this.value);
            ui.item ? openPage(ui.item.id) : "do nothing";
        }
    })

    $.ui.autocomplete.prototype._renderItem = function(ul, item) {
        item.label = item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(this.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append("<a>" + item.label + "</a>")
            .appendTo(ul);
    };


});

