﻿$(document).ready(function () {

	var searchForm = $(".local-search-form-wrapper");

	var countySelector = searchForm.find(".countySelector");
	var placeSelector = searchForm.find(".placeSelector");
	var placePicker = searchForm.find(".placePicker");
	var placeId = searchForm.find(".placePickerRegionId");

	var checkSingle = searchForm.find("span.single > input[type='checkbox']");
	var checkDouble = searchForm.find("span.double > input[type='checkbox']");
	var isRoomTypeSelectedField = searchForm.find(".isRoomTypeSelected");

	var isDouble = checkDouble.is(":checked");

	checkSingle.click(function () {

		if (checkSingle.is(":checked")) checkDouble.attr("checked", false);
		else {
			checkSingle.attr("checked", true);
			checkDouble.attr("checked", false);
		}
		isDouble = checkDouble.is(":checked");
		updatePrices();

	});

	checkDouble.click(function () {

		if (checkDouble.is(":checked")) checkSingle.attr("checked", false);
		else {
			checkSingle.attr("checked", false);
			checkDouble.attr("checked", true);
		}
		isDouble = checkDouble.is(":checked");
		updatePrices();

	});

	countySelector.change(function () {

		$.ajax({
			type: "GET",
			processData: true,
			url: "/Handlers/GetRegionList.ashx",
			data: ({ parent: countySelector.val(), includeCity: true, includeDistrict: true }),
			dataType: "text",
			success: function (data) {
				placeSelector.clearOptions(true);
				if (data != "NULL") {
					var items = eval(data);
					placeSelector.populateDropdown(items);
				}
			}
		});

	});

	placeSelector.change(function () {
		placeId.val(placeSelector.val());
	});

	if (placePicker.length > 0) {
		placePicker.autocomplete({
			autoFocus: true,
			source: "/Handlers/LocalRegionSearch.ashx",
			minLength: 2,
			select: function (event, ui) {
				placeId.val(ui.item.id);
			}
		});
	}


	var sliderElement = searchForm.find("#price-range-selector");
	var priceRangeRow = searchForm.find(".priceRangeWrapper");
	var facilitiesRow = searchForm.find(".facilitiesWrapper");

	var priceRangeRowTrigger = searchForm.find("a.priceRangeRowTrigger");
	var facilitiesRowTrigger = searchForm.find("a.facilitiesRowTrigger");

	var lblPriceRangeTitle = priceRangeRow.find(".price-range-title");
	var lblPriceRangeFrom = priceRangeRow.find(".price-range-display-from");
	var lblPriceRangeTo = priceRangeRow.find(".price-range-display-to");

	var txtPriceRangeFrom = priceRangeRow.find(".price-range-from");
	var txtPriceRangeTo = priceRangeRow.find(".price-range-to");
	var txtPriceRangeMinSingle = priceRangeRow.find(".price-range-min-single");
	var txtPriceRangeMaxSingle = priceRangeRow.find(".price-range-max-single");
	var txtPriceRangeMinDouble = priceRangeRow.find(".price-range-min-double");
	var txtPriceRangeMaxDouble = priceRangeRow.find(".price-range-max-double");

	var clearPriceRange = priceRangeRow.find("#clearPriceRange");
	var clearFacilities = facilitiesRow.find("#clearFacilities");

	var priceFormat = priceRangeRow.find(".price-range-format").val();

	var priceFrom = parseInt(txtPriceRangeFrom.val());
	var priceTo = parseInt(txtPriceRangeTo.val());

	var minRangeSingle = parseInt(txtPriceRangeMinSingle.val());
	var maxRangeSingle = parseInt(txtPriceRangeMaxSingle.val());
	var minRangeDouble = parseInt(txtPriceRangeMinDouble.val());
	var maxRangeDouble = parseInt(txtPriceRangeMaxDouble.val());

	//if (priceTo == 0) priceTo = maxRange;

	priceRangeRowTrigger.click(function () {

		if (priceRangeRowTrigger.hasClass("plus-active")) {
			priceRangeRowTrigger.removeClass("plus-active");

			priceRangeRow.slideUp();

			if (clearPriceRange.length > 0) clearPriceRange.click();

			isRoomTypeSelectedField.val("");
		}
		else {
			priceRangeRowTrigger.addClass("plus-active");

			priceRangeRow.slideDown(undefined, undefined, function () {
				// IE 7 fix
				setTimeout(function () {
					sliderElement.css('position', 'absolute');
					sliderElement.css('position', 'relative');
				}, 1);
			});

			isRoomTypeSelectedField.val("1");

		}

	});

	facilitiesRowTrigger.click(function () {

		if (facilitiesRowTrigger.hasClass("plus-active")) {
			facilitiesRowTrigger.removeClass("plus-active");

			facilitiesRow.slideUp();

			if (clearFacilities.length > 0) clearFacilities.click();
		}
		else {
			facilitiesRowTrigger.addClass("plus-active");

			facilitiesRow.slideDown();
		}

	});

	if (sliderElement.length > 0) {

		initSlider(priceFrom, priceTo, isDouble ? minRangeDouble : minRangeSingle, isDouble ? maxRangeDouble : maxRangeSingle);

		lblPriceRangeFrom.html(priceFormat.replace("{price}", priceFrom));
		lblPriceRangeTo.html(priceFormat.replace("{price}", priceTo));

		clearPriceRange.click(function () {

			priceFrom = isDouble ? minRangeDouble : minRangeSingle;
			priceTo = isDouble ? maxRangeDouble : maxRangeSingle;

			initSlider(priceFrom, priceTo, priceFrom, priceTo);

			lblPriceRangeFrom.html(priceFormat.replace("{price}", priceFrom));
			lblPriceRangeTo.html(priceFormat.replace("{price}", priceTo));
			txtPriceRangeFrom.val(priceFrom);
			txtPriceRangeTo.val(priceTo);

			priceRangeRow.find("input:checked").not(checkDouble).attr('checked', false);

		});

		clearFacilities.click(function () {

			facilitiesRow.find("input:checked").attr('checked', false);

		});

	}

	function updatePrices() {

		priceFrom = isDouble ? minRangeDouble : minRangeSingle;
		priceTo = isDouble ? maxRangeDouble : maxRangeSingle;

		initSlider(priceFrom, priceTo, priceFrom, priceTo);

		lblPriceRangeFrom.html(priceFormat.replace("{price}", priceFrom));
		lblPriceRangeTo.html(priceFormat.replace("{price}", priceTo));
		txtPriceRangeFrom.val(priceFrom);
		txtPriceRangeTo.val(priceTo);

	}

	function initSlider(from, to, min, max) {

		sliderElement.slider({
			range: true,
			min: min,
			max: max,
			values: [from, to],
			slide: function (event, ui) {
				lblPriceRangeFrom.html(priceFormat.replace("{price}", ui.values[0]));
				lblPriceRangeTo.html(priceFormat.replace("{price}", ui.values[1]));
				txtPriceRangeFrom.val(ui.values[0]);
				txtPriceRangeTo.val(ui.values[1]);
			}
		});

	}

});
