/*
################################################################################
ox.geo.js

requires
    jquery.js
    ox.js
################################################################################
*/

(function() {

    var earthRadius = 6378137,
        earthCircumference = 2 * Math.PI * earthRadius
        latMax = Ox.deg(Math.atan(Ox.sinh(Math.PI))),
        latMin = -latMax;
        /*
        map = {
            width: window.innerWidth,
            height: window.innerHeight,
            margin: 16
        };
        */

    Ox.getLatLng = (function() {
        /*
            returns {lat, lng} for a given (x, y)
        */
        function getLatLng(xy) {
            return (xy - 0.5) * 2 * Math.PI;
        }
        return function(x, y) {
            return {
                lat: -Ox.deg(Math.atan(Ox.sinh(getLatLng(y)))),
                lng: Ox.deg(getLatLng(x))
            };
        };
    })();

    Ox.getPlacemarks = (function() {
        /*
        Ox.getPlacemarks(name, callback)
        Ox.getPlacemarks(lat, lng, callback)
        */
        var cache = {};
        return function() {

            // $.getScript("http://maps.google.com/maps/api/js?sensor=false", function() {
                // getPlacemarks(args);
            // });


            // var args = arguments;
            var reverse = typeof arguments[0] == "number",
                query = $.extend({
                    language: "en"
                }, !reverse ? {
                    address: arguments[0]
                } : {
                    latLng: new google.maps.LatLng(arguments[0], arguments[1])
                }),
                id = JSON.stringify(query),
                callback = arguments[arguments.length - 1],
                geocoder = new google.maps.Geocoder();
            if (cache[id]) {
                callback(cache[id]);
            } else {
                geocoder.geocode(query, function(results, status) {
                    var data = {
                        results: results,
                        status: status
                    };
                    cache[id] = data;
                    callback(data);
                });
            }
            
        };
    })();

    Ox.getXY = (function() {
        /*
            returns {x, y} for a given (lat, lng), between 0 and 1
        */
        function getXY(xy) {
            return (xy / (2 * Math.PI) + 0.5);
        }
        return function(lat, lng) {
            return {
                x: getXY(Ox.rad(lng)),
                y: getXY(Ox.asinh(Math.tan(Ox.rad(-lat))))
            };
        };
    })();

    /*
    Ox.getZ = function(placemark, map) {
        // returns zoom level at which placemark is fully included
        map.width -= 2 * map.margin;
        map.height -= 2 * map.margin;
        var northEast = getXY(placemark.northEast),
            southWest = getXY(placemark.southWest),
            width = (northEast.x - southWest.x),
            height = (northEast.y - southWest.y),
        return parseInt(Ox.log(width / height > map.width / map.height ?
                map.width / width : map.height / height, 2));
    };
    */

})();