

function SearchSettings(ty, sec)
{
    this.Init(ty);

    this.sections = ["resi", "newhomes", "lonresi", "international", "farms", "commercial"];

    //defaults
    this.section = this.sections[sec];
    this.currency = "&pound;";

    this.minPrices = { "buy": [], "rent": [] };
    this.maxPrices = { "buy": [], "rent": [] };
    this.minBeds = { "buy": [], "rent": [] };

    this.sizes = { "ft": [], "m": [] };

    this.minPrices["buy"]["resi"] = [
    { "n": "Minimum", "v": 0 },
    { "n": this.currency + " 100,000", "v": 100000 },
    { "n": this.currency + " 200,000", "v": 200000 },
    { "n": this.currency + " 250,000", "v": 250000 },
    { "n": this.currency + " 300,000", "v": 300000 },
    { "n": this.currency + " 400,000", "v": 400000 },
    { "n": this.currency + " 500,000", "v": 500000 },
    { "n": this.currency + " 600,000", "v": 600000 },
    { "n": this.currency + " 750,000", "v": 750000 },
    { "n": this.currency + " 1,000,000", "v": 1000000 },
    { "n": this.currency + " 1,500,000", "v": 1500000 },
    { "n": this.currency + " 2,000,000", "v": 2000000 },
    { "n": this.currency + " 3,000,000", "v": 3000000 },
    { "n": this.currency + " 4,000,000", "v": 4000000}];

    this.minPrices["buy"]["newhomes"] = this.minPrices["buy"]["lonresi"] = this.minPrices["buy"]["resi"];

    this.maxPrices["buy"]["resi"] = [
    { "n": "Maximum", "v": 100000000 },
    { "n": this.currency + " 100,000", "v": 100000 },
    { "n": this.currency + " 200,000", "v": 200000 },
    { "n": this.currency + " 250,000", "v": 250000 },
    { "n": this.currency + " 300,000", "v": 300000 },
    { "n": this.currency + " 400,000", "v": 400000 },
    { "n": this.currency + " 500,000", "v": 500000 },
    { "n": this.currency + " 600,000", "v": 600000 },
    { "n": this.currency + " 750,000", "v": 750000 },
    { "n": this.currency + " 1,000,000", "v": 1000000 },
    { "n": this.currency + " 1,500,000", "v": 1500000 },
    { "n": this.currency + " 2,000,000", "v": 2000000 },
    { "n": this.currency + " 3,000,000", "v": 3000000 },
    { "n": this.currency + " 4,000,000", "v": 4000000 },
    { "n": this.currency + " 20,000,000+", "v": 50000000}];

    this.maxPrices["buy"]["newhomes"] = this.maxPrices["buy"]["lonresi"] = this.maxPrices["buy"]["resi"];

    this.minPrices["rent"]["resi"] = [
    { "n": "Minimum", "v": 0 },
    { "n": this.currency + " 200", "v": 200 },
    { "n": this.currency + " 250", "v": 250 },
    { "n": this.currency + " 300", "v": 300 },
    { "n": this.currency + " 400", "v": 400 },
    { "n": this.currency + " 500", "v": 500 },
    { "n": this.currency + " 600", "v": 600 },
    { "n": this.currency + " 750", "v": 750 },
    { "n": this.currency + " 1,000", "v": 1000 },
    { "n": this.currency + " 2,000", "v": 2000 },
    { "n": this.currency + " 3,000", "v": 3000 },
    { "n": this.currency + " 5,000", "v": 5000}];

    this.minPrices["rent"]["newhomes"] = this.minPrices["rent"]["lonresi"] = this.minPrices["rent"]["resi"];

    this.maxPrices["rent"]["resi"] = [
    { "n": "Maximum", "v": 100000 },
    { "n": this.currency + " 200", "v": 200 },
    { "n": this.currency + " 250", "v": 250 },
    { "n": this.currency + " 300", "v": 300 },
    { "n": this.currency + " 400", "v": 400 },
    { "n": this.currency + " 500", "v": 500 },
    { "n": this.currency + " 600", "v": 600 },
    { "n": this.currency + " 750", "v": 750 },
    { "n": this.currency + " 1,000", "v": 1000 },
    { "n": this.currency + " 2,000", "v": 2000 },
    { "n": this.currency + " 3,000", "v": 3000 },
    { "n": this.currency + " 5,000", "v": 5000 },
    { "n": this.currency + " 10,000", "v": 10000}];

    this.maxPrices["rent"]["newhomes"] = this.maxPrices["rent"]["lonresi"] = this.maxPrices["rent"]["resi"];

    this.minBeds["buy"]["resi"] = [
    { "n": "Studio+", "v": 0 },
    { "n": "1+", "v": 1 },
    { "n": "2+", "v": 2 },
    { "n": "3+", "v": 3 },
    { "n": "4+", "v": 4 },
    { "n": "5+", "v": 5 },
    { "n": "6+", "v": 6 },
    { "n": "7+", "v": 7 },
    { "n": "8+", "v": 8}];


    this.minBeds["buy"]["newhomes"] = this.minBeds["rent"]["newhomes"] =
    this.minBeds["buy"]["lonresi"] = this.minBeds["rent"]["lonresi"] =
        this.minBeds["rent"]["resi"] = this.minBeds["buy"]["resi"];

    this.sizes["ft"]["lonresi"] = [
    { "n": "Please Select", "v": 0 },
    { "n": "300+", "v": 300 },
    { "n": "750+", "v": 750 },
    { "n": "1000+", "v": 1000 },
    { "n": "1500+", "v": 1500 },
    { "n": "2000+", "v": 2000 },
    { "n": "3000+", "v": 3000 }
    ];

    this.sizes["m"]["lonresi"] = [
    { "n": "Please Select", "v": 0 },
    { "n": "30+", "v": 30 },
    { "n": "75+", "v": 75 },
    { "n": "100+", "v": 100 },
    { "n": "150+", "v": 150 },
    { "n": "200+", "v": 200 }
    ];


    //this.jsonGateway = "http://webdata.carterjonas.co.uk/propertysearchfx/flashservices/json.php/search_dev.SearchCJ";
    this.jsonGateway = "http://" + window.location.hostname + "/Webdata_proxy.aspx?s=";
    this.jsonGateway2 = "http://" + window.location.hostname + "/Webdata_proxy.aspx";


}

