Kyan = {

    office: null,
    map: null,
    icon: null,
    marker: null,
    directionsPanel: null,
    directions: null,
    
    debug: function(str) {
        var console = window.console || false;
        if(console) {
            console.debug(str);
        }
    },

    init: function() {
        Kyan.office = new GLatLng(37.763058, -3.945583);
    },
//maps.google.es/maps?f=d&saddr=37.763058,-3.945583&daddr=&hl=es&geocode=&mra=mi&mrsp=0&sz=17&sll=37.763099,-3.945583&sspn=0.00631,0.009656&ie=UTF8&z=17

    load: function() {
        Kyan.map = new GMap2(document.getElementById("map"),
            {mapTypes:[G_NORMAL_MAP, G_HYBRID_MAP]});
        Kyan.map.addControl(new GSmallZoomControl());
        Kyan.map.addControl(new GMapTypeControl());

        Kyan.icon = new GIcon(G_DEFAULT_ICON, 'imagenes/doblecara_map.png');
        Kyan.icon.shadow = 'images/marker_shadow.png';
        Kyan.icon.iconSize = new GSize(85,28);
        Kyan.icon.shadowSize = new GSize(52,27);
        Kyan.icon.iconAnchor = new GPoint(5, 26);
		Kyan.icon.transparent = null
		Kyan.icon.mozPrintImage = null
		Kyan.icon.printImage = null
        Kyan.marker = new GMarker(Kyan.office, {icon: Kyan.icon})
        Kyan.directionsPanel = document.getElementById("directions_list");
        Kyan.directions = new GDirections(Kyan.map, Kyan.directionsPanel);
        GEvent.addListener(Kyan.directions, 'error', Kyan.onDirectionsUnavailable);
        GEvent.addListener(Kyan.directions, 'addoverlay', Kyan.onDirectionsFinished);
        GEvent.addListener(Kyan.directions, 'load', Kyan.onDirectionsAvailable);
        Kyan.resetMap(Kyan.map);
    },
    
    Google: {
        geocodePostcode: function(postcode, callbackFunction) {
            var localSearch = new GlocalSearch();
            localSearch.setSearchCompleteCallback(null,
                function() {  
//                    Kyan.debug("Found "+ localSearch.results.length +" results");
                    if (localSearch.results[0]) {
                        var resultLat = localSearch.results[0].lat;
                        var resultLng = localSearch.results[0].lng;
                        var point = new GLatLng(resultLat,resultLng);
//                        Kyan.debug("Using " + point);
                        callbackFunction(point);
                    } else {
                        callbackFunction(null);
                    }
                }
            );

            localSearch.execute(postcode + ", ES");
        }
    },
    
    resetMap: function(map) {
        map.setCenter(Kyan.office, 16);
        map.removeOverlay(Kyan.marker);
        map.addOverlay(Kyan.marker);
    },
    
    getDirectionsFrom: function(place) {
        if(place == ""){
            Kyan.onDirectionsUnavailable(Kyan.map, "Please enter something - I don't know where you're coming from!");
            return;
        };
        Kyan.onDirectionsSearch(Kyan.map); 
//        Kyan.debug("Directions from "+place);
        Kyan.Google.geocodePostcode(place, Kyan.showDirectionsFrom);
    },
    
    showDirectionsFrom: function(point) {
        Kyan.resetMap(Kyan.map);
        if(point) {
            var directionsString = point.y+','+point.x + " to " + Kyan.office.y+','+Kyan.office.x;
//            Kyan.debug("Directions: "+directionsString);
            Kyan.directions.load(directionsString);
        } else {
            Kyan.onDirectionsUnavailable(Kyan.map);
        }
    },
    
    onDirectionsSearch: function(map) {
        Kyan.directions.clear();
        Effect.BlindUp('directions_errors', {duration:0.2});
    },

    onDirectionsAvailable: function(map) {
    },

    onDirectionsFinished: function(map) {
        Kyan.directions.getMarker(1).hide();
        window.location.hash = '#directions';
        $('directions_container').style.display = '';
    },


    onDirectionsUnavailable: function(map, error) {
        error = error || "We couldn't find directions to Kyan from here. Please ensure you"+
        " have supplied a UK location &ndash; a postcode will give best"+
        " results.";
        $('directions_errors').innerHTML = error;
        Effect.BlindDown('directions_errors', {duration:0.2});
        $('saddr').blur();
        $('saddr').focus();
    }
};

Kyan.init();