/*
Stanton Marris javascript

Designed and built by Jonathan Brain
http://jonathanbrain.com
*/

//global variables
var home_banners = [ "strategy", "leadership", "organisation", "people" ];
var home_banner = 0;
var rotate_banners = true;

var home_testimonial_count = 0;
var home_testimonial_index = 0;


//set up page and events
$(document).ready( function() {
	smooth_scroll();
	align_quotes();
	consultant_names();
	home_testimonials();
	/* home_banner_hover(); 
	setTimeout( function() { show_next_banner(); }, 10000 ); */
	/* home_columns_align(); */
	pop_up_bind();
});


//add smooth-scrolling to elements
function smooth_scroll() {
	$('.scroll-to').click( function() {
		var target = $(this).attr( 'href' );
		var destination = $("[name='" + target.substring( 1, target.length ) + "']").offset().top;
		$('html:not(:animated),body:not(:animated)').animate({ scrollTop: destination-10 }, 500 );
		return false;
	});
}


//align client page quote boxes
function align_quotes() {
	var max_height = 0;
	$('#quotes .quote-text').each( function() {
		if ( $(this).height() > max_height ) {
			max_height = $(this).height();
		}
	});
	
	$('#quotes .quote-text').height(max_height);
}


//pop-up consultant names
function consultant_names() {
	$('#team-member-list a').hover( function() {
		var reach = 145 - $(this).find('.member-name').height();
		$(this).find('.member-name').animate({ top: reach }, 200 );
	}, function() {
		$(this).find('.member-name').animate({ top: '157' }, 200 );
	});
}


//home page banner buttons - set up hover states
function home_banner_hover() {
	//set up 
	$('#banners li:not(:first)').hide();
	$('#banner-buttons li:first').addClass('on');
	
	//hover states
	$('#banner-buttons a').mouseover( function() {
		//get id of this button
		var this_id = $(this).attr('id');
		
		//only refresh banner if it is not already shown
		if ( !$(this).parent().hasClass('on') ) {
			show_banner( this_id );
		}
		
		//stop the banners rotating
		rotate_banners = false;
	});
}

//show a specific banner
function show_banner( banner_name ) {
	//hide all banners
	$('#banners li').hide(); 
	
	//show matching banner
	$('#banners #banner-' + banner_name).fadeIn(500);
	
	//set button on
	$('#banner-buttons .on').removeClass('on');
	$('#banner-buttons #' + banner_name).parent().addClass('on');
}


//home page banner - show next
function show_next_banner() {
	//only rotate the banners if they have not been manually selected
	if ( rotate_banners ) {
		//get next banner
		home_banner++;
		if ( home_banner == 4 ) {
			home_banner = 0;
		}
		
		//display it
		show_banner( home_banners[home_banner] );
		
		//set timer
		setTimeout( function() { show_next_banner(); }, 10000 );
	}
}


//bind all pop-up links 
function pop_up_bind() {
	$('.pop-up').click( function() {
		var href = this.href;
		pop_up_show( href );
		
		return false;
	});
}


//show pop-up window
function pop_up_show( url ) {	

	try {
		if ( document.getElementById( 'popup-overlay' ) === null ) {
			$('body').append( '<div id="popup-overlay"></div><div id="popup-window"></div>' );
		}
		$('#popup-overlay').addClass( 'popup-overlay-bg' );
		$('#popup-overlay').height( $( document ).height() ); //stretch overlay to fill document
		
		$('#popup-window').css({ top: $( window ).scrollTop() + "px" }); //position popup at optimum viewpoint

		$('body').append( '<div id="popup-load"><img src="/images/pop_up_loader.gif" /></div>' );
		$('#popup-load').css({ top: $( window ).scrollTop() + "px" }); //position optimally
		$('#popup-load').show();//show loader

		$('#popup-iframe-content').remove();
		$('#popup-window').append( '<div id="pop-up-upper"><a href="#" onclick="pop_up_close(); return false;"><span>Close</span></a></div><iframe class="autoHeight" scrolling="no" frameborder="0" hspace="0" src="' + url + '" id="popup-iframe-content" name="popup-iframe-content" onload="pop_up_content(this);" allowTransparency="true"></iframe><div id="pop-up-lower"></div>' );	
		
	} 
	catch(e) { 
		alert(e); 
	}
}


//show pop-up iframe
function pop_up_content( e ) {

	$('#popup-load').hide();//hide loader
	$('#popup-window').show();
	e.height = 0;
	
	var iframeHeight = null;  
	if ( e.contentDocument ) {
		// Firefox, Opera  
		iframeHeight = e.contentDocument.body.scrollHeight;  
	}
	else if( e.contentWindow ) {
		// Internet Explorer  
		iframeHeight = e.contentWindow.document.body.offsetHeight;  
	}
	else if( e.document ) {
		// Others?  
		iframeHeight = e.document.body.offsetHeight;  
	}
	
	e.height = iframeHeight;
}


//close pop-up
function pop_up_close( reload_document ) {
	
	$('#popup-close').unbind('click');

	//slight fade-out on popup
	$('#popup-window').fadeOut( 200, function() { $('#popup-window,#popup-overlay').unbind().remove(); });

	$('#popup-load').remove(); //remove loader
	
	//reload the page if required
	if ( reload_document ) {
		window.location.href = window.location.href;
	}
}


//home testimonials
function home_testimonials() {
	$('#testimonial-list li:not(:first)').hide();
	
	var testimonial_index = 0;
	$('#testimonial-list li').each( function() {
		testimonial_index++;
		$(this).addClass('testimonial-index-' + testimonial_index);
	});
		
	home_testimonial_count = $('#testimonial-list li').length;
	home_testimonial_index = 1;

	//set timer
	setTimeout( function() { show_next_testimonial(); }, 5000 );
}

function show_next_testimonial() {
	
	//hide current testimonial
	$('#testimonial-list .testimonial-index-' + home_testimonial_index).hide();
	
	//update index
	home_testimonial_index++;
	if ( home_testimonial_index > home_testimonial_count ) {
		home_testimonial_index = 1;
	}
	
	//display new testimonial
	$('#testimonial-list .testimonial-index-' + home_testimonial_index).fadeIn(500);
	
	//set timer
	setTimeout( function() { show_next_testimonial(); }, 5000 );
}


//align the bottom of the three homepage columns
function home_columns_align() {
	
	var max_height = 0;
	$('#home-columns .column').each( function() {
		if ( $(this).height() > max_height ) {
			max_height = $(this).height();
		}
	});
	
	$('#home-columns .column').css({ height: max_height + 'px' });
}

