﻿//===================== Find-Browse Panel functionality ====================//

$(document).ready(function () {

    if ($('div#modify-results').length) {
        $("li.browse-all-locations").live('click', function () {
            setLocationData("left2right");
        });
        $("li.find-a-location-tab").live('click', function () {
            setLocationData("right2left");
        });

        var setLocationData = function (direction) {
            if (direction == "left2right") {
                //$("li#find-a-location-panel span.inputLabelPair input").each(function () {
                //fieldValue = $.trim($(this).val());
                var objLocationInput = $("input#location-postal-webid1");
                fieldValue = $.trim(objLocationInput.val());
                //locationIndex = objLocationInput.attr("id").substring(objLocationInput.attr("id").length - 1);
                //locationDiv = "div#searched-location" + locationIndex;
                if (fieldValue == "") {
                    //$("input#autoComplete" + locationIndex).val("").attr("source", "");
                    //clearLocationDiv(locationIndex);
                    $("div#browse-all-locations-panel").find("input[value=0]:eq(0)").click();
                }
                //If autocompleteid is set AND the find-tab set this id, send value to the detailDiv
                else if (objLocationInput.attr("autocpid") != "") {
                    //$(locationDiv + " div.search-details").text(fieldValue);
                    //setLocationDisplay(locationDiv, locationIndex, direction);
                    $("input#temp").val("").attr({ typeid: "", location: "", place: "" });
                    setLocationDivOptions();
                }
                //});
                //setLocationDivOptions();
                //fixLocationDisplay();
            }
            else {
                if ($("input#temp").val() == "") {  //If no change was made...
                    //if ($("input#location-postal-webid1").val == "") {}
                    //$("input#location-postal-webid1").val("").attr({autocpid:"", autocptypeid:"", countryid:""});   //clear the input textbox's data
                }
                else {  //If change was made...
                    var vals = new Array();
                    idx = 0;
                    for (i = 0; i < 4; i++) {
                        //var option = $(locationDiv).find('select').eq(i).val();
                        //var option = $("div#browse-all-locations-panel").find("select").eq(i).val();
                        var option = $("div#browse-all-locations-panel").find('select').eq(i).find('option:selected').val();
                        var optionVal = "";
                        if (option > 0) {
                            //optionVal = $("div#browse-all-locations-panel").find("[value='" + option + "']").text();
                            optionVal = $("div#browse-all-locations-panel").find('select').eq(i).find('option:selected').text();
                            if (optionVal != "") {
                                vals[idx] = optionVal;
                                idx++;
                            }
                        }
                    }
                    vals.reverse();
                    mainVal = vals.join(", ");
                    $("input#temp").attr("location", mainVal);
                    $("input#location-postal-webid1").val($("input#temp").attr("location"));
                    $("input#location-postal-webid1").attr({ autocpid: $("input#temp").val(),
                        autocptypeid: $("input#temp").attr("typeid"),
                        countryid: ""
                    });
                }
            }
        };  //end setLocationData()

        var setLocationDivOptions = function () {
            SwitchDataArray = {};
            locationIndex = 0;
            fieldValue = $.trim($("input#location-postal-webid1").val());
            autocpid = $("input#location-postal-webid1").attr("autocpid");
            autocptypeid = $("input#location-postal-webid1").attr("autocptypeid");
            //countryid = $("input#location-postal-webid1").attr("countryid");
            //locationDiv = "div#searched-location1";
            //Do for Cities, Countries, Regions, but not Properties or Postal Codes
            if (autocptypeid != 11 && autocptypeid != 8) {
                //Setting up the data for this autocomplete phrase
                optionsRay = fieldValue.split(", ");
                optionsRay.reverse();
                var SwitchData = { locationIndex: locationIndex,
                    itemArray: optionsRay,
                    country_id: 0,
                    state_id: 0,
                    city_id: 0,
                    neighborhood: 0
                };

                //Displaying the country for this autocomplete phrase
                optionID = $("div#browse-all-locations-panel select:eq(0) option").filter(function () { return $(this).text() == optionsRay[0] }).val();
                //$("div#browse-all-locations-panel").find("input[value='"+ optionID +"']:eq(0)").click();
                $("div#browse-all-locations-panel select:eq(0)").next(".ui-multiselect").find("span:eq(1)").text(optionsRay[0]);
                $("div#browse-all-locations-panel select:eq(0)").val(optionID);

                SwitchData.country_id = optionID;   //autocompleteID of the country, the 1st portion of the autocomplete item
                SwitchDataArray[0] = SwitchData;
                SearchResultsJS.Switching = true;
                SearchResultsJS.OnClickCountry(); //Call this to populate the states
            }
        };
    }

});      //end document-ready
