﻿var map;
var initialized = false;
var markers = Array();

google.load("maps", "2", {"other_params": "sensor=false"});
function loadMap() {
    map = new google.maps.Map2(document.getElementById("map"), {size: new google.maps.Size(550, 550)});
    map.addControl(new google.maps.MenuMapTypeControl());
    map.addControl(new google.maps.SmallZoomControl3D());
    map.addControl(new google.maps.ScaleControl());
    map.setCenter(new google.maps.LatLng(51.165567, 10.458984), 6);
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();
    initialized = true;
    
    $.getJSON("../JSONGetDealers.ashx", initDealers);
}
google.setOnLoadCallback(loadMap);

$(function() {
    $("#noDealersFound:visible").hide();
    
    $("#searchZip").bind("keypress", function(ev) {
        if(ev.which == 13) {
            if($(this).val() != "")
                search();
            return false;
        }
    });
    
    $("#searchZip").bind("keyup", function(ev) {
        if($(this).val() != '') {
            $("#searchCity").attr("disabled", "disabled");
        } else {
            $("#searchCity").removeAttr("disabled");
        }
    });
    
    updateCities();
});

function addMarker(coords, html) {
    if(initialized) {
        var point = new google.maps.LatLng(coords.lat, coords.lng);
        var marker = new google.maps.Marker(point);
        marker.bindInfoWindowHtml(html);
        google.maps.Event.addListener(marker, "click", function() {
            if(map.getZoom() < 14) {
                map.setZoom(14);
            }
            map.setCenter(point);
        });
        map.addOverlay(marker);
    }
}

function initDealers(dealers) {
        
    map.clearOverlays();
    if(dealers.length == 0) {
        $("#noDealersFound:hidden").fadeIn(300);
        return;
    }
    $("#noDealersFound:visible").fadeOut(300);
    var bounds = new google.maps.LatLngBounds();
    $.each(dealers, function(i, dealer) {
        var html = '<table>';
        html += '<tr><td>' + dealer.name + '</td></tr>';
        html += '<tr><td>' + dealer.address + '</td></tr>';
        html += '<tr><td>' + dealer.zipcode + "  " + dealer.city + '</td></tr>';
        html += '<tr><td><a href="' + dealer.url + '">' + dealer.url + '</a></td></tr>';
        html == '<tr><td>Phone: ' + dealer.phone + '</td></tr>';
        html += '<tr><td>' + dealer.email.toLowerCase() + '</td></tr>';
        html += '</table>';
        var coords = dealer.coords;
        var point = new google.maps.LatLng(coords.lat, coords.lng);
        bounds.extend(point);
        addMarker(coords, html);
    });
    var zoom = map.getBoundsZoomLevel(bounds);
    map.setZoom(zoom > 16 ? 16 : zoom);
    map.setCenter(bounds.getCenter());
}

function processDealers(dealers) {
    map.clearOverlays();
    initDealers(dealers);
//    
//    var bounds = new google.maps.LatLngBounds();
//    $.each(dealers, function(i, dealer) {
//        var latLng = new google.maps.LatLng(dealer.coords.lat, dealer.coords.lng);
//        bounds.extend(latLng);
//    });
//    var zoom = map.getBoundsZoomLevel(bounds);
//    map.setZoom(zoom > 16 ? 16 : zoom);
//    map.setCenter(bounds.getCenter());
}

function search() {
    var search;
    var method;
    var country = $("#searchCountry").val();
    
    if(country == -1) {
        search = '';
        country = '';
        method = '';
    } else if($("#searchZip").val() == "") {
        if($("#searchCity").val() == -1) {
            search = '';
            method = 'country';
        } else {
            search = $("#searchCity").val();
            method = "city";
        }
    } else {
        search = $("#searchZip").val();
        method = "zip";
    }
    $.getJSON("../JSONGetDealers.ashx", {method: method, search: search, country: country}, processDealers);
}

function updateCities() {
    var country = $("#searchCountry").val();
    if(country != -1) {
        $.getJSON("../JSONGetCities.ashx", {country: country}, processCities);
        $("#searchCity").removeAttr("disabled");
        $("#searchZip").removeAttr("disabled");
    } else {
        $("#searchCity").empty();
        $("#searchCity").attr("disabled", "disabled");
        $("#searchZip").attr("disabled", "disabled");
    }
}

function processCities(cities) {
    var citiesOptions = "";
    $.each(cities, function(i, item) {
        citiesOptions += '<option value="' + item.value + '">' + item.name + '</option>';
    });
    $("#searchCity").html(citiesOptions);
    
}
