var objectSelectProcess = false;
var objectRentProcess = false;

var roomsSlider;
var roomsSliderChange;

var areaSlider;
var areaSliderChange;

var field_areaSlider;
var field_areaSliderChange;

var priceSlider;
var priceSliderChange;

var priceSquareSlider;
var priceSquareSliderChange;

var floorSlider;
var floorSliderChange;

var floor_noSlider;
var floor_noSliderChange;

var locationTable;

$(document).ready(function() {
    $.ajax({
        url: "wyszukiwarka/pobierzLokalizacje/",
		success: function(data){
			locationTable = jQuery.parseJSON(data);
            
            var substringMatcher = function(strs) {
                return function findMatches(q, cb) {
                    var matches, substringRegex;
                    // an array that will be populated with substring matches
                    matches = [];
                    
                    term = q;
                    regTerm = term.replace(/,/g, '');
                    regTerm = regTerm.replace(/ /g, '(.*)');
                    if(regTerm.substr(0, -4) === '(.*)')
                    {
                        regTerm = regTerm.substr(0, regTerm.length - 4);
                    }
                    substrRegex = new RegExp(regTerm, "i");
                    
                    // regex used to determine if a string contains the substring `q`
                    // substrRegex = new RegExp(q, 'i');
                    // iterate through the pool of strings and for any string that
                    // contains the substring `q`, add it to the `matches` array
                    $.each(strs, function(i, str) {
                        if (substrRegex.test(str)) {
                            matches.push(str);
                        }
                    });

                    cb(matches);
                };
            };
            
            $('.searchLocation').typeahead({
                hint: true,
                highlight: true,
                minLength: 1
            },
            {
                name: 'states',
                source: substringMatcher(locationTable),
                limit: 100
            });
            
            $('.searchLocation').bind('typeahead:close', function(ev, suggestion) {
                if($(this).val()) {
                    searchText = $(this).val().trim();
                    searchText = searchText.replace(/,/g,'');

                    pattern = '^'+searchText+'(.*)';
                    queryRegex = new RegExp(pattern, 'i');
                    for(i = 0; i < locationTable.length; i++)
                    {                    
                        if(locationTable[i].match(queryRegex))
                        {
                            $(this).val(locationTable[i]);
                            $(this).typeahead('val', $(this).val()); 
                            return true;
                        }
                    }

                    searchTextArray = searchText.split(' ');
                    pattern = searchTextArray.join('(.*)');
                    queryRegex = new RegExp(pattern, 'i');
                    for(i = 0; i < locationTable.length; i++)
                    {   
                        if(locationTable[i].match(queryRegex))
                        {
                            $(this).val(locationTable[i]);
                            $(this).typeahead('val', $(this).val()); 
                            return true;
                        }
                    }
                }
            });
        }
    });
});

function sliderUpdate(minmax) {
    // SLIDER UPDATE
    fields = ['rooms', 'area', 'field_area', 'price', 'price_square', 'floor', 'floor_no'];
    for(i in fields) {
        min = parseInt(minmax[fields[i]][0]);
        max = parseInt(minmax[fields[i]][1]);

        $("#" + fields[i] + "FromText").html(min.formatMoney(0, '.', ' '));
        $("#" + fields[i] + "ToText").html(max.formatMoney(0, '.', ' '));
        $("#" + fields[i] + "InputFrom").val('');
        $("#" + fields[i] + "InputTo").val('');
        $("#" + fields[i] + "Slider").slider('setAttribute', 'min', min);
        $("#" + fields[i] + "Slider").slider('setAttribute', 'max', max);
        $("#" + fields[i] + "Slider").slider('refresh');
        $("#" + fields[i] + "Slider").slider('setValue', [min, max]);

        if(min === max && min === 0) {
            $("#" + fields[i] + "Slider").slider("disable");
        }
        else {
            $("#" + fields[i] + "Slider").slider("enable");
        }
    }
}

Number.prototype.formatMoney = function(c, d, t){
var n = this, 
    c = isNaN(c = Math.abs(c)) ? 2 : c, 
    d = d == undefined ? "." : d, 
    t = t == undefined ? "," : t, 
    s = n < 0 ? "-" : "", 
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
    j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };
$(document).ready(function() {
	$("#cookie-info span").click(function() {
		var date = new Date();
		date.setTime(date.getTime()+(180*24*60*60*1000));
		document.cookie = "cookie_message_shown=1;expires="+date.toGMTString()+";path=/;";
		$("#cookie-info").fadeOut();
		
		return false;
	});
});