function preload_page(page, link) {
	if(page < lead_pages.length && page_content[page] == null) {
		// do ajax request
		$.post(link, {ajax:"true"}, function(data) {
			page_content[page] = data;
		});
	}
}

function navigation_change(p) {
	// make sure this only happens if there is a page to move to
	if(p > -1 && p < lead_pages.length) {
		var lead_width = $('.lead_unit').width();
		// move content distance of window left (+) or right (-)
		if(p > current_page) {
			var move_out = lead_width;
		} else {
			var move_out = -lead_width;
		}

		// move current text out of box
		$('.lead_body').animate({ 'left': -move_out }, 500, 'easeInSine', function(){
			
			var html = '';
			
			// if it's the home page
			if(p != 0) {
				html = '<h1>'+lead_pages[p].title+'</h1>';
			} else {
				html = page_content[0];
			}
			
			html = page_content[p];
			
			// add contents to page
			$(this).html(html);
			
			do_after_loaded();
		
			// move content to other side - ready to come in
			$(this).css('left', move_out);
			// animate new text back in
			$(this).animate({ 'left': 60 }, 500, 'easeOutSine', function(){});
		});

		// animate background to make paralax motion
		// minus (page * ( (background image width - window width) / number of pages)
		var bg_pos = -(p * (($('.lead_unit .bg_img').width() - lead_width) / lead_pages.length));
		$('.lead_unit .bg_img').animate({
			'left': bg_pos
		},1100, 'easeInOutSine', function() {
			// aftereaseInOutSine
		});
		
		// set the new page ready for the next calculation						
		current_page = p;
		next_page = current_page + 1;
		prev_page = current_page - 1;
		if(next_page < lead_pages.length) {
			preload_page(next_page, lead_pages[next_page].link);
		}
		
		// show / hide left button
		if(current_page == 0) {
			$('.lead_left').animate({
				'opacity': 0
			}, 500, 'jswing', function(){});
		} else {
			$('.lead_left').animate({
				'opacity': 1
			}, 500, 'swing', function(){});
		}
		
		// show / hide right button
		if(current_page == (lead_pages.length-1)) {
			$('.lead_right').animate({
				'opacity': 0
			}, 500, 'jswing', function(){});
		} else {
			$('.lead_right').animate({
				'opacity': 1
			}, 500, 'jswing', function(){});
		}
		
	} // end if
}

function flash_animation(element) {
	// remove these 4 line to go back to old way
	$(element).css('opacity', 1);
	$('.arrow').css('display', 'block');
	$('.menu_img').css('display', 'none');
	//play = 'stop';

	if(play == 'play') {
		setTimeout('check_highlight_menu()', 15000);
		/*
		$(element).animate({
			'opacity': 0.4
		}, 1000, 'swing', function(){
			$(element).animate({
				'opacity': 1
			}, 1000, 'swing', function(){
				setTimeout('flash_animation("'+element+'")', 1000);
			});
		});
		*/
	}
	
	$(element).click(function(){
		play = 'stop';
		$(element).css('opacity', 1);
		$('.arrow').css('display', 'block');
		$('.menu_img').css('display', 'none');
		remove_highlight();
	});
}

function do_after_loaded() {
	// once new page has been loaded - set the new links
	$('.pagination a').click(function(ev){
		// stop linking to new page and set href as link
		ev.preventDefault();
		var link = $(this).attr('href');
		
		// make contentce disappear
		$('.lead_body').animate({ 'opacity': 0 }, 1000, 'swing', function(){
			$(this).html('<p>Loading...</p>').animate({'opacity': '1'}, 50, function(){
				// start loading new page
				$.post(link, {ajax:"true"}, function(data) {
					pagination_load(data);
				});
			});
			
		});		
	});
}

