﻿var currentSource;

function SearchItemSelected(source, eventArgs) {

    var hn = window.location.hostname;
    if (hn == "localhost") {
        var webMethod = 'http://localhost:2012/WebServices/AutoCompleteService.asmx/SearchItemSelected';
    } else {
        var webMethod = 'http://www.neptune.com/WebServices/AutoCompleteService.asmx/SearchItemSelected';
    }

    var param;
    var target = source.get_completionList();
    var selectedIndex = source._selectIndex;
    
    if (eventArgs.get_value() == null) {
        param = target.childNodes[selectedIndex]._value;
    } else {
    param = eventArgs.get_value();
    }
    
    var parameters = "{'text':'" + param + "'}"

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            window.location = msg.d;
        },
        error: function(e) {
        }
    });

    return false;

}

function suggestionSearchShown(source, eventArgs) {
    $('.SuggestionSearchContainer img').attr('style', 'display:block;');
    //Pause slideshow if on homepage
    try {
        $('#slideshow').cycle('pause');
    } catch (error) {
        // probably not on homepage
    }
}

function suggestionSearchHidden(source, eventArgs) {
    $('.SuggestionSearchContainer img').attr('style', 'display:none;');
    //Resume slideshow if on homepage
    try {
        $('#slideshow').cycle('resume');    
    } catch (error) {
        // probably not on homepage
    }
}

function suggestionSearchFormat(source, eventArgs) {

    currentSource = source;
    var target = source.get_completionList();
    var keyHeaderName = "";
    var i;

    for (i = 0; i < target.childNodes.length; i++) {

        var splitdata = target.childNodes[i].innerHTML.split(" | ");
        var topItem = "";
        var bottomItem = "";
        var hiddenStyle = "";
        var hiddenItemStyle = "";

        if (i == 0) { topItem = " topItem"; keyHeaderName = splitdata[0]; }
        if (i == (target.childNodes.length - 1)) { bottomItem = " bottomItem"; }

        if (keyHeaderName == splitdata[0] && i != 0) {
            hiddenStyle = " style=\"color:Transparent;\" ";
            hiddenItemStyle = " style=\"border-left:Solid 1px #dddddd;width:160px;display:inline;text-align:left;position:relative;float:left;padding-left:8px;\" ";
        } else {
        if (splitdata[0].length > 15) {
            hiddenItemStyle = " style=\"height:30px;padding-top:10px;border-left:Solid 1px #dddddd;width:160px;display:inline;text-align:left;position:relative;float:left;padding-left:8px;\" ";
        } else {
            hiddenItemStyle = " style=\"padding-top:10px;border-left:Solid 1px #dddddd;width:160px;display:inline;text-align:left;position:relative;float:left;padding-left:8px;\" ";
        }
            hiddenStyle = " style=\"padding-top:10px;\" ";
            keyHeaderName = splitdata[0];
        }

        var header = "<span" + hiddenStyle + " itemID=\"" + i + "\" class=\"autoCompleteHeader" + topItem + bottomItem + "\">" + splitdata[0].slice(1, splitdata[0].length) + "</span>";
        var item = "<span" + hiddenItemStyle + " itemID=\"" + i + "\" class=\"autoCompleteItem" + topItem + bottomItem + "\">" + splitdata[1] + "</span>";
        target.childNodes[i].innerHTML = header + item;
    }
    
    $('.autoCompleteHeader').hover(suggestionSearchMouseOver, suggestionSearchMouseOut);
    $('.autoCompleteItem').hover(suggestionSearchMouseOver, suggestionSearchMouseOut);
    $('.autoCompleteHeader').click(suggestionItemClick);
    $('.autoCompleteItem').click(suggestionItemClick);

    $('.autoCompleteItem').each(function(index) {
        var thisElement = $(this);
        var sisterElement = $(this).siblings();
        var t = 1;
    });
    
}

function suggestionSearchMouseOver() {
    currentSource._selectIndex = $(this).attr("itemID");
    $(this).parent().addClass('suggestionSearchHighlight');
}

function suggestionSearchMouseOut() {
    $(this).parent().removeClass('suggestionSearchHighlight');
    $(this).parent().addClass('suggestionSearchItem');
}

function suggestionItemClick() {
    SearchItemSelected(currentSource,false);
} 
