var Cuban = Class.create();
Object.extend(Cuban, {
    map: {map: null, centre: null, width: 560, height: 310}
  , cache: null
  , geocoder: null

  , setMapEventHandling: function() {
        var p;
        Cuban.geocoder = new GClientGeocoder; // separated into two steps because of Internet Explorer
        Cuban.geocoder.getLatLng($$('#rcolbook a:last-of-type')[0].readAttribute('href').replace(/\+/g, '%20').toQueryParams()['q'], function(point) {
            Cuban.map.centre = point;
        });
        Cuban.cache = [$div({id: 'xxxmap'}, p = $p({'title': _('Close')}, _('Close')), $div().setStyle({width: Cuban.map.width + 'px', height: Cuban.map.height + 'px'}))];
        p.observe('click', Cuban.toggleCache.bindAsEventListener(Cuban));
        $$('#rcolbook a:last-of-type')[0].observe('click', Cuban.toggleCache.bindAsEventListener(Cuban));
    }

//  , setMapEventHandling: function() {
//        var p;
//        Cuban.geocoder = new GClientGeocoder; // separated into two steps because of Internet Explorer
//        Cuban.geocoder.getLatLng($$('#rcolbook a:last-of-type')[0].readAttribute('href').replace(/\+/g, '%20').toQueryParams()['q'], function(point) {
//            Cuban.map.centre = point;
//        });
        /*
        Cuban.cache = [$div({id: 'xxxmap'}, p = $p({'title': _('Close')}, _('Close')), $div().setStyle({width: Cuban.map.width + 'px', height: Cuban.map.height + 'px'}))];
        p.observe('click', Cuban.toggleCache.bindAsEventListener(Cuban));
        $$('#rcolbook a:last-of-type')[0].observe('click', Cuban.toggleCache.bindAsEventListener(Cuban));
        */
//    }

  , bindMap: function() {
        Cuban.map.map = new GMap2($$('#xxxmap>div')[0]);
    }

  , centreMap: function() {
        Cuban.map.map.clearOverlays();
        Cuban.map.map.setCenter(Cuban.map.centre, 16);
        var icon = new GIcon;
        icon.image = 'images/starMarker.png';
        icon.shadow = 'images/starMarkerShadow.png';
        icon.iconSize = new GSize(18, 17);
        icon.shadowSize = new GSize(25, 18);
        icon.iconAnchor = new GPoint(9, 9);
        var markerOptions = {icon: icon};
        Cuban.map.map.addOverlay(new GMarker(Cuban.map.centre, markerOptions));

        var fragment = document.createDocumentFragment();
        fragment.appendChild(document.createTextNode('foosadfsadfsdf bar'));
        Cuban.map.map.openInfoWindow(Cuban.map.map.getCenter(), fragment);
        Cuban.map.map.addControl(new GLargeMapControl);
        Cuban.map.map.addControl(new GMapTypeControl)
    }

  , toggleCache: function(event) {
        var tmpCache = $('bookingbox').childElements();
        tmpCache.invoke('remove');
        Cuban.cache.each(function(element) {$('bookingbox').appendChild(element);});
        if ($('xxxmap') !== null) {
            Cuban.bindMap();
            Cuban.centreMap();
        }
        Cuban.centreMap();
        Cuban.cache = tmpCache;
        location.href = '#TOP';
        Cuban.map.map.setCenter(Cuban.map.centre, 16);
        event.stop();
    }

  , unloadMap: function() {
        GUnload();
    }
});
if (GBrowserIsCompatible()) {
    //document.observe('dom:loaded', Cuban.setMapEventHandling);
    //Event.observe(window, 'onunload', Cuban.unloadMap);
}