﻿var search_marker;
var what_id = "#What";
var address_id = "#Address";
var category_id = "#Type";
var coordinates_lat = "#Lat";
var coordinates_lon = "#Lon";
var validator_id = "#ErrorMessage_validationMessage";
var geocoding_url = "http://maps.google.com/maps/geo";

var search_marker_layer;

function object_init() {
    var control = getClickDragControl();
    control.onMapClick = function(e) {
        var point = window.toEPSG4326Coordinate(map.getLonLatFromViewPortPx(e.xy));
        SetMarkerToMap(point);
    };

    control.onDragComplete = function(feature, pixel) {
        var point = window.toEPSG4326Coordinate(map.getLonLatFromViewPortPx(pixel));
        SetMarkerToMap(point);
    };

    control.onClickComplete = function (feature) {

        if (feature.CLASS_NAME != SHTURMANN.VectorWithPopup.prototype.CLASS_NAME)
            return;

        var id;

        if (feature.attributes.ids == "") {
            id = feature.attributes.id;
        }
        else {
            id = feature.attributes.ids.split(',')[0];
        }
        
        var t = jQuery('#poi' + id);
        selected_li(t[0]);
        var page = t.attr('class').replace('simplePagerPage', '').trim();
        jQuery('.simplePageNav' + page + ' a').click();
        window.location.hash = 'poi' + id;
    };
}

function show_main_categories() {
    jQuery('#object_types [id^=type_]').addClass('divType').css('display', 'block');
    jQuery('#object_types [id^=subtypes_]').addClass('divSubType').css('display', 'none');
    set_current_category('');
}

function show_sub_categories(divID) {
    jQuery('#object_types [id^=subtypes_]').css('display', 'none');
    jQuery('#object_types [id^=type_]').css('display', 'none');

    jQuery('#object_types [id=' + divID + ']').addClass('divSubType').css('display', 'block');

}
function set_current_category(id) {
    jQuery(category_id).val(id);
    jQuery(what_id).val("");
}
function check_coordinates() {
    if (jQuery.trim(jQuery(what_id).val()) == "" && (jQuery.trim(jQuery(category_id).val()) == "" || jQuery.trim(jQuery(category_id).val()) == "0")) {
        show_error(empty_what);
        return false;
    }
    var point = window.toEPSG4326Coordinate(map.getCenter());
    if (jQuery(coordinates_lat).val() == "-360" || jQuery(coordinates_lon).val() == "-360") {
        jQuery(coordinates_lat).val(point.lat.toFixed(6));
        jQuery(coordinates_lon).val(point.lon.toFixed(6));
    }
    return true;
}
function show_error(str) {
    jQuery(validator_id).html(str).addClass("field-validation-error").removeClass("field-validation-valid");
}

function SetCoordinatesSearchMarker(latlng) {
    jQuery.cookie('default_lat', latlng.lat.toFixed(6), { expires: 365, path: '/' });
    jQuery.cookie('default_lng', latlng.lon.toFixed(6), { expires: 365, path: '/' });
    jQuery(coordinates_lat).val(latlng.lat.toFixed(6));
    jQuery(coordinates_lon).val(latlng.lon.toFixed(6));
}
function ConstructSearchMarker(point) {
    if (point) {
        var tmp = window.toShowOnGoogleLayers(point);
        var marker = new SHTURMANN.SearchMarker(new OpenLayers.Geometry.Point(tmp.lon, tmp.lat));
        pois_layer.addFeatures([marker]);
        return marker;
    }
    else {
        return null;
    }
}

function SetMarkerToMap(latlng) {
 
    if (search_marker == null) {
        search_marker = ConstructSearchMarker(latlng);
    }

    var tmp = window.toShowOnGoogleLayers(latlng);
    var xy = map.getViewPortPxFromLonLat(tmp);
    
    search_marker.move(xy);
    SetCoordinatesSearchMarker(latlng);
    map.panTo(tmp);

}


jQuery(document).ready(function () {
    jQuery("#Address").autocomplete({ delay: 300, minLength: 4,
        source: function (request, response) {
            geocoder.geocode(jQuery("#Address").val(), window.toEPSG4326Coordinate(map.getCenter()), function (results) {
                response(jQuery.map(results, function (item) {
                    return {
                        label: item.address,
                        value: item.address,
                        lonlat: item.lonlat
                    };
                }));
            });
        },
        select: function (event, ui) {
            hideAutocompleteProgressBar(event.target);
            SetMarkerToMap(ui.item.lonlat);
            
        }
    });



    jQuery.preLoadImages('/images/btn_search_on.png');
});

jQuery(mapInitializeBind.on).bind(mapInitializeBind.event, object_init);
        