SearchSettings.prototype.Init = function(type)
{
    //query settings
    this.id = 0;

    this.priceMin = 0;
    this.priceMax = 0;
    this.bedsMin = 0;
    this.bedsMax = 0;
    this.propertyType = "";
    this.purchaseType = type ? type : "buy";
    
    this.searchType = "";
    this.criteria = "new,sld";
    this.areaType = "";
    this.mapBounds = null;
    this.searchCategory = 0;
    this.squareUnits = "ft";
    this.squareArea = 0;

    this.lastSearchMessage = "";

    this.areaType = "";
    this.locationId = 0;

    this.pg = "";

    this.locationData = [];

    this.defaultSearchTerm = "Enter up to 3 locations (use commas to separate)";
    this.searchTerm = this.defaultSearchTerm;
    
    this.mapping = {
        "q": "searchTerm",
        "pt": "purchaseType",
        "at": "areaType",
        "np": "priceMin",
        "xp": "priceMax",
        "nb": "bedsMin",
        "c": "criteriaIds",
        "id": "locationId",
        "pg": "pg",
        "sqa": "squareArea",
        "squ": "squareUnits",
        "sc": "searchCategory",
        "ldt": "locationData"
    }


}

SearchSettings.prototype.AddLocationData = function(searchTerm, id, at)
{
    
    if (searchTerm == undefined)
        return;

    searchTerm = this.FormatLocationName(searchTerm);

    this.locationData[searchTerm] = { "id": id, "at": at };
}

SearchSettings.prototype.LocationDataToString = function()
{
    var str = "";
    for (var i in this.locationData)
    {
        str += ":::" + i + "::" + this.locationData[i].id + "::" + this.locationData[i].at;
    }
    return str.replace(/^:::/, "");
}

