var map = null;
var geocoder = null;
var restaurant_id = null;
var TB_WIDTH = 800;
var TB_HEIGHT = 500;
var markerOptions = {};
	
$(document).ready(function(){
	if (typeof GMap2 == 'undefined') return;
	tobj = document.getElementById("map_canvas");
    /*alert (tobj);*/
	map = new GMap2(tobj, { size:new GSize(726,443) } );
	geocoder = new GClientGeocoder();
	markerOptions = getMarkerOptions();
  	
	$('a.showmap').click(function(){
		var elm_id = $(this).attr('id');
		restaurant_id = elm_id.split("_")[1];
		var address = $(this).attr('rel');
		if (address != ""){
			address = getAddress(address);
			showAddress(address);
			//showMap();
		}
		return false;
	});	

	$('a.closeinfo').click(function(){
		var res_id = $(this).attr('rel');
		$('#info_'+res_id).hide();
	});
	
	$('a.mapresults').click(function(){
		geocoder.getLatLng('Киев', function(point){
			//map.setUIToDefault();
			map.setCenter(point, 11);
			
		});		
		$('.restaurant_address').each(function(){
			var address = $(this).text();
			var res_id = $(this).attr('rel');
			if (address != ""){
				address = getAddress(address);
				geocoder.getLatLng(address, function setMarkers(point){
					//markerOptions = getMarkerOptions();
    				var marker = createMarker(point,res_id, markerOptions);//new GMarker(point, markerOptions);
    				map.addOverlay(marker);
					
					openInfo(res_id);
					var html = $('#info_'+res_id)//.html();
					var info = new Infowin(point, html);
  					map.addOverlay(info);
					$('#info_'+res_id).hide();
				});
			}			
		});
		showMap();
		return false;
	});
});

$(window).unload( function () { GUnload(); } );

function getAddress(address){
	return address = 'Киев, ' + address;
}
function getMarkerOptions(){
	// Create our "tiny" marker icon
	var tinyIcon = new GIcon();
	tinyIcon.image = "/images/map-marker.png";
	//tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	tinyIcon.iconSize = new GSize(25, 35);
	//tinyIcon.shadowSize = new GSize(22, 20);
	tinyIcon.iconAnchor = new GPoint(6, 30);
	tinyIcon.infoWindowAnchor = new GPoint(5, 1);
	
	// Set up our GMarkerOptions object literal
	markerOptions = { icon:tinyIcon };	
	return markerOptions;
}	

function showAddress(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 16);
		showMap();
		
        var html = $('#info_'+restaurant_id)//.html();
        openInfo(restaurant_id);
	
		//markerOptions = getMarkerOptions();
			
        var marker = createMarker(point, restaurant_id, markerOptions);//new GMarker(point, markerOptions);
        map.addOverlay(marker);
        //marker.openInfoWindowHtml(html);
  		var info = new Infowin(point, html);
  		map.addOverlay(info);
		map.panBy(new GSize(20, 150));
      }
    }
  );
}

function openInfo(id){
	$('.map-info').hide();
	$('#info_'+id).show();
}
function createMarker(point,id, options) {
    var marker = new GMarker(point, options);
    GEvent.addListener(marker, "click", function() {
      openInfo(id);
    });
    return marker;
}
function showMap(){
	showModal('#modal-map', cancelMap);
}
function cancelMap(){
	$('#modal-map').hide();
	$('#sd_overlay').remove()

	return false
} 

function showModal(dialogEl, cancelFunc) {
	// overlay
	oObj = jQuery('<div />')
      .attr('id', 'sd_overlay')
      .addClass('sd_overlay')
      .css({
        position: 'absolute',
        width: $(window).width(),
        height: $(document).height(),
        //opacity: '0.72',
        zIndex: '3999'
      })
      .appendTo(document.body);
	if (cancelFunc) $(oObj).bind('click', cancelFunc)
	
	$(dialogEl).css({'zIndex':4000}).show();
	modal_position(dialogEl);
}
function modal_position(dialogEl) {
	$(dialogEl).css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	//if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$(dialogEl).css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	//}
}


function Infowin(latlng, html) {

        this.latlng_ = latlng;
        this.html_ = html;
        this.prototype = new GOverlay();
 
        // Creates the DIV representing the infowindow
        this.initialize = function(map) {
                var div = $('<div />');
                div.css({
                        position : 'absolute',
						width: 240
                }).appendTo(map.getPane(G_MAP_FLOAT_PANE))
 
                this.map_ = map;
                this.div_ = div;
 
                this.update(html);
        }
 
        this.update = function(html){
                this.html_ = html;
 
                this.div_.empty();

 
                var content = $('<div />').addClass('infowin-content').css({
                        'position' : 'relative',
                        'overflow' : 'hidden',
                        'max-height' : 420
                }).html(html);
 
 				content.appendTo(this.div_);
 
                this.redraw(true);
        }
 
        // Remove the main DIV from the map pane

        this.remove = function() {

          this.div_.remove();

        }
 
        // Copy our data to a new instance

        this.copy = function() {

          return new Infowin(this.latlng_, this.html_);

        }

        // Redraw based on the current projection and zoom level

        this.redraw = function(force) {

                if (!force) return;

                var point = this.map_.fromLatLngToDivPixel(this.latlng_);
                // Now position our DIV based on the DIV coordinates of our bounds

                this.div_.css({
                        left : point.x - 47,
                        top : point.y - this.div_.height() - 22

                });

        }

}
