var curr_section	= 1;
var locked			= 1;

$(document).ready(function(){
	
	var paths = $.address.pathNames();
	
	if (paths.length > 0) {

		curr_section = paths[0].charAt(0);
		$('#p-' + curr_section).show();
		$('#c-' + curr_section).show();
		$('#nav a').removeClass('active');
		$('#n-' + curr_section).addClass('active');
		photo_cycle(curr_section);
		change_page();					
		
		if (paths.length > 1 && paths[1].length == 1) {

			$('#sub li[rel="' + paths[1].charAt(0) + '"]').trigger('click');
		}

	} else {
		
		$('#p-' + curr_section).show();
		$('#c-' + curr_section).show();
		photo_cycle(1);
		change_page();
	}

	$.address.change(function(event){
		
		if (locked != 1) {

			window.location = '/move.php?map=' + event.value;
		}

		locked = 0;
	});

	$('#photo-nav a').click(function(){
		
		$(this).blur();
	});

	$('#nav a, #logo').click(function(){

		var new_section = $(this).is('#logo') ? 1 : parseInt($(this).attr('id').substr(2));
		
		locked	= 1;
		var map		= ['the-farm', 'the-homes', 'the-land'];
		$.address.value(new_section.toString() + '/' + map[new_section - 1]);
		
		change_photos(new_section, curr_section);

		$('#c-' + curr_section).fadeOut('slow', function(){
				
			$('#c-' + new_section).fadeIn('slow', function(){
				
				change_page();
			});	
		});
		
		curr_section = new_section;
		
		if ($(this).is('#logo')) {
			
			$('#nav a').removeClass('active');
			$('#n-1').addClass('active');

		} else {
			
			$(this).parent().find('a').removeClass('active');
			$(this).addClass('active');
			$(this).blur();
		}
		
		return false;
	});
});

function change_page() {
	
	$('#sub ul').empty();

	$('#c-' + curr_section + ' h1').each(function(i){
		
		$('#sub ul').append('<li rel="' + i + '"><a href="javascript:;">' + $(this).html() + '</a></li>');
		$(this).attr('rel', i);
	});

	$('#sub li:first').addClass('active');
	$('#sub li').click(function(){
		
		locked = 1;
		var slug = $(this).find('a').html().replace(' ', '-');
		$.address.value(curr_section.toString()  + '/' + $(this).attr('rel').toString() + '/' + slug);
		$('#sub li').removeClass('active');
		$(this).addClass('active');
		$('#c-' + curr_section + ' h1:visible').hide();
		$('#c-' + curr_section + ' .copy:visible').hide();
		var h1 = $('#c-' + curr_section + ' h1[rel="' + $(this).attr('rel') + '"]');
		h1.fadeIn('slow');
		h1.next().fadeIn('slow');
		$(this).find('a').blur();
	});

	$('#c-' + curr_section + ' h1').not(':first').hide();
	$('#c-' + curr_section + ' .copy').not(':first').hide();

	$('#c-' + curr_section + ' h1:first').show();
	$('#c-' + curr_section + ' .copy:first').show();
}

function change_photos(n, x) {
	
	$('#p-' + x).cycle('stop');
	$('#p-' + x).hide('blind', { direction : 'vertical' }, 1600);
	$('#p-' + n).show('blind', { direction : 'vertical' }, 1600);
	photo_cycle(n);
}

function photo_cycle(n) {

	$('#p-' + n).cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 8000, 
		next:   '#next', 
		prev:   '#prev' 
	});
}
