﻿var currentPageUrl = '';
function resetPageNr(url) {
    var re = new RegExp('page=[0-9]*', 'gi');
    var location = url.replace(re, 'page=0');
    return location;
}
function applyPriceFilter() {
    var minPrice = document.getElementById('minPriceInput').value.replace(/,/g, '.');
    var maxPrice = document.getElementById('maxPriceInput').value.replace(/,/g, '.');
    var noPrice = document.getElementById('noPriceCheckbox').checked;
    var re = new RegExp('&(min|max|noprice)=[0-9\.]*', 'gi');
    //var location = window.location.href.replace(re, '');
    var location = currentPageUrl.replace(re, '');
    location = location + '&min=' + minPrice + '&max=' + maxPrice + '&noprice=' + (noPrice ? '1' : '0');
    window.location = resetPageNr(location);
}
function applyPhotoFilter() {
    var onlyWithPhoto = document.getElementById('onlyWithPhotoCheckbox').checked;
    var re = new RegExp('photo=[0-9]*', 'gi');
    //var location = window.location.href.replace(re, '');
    var location = currentPageUrl.replace(re, '');
    location = location + '&photo=' + (onlyWithPhoto ? '1' : '0');
    window.location = resetPageNr(location);
}
function applyMaxAgeFilter() {
    var maxAge = document.getElementById('maxAge').value;
    var re = new RegExp('&maxage=[0-9]*', 'gi');
    //var location = window.location.href.replace(re, '');
    var location = currentPageUrl.replace(re, '');
    location = location + '&maxage=' + maxAge;
    window.location = resetPageNr(location);
}
function applyKeywordFilter() {
    var newKeyword = document.getElementById('addKeywordInput').value;
    var re = new RegExp('([&\?]keywords=([^&]*))', 'gi');
    var location = currentPageUrl;
    var found = re.exec(location); //window.location.href
    if (found) {
        var space = (RegExp.$2.length == 0) ? '' : '+';
        location = location.replace(re, '$1' + space + newKeyword); // window.location.href
    }
    else {
        location = location + '&keywords=' + newKeyword; // window.location
    }
    window.location = resetPageNr(location);
}
function applyCarYearFilter() {
    var minYear = document.getElementById('minYearInput').value.replace(/,/g, '.');
    var maxYear = document.getElementById('maxYearInput').value.replace(/,/g, '.');
    var re = new RegExp('&filter=([0-9]+=[0-9]+;)*?4=[0-9]+-[0-9]+;([0-9]+=[0-9]+;)*?', 'gi');
    //var location = window.location.href.replace(re, '');
    var location = currentPageUrl;
    var found = re.exec(location);
    if (found) {
        location = location.replace(re, '&filter=$1$24=' + minYear + '-' + maxYear + ';');
    }
    else {
        location = location + '&filter=4=' + minYear + '-' + maxYear + ';';
    }
    //location = location + '&filter=4=' + minYear + '-' + maxYear + ';';
    window.location = resetPageNr(location);
}
function applyFilterRange(id, minField, maxField) {
    var min = document.getElementById(minField).value.replace(/,/g, '.');
    var max = document.getElementById(maxField).value.replace(/,/g, '.');
    if (min == '') {
        min = '0';
    }
    if (max == '') {
        max = '0';
    }
    if (eval(min) > eval(max)) {
        var tmp = min;
        min = max;
        max = tmp;
    }
    var newFilter = id + '=' + min + '-' + max + ';';
    if (min == max) {
        newFilter = '';
    }

    var re = new RegExp('&filter=((?:[0-9]+=[0-9]+(?:-[0-9]+)?;)*?)' + id + '=[0-9]+-[0-9]+;((?:[0-9]+=[0-9]+(?:-[0-9]+)?;)*)', 'gi');
    //var location = window.location.href.replace(re, '');
    var location = currentPageUrl;
    var found = re.exec(location);
    if (found) {
        location = location.replace(re, '&filter=$1' + newFilter + '$2');
    }
    else {
        re = new RegExp('&filter=(.*?)(?=&|$)', 'gi');
        found = re.exec(location);
        if (found) {
            location = location.replace(re, '&filter=$1' + newFilter);
        } else {
            location = location + '&filter=' + newFilter;
        }
    }
    window.location = resetPageNr(location);
}

var infowindow;
var map;
var markers = new Array();
var mapCenter;

function initialize() {
    var latlng = mapCenter;
    var myOptions = {
        zoom: 6,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        //mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    for (var i = 0; i < markers.length; i++) {
        //window.alert(marker.lat);
        createMarker(markers[i].name, markers[i].lat, markers[i].lng);
    }
}

function createMarker(name, lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({ position: latlng, map: map });
    google.maps.event.addListener(marker, "click", function() {
        if (infowindow) infowindow.close();
        infowindow = new google.maps.InfoWindow({ content: name });
        infowindow.open(map, marker);
    });
    //map.addMarker(marker);
    //return marker;
}
