// JavaScript Document

jQuery(document).ready(function(){
	
	puppet();
	leave();
	contactForm();
	footerExtend();
});

function puppet(){
	//
	// loop through abd add class of section to each area
	//
	jQuery('area').each(function(i){
		jQuery(this).addClass('section');
	});
	
	//
		// when you click a section expand the div
		//

function whatDistance(el) {
	var whatParent;
	var parts = {
					'head': 1,
					'neck': 2,
					'shoulder1': 3,
					'shoulder2': 3,
					'back': 4,
					'pelvis': 5,
					'arm1': 6,
					'arm2': 6,
					'hand1': 7,
					'hand2': 7,
					'leg1': 8,
					'leg2': 8,
					'feet': 9
				};
			
			whatParentOutput = parts[el] || 0;
			
			var whatDistance = 0;
			var whatSection = ('.section' + whatParent);
			
			return (whatParentOutput * -160);		
}
		
		jQuery('.section').mouseover(function(){
			
			jQuery('#puppet').css('backgroundPosition',whatDistance(jQuery(this).attr("id")));

		});
	//
	// on click 
	//
	jQuery('.section').click(function(e){
		e.preventDefault();
		if (jQuery("#close").length < 1) {
			jQuery('#injuryCalculator').append('<a id="close" href="#">close</a>');
		} 
	
		jQuery('#puppet').css('backgroundPosition',whatDistance(jQuery(this).attr("id")));
		
		//
		// if open igonore animation
		//
		if(jQuery('.open').length < 1){
			jQuery('#injuryCalculator').animate({
				width : '896'
			});
			jQuery('#injuryCalculator').addClass('open');
		}
		//
		// if no div'import then create it
		//
		if (jQuery('#import').length != 0) {
			jQuery('#import').fadeOut(600, function(){ 
				jQuery('#import').remove();
				create();
			});
		} else {
			create();
		}
		
		function create(){
			var whatToLoad = ('/wp-content/themes/gormanhamilton/includes/injuries.php #head' + whatParentOutput);
			jQuery('#injuryCalculator').append('<div id="import"></div>');
			jQuery('#import').load(whatToLoad).css('display','none').delay(400).fadeIn('slow');
			
			openFancybox();
		}
	});
}

function leave(){
	jQuery('#close').live('click',function(e){
		e.preventDefault();
		jQuery('#injuryCalculator').removeClass('open');
		jQuery('#close').remove();
		jQuery('#puppet').css('backgroundPosition',0);
		jQuery('#injuryCalculator').animate({
			width : '160'
		});
		jQuery('#import').remove();
	});	
}

function contactForm(){
	if(jQuery('#contactForm').length == 0) {
		jQuery('#page').append('<div id="contactForm"></div>');
		//
		// Bring form onto each page but making sure it is display none, ready to be called from Fancy Box
		//
		jQuery('#contactForm').load('/index.php/contact #wpcf7-f47-p48-o1').css('display','none');

		openFancybox();
	} else 
	{
		jQuery('#contactForm').remove();
		jQuery('#page').append('<div id="contactForm"></div>');
		jQuery('#contactForm').load('/index.php/contact #wpcf7-f47-p48-o1').css('display','none');

		openFancybox();
	}
}


function openFancybox() {
	//console.log('open');
	jQuery('a.button').live('click',function(){
		//console.log('click');
		jQuery.fancybox({
			'type' : 'inline',
			'href' : '#wpcf7-f47-p48-o1',
			'scrolling' : 'no',
			'overlayColor' : '#fff',
			'overlayOpacity' : 0.5,
			'showNavArrows' : 'true',
			'autoScale' : 'true',
			'onComplete': function(){
					formHandling();
				},
			'onClosed': function(){
				contactForm();	
			}
		});
	});
}


function formHandling(){
	jQuery('form.wpcf7-form').bind('submit', function(e){
		e.preventDefault();
		var theForm = jQuery(this);
		jQuery.ajax({
			type: theForm.attr('method'),
			url: theForm.attr('action'),
			data: theForm.serialize(),
			success: function(data){
				var resp = jQuery('div.wpcf7-response-output', data).text();
				
				if ( resp.indexOf('Validation') === -1 )
				{
					theForm.html(resp);
				}
				else
				{
					theForm.html(jQuery('form.wpcf7-form', data));
					formHandling();
				}
			}
		});
		return false;
		
	});
}

function footerExtend() {
	var windowHeight = jQuery(window).height();
	var footerHeight = jQuery('#colophon').height();
	var headerHeight = jQuery('header#branding').height();
	var contentHeight = jQuery('#primary').height();
	
	var siteHeight = (headerHeight + contentHeight + footerHeight);
	var computedFooterHeight = windowHeight - siteHeight;
	
	if (computedFooterHeight > footerHeight) {
		//console.log('greater than');
		jQuery('#colophon').css('height',computedFooterHeight);
	}
	//console.log('less than');
}
