// Properties

_tab = 0;
_hasMap = false;

// Functions

function showInformation(){
	if (_tab == 0) return;
	_tab = 0;
	$('#location-information-tab').addClass('tab-on');
	$('#location-map-tab').removeClass('tab-on');
	$('#location-information').css({display:'block'});
	$('#location-map').css({display:'none'});
	
}
function showMap(){
	if (_tab == 1) return;
	_tab = 1;
	$('#location-information-tab').removeClass('tab-on');
	$('#location-map-tab').addClass('tab-on');
	$('#location-information').css({display:'none'});
	$('#location-map').css({display:'block'});
	if ( ! _hasMap) {
		if (GBrowserIsCompatible()) {
			var geocoder = new GClientGeocoder();
			if (geocoder) {
				geocoder.getLatLng(_address, function(point){
					if ( ! point) {
						alert(_address + ' not found');
					} else {
						var div = document.getElementById('gmap');
						if (div) {
							var options = {size:new GSize(540,400)};
							var map = new GMap2(div, options);
							if (map) {
								map.setCenter(point, 16);
								map.addControl(new GSmallMapControl());
								var marker = new GMarker(point);
								if (marker) {
									map.addOverlay(marker);
									marker.openInfoWindowHtml('<div class="infoWindow"><h5>'+_title+'</h5><address>'+_address+'</address></div>');
								}
								_hasMap = true;
							}
						}
					}
				});
			}
		}
	}
}

// DOM Ready

$(document).ready(function(){
	
	// Move map tab
	var mapTab = $('#location-map-tab');
	mapTab.css({display:'block'});
	mapTab.clone().insertAfter('#location-information-tab');
	$(mapTab).remove();
	
	// Add tab class
	$('#location-information-tab').addClass('tab-on');
	
	// Hide map
	$('#location-map').css({display:'none'});
	
	// Link information tab
	$('#location-information-tab h4').wrapInner('<a href="#" />');
	$('#location-information-tab a').click(function(){
		showInformation();
		return false;
	});
	// Link map tab
	$('#location-map-tab h4').wrapInner('<a href="#" />');
	$('#location-map-tab a').click(function(){
		showMap();
		return false;
	});
});

// Window Unload

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

