﻿function itemSelected(sender, ev) {



    var txtSeacrh = document.getElementById(mnu_txtSearchID);
   
    if (txtSeacrh.value != 'View All') {
        var otherText = ev._item.innerHTML;
        if (otherText == "") {
            otherText = ev._item.alt;
        }
        if (txtSeacrh.value == 'null') {
            txtSeacrh.value = otherText;
        }
        for (i = 0; i < suggestedList.length; i++) {
            var _value = suggestedList[i];

            if (txtSeacrh.value == _value) {
                var mod = i % 3;
                if (mod == 0)
                    mod = i + 2;
                else if (mod == 1)
                    mod = i + 1;
                 
                
                var url = suggestedList[mod];
                if (url != "") {
                    document.location.href = window.location.protocol + "//" + window.location.host + url;
                    return;
                }
                break;
            }
        }
   
    }
    else if (txtSeacrh.value == 'View All') {
        txtSeacrh.value = currentText;
    }
    if (txtSeacrh.value == 'Coupons' || txtSeacrh.value == 'Deals' || txtSeacrh.value == 'Store')
        txtSeacrh.value = "";
    else
        submitSearch();    

}
var suggestedList = new Array();
var currentText = '';

var cnstDEAL ='DEAL'
var cnstCOUPON='COUPON'
var cnstSTORE ='STORE'

function onClientPopulated(sender, e) {

    var comletionList = $find("AutoCompleteEx").get_completionList();
    var txtSeacrh = document.getElementById(mnu_txtSearchID);
    suggestedList = new Array();
    var arrayCount = 0;

    var changeHeader = true;
    var header = 'Deals'
    var lastType = cnstDEAL;
    for (i = 0; i < comletionList.childNodes.length; i++) {
        var _value = comletionList.childNodes[i]._value;
        var text = _value[0];
        var price = _value[1];
        var imgeUrl = _value[2];
        var navigateUrl = _value[3];
        var type = _value[4];
        comletionList.childNodes[i]._value = text;

        if (type == cnstDEAL && i==0) {
            //  This Will never fire
              header = 'Deals'
              lastType = type;
              changeHeader = true;                 
        }
        else if (type == cnstCOUPON && lastType != type) {
            lastType = type;
            header = 'Coupons';
            text = "See All " + text + " Coupons";
            changeHeader = true;
        }
        else if (lastType != type) {
            lastType = type;
            header = 'Stores';
            text = "See All " + text + " Deals";
            changeHeader = true;
        }
        //onmouseout="this.style.backgroundColor='#FFFFFF';" onclick="window.location.href='yourlinklocationhere'"
        if (type == cnstDEAL)
            price = "$ " + price;
        else
            price = "";
        
        if (changeHeader) {
            comletionList.childNodes[i].innerHTML = "<div class='suggestHeader'>" + header + "</div><div class='SingleSuggestionBox' id='" + i + "' onmouseover='this.style.backgroundColor=\"#eaeaea\";' onmouseout='this.style.backgroundColor=\"#FFFFFF\";' ><img src=" + imgeUrl + " alt='" + text + "' /><span class='suggestTitle'>" + text + "</span><span class='suggestInformation'>" + price + "</span></div>";
            changeHeader = false;
            if (i == comletionList.childNodes.length - 1)
                comletionList.childNodes[i].innerHTML = comletionList.childNodes[i].innerHTML + "<div class='searchBaseViewAll'><a href='javascript:submitSearch()'>View All</a></div>";
        } 
        else if (i==comletionList.childNodes.length-1)
            comletionList.childNodes[i].innerHTML = "<div class='SingleSuggestionBox' id='" + i + "'  onmouseover='this.style.backgroundColor=\"#eaeaea\";' onmouseout='this.style.backgroundColor=\"#FFFFFF\";'><img src=" + imgeUrl + " alt='" + text + "' /> <span class='suggestTitle'>" + text + "</span><span class='suggestInformation'>" + price + "</span></div><div class='searchBaseViewAll'><a href='javascript:submitSearch()'>View All</a></div>";
        else if (i!=0)
            comletionList.childNodes[i].innerHTML = "<div class='SingleSuggestionBox' id='" + i + "'  onmouseover='this.style.backgroundColor=\"#eaeaea\";' onmouseout='this.style.backgroundColor=\"#FFFFFF\";'><img src=" + imgeUrl + " alt='" + text + "' /> <span class='suggestTitle'>" + text + "</span><span class='suggestInformation'>" + price + "</span></div>";

        suggestedList[arrayCount] = text;
        suggestedList[arrayCount + 1] = price;
        suggestedList[arrayCount+2] = navigateUrl;

        arrayCount = arrayCount + 3;
    }

    currentText = txtSeacrh.value;
}

function onSuggestShown(source, eventArgs) {
    $get("divAutoComp").style.width = 400;
}