function reorganise() {
	$('.reorganise').each(function(){

		var ls_length = $(this).find('li').length;
		var output = '';
		var list_1 = 0;
		var list_2 = Math.ceil((ls_length / 3));
		var list_3 = Math.ceil((ls_length / 3)) * 2;
		var c_0 = c_1 = c_2 = false;
		var i;
		
		for(i=0; i<ls_length; i++) {
		
			// first list
			if(i >= list_1 && i < list_2) {
				if(c_0 == false) {
					output += '<ul>'
					c_0 = true;
				}
				output += '<li>'+$(this).find('li:eq('+i+')').html()+'</li>';
			}
			// second list
			if(i >= list_2 && i < list_3) {
				if(c_1 == false) {
					output += '</ul><ul>'
					c_1 = true;
				}
				output += '<li>'+$(this).find('li:eq('+i+')').html()+'</li>';
			}
			// third list
			if(i >= list_3) {
				if(c_2 == false) {
					output += '</ul><ul>'
					c_2 = true;
				}
				output += '<li>'+$(this).find('li:eq('+i+')').html()+'</li>';
			}
			
		}
		
		output += '</ul>';
		
		$(this).html(output);
		$(this).find('ul').css({
			'float': 'left',
			'width': '215px',
			'margin-bottom' : '0px'
		});		
	});
	
	$('.reorganise2').each(function(){
	
		var ls_length = $(this).find('li').length;
		var output = '';
		var list_1 = 0;
		var list_2 = (ls_length / 2);
		var c_0 = c_1 = false;
		var i;
		
		for(i=0; i<ls_length; i++) {
		
			// first list
			if(i < list_2) {
				if(c_0 == false) {
					output += '<ul>'
					c_0 = true;
				}
				output += '<li>'+$(this).find('li:eq('+i+')').html()+'</li>';
			}
			// second list
			if(i >= list_2) {
				if(c_1 == false) {
					output += '</ul><ul>'
					c_1 = true;
				}
				output += '<li>'+$(this).find('li:eq('+i+')').html()+'</li>';
			}
			
		}
		
		output += '</ul>';
		
		$(this).html(output);
		$(this).find('ul').css({
			'float': 'left',
			'width': '152px',
			'margin-bottom' : '0px'
		});		

	});
	
}

function highlight_menu() {
	var html = '<div class="highlight_menu"><p>Open the menu</p></div>';
	$('.lead_unit').append(html);
	$('.highlight_menu').css({
		'position' : 'absolute',
		'bottom' : '-100px',
		'right' : '42px',
		'z-index' : '1001',
		'opacity' : '0',
		'background-image' : 'url(/images/mesh_black.png)',
		'padding' : '0 10px'
	}).find('p').css({
		'color' : '#ffffff',
		'background-image' : 'url(/images/arrow_right.png)',
		'background-position' : 'center right',
		'background-repeat' : 'no-repeat',
		'padding-right' : '20px'
	});
	
	$('.highlight_menu').animate({
		'opacity' : '1',
		'bottom' : '10px'
	}, 1200, 'swing', function(){
		setTimeout('remove_highlight()', 4000);
	});
}

function remove_highlight() {
	$('.highlight_menu').animate({'opacity' : '0'}, 500, function(){
		$(this).remove();
	});
}

function check_highlight_menu() {
	if(play == 'play') {
		highlight_menu();
		setTimeout('check_highlight_menu()', 15000);
	}
}

function pagination_load(html) {
	// change code for the page for when it appears
	$('.lead_body').animate({'opacity': '0'}, 50, function(){
		$(this).html(html);
		// set the links ready for next time
		do_after_loaded();
		// animate everything back in
		$('.lead_body').animate({'opacity' : 1 }, 1000, 'swing', function() {});
	});
}

function set_pagination() {
	$('#thumbnails_pagination a').click(function(ev){
		ev.preventDefault();
		var href = $(this).attr('href') + " #thumbnails";
		var tmb = $('#thumbnails');
		$('.sub_unit h1').append('<img id="loader" src="/images/ajax-loader-small-white.gif" />');
		
		tmb.animate({'opacity':'0'}, 500, function(){
			tmb.load(href, function(){
				$('#loader').remove();
				tmb.animate({'opacity':'1'}, 500, function(){
					set_pagination();
				});
			});
		});
	});
}
