jQuery(document).ready(function() {
																
	locationHash = window.location.hash;
	locationHash = locationHash.substr(1,locationHash.length);
	pathname = window.location.pathname.split('/');
	if(locationHash.length != 0){
		window.location.href = '/' + pathname[1] + '/' + locationHash;
		return true;
	} 
	
	$('#table-selection a').click(nextDataClicker);
	$('#selected-path li').click(nextDataTrigger);
	
	dataHistoty = new Object();
	unFocus.History.addEventListener('historyChange',historyListener);
});



function nextDataClicker(){
	$(this).parent().addClass('selected').parent().find('li').not($(this).parent()).removeClass('selected');
	
	nextCells = $(this).parent().parent().parent().nextAll('td').empty();
	
	if($(nextCells).length == 0) return true;
	
	nextDataField = $(this).parent().parent().parent().next('td').addClass('loading');
	
	href = window.location.host == 'localhost' || window.location.host == '' ? this.href : this.href.replace(/(\/[a-zA-Z0-9-]+\/)(.*)/,'$1ajax/$2');
	scheme = window.location.host == 'localhost' || window.location.host == '' ? this.href : $(this).attr('href').replace(/(\/[a-zA-Z0-9-]+\/)(.*)/,'$2');
		
	dataHistoty[scheme] = $(this);
	unFocus.History.addHistory(scheme);
		
	$.get(href, {debug:0}, function(data){
		$(nextDataField).removeClass('loading').html(data).find('a').click(nextDataClicker);
	});
	
	$('#selected-path li').remove();
	$('#table-selection li.selected').clone().appendTo('#selected-path > ul');	
	
	$('#selected-path li').click(nextDataTrigger);
	
	return false;
}
function nextDataTrigger(){
	$('#table-selection .selected a').eq($('#selected-path li').index($(this))).trigger('click');
	return false;
}

function historyListener(historyHash){
	$(dataHistoty[historyHash]).trigger('click');
};