SearchSettings.prototype.FormatLocationName = function(str)
{
    str = str.toLowerCase();
    str = str.replace(/,[ ]+$/, "");
    str = str.replace(/[ ]*$/, "");
    str = str.replace(/^[ ]*/, "");
    return str;
}

SearchSettings.prototype.LocationDataFromString = function(str)
{
    if (str.length < 1) return;

    var locations = str.split(":::");
    this.locationData = [];
    for (var i in locations)
    {
        var location = locations[i].split("::");
        location[0] = this.FormatLocationName(location[0]);
        this.locationData[location[0].toLowerCase()] = { "id": location[1], "at": location[2] };
    }
}

SearchSettings.prototype.CleanLocationData = function(searchTerms)
{
    var searchTerms_arr = searchTerms.replace(/,[ ]+$/, "").split(",");
    for (var i = 0; i < searchTerms_arr.length; i++)
    {
        searchTerms_arr[i] = this.FormatLocationName(searchTerms_arr[i]);

        if ((searchSettings.locationData[searchTerms_arr[i]] == undefined
            || searchSettings.locationData[searchTerms_arr[i]] == null)
            && searchTerms_arr[i].length > 0
            )
        {
            searchSettings.locationData[searchTerms_arr[i]] = { "id": "null", "at": "null" };
        }
    }

    for (var i in this.locationData)
    {
        if (jQuery.inArray(i, searchTerms_arr) < 0)
        {
            delete this.locationData[i];
        }
    }
}

SearchSettings.prototype.LoadFromQuery = function(str)
{
    //alert(str);
    mapping = this.mapping;

    if (str)
    {
        str = str.substr(str.indexOf("?") + 1);
        var pairs = str.split("&");
        var pair;
        for (var i in pairs)
        {
            pair = pairs[i].split("=");

            if (pair[0].length > 0)
            {
                if (mapping[pair[0]])
                {
                    this[mapping[pair[0]]] = pair[1];
                    /*if(pair[0] == "c") {
                    this[mapping[pair[0]]] = this[mapping[pair[0]]].split(",")
                    }*/
                }
            }
        }
    }
    //comes in as string, convert to obj
    this.LocationDataFromString(this.locationData);
}





function MapBounds(arr)
{
    this.lat = 0;
    this.lng = 0;
    this.latMin = 0;
    this.lngMin = 0;
    this.latMax = 0;
    this.lngMax = 0;

    if (arr)
    {
        for (var i in arr)
        {
            this[i] = arr[i];
        }
    }
}




function SearchForm(id)
{
    this.id = id;
    this.type = searchSettings.purchaseType;
    this.section = searchSettings.section;

    this.InitTabs();
    this.Init();

    this.InitSearchField();
    this.InitEventHandlers();    

    this.lastSearch = "";

    var obj = $("#" + this.id);
    obj.hide();
    obj.fadeIn(500);
}

SearchForm.prototype.ToJson = function() {
    var str = "";
    var val;
    for (name in searchSettings.mapping) {
        val = this.get(name);
        if (this.get(name) != "" && val != undefined) {
            if (name == "c") {
                val = '["' + val.split(",").join("\",\"") + '"]';
            } else {
                val = '"' + escape(val) + '"';
            }
            str += '"' + searchSettings.mapping[name] + '":' + val + ",";
        }
    }

    str += '"locationData":"' + searchSettings.LocationDataToString() + '"';
    //alert(str);
    return "{" + str.replace(/,$/, "") + "}";
}

