﻿var SelectedTab = "";

function pageLoad() {
    $('#txtPrice').priceFormat();
    if (SelectedTab == "") {
        //When page loads...
        $(".tab_content").hide(); //Hide all content
        $("ul.tabs li:first").addClass("active").show(); //Activate first tab
        $(".tab_content:first").show(); //Show first tab content
    }
    else if (SelectedTab == "onlySell") {
        $(".tab_content").hide(); //Hide all content
        $(".nosell").hide(); //Hide all content
        var $this = $("ul.tabs li.sell:first");
        $this.addClass("active").show(); //Activate first tab
        var activeTab = $this.find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
    }
    else {
        $("ul.tabs li").each(function (i) {
            var $this = $(this);
            if ($this.find("a").attr("href") == SelectedTab) {
                $("ul.tabs li").removeClass("active"); //Remove any "active" class
                $this.addClass("active"); //Add "active" class to selected tab
                $(".tab_content").hide(); //Hide all tab content

                var activeTab = $this.find("a").attr("href"); //Find the href attribute value to identify the active tab + content
                $(activeTab).fadeIn(); //Fade in the active ID content
            }
        });
    }

    //On Click Event
    $("ul.tabs li").click(function () {

        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });

    $.widget("custom.catcomplete", $.ui.autocomplete, {
        _renderMenu: function (ul, items) {
            var self = this,
				currentCategory = "";
            $.each(items, function (index, item) {
                if (item.category != currentCategory) {
                    ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
                    currentCategory = item.category;
                }
                self._renderItem(ul, item);
            });
        }
    });
    $(function () {
        $('#txtRegionSearch').catcomplete({
            source: function (request, response) {
                var endPoint = "http://services.realtywebbing.com/Regions/getRegions";
                var masterID = $('#hiddenMasterID').val();
                $.ajax({
                    url: endPoint,
                    dataType: "jsonp",
                    data: {
                        query: request.term
                        , masterID: masterID
                        , format: "json"
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    },
                    success: function (data) {
                        var obj = [];
                        if (data.rsp.regions != null) {
                            if (typeof (data.rsp.regions.Region.length) == "undefined")
                                obj = [{
                                    id: data.rsp.regions.Region.id
                                    , name: data.rsp.regions.Region.name
                                    , level: data.rsp.regions.Region.level
                                }];
                                else
                                    obj = data.rsp.regions.Region;
                        }
                        response($.map(obj, function (item) {
                            return {
                                label: item.name
                                , value: item.name
                                , id: item.id
                                , category: item.level
                            }
                        }));
                    }
                });
            },
            minLength: 3,

            select: function (event, ui) {
                $('#hiddenIdRegion').val(ui.item.id);
                $('#txtRegionSearch').css("backgroundColor", "#e4fbff");
                $('#LabelRegionSelected').html(ui.item.value);
                if ($('#needPostBack').val() == "true")
                    __doPostBack('linkChange', '');
            }
        });
    });
}
