$(document).ready(function() {
	google.load('maps', '2', { 'callback': initialiseMap });
	$('#hcard_read_more').click(function() {
		$('.hcard_tabs a').eq(1).click();
		return false;
	});
	$('.hcard_tabs li').click(function() {
		var selected = $('.hcard_tabitemselected');
		var nth = null;
		if ($(selected).attr('id').match(/(\d+)$/)) {
			nth = RegExp.$1;
			$(selected).toggleClass('hcard_tabitemselected').toggleClass('hcard_tabitem');
			$('.hcard_tabcontent').eq(nth).toggleClass('hcard_tabcontent_hidden');
		}
		if ($(this).attr('id').match(/(\d+)$/)) {
			nth = RegExp.$1;
			$(this).toggleClass('hcard_tabitemselected').toggleClass('hcard_tabitem');
			$('.hcard_tabcontent').eq(nth).toggleClass('hcard_tabcontent_hidden');
		}
		return false;
	});
	$('#previous_image').click(function() {
		var current = $('.hcard_imagebox .image:not(.hidden)');
		var prev = $(current).prev('.hidden');
		if ($(prev).hasClass('image')) {
			$(current).toggleClass('hidden');
			$(prev).toggleClass('hidden');
			if ($(prev).attr('id').match(/(\d+)$/)) {
				var number = RegExp.$1;
				$('#hcard_image_number').text(number);
			}
		}			
		return false;
	});
	$('#next_image').click(function() {
		var current = $('.hcard_imagebox .image:not(.hidden)');
		var next = $(current).next('.hidden');
		if ($(next).hasClass('image')) {
			$(current).toggleClass('hidden');
			$(next).toggleClass('hidden');
			if ($(next).attr('id').match(/(\d+)$/)) {
				var number = RegExp.$1;
				$('#hcard_image_number').text(number);
			}
		}
		return false;
	});
	$('.hcard_imagebox a').lightBox({
		fixedNavigation: true,
		txtImage: 'Kuva',
		txtOf: '/'
	});
});

function initialiseMap() {
	if (!GBrowserIsCompatible()) return;

	var target = document.getElementById('hcard_map');
	if (!target || target.isInitialised) return;

	target.map = new GMap2(target);
	target.map.setCenter(new GLatLng(65.109148,26.323242),5);
    target.map.addControl(new GLargeMapControl3D());
    target.map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100,10)));
    target.bounds = new GLatLngBounds();
    
    var coords = $('#hcard_coords').text().split(',');
    if (!(coords[0] && coords[1])) return;
    
	var point = new GLatLng(coords[0], coords[1]);
	target.bounds.extend(point);
	var marker = new GMarker(point);
	target.map.addOverlay(marker);
	target.map.setCenter(target.bounds.getCenter(), Math.min(target.map.getBoundsZoomLevel(target.bounds), 15));
    
    target.isInitialised = true;
}