SearchForm.prototype.Init = function()
{
    if (this.section == "resi" || this.section == "lonresi" || this.section == "newhomes")
    {
        this.FillSelect("np", searchSettings.minPrices[this.type][this.section], searchSettings.priceMin);
        this.FillSelect("xp", searchSettings.maxPrices[this.type][this.section], searchSettings.priceMax);
        this.FillSelect("nb", searchSettings.minBeds[this.type][this.section], searchSettings.bedsMin);

        this.set("pt", this.type);
        this.set("at", this.areaType);
        this.set("id", this.id);
        this.set("c", searchSettings.criteria);
        this.set("pg", searchSettings.pg);
        this.set("sc", 0);

        

        if (this.section == "lonresi")
        {
            this.set("squ", searchSettings.squareUnits);
            this.FillSelect("sqa", searchSettings.sizes[searchSettings.squareUnits][this.section], searchSettings.squareArea);
            this.set("sc", 2);
        }

        if (this.section == "farms")
        {
            if (searchSettings.pg == "")
            {
                this.set("pg", "map");
            }
        }

        if (searchSettings.searchTerm.length > 0)
        {
            this.set("q", searchSettings.searchTerm);
        }



        $("#" + this.id + " input[value='sld']").show();
        $("#" + this.id + " label[for='incSold']").show();

        if (this.type == "rent")
        {
            $("#" + this.id + " input[value='sld']").hide();
            $("#" + this.id + " label[for='incSold']").hide();
            $("#" + this.id + " input[value='sld']").attr("checked", "");
        }

        if (this.section == "newhomes")
        {
            $("#" + this.id + " input[value='sld']").hide();
            $("#" + this.id + " label[for='incSold']").hide();
            $("#" + this.id + " input[value='sld']").attr("checked", "checked");
            $("#" + this.id + " input[value='new']").hide();
            $("#" + this.id + " label[for='incNewHomes']").hide();
            $("#" + this.id + " input[value='new']").attr("checked", "checked");
            $("#" + this.id + " #purchaseTypeRent").hide();
            $("#" + this.id + " label[for='purchaseTypeRent']").hide();
            this.set("sc", 1);
        }
    }

}


SearchForm.prototype.InitEventHandlers = function()
{
    var formObj = this;
    $("#" + this.id + " input[name='searchSubmit']").bind("click", formObj, formObj.OnSubmit);

    $("#" + this.id + " input[name='searchSubmit']").hover(function()
    {
        //over
        if (searchSettings.section == "farms")
        {
            $(this).css("background-position", "0 -54px");
        }
        else if (searchSettings.section == "international")
        {
            $(this).css("background-position", "0 -54px");
        }
        else
        {
            $(this).css("background-position", "0 -43px");
        }
    },
    function()
    {
        //out
        if (searchSettings.section == "farms")
        {
            $(this).css("background-position", "0 0");
        }
        else if (searchSettings.section == "international")
        {
            $(this).css("background-position", "0 0");
        }
        else
        {
            $(this).css("background-position", "0 0");
        }
    });

    $("#" + this.id + " select, #" + this.id + " input[type='checkbox']").change(function()
    {
        formObj.UpdateMatches();
    });

    //    $("#" + formObj.id + " #q").blur(function()
    //    {
    //        formObj.UpdateMatches();
    //    });

    $("#" + this.id + " #q").keydown(function(event)
    {

        //alert(event.keyCode);
        switch (event.keyCode)
        {

            //enter, comma       
            case 13:
                //$("#" + formObj.id + " #q").blur();
                $(".ac_results").hide();
                formObj.UpdateMatches();
                break;
            //            case 188: 
            //                formObj.UpdateMatches(); 
            //                break; 
        }
    });


    $("#" + this.id + " input[name='pt']").click(function()
    {
        var q = formObj.get("q");
        var location = formObj.get("location");
        searchSettings.Init(formObj.get("pt"));
        formObj.type = $("#" + formObj.id + " input[name='pt']:checked").val();
        formObj.Init();
        formObj.set("q", q);
        if (q != searchSettings.defaultSearchTerm)
        {
            formObj.UpdateMatches();
        }
    });

    $("#" + this.id + " input[name='squ']").click(function()
    {
        searchSettings.squareUnits = $("#" + formObj.id + " input[name='squ']:checked").val();
        formObj.FillSelect("sqa", searchSettings.sizes[searchSettings.squareUnits][formObj.section]);
    });
}




