﻿
$(document).ready(function() {

    $("html").click(function() {

        if ($('input[sid="search"]').val() == "") {
            $('input[sid="search"]').val("Поиск сервисного центра по названию");
        }


    });

    $("#btn_all_cities").click(function(event) {

        event.stopPropagation();
        $("#float_cities").toggle();

        $(":not(#float_cities, #float_cities *)").one("click", function(e) {

            if ($(this).attr("id") == "btn_all_cities") {

                return;

            }
            if ($(this).attr("id") == "aspnetForm" || $(this).attr("id") == "") { return; }
            else {
                e.preventDefault();
                $("#float_cities").hide();
            }
        });

    });



    $("#show_all_cities").click(function(event) {

        event.stopPropagation();
        event.preventDefault();

        $("#float_all_cities").toggle();
        $("#float_cities").toggle();
        $(":not(#float_all_cities, #float_all_cities *)").one("click", function(e) {

            if ($(this).attr("id") == "show_all_cities") {

                return;

            }
            if ($(this).attr("id") == "aspnetForm" || $(this).attr("id") == "") { return; }
            else {

                $("#float_all_cities").hide();
            }
        });

    });


    $("#close_cities").click(function(event) {

        event.preventDefault();
        $("#float_cities").toggle();

    });


    $("#close_all_cities").click(function(event) {

        event.preventDefault();
        $("#float_all_cities").toggle();

    });

    $(".region").click(function(event) {

        var x = {};
        $("#cities_in_reg").html("");
        $("#img_preloader").show();


        x.id = $(this).attr("rId");

        var url = "/ajax.aspx/GetCities";


        var f = function(msg) {
            if (msg.d != null) {



                $(msg.d).each(function() {

                    if (this.name == undefined) {
                        $("#cities_in_reg").append('<li><span>Сервисные центры в данной области отсутствуют</span></li>');
                    }
                    else {
                        $("#cities_in_reg").append('<li><a class="city" uName="' + this.url + '" href="#">' + this.name + '</a></li>');
                    }

                    $("#img_preloader").hide();

                });

            }
        };

        Ajax(url, x, f);
        return false;
    });


    $(".city").live("click", function(event) {

        event.stopPropagation();
        event.preventDefault();

        urlName = $(this).attr("uName");

        var path = window.location.pathname;
        var pathArray = path.split('/');
        var pathname = "";
        var newUrl = "";

        if (pathArray.length == 4) {
            if (pathArray[2] == "manuf")
                pathname = urlName + '/manuf/' + pathArray[3];
            else
            pathname = urlName + '/' + pathArray[3];
        }
        if (pathArray.length == 1 || pathArray.length == 2) {
            pathname = urlName + ".aspx";
        }
        if (pathArray.length == 3) {
            pathname = urlName + '/' + pathArray[2];
        }

        if (pathname == "kiev.aspx") {

            newUrl = "/";

        }
        else {
            newUrl = "/" + pathname;
        }

        $.cookie("city", urlName, {
            expires: 100,
            path: "/"
        });

        window.location.href = newUrl;

    });

    $('input[sid="search"]').click(function(event) {

        event.stopPropagation();
        $('input[sid="search"]').val("");

    });

}); 
    

