$(document).ready(function() {
    
});
var geostorm_page_restart = false;
function GeoDistrictSpecify(refresh) {  
    $.fn.ecpopup({
		url : "/geostorm/change_city/",
		small : true
	});
	if (typeof refresh != "undefined") {
		geostorm_page_restart = true;
	}
	return false;
}

function GeoCitySpecify() {
    $.fn.ecpopup({
        url : "/geostorm/change_city/?step=2&district="+$("#geostorm_districts").val(),
        small : true
    });
    return false;
}

function GeoSetCity() {
	id = $("#geostorm_cities").val();
	$.getJSON("/geostorm/set_city/", {"city" : id}, function (city) {
		
        $("#geoblock").html(city.block);
		
		if (geostorm_page_restart) {
			url = window.location.href.split("#")
			window.location.href = url[0];
		 } else {
		 	$.fn.ecpopup.close();
		 }
	});
    return false;
}

function _geoCitySpecify(cities, refresh) {

    list = $("<select>").attr("name", "city");
    for (i in cities) {
        $(list).append($("<option>").attr("value", cities[i].id).text(cities[i].title));
    }
    cmessage = "<h2>Выберите Ваш город</h2> "+$("<div>").append($("<label>").text("Город:")).append(list).html()+"<h3><a href='/geoip/add_city/?redirect="+window.location.href+"'>В списке нет моего города</a></h3>";
    $.prompt(cmessage, {
        buttons: { "Вернуться к списку регионов" : false, "Выбрать" : true },
        submit : function(v, m, f) {
            if (v) {
                if (f.city) {
                    id = f.city;
                } else {
                    id = $("select option:selected", m).val();
                }
                city = _getCityByID(id);
                message = "Ваш город: <b>"+city.title+"</b> / Доставка: <b>"+_getPriceForZone(city.zone, city.capital)+"</b> руб. <small>(<a href='javascript:void();' onclick='GeoDistrictSpecify();'>это не мой город</a>)</small>";
                $("#geoip").html(message);
                $.getJSON("/geostorm/set_city/", {"city" : id}, function (json) {
                	if (typeof refresh == "undefined") {
		            	$.prompt.close();
		            } else {
		        		url = window.location.href.split("#")
		            	window.location.href = url[0];
		            }
		        });
            } else {
                $.prompt.close();
                GeoDistrictSpecify();
                return false;
            }
        }
    });
}

function _getCityByID(id) {
    for (i in geocities) {
        if (geocities[i].id == id) {
            return geocities[i];
        }
    }
}

function _getGeoDistrictCities(district) {
    results = []
    for (i in geocities) {
        if (geocities[i].district == district) {
            results[results.length] = geocities[i];
        }
    }
    return results;
}

function GeoAddCity(step) {
	if (step == 1) {
		$("#geostorm_cities_block").hide();
		$("#geostorm_add_city_block").show();
	}
	else {
		title = $("#add_city_title").val();
		district = $("#add_city_district").val();
		if (!title) {
			$("#add_city_title").css("border", "1px solid #ff0000");
			return;
		}
		data = {
			"district": district,
			"title": title
		}
		$.getJSON("/geostorm/add_city/", data, function(city){
            $("#geoblock").html(city.block);
			 
            if (geostorm_page_restart) {
                url = window.location.href.split("#")
                window.location.href = url[0];
            } else {
                $.fn.ecpopup.close();
            }
        });
        return false;
	}
}

function _getPriceForZone(zone, is_capital) {
    if (zone == 0) {
        if (is_capital) {
            return 300;
        } else {
            return 500;
        }
    }
    if (zone == 1) {
        if (is_capital) {
            return 500;
        } else {
            return 550;
        }
    }
    if (zone == 2) {
        if (is_capital) {
            return 550;
        } else {
            return 620;
        }
    }
    if (zone == 3) {
        if (is_capital) {
            return 580;
        } else {
            return 680;
        }
    }
    if (zone == 4) {
        if (is_capital) {
            return 650;
        } else {
            return 750;
        }
    }
    if (zone == 5) {
        if (is_capital) {
            return 720;
        } else {
            return 850;
        }
    }
}
