﻿function log(msg) {
    if (window.console != null) {
        window.console.log(msg);
    }
}

function ClickOnEnter(e, button) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        button.click();
        return false;
    }

    return true;
}

function ajaxDownload(url) {
    $('#ajax-download').slideDown();
    jQuery('<form action="' + url + '" method="POST"></form>').appendTo('body').submit().remove();
    setTimeout(function () { $('#ajax-download').slideUp(); }, 1500);
}

function initColorbox(sel) {
    $(sel).colorbox({ width: 625, height: 575, iframe: true });
}

function handleError(error, userContext) {
    alert('We apologize, an error occurred: ' + error.get_message());
}

/* Map functions */

var maps = new Array();
var directions = new Array();
var geocoder = null;
var directionsService = null;
var mapsloaded = false;

function loadMaps() {
    if (mapsloaded == false) {
        google.load("maps", "3", { other_params: "sensor=false" });
        mapsloaded = true;
    }
}
function initializeMap(key, address, zoom, directions) {
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
        zoom: zoom,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    maps[key] = new google.maps.Map(document.getElementById("map_" + key), myOptions);

    if(directions)
    {
        directions[key] = new google.maps.DirectionsRenderer();
        directions[key].setMap(maps[key]);
        directions[key].setPanel(document.getElementById("directions_" + key));
    }
    if (address) {
        showProp(maps[key], address, zoom);
    }
}
function showProp(map, address, zoom) {
    zoom = zoom || 13;
    //map.clearOverlays();
    if (geocoder == null) {
        geocoder = new google.maps.Geocoder();
    }
    if (geocoder) {
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                map.setZoom(zoom)
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}
function getDirections(key, start, end) {
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    if (directionsService == null) {
        directionsService = new google.maps.DirectionsService();
    }
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directions[key].setDirections(response);
        }
    });
}
function toggleDirections(key) {
    $('#getdir_' + key).toggle();
    //maps[key].checkResize();
    google.maps.event.trigger(maps[key], 'resize');
    $('#toaddr_' + key).focus();
}

function hashForPage(scrollTop) {
    var url = location.href;
    var hash = "";

    if (url.indexOf('#') > 0) {
        hash = url.substring(url.indexOf('#') + 1);
    }
    
    if (scrollTop && hash.length > 0) {
        setTimeout(function () { $('body').scrollTop(0); }, 1);
    }

    return hash;
}
