// Run when the page has loaded
$('document').ready(function() {
	
	$('.pager a.item:first').parent().addClass('active');
	
	$('.pager a.item').click(function() {
		
		// Make appropriate li active
		$(this).parent().addClass('active');
		$(this).parent().siblings().removeClass('active');
		
		// Determine which item was clicked on
		var item = $(this).parent().prevAll().length;
		
		jump_to_item(item);
		
		return false;
		
	});
	
	$('.box.outer_highlight').hover(function() {
		$(this).addClass('highlight');
	}, function() {
		$(this).removeClass('highlight');
	});
	
	$('.hz_scroll_pager a:first').parent().addClass('active');
	
	$('.hz_scroll_pager a').click(function() {
		
		// Make appropriate li active
		$(this).parent().addClass('active');
		$(this).parent().siblings().removeClass('active');
		
		// Determine which item was clicked on
		var item = $(this).parent().prevAll().length;
		
		jump_to_hz_item(item);
		
		return false;
		
	});
	
	
	$('.hz_scroll_panel_nav a.next').click(function() {
		$('.hz_scroll_pager .active a').parent().next().find('a').click();
		return false;
	});
	
	$('.hz_scroll_panel_nav a.prev').click(function() {
		$('.hz_scroll_pager .active a').parent().prev().find('a').click();
		return false;
	});
	
	// This is for IE6 and IE7
	if (jQuery.browser.msie && (parseInt(jQuery.browser.version) == 6 || parseInt(jQuery.browser.version) == 7)) {
		$('a.block *').click(function() {
			window.location = $(this).parents('a').attr('href');
			return false; // Prevent bubbling
		});
	}
	
	$('.fader img').hide();
	
	$(window).load(function() { // Make sure the images are loaded before we calculate heights (for webkit)
		
		$('.fader img').show();

		// Only run if there's more than 1 news item
		$('.fader').each(function() {
		
			var max_img_height = 0;
			$(this).find('img').each(function() {
				if ($(this).height() > max_img_height) {
					max_img_height = $(this).height();
				}
			});
		
			$(this).height(max_img_height);
		
			var cur_fader = this;
		
			if ($(this).find('img').length > 1) {
			
				$(this).find('img:eq(0)').addClass('current');
				$(this).find('img:gt(0)').hide();
			
				setInterval(function() {
					fade_to_next_photo(cur_fader);
				}, 4000);
			}
		});
	
	});
	
	// Hide all but the first promo box
	$('.promo_box').css('position', 'absolute');

	home_promo_animation(); // Star the animation
	
	$('.promo_box').hover(function() {
		stop_home_promo();
	}, function() {
		home_promo_animation();
	});
	
	// Flash placement...
	if ($.flash.available) {
		$('.flash_container').each(function() {
			$(this).flash({
				swf: $(this).data('src'),
				width: $(this).data('width'),
				height: $(this).data('height'),
				wmode: $(this).data('wmode')
			});
		});
	}
	
	// Make corporate finance links all activate when a single one is hovered
	$('ul li a.linked').hover(function() {		
		$(this).parent().siblings('li').find('a').addClass('hover');
	}, function() {
		$(this).parent().siblings('li').find('a').removeClass('hover');	
	});
	
});


var promo_interval;

function home_promo_animation() {
	
	// If there is more than one promo box
	if ($('.promo_box').length > 1) {
		
		promo_interval = setInterval(function() {
			
			var cur = $('.promo_box:visible');
			
			if (cur.next().length > 0) {
				cur.fadeOut(1000);
				cur.next().fadeIn(1000);
			} else {
				cur.fadeOut(1000);
				$('.promo_box:first').fadeIn(1000);
			}
			
		}, 6000);
		
	}
	
}

function stop_home_promo() {
	clearInterval(promo_interval);
}



function jump_to_item(item_number) {
	
	$('.overview').stop();
	
	var offset = 0;

	$('.overview > .item:lt(' + item_number + ')').each(function() {
		offset += $(this).height();
	});
	
	$('.overview').animate({
		top: offset * -1
	}, 500, 'swing');
	
}

function jump_to_hz_item(item_number) {
	
	$('.hz_scroll_panel').stop();
	
	var offset = 0;

	$('.hz_scroll_panel > .item:lt(' + item_number + ')').each(function() {
		offset += $(this).width() + 20;
	});
	
	$('.hz_scroll_panel').animate({
		left: offset * -1
	}, 500, 'swing');
	
}


function fade_to_next_photo(fader_ref) {
	
	mark_current_fader_photo(fader_ref);

	$(fader_ref).find('img').not('.current').fadeOut(1000);

	$(fader_ref).find('img.current').fadeIn(1000);
	
}

function mark_current_fader_photo(fader_ref) {
	$(fader_ref).find('img').not(get_next_fader_photo(fader_ref).addClass('current')).removeClass('current');
}

function get_next_fader_photo(fader_ref) {
	
	// Determine next item
	var next_photo = $(fader_ref).find('img.current:last').next();
	
	// Defatult to first item if no items are selected
	if (next_photo.length < 1) next_photo = $(fader_ref).find('img:first');
	
	return next_photo;
}

