﻿$(function () {
    //Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    setTimeout(function () {
        if (typeof (sboverlay) == "undefined") GetDescription();
    }, 20);

    $("#clearForm").tooltip({
        relative: true,
        position: "bottom center",
        offset: [10, 0],
        effect: "fade",
        opacity: 0.8,
        tip: '#clearFormTooltip',
        delay: 100
    });


    $("#searchBox .tleft").tooltip({
        relative: true,
        position: "center left",
        offset: [0, -20],
        effect: "fade",
        opacity: 0.8,
        tip: '#inputTooltip',
        delay: 100
    });

    $("#searchBox .tright").tooltip({
        relative: true,
        position: "center right",
        offset: [0, 10],
        effect: "fade",
        opacity: 0.8,
        tip: '#inputTooltip',
        delay: 100
    });

    $("#LocationDropDown").colorbox({
        scrolling: false,
        iframe: true,
        width: 500,
        height: 435,
        maxWidth: "98%",
        maxHeight: "98%",
        opacity: 0.5,
        close: ""
    });

    $("#CategoryDropDown").colorbox({
        scrolling: false,
        iframe: true,
        width: 430,
        height: 435,
        maxWidth: "98%",
        maxHeight: "98%",
        opacity: 0.5,
        close: ""
    });

    $(cid_tbLocation).change(function (event) {
        $.ajax({
            type: "POST",
            url: "/Results.asmx/SetLocation",
            data: "{'name':'" + $(cid_tbLocation).val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                ShowDescription(msg.d);
            }
        });
    });

    $(cid_tbCategory + ", " + cid_chkSearchNames).change(function (event) {
        $.ajax({
            type: "POST",
            url: "/Results.asmx/SetCategoryOrSearchStr",
            data: "{'name':'" + escape($(cid_tbCategory).val()) + "'" +
                            ", 'IsBusinessName':" + $(cid_chkSearchNames).attr("checked") + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                ShowDescription(msg.d);
            }
        });
    });
});

function GetDescription() {
    $.ajax({
        type: "POST",
        data: "{}",
        url: "/Results.asmx/GetSearchParamsStr",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            ShowDescription(msg.d);
        }
    });
}

function ShowDescription(str) {
    $("#searchDescription").html(str);
    if (typeof (searchDescriptionTooltip) == "undefined") {
        searchDescriptionTooltip = $("#SearchBoxBottom").tooltip({
            api: true,
            relative: true,
            position: "bottom center",
            offset: [-80, -40],
            onBeforeShow: function() {
                if ($("#searchDescription").html() == "")
                    return false;
            },
            onBeforeHide: function() {
                if ($("#searchDescription").html() == "")
                    return true;
                return false;
            }
        }).show();
    }
    else
        searchDescriptionTooltip.show();
}

function ClearForm() {
    ClearField(cid_tbLocation);
    ClearField(cid_tbCategory);
    //ClearField(cid_tbName);
    $("#searchDescription").html("");
    searchDescriptionTooltip.hide();
}

function ClearField(field) {
    if (HasVal(field)) {
        $(field).val("");
        $(field).change();
        $(field).blur();
        $(field).addClass("watermarked")
    }
}

function ChangeGeocode() {
    if (HasVal(cid_tbLocation)) {
        $(cid_tbLocation).val("");
    }
    
    var params =
            "{'name':'" + $(cid_tbPostalCode).val() +
            "', radius:" + $(cid_ddlRange + ' option:selected').val() +
            ", inKm:" + ($(cid_rbKm + ':checked').val() == 'rbKm') + "}";
    $.ajax({
        type: "POST",
        url: "/Results.asmx/Geocode",
        data: params,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            ShowDescription(msg.d);
        }
    });
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

function CategoryDropDown() {
    var autoComplete = $find("AutoCompleteEx2");
    DropDown(autoComplete);
}

function DropDown(autoComplete) {
    autoComplete.get_element().focus();
    autoComplete._textBoxHasFocus = true;
    var prefix = autoComplete.get_element().value;
    if ( prefix.length > 3) prefix = '';
    autoComplete.get_element().value = '';
    Sys.Net.WebServiceProxy.invoke(autoComplete.get_servicePath(),
       autoComplete.get_serviceMethod(),
       false,
       { prefixText: prefix, count: 1000 },
       Function.createDelegate(autoComplete, autoComplete._onMethodComplete),
       Function.createDelegate(autoComplete, autoComplete._onMethodFailed),
       '',
       10000);

    $common.updateFormToRefreshATDeviceBuffer();
}

function DropDownShown(source, args) {
    source._popupBehavior._element.style.zIndex = 10001;
}

function HasVal(field) {
    if ($(field).hasClass("watermarked") || $(field).val().trim() == "") return false;
    return true;        
}
