$(document).ready(function() {

    // list of possible (sorted) price values
    var listValues = [0, 50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000, 250000, 275000, 300000, 325000, 350000, 375000, 400000, 450000, 500000, 550000, 600000, 650000, 700000, 750000, 800000, 900000, 1000000, 1250000, 1500000, 2000000];

    // min inf and max inf constants
    var MAX_PRICE = 'Geen maximum', MIN_PRICE = 'Geen minimum';

    // grouping digits using period
    var formatNumber = function(val) {
        val += '';
        x = val.split(',');
        x1 = x[0];
        x2 = x.length > 1 ? ',' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + '.' + '$2');
        }
        return x1 + x2;
    }

    // convert a string value into a number (or min/max const)
    var parseNumber = function(str) {
        if (str == MAX_PRICE || str == MIN_PRICE) {
            return str;
        } else {
            var clean = (str + '').replace(/[^0-9]/g, '');
            return parseInt(clean);
        }
    }

    // get the next greater price value based on the given price
    // if the given value is from the list of pre-defined values, get the next greater from the list
    // otherwise, increase the given value for 1000
    var getNextGreaterVal = function(val) {

        for (var i = 0; i < listValues.length; i++) {
            if (val == listValues[i]) {
                if (i == (listValues.length - 1)) {
                    return MAX_PRICE;
                } else {
                    return listValues[i + 1];
                }
            }
        }

        return val + 1000;
    }

    // get the next smaller price value based on the given price
    // if the given value is from the list of pre-defined values, get the next smaller from the list
    // otherwise, decrease the given value for 1000
    var getNextSmallerVal = function(val) {

        for (var i = 0; i < listValues.length; i++) {
            if (val == listValues[i]) {
                if (i == 0) {
                    return MIN_PRICE;
                } else {
                    return listValues[i - 1];
                }
            }
        }

        return (val >= 1000) ? (val - 1000) : 0;
    }

    // based on the given min price, check if the max price should be corrected
    var checkMaxPrice = function(minPrice) {
        var minPrice = parseNumber(minPrice);
        var maxPrice = parseNumber(maxPriceCombo.getTextValue());
        if (minPrice != MIN_PRICE && maxPrice != MAX_PRICE && minPrice >= maxPrice) {
            var newMaxPrice = getNextGreaterVal(minPrice);
            maxPriceCombo.input.val(formatNumber(newMaxPrice));

            return formatNumber(newMaxPrice);
        }
    }

    // based on the given max price, check if the min price should be corrected
    var checkMinPrice = function(maxPrice) {
        var maxPrice = parseNumber(maxPrice);
        var minPrice = parseNumber(minPriceCombo.getTextValue());
        if (minPrice != MIN_PRICE && maxPrice != MAX_PRICE && maxPrice <= minPrice) {
            var newMinPrice = getNextSmallerVal(maxPrice);
            minPriceCombo.input.val(formatNumber(newMinPrice));

            return formatNumber(newMinPrice);
        }
    }

    var minPriceCombo, maxPriceCombo;

	function check(combo) {
	   var checkMinMax = true;
		var currentVal = combo.getTextValue();

		if (currentVal != '' && currentVal != MIN_PRICE && currentVal != MAX_PRICE) {
			var newVal = currentVal.replace(/[^0-9\.]/g, '');
			combo.input.val(newVal.substr(0, 9));
		}
		else {
			if ($(combo.selectbox).attr('name').indexOf('min_price') != -1) {
				checkMinMax = false;
				var newVal = MIN_PRICE;
				combo.input.val(MIN_PRICE);
			}
			else {
				checkMinMax = false;
				var newVal = MAX_PRICE;
				combo.input.val(MAX_PRICE);
			}
		}

		var minPrice;
		var maxPrice;

		if ($(combo.selectbox).attr('name').indexOf('min_price') != -1) {
			minPrice = newVal;
			if (checkMinMax) {
				maxPrice = checkMaxPrice(newVal);
			}
		} else {
			if (checkMinMax) {
				minPrice = checkMinPrice(newVal);
			}
			maxPrice = newVal;
		}

		$('.m28-2').find('input[name$=hndMinPrice]').val(minPrice);
		$('.m28-2').find('input[name$=hndMaxPrice]').val(maxPrice);
	}
	
    $('.m28-2 .select-box select').sexyCombo({
        skin: 'combobox-skin',
        suffix: '_value',
        hiddenSuffix: '_value_hidden',
        autoFill: false,

        filterFn: function() {
            return true;
        },

        initCallback: function() {
            var hiddenValue = '0';

            if ($(this.selectbox).attr('name').indexOf('min_price') != -1) {
                hiddenValue = $('.m28-2').find('input[name$=hndMinPrice]').val();
                minPriceCombo = this;
            }
            else {
                hiddenValue = $('.m28-2').find('input[name$=hndMaxPrice]').val();
                maxPriceCombo = this;
            }

            this.input.val(hiddenValue);
			
			this.input.bind('blur', {combo:this}, function(e) {
				var combo = e.data.combo;
				//setTimeout(function() { check(combo); }, 100);
				check(combo);
			});

        },
		
		hideListCallback: function() {
			check(this);
		},
		
        textChangeCallback: function() {

        }
    });
});

