﻿var jLoading = [{ value: "none", text: "загрузка"}];
var none = "none";
// Region->Country->City channel
var bRCCch = false;
// RouteType->Company->Ship channel
var bTCSch = false;
var parameters = {};

function getURLParam(name, bChannelInited) {
    var value = jQuery.url.param(name);
    var u_value = parameters[name];

    return (bChannelInited) ? none : ((value != null) ? value : ((u_value != null) ? u_value : none));
};

function showWorld() {
    try {
        window.document.FlashMap.setWorldMode();
    }
    catch (err) {
    };
};

function getFilterJSON(correction) {
    var filter = {
        CTypeID: $("#ddCType").linkselect("val"),
        CompanyID: $("#ddCompany").linkselect("val"),
        ShipID: $("#ddShip").linkselect("val"),
        RegionID: $("#ddRegion").linkselect("val"),
        CountryID: $("#ddCountry").linkselect("val"),
        CityID: $("#ddCity").linkselect("val"),
        DCityID: $("#ddDCity").linkselect("val"),
        IsBox: $("#ddBox").linkselect("val"),
        IsNoVisa: $("#ddNoVisa").linkselect("val"),
        IsRus: $("#ddRus").linkselect("val"),
        From: $("#ddFrom").linkselect("val"),
        PriceRange: $("#ddPriceRange").linkselect("val"),
        Duration: $("#ddDuration").linkselect("val")
    };

    if ($("#ddSortBy").length > 0) {
        $.extend(filter, { sb: $("#ddSortBy").linkselect("val") });
    };
    if ($("#sldrDate .slider").length > 0) {
        $.extend(filter, { dc: $("#sldrDate .slider").slider("values", 0) + '_' + $("#sldrDate .slider").slider("values", 1) });
    };

    $.extend(filter, correction);
    return filter;
};

function countOffers(correction) {
    $.getJSON("/Data/Json/CountOffers.ashx", getFilterJSON(correction), function(json) {
        $("#fltResult").text(json.count);
    });
};

function submit(correction) {
    var qs = jQuery.param(getFilterJSON(correction));
    window.location.search = qs;
    //window.location.replace("/offers/?" + qs);
}