SearchForm.prototype.SetMatches = function(num, loc)
{
    var obj = $("#" + this.id + " #searchNotice");
    if (num == null || num == undefined)
    {
        num = "";
    } else if (loc)
    {
        num = num.toString() + " propert" + (num != 1 ? "ies" : "y") + " found in and around " + loc;
    }
    obj.html(num);
}




SearchForm.prototype.InitSearchField = function()
{
    var defaultStr = searchSettings.defaultSearchTerm;
    var formObj = this;
    var q = $("#" + this.id + " #q");
    formObj.set("q", searchSettings.searchTerm);
    
    q.focus(function()
    {
        if (q.val() == defaultStr)
        {
            q.val("");
        }
    });
    q.blur(function()
    {
        if (q.val() == "")
        {
            q.val(defaultStr);
        }
        formObj.UpdateMatches();
    });

    //q.keyup(this.SetUpdateInterval);

    q.autocomplete(searchSettings.jsonGateway2, {
        max: 30,
        delay: 150,
        cacheLength: 1,
        minChars: 2,
        extraParams: { sc: searchSettings.section },
        multiple: true,
        multipleSeparator: ", ",
        matchSubset: false,
        scrollHeight: 110,
        selectFirst: false
    });

    

    q.result(function(event, data, formatted)
    {
        //alert(!data ? "No match!" : "Selected: " + formatted + " " + data);
        if (!data)
        {
            formObj.SetMatches("Error, please try again.");
        }
        else
        {
            var term = formObj.get("q");
            term = term.replace(/,[ ]*$/, "");
            formObj.set("q", term);

            //alert(term);

            if (data.toString().indexOf(":::") >= 0)
            {
                formObj.SetMatches("Searching...");
                //$("#" + formObj.id + " #q").attr('disabled',true);
                var location = data.toString().split(","); //name|id:::areaType
                formObj.set("location", location);
                formObj.UpdateMatches();
            }
        }

        q.flushCache();
    });
    this.SetMatches();
}




SearchForm.prototype.get = function(id)
{

    if (id == "pt" || id == "squ")
    {
        return $("#" + this.id + " input[name='" + id + "']:checked").val();
    } else if (id == "c")
    {

        var cv = [];
        $("#" + this.id + " input[name='" + id + "']:checked").each(function()
        {
            cv.push($(this).val());
        });
        return cv.join(",");
    } else if (id == "location")
    {
        return this.get("id") + ":::" + this.get("at");
    } else
    {
        return $("#" + this.id + " #" + id).val();
    }
}





SearchForm.prototype.set = function(id, val)
{

    if (id == "pt" || id == "squ")
    {
        $("#" + this.id + " input[name='" + id + "']:checked").attr("checked", "");
        $("#" + this.id + " input[value='" + val + "']").attr("checked", "checked");
    } else if (id == "c")
    {
        var cv = val.split(",");
        $("#" + this.id + " input[name='" + id + "']").attr("checked", "");
        for (var i in cv)
        {
            $("#" + this.id + " input[value='" + cv[i] + "']").attr("checked", "checked");
        }
    } else if (id == "location")
    {
        var location = ["", ""];

        var name = "";
        if (val != "")
        {
            name = val[0];
            val = val[val.length - 1];
        }

        if (val.length > 1)
        {
            location = val.split(":::");
        }
        this.set("id", location[0]);
        this.set("at", location[1]);
        if (name != undefined && name.length > 0)
        {
            searchSettings.AddLocationData(name, this.get("id"), this.get("at"));
        }
        searchSettings.CleanLocationData(this.get("q"));
    } 
    else
    {
        $("#" + this.id + " #" + id).val(unescape(val));
    }
}

