

gmap = {



  map: null,



  readyCallbacks: [],



  infoBox: null,



  items: [],



  init: function() {

    var script = document.createElement("script");

    script.type = "text/javascript";

    script.src = "http://maps.google.com/maps/api/js?language="+current_lang+"&sensor=false&callback=gmap.setup";

    document.body.appendChild(script);

  },



  mapReady: function(fn) {

    if (gmap.map == null) {

      gmap.readyCallbacks.push(fn);

    } else {

      fn();

    }

  },



  setup: function() {

    var center = new google.maps.LatLng(34.93186891139165, 32.8977087314453);

    var options = {

      zoom: 10,

      center: center,

      mapTypeId: google.maps.MapTypeId.ROADMAP,

      scrollwheel: false,



      mapTypeControl: true,

      mapTypeControlOptions: {

        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,

        position: google.maps.ControlPosition.TOP_RIGHT

      },



      navigationControl: true,

      navigationControlOptions: {

        style: google.maps.NavigationControlStyle.ZOOM_PAN,

        position: google.maps.ControlPosition.TOP_LEFT

      },

      scaleControl: false,

      streetViewControl: false

    }



    var searchBounds = document.id('search-block').getDimensions(true);

    var mapHeight = window.getSize().y;



    document.id('map').setStyle('height', Math.max(600, mapHeight));

    document.id('search-results').setStyle('height', Math.max(600, mapHeight));

    document.id('listitems').getParent().setStyle('height', Math.max(530, mapHeight - 70));



	scrollBox = new MooScroller($('scrollbox'), $('knob-middle'), {mode: 'vertical'});



    gmap.map = new google.maps.Map(document.getElementById('map'), options);



	overlay = new google.maps.OverlayView();

    overlay.draw = function() {};

    overlay.setMap(gmap.map);



    gmap.readyCallbacks.each(function(fn) {

      fn();

    });

    gmap.readyCallbacks = [];



    var script = document.createElement("script");

    script.type = "text/javascript";

    script.src = "/app/assets/js/bfinfowindow.js";

    document.body.appendChild(script);

  },



  loadLatestItems: function(url) {

    if (!url) url = '/' + current_lang + '/latest-items.json';



    new Request.JSON({

      url: url,

      secure: false,

      onSuccess: function(items) {

        gmap.addMarkers(items);

      }

    }).send();

  },



  addMarker: function(item) {

    var marker = new google.maps.Marker({

      position: new google.maps.LatLng(item.map_lat, item.map_lng),

      map: gmap.map,

      icon: (item.latest == 0) ? '/app/assets/i/map-marker.png' : '/app/assets/i/map-marker-hot.png'

    });

    google.maps.event.addListener(marker, 'click', function(event) {

      gmap.showInfoBox(item, marker);

    });

    return marker;

  },



  addMarkers: function(items) {

    gmap.items = $H(items);



    var bounds = new google.maps.LatLngBounds();

    gmap.items.each(function(item) {

      var marker = gmap.addMarker(item);

      item.marker = marker;

      bounds.extend(marker.getPosition());

    });



    // Fit map with new bounds

    if (!bounds.isEmpty()) {

      gmap.map.fitBounds(bounds);

	  var listener = google.maps.event.addListener(gmap.map, 'idle', function() {

	    if (gmap.map.getZoom() > 15) gmap.map.setZoom(15);

	    google.maps.event.removeListener(listener);

	  });

    }

  },



  setBounds: function(x1, y1, x2, y2) {

    var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(x1, y1), new google.maps.LatLng(x2, y2));

    if (!bounds.isEmpty()) {

      gmap.map.fitBounds(bounds);

    }

  },



  showInfoBox: function(item, marker) {

    if (typeof('BFInfoWindow') == 'undefined') return;



    if (!gmap.infoBox) {

        var myOptions = {

            closeBoxMargin: "10px 2px 2px 2px",

			closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"

        };

      gmap.infoBox = new BFInfoWindow(myOptions);

    }

    var img = (item.thumb.trim() != '') ? item.thumb : '/app/assets/i/no-photo.gif';

    var content = '<b>' + item.city + '</b> ' + item.region + '<br/>' + item.note;

    var statuses = [];

	var favorite;

    if (item.sold) {

      statuses.push('item-sold');

    }



	if (item.infav=='1') {

		favorite = '<span id="favin_item_' + item.id + '"' + item.favclass + '>' + item.favtext + '</span>';

	} else {

		favorite = '<a id="favin_item_' + item.id + '"' + item.favclass + ' href="' + item.favurl + '"><span>' + item.favtext + '</span></a>';

	}



	if (item.favorite!='') {

		favorite = item.favorite

	}



    gmap.infoBox.setContent('\
<div class="toright"><a href="javascript:gmap.infoBox.close()"><img src="/app/assets/i/info_close.gif" border="0" alt="" /></a></div>\
<div class="item-preview ' + statuses.join(' ') + '">\
  <div class="item-preview-photo">\
    <a href="' + item.url + '"><img src="' + img + '" border="0" alt="" /><span class="sell">' + item.sellText + '</span></a>\
  </div>\
  <div class="item-preview-content">' + content + '<div class="item-preview-more">\
	<span class="price">&euro; ' + item.price + '</span>\
	<a href="' + item.url + '"><span>' + item.more + '</span></a>\
	<span style="vertical-align:top;">' + favorite + '</span>\
	</div>\
  </div>\
</div>');

    gmap.infoBox.open(gmap.map, marker);

  },



  show: function(id) {

    if (!gmap.map) return;

  	var item = gmap.items[id];

  	if (!item || !item.marker) return;



	gmap.map.setZoom(map_zoom);



	var center = item.marker.getPosition();

	gmap.map.setCenter(center);



	var listener = google.maps.event.addListener(gmap.map, 'idle', function() {

	  gmap.showInfoBox(item, item.marker);

	  google.maps.event.removeListener(listener);

	});

  }



};



window.addEvent('domready', gmap.init);



