
function SearchBox(){};
SearchBox.prototype.idRealEstateType = '1';
SearchBox.prototype.idOrderType = '1';
SearchBox.prototype.idCity = '';
SearchBox.prototype.idQuarter = '';
SearchBox.prototype.cityName = '';
SearchBox.prototype.quarterName = '';
SearchBox.prototype.streetName = '';
SearchBox.prototype.price_from = '';
SearchBox.prototype.price_to = '';
SearchBox.prototype.priceM_from = '';
SearchBox.prototype.priceM_to = '';
SearchBox.prototype.area_from = '';
SearchBox.prototype.area_to = '';
SearchBox.prototype.floor_from = '';
SearchBox.prototype.floor_to = '';
SearchBox.prototype.year_from = '';
SearchBox.prototype.year_to = '';
SearchBox.prototype.vt = '0';
SearchBox.prototype.marina = '0';
SearchBox.prototype.order = 'dateAdded';
SearchBox.prototype.orderDir = 'desc';

SearchBox.prototype.fillBox = function(){
    limeRadioClick('idOrderType', this.idOrderType);
    limeRadioClick('idRealEstateType', this.idRealEstateType);
    limeCheckboxFill('vt', this.vt);
    limeCheckboxFill('marina', this.marina);

    //manage dla wszystkich pol input text
    var InputName = '';
    var Value = '';
    var thiss = this;
    $('input[name="cityName"]').val(this.cityName);
    $('input[name="quarterName"]').val(this.quarterName);
    $('input[name="idCity"]').val(this.idCity);
    $('input[name="idQuarter"]').val(this.idQuarter);

    $('input[type="text"]', '#searchBox').each(function(){
        InputName = $(this).attr('name');
        Value = thiss[InputName];
        $(this).val(Value);
        manageInputFill($(this));
    });
    $('input[name="order"]', '#searchBox').val(thiss.order);
    $('input[name="orderDir"]', '#searchBox').val(thiss.orderDir);

}

$(document).ready(function(){
   manageCityName();
   manageQuarterName();
   manageFormSubmit();
});

function manageFormSubmit(){
    $('#searchBox').submit(function(){
       showLoaderBig();
    });
    $('input[type="submit"]', '#searchBox').click(function(){
       showLoaderBig();
    });
}

function manageCityName(){
    manageOutOver();
    var Input = $('input[name="cityName"]', '#searchBox');
    Input.keyup(function(){
        cityNameProcess();
    });
    Input.blur(function(){
        getIdFromCityName();
    });
}

function getIdFromCityName(){
    var InputId = $('input[name="idCity"]', '#searchBox');
    var Input = $('input[name="cityName"]', '#searchBox');
    if(InputId.val() == ''){
        $.ajax({
            url: AjaxUrl + 'json/search/city-from-part',
            type: 'POST',
            data: {'cityName': encodeURI(Input.val()), 'first': true},
            success: function(result){
                InputId.val($.trim(result));
            }
        });
    }
}

function manageQuarterName(){
    manageOutOverQuarter();
    var Input = $('input[name="quarterName"]', '#searchBox');
    Input.keyup(function(){
        quarterNameProcess();
    });
}

function cityNameProcess(){
    var Input = $('input[name="cityName"]', '#searchBox');
    var InputId = $('input[name="idCity"]', '#searchBox');
    InputId.val('');
    var Value = Input.val();
    if(Value.length > 0){
        searchBoxCityAutoShow();
        searchBoxCityFindProcess();
    }else{
        searchBoxCityAutoHide();
    }
}

function quarterNameProcess(){
    var Input = $('input[name="quarterName"]', '#searchBox');
    var InputId = $('input[name="idQuarter"]', '#searchBox');
    var InputCity = $('input[name="idCity"]', '#searchBox');
    InputId.val('');
    var Value = Input.val();
    var IdCity = $.trim(InputCity.val());
    if((IdCity.length < 1 || IdCity == 0) && Value.length > 0){
        searchBoxQuarterAutoShow('noCity');
    }else if(Value.length > 0){
        searchBoxQuarterAutoShow();
        searchBoxQuarterFindProcess();
    }else{
        searchBoxQuarterAutoHide();
    }
}