SearchForm.prototype.UpdateMatches = function()
{
    var term = this.get("q");
    var formObj = this;

    $("#" + this.id + " #q").flushCache();

    if (term == searchSettings.defaultSearchTerm)
    {
        return;
    }

    searchSettings.CleanLocationData(this.get("q"));

    if (this.lastSearch == this.ToJson())
    {
        formObj.SetMatches(searchSettings.lastSearchMessage, term.replace(/,[ ]*$/, ""));
        return;
    }

    this.lastSearch = this.ToJson();
    var jsonUrl = searchSettings.jsonGateway + 'getResultsIndicator/' + this.lastSearch;

    xhr = $.ajax({
        type: "GET",
        url: jsonUrl,
        async: false,
        dataType: "json",
        beforeSend: function()
        {
            formObj.SetMatches("");
            if (xhr != null)
                xhr.abort();
        },
        success: function(content)
        {
            //alert("success: " + (content.results != null ? content.results : 0)); //content.name + " " +
            term = term.replace(/, $/, "");
            searchSettings.lastSearchMessage = parseInt(content.results != null ? content.results : 0);
            formObj.SetMatches(searchSettings.lastSearchMessage, term);
        },
        complete: function(xhr, status)
        {
            //alert("complete");
            //$("#" + formObj.id + " #q").attr("disabled", false);
        },
        error: function(xhr, desc, exceptionobj)
        {
            formObj.SetMatches("Error, please refresh the page and try again.");
            alert("error " + desc);
        }
    });


}



SearchForm.prototype.FillSelect = function(selectId, data, selected)
{
    var options = '';
    var obj = $("#" + this.id + " #" + selectId);

    if (obj.length)
    {
        for (var i in data)
        {
            if (data[i].v == selected)
            {
                selected = i;
            }
            options += '<option value="' + data[i].v + '">' + data[i].n + '</option>';
        }
        obj.html(options);

        if (selected == null || selected == undefined)
        {
            selected = 0;
        }
        $("#" + this.id + " #" + selectId + " option:eq(" + selected + ")").attr("selected", "selected");
    }

}



SearchForm.prototype.OnSubmit = function(e)
{
    var obj = e.data;

    var sectionLong = "residential";

    inputs = $("#" + obj.id + " input, #" + obj.id + " select");
    searchReq = {};

    for (i = 0; i < inputs.length; i++)
    {
        el = $("#" + inputs[i].id);
        if (el.attr("type") == "text" ||
            el.attr("type") == "hidden" ||
            el.attr("type") == "checkbox" ||
            el.attr("type") == "radio" ||
            el.get(0).tagName.toLowerCase() == "select"
        )
        {
            searchReq[el.attr("name")] = obj.get(el.attr("name"));
        }
    }
    //"resi", "lonresi", "farms", "commercial"
    switch (obj.section)
    {
        case "lonresi":
            sectionLong = "london-residential";
            break;
        case "farms":
            sectionLong = "farms-land-and-estates";
            break;
        case "newhomes":
            sectionLong = "new-homes";
            break;
    }


    str = "/" + obj.type + "/" + sectionLong + "/search.aspx?" + ObjToQueryString(searchReq);
    str += "&ldt=" + searchSettings.LocationDataToString();
    //alert(str);


    if (obj.section == "international")
    {
        str = "/our-services/residential-agency/international/available-properties.aspx";
    }

    if (obj.section == "commercial")
    {
        str = "http://commercial.carterjonas.co.uk/";
    }

    if (obj.section == "farms")
    {
        str = "/" + obj.type + "/farms-land-and-estates/search.aspx?pg=map&q=farmsAndEstates&pt=" + obj.type + "&np=0&xp=0&nb=0&st=0&sc=4&sqa=0";
    }

    if (searchSettings.LocationDataToString().length > 1
        || obj.section == "international"
        || obj.section == "commercial"
        || obj.section == "farms"
    )
    {
        window.location = str;
    }
    return false;
}




