//API key ABQIAAAAonaeTf3Wk571Fr7GIiKdqxSqtXYqD0aR5GtxkBeCRxMqyOFlhxTu_orKP83IzL_jv2f27qRtGb8rTQ

function StorevanMap() {
    if (GBrowserIsCompatible()) {
        this.map = new GMap2(document.getElementById('map'));
        this.gdir = new GDirections(this.map, document.getElementById('directions'));
        this.bucuresti = new GLatLng(44.461259, 26.106485);
        this.buzau = new GLatLng(45.157436, 26.82248);

        GEvent.addListener(this.map, 'singlerightclick', this.rightClickHandler);
        GEvent.addListener(this.gdir, 'error', this.errorHandler);
        
        this.map.addControl(new GLargeMapControl());
        this.map.addControl(new GMapTypeControl());
    }
    else {
        alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

StorevanMap.prototype.addMarker = function(point, html) {
    var marker = new GMarker(point);
    var content = '<div style="width:240px">' + html + '</div>';
    
    GEvent.addListener(marker, "click", function() {
                           marker.openInfoWindowHtml(content);
                       });
    GEvent.addListener(marker, "mouseover", function() {
                           marker.openInfoWindowHtml(content);
                       });
    
    this.map.addOverlay(marker);
}

StorevanMap.prototype.plot = function() {
    this.addMarker(this.bucuresti, 'Str. W. A. Mozart, nr. 2, sc. A, ap. 2, sector 2<br />Bucuresti');
    this.map.setCenter(this.bucuresti, 15);
    return false;
}

StorevanMap.prototype.directions = function(to) {
    this.map.clearOverlays();
    
    var from_address = document.getElementById('from_address').value;
    var to_address = this.bucuresti.toUrlValue();
    
    this.gdir.clear();
    this.gdir.load("from: " + from_address + " to: " + to_address, { 'locale': 'ro' });
    return false;
}

StorevanMap.prototype.clear = function() {
    this.gdir.clear();
    this.plot();
    document.getElementById('from_address').value = '';
    return false;
}

StorevanMap.prototype.rightClickHandler = function(point, source, overlay) {
    document.getElementById('from_address').value = this.fromContainerPixelToLatLng(point).toUrlValue();
}

StorevanMap.prototype.errorHandler = function() {
    alert('Adresa introdusa nu poate fi gasita!\n\n' +
          'Puteti introduce adresa de plecare direct pe harta:\n' + 
          '1. Aranjati harta ca adresa de plecare sa fie vizibila.\n' +
          '2. Click cu butonul dreapta pe adresa de plecare.\n' +
          '3. Click pe butonul "Arata traseu".');
    storevan_map.clear();
}


function storevan_init() {
    window.storevan_map = new StorevanMap();
    
    storevan_map.plot();
}

function addLoadEvent(func) {
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(storevan_init);