$(document).ready(function() {
    $("#btnSubmit").click(function() {
        submit({ act: "scroll" });
        return false;
    });
    // Region->Country->City in reverse order
    $("#ddDCity").linkselect({
        change: function(li, value, text) {
            if (!bRCCch) bRCCch = true;
            countOffers({ DCityID: value });
        }
    });
    $("#ddCity").linkselect({
        change: function(li, value, text) {
            if ((swfMap) && (bRCCch)) {
                if (value != none) swfMap.setCityMode(value);
            }
            $("#ddDCity").linkselect("replaceOptions", jLoading, false);
            $.getJSON("/Data/Json/DCitiesByGeo.ashx", { RegionID: $("#ddRegion").linkselect("val"), CountryID: $("#ddCountry").linkselect("val"), CityID: value, DCityID: getURLParam("DCityID", bRCCch) }, function(json) {
                $("#ddDCity").linkselect("replaceOptions", json);
            });
            //countOffers({ CityID: value });
        }
    });
    $("#ddCountry").linkselect({
        fixedWidth: true,
        change: function(li, value, text) {
            if ((swfMap) && (bRCCch)) {
                if (value != none) swfMap.setCountryMode(value);
            }
            $("#ddCity").linkselect("replaceOptions", jLoading, false);
            $.getJSON("/Data/Json/CitiesByCountry.ashx", { CountryID: value, CityID: getURLParam("CityID", bRCCch) }, function(json) {
                $("#ddCity").linkselect("replaceOptions", json);
            });
            $("#ddCity").linkselect("disable", (value == "none"));
        }
    });
    $("#ddRegion").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/Regions.ashx", { RegionID: getURLParam("RegionID", bRCCch) }, function(json) {
                $("#ddRegion").linkselect("replaceOptions", json);
            });
        },
        change: function(li, value, text) {
            if ((swfMap) && (bRCCch)) {
                (value != none) ? swfMap.setRegionMode(value) : swfMap.setWorldMode()
            }
            $("#ddCountry").linkselect("replaceOptions", jLoading, false);
            $.getJSON("/Data/Json/CountriesByRegion.ashx", { RegionID: value, CountryID: getURLParam("CountryID", bRCCch) }, function(json) {
                $("#ddCountry").linkselect("replaceOptions", json);
            });
            $("#ddCountry").linkselect("disable", (value == "none"));
        }
    });
    // RouteType->Company->Ship in reverse order
    $("#ddShip").linkselect({
        change: function(li, value, text) {
            if (!bTCSch) bTCSch = true;
            countOffers({ ShipID: value });
        }
    });
    $("#ddCompany").linkselect({
        change: function(li, value, text) {
            $("#ddShip").linkselect("replaceOptions", jLoading, false);
            $.getJSON("/Data/Json/ShipsByCompany.ashx", { CompanyID: value, ShipID: getURLParam("ShipID", bTCSch) }, function(json) {
                $("#ddShip").linkselect("replaceOptions", json);
            });
            $("#ddShip").linkselect("disable", (value == "none"));
        }
    });
    $("#ddCType").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/CompanyTypes.ashx", { CTypeID: getURLParam("CTypeID", bTCSch) }, function(json) {
                $("#ddCType").linkselect("replaceOptions", json);
            });
        },
        change: function(li, value, text) {
            $("#ddCompany").linkselect("replaceOptions", jLoading, false);
            $.getJSON("/Data/Json/Companies.ashx", { CTypeID: value, CompanyID: getURLParam("CompanyID", bTCSch) }, function(json) {
                $("#ddCompany").linkselect("replaceOptions", json);
            });
        }
    });
    // Bool flags
    $("#ddBox").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/YesNo.ashx", { Type: getURLParam("IsBox", false) }, function(json) {
                $("#ddBox").linkselect("replaceOptions", json);
            });
        },
        change: function(li, value, text) {
            countOffers({ IsBox: value });
        }
    });
    $("#ddNoVisa").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/YesNo.ashx", { Type: getURLParam("IsNoVisa", false) }, function(json) {
                $("#ddNoVisa").linkselect("replaceOptions", json);
            });
        },
        change: function(li, value, text) {
            countOffers({ IsNoVisa: value });
        }
    });
    $("#ddRus").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/YesNo.ashx", { Type: getURLParam("IsRus", false) }, function(json) {
                $("#ddRus").linkselect("replaceOptions", json);
            });
        },
        change: function(li, value, text) {
            countOffers({ IsRus: value });
        }
    });
    // From
    $("#ddFrom").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/Months.ashx", { From: getURLParam("From", false) }, function(json) {
                $("#ddFrom").linkselect("replaceOptions", json);
            });
        },
        change: function(li, value, text) {
            countOffers({ From: value });
        }
    });
    // PriceRange
    $("#ddPriceRange").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/PriceRanges.ashx", { PriceRange: getURLParam("PriceRange", false) }, function(json) {
                $("#ddPriceRange").linkselect("replaceOptions", json);
            });
        },
        change: function(li, value, text) {
            countOffers({ PriceRange: value });
        }
    });
    // Duration
    $("#ddDuration").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/RouteDurations.ashx", { Duration: getURLParam("Duration", false) }, function(json) {
                $("#ddDuration").linkselect("replaceOptions", json);
            });
        },
        change: function(li, value, text) {
            countOffers({ Duration: value });
        }
    });
    // Sorting
    $("#ddSortBy").linkselect({
        init: function() {
            this.replaceOptions(jLoading, false);
            $.getJSON("/Data/Json/OfferSortBy.ashx", { sb: getURLParam("sb", false) }, function(json) {
                $("#ddSortBy").linkselect("replaceOptions", json, false);
            });
        },
        change: function(li, value, text) {
            submit({ sb: value });
        }
    });
});