SearchForm.prototype.updateBoundary = function(lat, lng, latMin, latMax, lngMin, lngMax, zoom)
{
    var obj;
    var str;
    var arr;
    $(".list, .table, .gallery").each(function()
    {
        str = $(this).attr("href");
        if (str.indexOf("?") >= 0)
        {
            str = str.substring(1);
            obj = QueryStringToObj(str);
            obj["lat"] = roundNumber(lat, 5);
            obj["lng"] = roundNumber(lng, 5);
            obj["latMin"] = roundNumber(latMin, 5);
            obj["latMax"] = roundNumber(latMax, 5);
            obj["lngMin"] = roundNumber(lngMin, 5);
            obj["lngMax"] = roundNumber(lngMax, 5);
            obj["st"] = 1;
            obj["z"] = zoom;
            str = ObjToQueryString(obj);
            $(this).attr("href", "?" + str.replace(/^&/, ""));
        }
    });
}




SearchForm.prototype.InitTabs = function()
{
    $("#psearch_tabs li").removeClass("active");
    var idLong = "pstab_";

    switch (this.section)
    {
        case "lonresi":
            idLong += "london";
            break;
        case "farms":
            idLong += "farms";
            break;
        case "international":
            idLong += "international";
            break;
        case "newhomes":
            idLong += "newhomes";
            break;
        case "commercial":
            idLong += "commercial";
            break;
        default:
            idLong += "residential";
            break;

    }

    $("#psearch_tabs #" + idLong).addClass("active");
}

function ObjToQueryString(obj)
{
    var str = "";
    for (var i in obj)
    {
        str += "&" + i + "=" + obj[i];
    }
    if (str.length > 1)
    {
        str = str.substr(1);
    }
    return str;
}

function QueryStringToObj(str)
{
    var obj = {};
    if (str)
    {
        str = str.substr(str.indexOf("?") + 1);
        var pairs = str.split("&");
        var pair;
        for (var i in pairs)
        {
            pair = pairs[i].split("=");

            if (pair[0].length > 0)
            {
                if (mapping[pair[0]])
                {
                    obj[pair[0]] = pair[1];
                }
            }
        }
    }
    return obj;
}


function SmoothImageLoad()
{
    //galleryHolder 
    $(".searchResultlisiting img").hide();
    $(".searchResultlisiting img").load(function()
    {
        $(".searchResultlisiting img").fadeIn(500);
    });

    $(".galleryHolder img").hide();
    $(".galleryHolder img").load(function()
    {
        $(".galleryHolder img").fadeIn(500);
    });
}


////////////////////////////////////////////////////


function toggleHelp()
{
    $(".searchHelp").toggle("fast");
}

function launchWin(url, w, h)
{
    window.open(url, 'popup_win', 'width='+w+',height='+h+',toolbar=0,status=0,toolbar=0,resizable=1,scrollbars=1');
}

function roundNumber(num, dec)
{
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    return result;
}


////////////////////////////////////////////////////

var xhr;

var searchSettings = new SearchSettings(purchaseType, scID);
searchSettings.LoadFromQuery(window.location.search);
var searchForm;

var home_bg_src = [
'/read-my-story/pop-up6.html',
'/read-my-story/pop-up4.html',
'/read-my-story/pop-up3.html',
'/read-my-story/pop-up5.html',
'/read-my-story/pop-up1.html', 
'/read-my-story/pop-up2.html'];

$(document).ready(function()
{
    searchForm = new SearchForm("psearch_form");
    //SmoothImageLoad();
    //if not on homepage...
    if (scLevel > 0)
    {
        $(".flash_margin").attr("id", "");
        $("#top_bg_img").hide();
    } 
    else
    {
        $("#top_bg_img").hide();
        $("#psearch_box").removeClass("psearch_top");
        $("#psearch_box").addClass("psearch_home");
        $("#psearch_box").css("position", "absolute");
        $("#psearch_box").css("bottom", "25px");
        $("#psearch_box").css("left", "0");
        var bg_rand = Math.floor(Math.random() * 6)
        $("#top_bg_img").attr('src', "/images/home_bg/home_bg_" +
            (bg_rand + 1) + ".jpg");
        $("#top_bg_link").attr("href", "javascript:void(launchWin('" + home_bg_src[bg_rand] + "', '" + 750 + "', '" + 780 + "'))");
        $("#top_bg_img").show();
    }



});