/*
 * Copyright (c) 2010, Inmite s.r.o. (www.inmite.eu). All rights reserved.
 *
 * This source code can be used only for purposes specified by license contract
 * signed by rightful deputy of Inmite s.r.o. This source code can be used only
 * by owner of the license.
 * Any disputes arising in respect of this agreement (license) shall be brought
 * before the Municipal Court of Prague.
 */

/**
 * iMap-searchControl can be used as custom control within map, or outside.
 *
 * For using in iMap, just put "searchControl" into controls list in iMap initialization.
 * For using outside iMap, call initializeOutside() method with parent DIV as parameter.
 *
 * Version 0.2
 */

var searchControl = new GControl(false, true);// printable & selectable

searchControl.initControl = function(map) {
    this.map = map;
    this.gc = new GClientGeocoder();
    this.defaultValue = l('enter_place');

    this.outside = false;

    if (!this.outside) {
        map.addControl(this);
    }
};

searchControl.initDiv = function() {
    var tmpDiv = document.getElementById('searchControl');
    if (!tmpDiv) {
        tmpDiv = document.createElement('div');
        tmpDiv.setAttribute('id', 'searchControl');
    }

    tmpDiv.innerHTML = '<input id="searchInput" type="text" value="'+this.defaultValue+'" style="width: 150px; margin-right: 4px;" />' +
                       '<input id="searchButton" type="button" value="'+l('search')+'" />';

    return tmpDiv;
};

// called after div is in DOM
searchControl.finishDiv = function() {
    var me = this;

    $('#searchInput').clearField();
    $('#searchInput').bind('keypress', function(ev) {
        if (ev.which == 13) {
            me.fireSearch();
        }
    });

    $('#searchButton').bind('click', function(ev) {
        me.fireSearch();
    });
};

searchControl.initialize = function(map) {
    var div = this.initDiv();
    map.getContainer().appendChild(div);

    this.finishDiv();

    return div;
};

searchControl.initializeOutside = function(toDiv) {
    var div = this.initDiv();

    toDiv.appendChild(div);

    this.finishDiv();

    return div;
};

searchControl.getDefaultPosition = function() {
//  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(230, 5));
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(5, 5));
};

searchControl.fireSearch = function() {
    var me = this;
    var addr = $('#searchInput').val();
    if (addr == '' || addr == this.defaultValue) { alert('No place entered.'); return; };
    this.gc.getLocations(
        addr,
        function(ret) {
            if (!ret || ret.Status.code != 200) {
                alert('Address "'+addr+'" not found.');
            } else {
                me.zoomToResult(ret);
            }
        }
    );
};

searchControl.zoomToResult = function(ret) {
    var zoom = this.map.getBoundsZoomLevel(new GLatLngBounds(
            new GLatLng(ret.Placemark[0].ExtendedData.LatLonBox.south,
                    ret.Placemark[0].ExtendedData.LatLonBox.west),
            new GLatLng(ret.Placemark[0].ExtendedData.LatLonBox.north,
                    ret.Placemark[0].ExtendedData.LatLonBox.east)
            ));
    this.map.setCenter(new GLatLng(ret.Placemark[0].Point.coordinates[1], ret.Placemark[0].Point.coordinates[0]), zoom);

    $('#searchInput').val(ret.Placemark[0].address);

//    this.map.addOverlay(new GMarker(new GLatLng(ret.Placemark[0].Point.coordinates[1], ret.Placemark[0].Point.coordinates[0])));
};
