
$(function() {
	var accordion = $('#accordion');
	accordion.accordion();
	
	var splittedHash = window.location.hash.split('/');
	var hash = splittedHash[0];
	
	var id = splittedHash[1];
	if (id) {
		selectInfo(id, false);
	}
	
	if (hash) {
		var accordionItems = $('h3', accordion);
		var clickedItem = $('h3 a[href$=' + hash +']', accordion).parent()[0];
		var clickedIndex = $.inArray(clickedItem, accordionItems);
		accordion.accordion('activate', clickedIndex);
	}
	
	accordion.bind('accordionchange', function(e, ui) {
		var a = $('a', ui.newHeader);
		
		var tmp = a[0].hash;
		if (id) {
			tmp = tmp + '/' + id;
		}
		
		window.location.hash = tmp;
	});
	
});

function selectInfo(id, updateFragment) {
	updateFragment = updateFragment == null ? true : updateFragment;
	//rotate locations content 
	var divs = document.getElementById('infield_mainbody').getElementsByTagName('div');
	for (i = 0; i < divs.length; i++) {
		if (divs[i].className == 'info') divs[i].className = 'info invisible';
		if (divs[i].id == 'info'+id) divs[i].className = 'info';
		
	}

	if (updateFragment) {
		var hash = window.location.hash.split('/')[0];
		window.location.hash = hash + '/' + id;
	}
	
	return false;
}
