// JavaScript Document

$(document).ready(function(){
	
	init_flexi_form();
	config_primary_nav();
	h1_add_span();
	init_target_blank();
	
	adjust_scape_bg();
	$(window).resize(function(){
		adjust_scape_bg();
	});
	
});

function init_target_blank(){
	$('.tb').each(function(){
		$(this).click(function(e){
				e.preventDefault();
				window.open(this.href);
				
		});
	
	});
}



/**
	H1 items need a preceeding span tag to show the nice
	fold around the edge. This adds it to the right place
	where needed.
**/
h1_add_span = function(){
	$('.body-text h1').each(function(){
		if($(this).find('h1 span').length < 1){
			$(this).prepend('<span></span>');
		}
	});
}


init_flexi_form = function(){
	
	$.flexiForm();
	
	//show loading circle before form is input
	$('#right-contact-form').show();
	$('#right-contact-form').html('<div class="loading"><p>Loading Contact Form</p></div>');
	

	$.flexiForm.addForm('external','http://www.fmbusinessschool.com/ajax/rightContact.html',{
									ajaxURL:'http://www.fmbusinessschool.com/ajax/formHandler.ajax.php',
									alertErrors: false,
									loadInto : '#right-contact-form',
									clearOnSend: true,
									successAnimStyle: 'slide',
									autoRespond: 			1, 				
									autoResponderFields: 	['email']
								});
}

/**
	This configures the actions for the primary navigation slide down
**/
config_primary_nav = function(){
	
	$('#navigation li').each(function(){
		
		var theheight = 0;
		theheight = $('ul', this).height();
		
		//set the css height to the actual height so slides work
		$(this).find('ul').css('height',$(this).find('ul:first').height());

		$(this).hover(
			function () {$('ul', this).slideDown(300);}, 	//show 
			function () {$('ul', this).slideUp(300);}		//hide
		);
	});
	
}

/**
	This function manages dynamically moving the background image picure so that the design looks right
	in a range of different window sizes.
**/
adjust_scape_bg = function(){
	
		var min_w = 1000;
		var max_w = 1600;
		var current_w = $(window).width();
		var bg_position_x = -300;
		var diff = (current_w - min_w) / 2;
		var new_position = bg_position_x;
		var bgImageWidth = 760;
		
		if(current_w > min_w && current_w <= max_w){
			
			new_position = bg_position_x + diff;
			
			
			
		}else if(current_w > max_w){
			
			new_position = 0;
			
		}else if(current_w <= min_w){
			
			new_position = bg_position_x;
			
		}
		
		$('#wrapper').css('background-position',new_position+'px 0px');
	
}