function searchBoxCityFindProcess(){
    var Input = $('input[name="cityName"]', '#searchBox');
    $.ajax({
        url: AjaxUrl + 'json/search/city-from-part',
        type: 'POST',
        data: "cityName="+encodeURI(Input.val()),
        success: function(result){
            Data = eval('(' + result + ')');
            var Name = '';
            var Id = '';
            var Res = '';
            if(Data.length == 0){
                Res = '<div>Nie znaleziono</div>';
            }else{
                for(I in Data){
                    Name = Data[I]['cityName'];
                    Id = Data[I]['idCity'];
                    Res += '<div class="button" rel="' + Id + '" onclick="searchBoxSelectCity(this);">' + Name + '</div>';
                }
            }
            $('#searchBoxCityAutoInside').html(Res);
        }
    });
}

function searchBoxQuarterFindProcess(){
    var Input = $('input[name="quarterName"]', '#searchBox');
    var InputIdCity = $('input[name="idCity"]', '#searchBox');
    $.ajax({
        url: AjaxUrl + 'json/search/quarter-from-part',
        type: 'POST',
        data: {'quarterName': encodeURI(Input.val()), 'idCity': encodeURI(InputIdCity.val())},
        success: function(result){
            Data = eval('(' + result + ')');
            var Name = '';
            var Id = '';
            var Res = '';
            if(Data.length == 0){
                Res = '<div>Nie znaleziono</div>';
            }else{
                for(I in Data){
                    Name = Data[I]['quarterName'];
                    Id = Data[I]['idQuarter'];
                    Res += '<div class="button" rel="' + Id + '" onclick="searchBoxSelectQuarter(this);">' + Name + '</div>';
                }
            }
            $('#searchBoxQuarterAutoInside').html(Res);
        }
    });
}

function searchBoxSelectCity(Obj){
    var Input = $('input[name="cityName"]', '#searchBox');
    var InputId = $('input[name="idCity"]', '#searchBox');
    var IdCity = $(Obj).attr('rel');
    var CityName = $(Obj).html();
    Input.val(CityName);
    InputId.val(IdCity);
    searchBoxCityAutoHide();
}

function searchBoxSelectQuarter(Obj){
    var Input = $('input[name="quarterName"]', '#searchBox');
    var InputId = $('input[name="idQuarter"]', '#searchBox');
    var IdQuarter = $(Obj).attr('rel');
    var QuarterName = $(Obj).html();
    Input.val(QuarterName);
    InputId.val(IdQuarter);
    searchBoxQuarterAutoHide();
    selectFromJs(IdQuarter);
}

function manageOutOver(){
    var Input = $('input[name="cityName"]', '#searchBox');
    CityAutoVisible = false;
    CityAutoOver = false;
    $('#searchBoxCityAuto').mouseenter(function(){
        CityAutoOver = true;
    });
    $('#searchBoxCityAuto').mouseleave(function(){
        CityAutoOver = false;
    });
    Input.click(function(Obj){
       if(Input.val().length > 0){
           Obj.stopPropagation();
            searchBoxCityAutoShow(false);
       }
    })
    $(document).click(function(Obj){
        if(CityAutoOver == false){
            searchBoxCityAutoHide();
        }
    });
}

function manageOutOverQuarter(){
    var Input = $('input[name="quarterName"]', '#searchBox');
    QuarterAutoVisible = false;
    QuarterAutoOver = false;
    $('#searchBoxQuarterAuto').mouseenter(function(){
        QuarterAutoOver = true;
    });
    $('#searchBoxQuarterAuto').mouseleave(function(){
        QuarterAutoOver = false;
    });
    Input.click(function(Obj){
       if(Input.val().length > 0){
           Obj.stopPropagation();
            searchBoxQuarterAutoShow(false);
       }
    })
    $(document).click(function(Obj){
        if(QuarterAutoOver == false){
            searchBoxQuarterAutoHide();
        }
    });
}

function searchBoxCityAutoHide(){
    $('#searchBoxCityAuto').hide();
}
function searchBoxCityAutoShow(Clear){
    if(Clear == undefined){
        Clear = true;
    }
    $('#searchBoxCityAuto').show();
    if(Clear){
        $('#searchBoxCityAutoInside').html('<div>...wyszukiwanie...</div>');
    }
    searchBoxQuarterAutoHide();
}

function searchBoxQuarterAutoHide(){
    $('#searchBoxQuarterAuto').hide();
}
function searchBoxQuarterAutoShow(Clear){
    if(Clear == undefined){
        Clear = true;
    }
    $('#searchBoxQuarterAuto').show();
    if(Clear == 'noCity'){
        $('#searchBoxQuarterAutoInside').html('<div>Wybierz miasto</div>');
    }else if(Clear){
        $('#searchBoxQuarterAutoInside').html('<div>...wyszukiwanie...</div>');
    }
    searchBoxCityAutoHide();
}

