/*
 * Google Maps API for place/locality
 */
 
$(function () 
{
    var countryMap = $('#country');
	countryMap.css({width:'600px', height:'400px'})

	var map = this.map = new google.maps.Map(
		document.getElementById('country'), 
		{ 
			center: window.theseLatLng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			zoom:11,
			navigationControl: true,
		  	mapTypeControl: true,
		  	scaleControl: true
		}
	);

	var marker = new google.maps.Marker({
		position: theseLatLng,
		map: map,
		title: window.markerTitle
	});
    
    /*
     * Adm regions
     */
	var geocoder = new google.maps.Geocoder();
    
	geocoder.geocode(
		{
			latLng: theseLatLng,
			region: window.geocodeRegion
		}, 
		function (results, status)
		{
			if(status == google.maps.GeocoderStatus.OK) 
			{
				if (results[0]) setMap(results[0]);
			} 
			else //show country area
			{ 
                geocoder.geocode(
                    {
            			region: window.geocodeRegion,
            			address: window.geocodeAddress
                    },
                    function (results, status)
                    {
                        if(status == google.maps.GeocoderStatus.OK) 
           			    {
               				if (results[0]) setMap(results[0]);
               			} 
                    }
                );
			}
		}
	);
	
	function setMap (res)
	{
	    map.setCenter(res.geometry.location);
		map.fitBounds(res.geometry.viewport);
	}
	
	/*
	 * Traffic jams
	 */
	
	var trafficOptions = {
      getTileUrl: function(coord, zoom) {
        return "http://mt3.google.com/mapstt?" +
        "zoom=" + zoom + "&x=" + coord.x + "&y=" + coord.y + "&client=google";
      },
      tileSize: new google.maps.Size(256, 256),
      isPng: true
    };

    var trafficMapType = new google.maps.ImageMapType(trafficOptions);
    
    var expireDate = new Date();
    expireDate.setFullYear(expireDate.getFullYear() + 2);
    
    var checkbox = $('#show-traffic-jams').get(0);
    
    $(checkbox).click(function () {
        $.cookie(
            'show-traffic-jams', 
            this.checked, 
            {path: '/', expires: expireDate}
        );
        if (this.checked)
            map.overlayMapTypes.insertAt(0, trafficMapType);
        else
            map.overlayMapTypes.removeAt(0);
    });
    
    checkbox.checked = $.cookie('show-traffic-jams') == 'true';    
    
    if (checkbox.checked)
        map.overlayMapTypes.insertAt(0, trafficMapType);
})