// JavaScript Document

//Document Ready
jQuery(function(){
	
	//Remove wings background from last subsection menu.
	if (jQuery('#SubSectionNavigation').length > 0){
		jQuery('#SubSectionNavigation ul:first li:last').addClass('NoWings');
	}
	
	if(jQuery('#Private').length > 0 || jQuery('#Open').length > 0){
		//Hide BC nav arrows.
		jQuery('#SubSectionNavigation li img').remove();
		
		//Make normal dropdowns work.
		jQuery('#SubSectionNavigation ul:first li:first').hover(function(){
		jQuery(this).children('ul')
			.stop('true', 'true')
			.fadeIn('300');
		}, function(){	
			jQuery(this).children('ul')
				.stop('true', 'true')
				.fadeOut('300');	
		});
	}
		
	//Adds the wing to the right of the h1 and h2.
	var HeadingWidth = "";
	var MarginLeft = "";
	var MarginRights = "";
	
	jQuery('h1').each(function(i){
		HeadingWidth = jQuery(this).width() + 15;
		if (jQuery(this).prev().hasClass('ActivityImage') == true ){			
			HeadingWidth = HeadingWidth + jQuery(this).prev().width();
			//Add up the margin.
			MarginLeft = parseFloat((jQuery(this).prev().css('marginLeft')).replace('auto', '0'));			
			MarginRight = parseFloat((jQuery(this).prev().css('marginRight')).replace('auto', '0').replace('px',''));
			HeadingWidth =  MarginLeft + MarginRight + HeadingWidth + 14; //I do not know why the "14" is required..
		}
		jQuery(this).after('<div class="RightWing" style="margin-left:' + HeadingWidth + 'px"></div>');
	});
	
	//Add topnav cover
	jQuery('#cat_14623_divs').append('<div id="TopNavOverLay"></div>');
	
	//Set TopNav current status
	var CurrentPage = location.pathname;
	if(jQuery('#nav_14623 li a[href="' + CurrentPage + '"]').length > 0){
		jQuery('#nav_14623 li a[href="' + CurrentPage + '"]').addClass('Current');
		jQuery('#nav_14623 li a[href="' + CurrentPage + '"]').animate({
			top: '-=10'
		}, 100,function(){
		});
	}
	else if(CurrentPage == '/'){
		jQuery('#nav_14623 li a[href="/home"]').addClass('Current');
		jQuery('#nav_14623 li a[href="/home"]').animate({
			top: '-=10'
		}, 100,function(){
		});
	}
	
	//TopNav Hover Animation
	jQuery('#nav_14623 li a').hover(function(){		
										
		if(jQuery(this).parent('li').attr('id') == 'TopNavHome' &&  jQuery(this).hasClass('Current') == false){//Animation for the home link.
			jQuery(this).animate({
				top: '-=10'
			}, 100,function(){
			});
		}
		else if(jQuery(this).parent('li').attr('id') == 'TopNavAboutUs' &&  jQuery(this).hasClass('Current') == false){//Animation for the about us link.
			jQuery(this).animate({
				top: '-=10'
			}, 100,function(){
			});
		}
		else if(jQuery(this).parent('li').attr('id') == 'TopNavContactUs' &&  jQuery(this).hasClass('Current') == false){//Animation for the contact us link.
			jQuery(this).animate({
				left: '+=1',
				top: '-=10'
			}, 100,function(){
			});
		}
	}, function(){
		if(jQuery(this).parent('li').attr('id') == 'TopNavHome' &&  jQuery(this).hasClass('Current') == false){//Animation for the home link.
			jQuery(this).animate({
				top: '+=10'
			}, 100,function(){
			});
		}
		else if(jQuery(this).parent('li').attr('id') == 'TopNavAboutUs' &&  jQuery(this).hasClass('Current') == false){//Animation for the about us link.
			jQuery(this).animate({
				top: '+=10'
			}, 100,function(){
			});
		}
		else if(jQuery(this).parent('li').attr('id') == 'TopNavContactUs' &&  jQuery(this).hasClass('Current') == false){//Animation for the contact us link.		
			jQuery(this).animate({
				left: '-=1',
				top: '+=10'
			}, 100,function(){
			});
		}
	});
	
	/* Generic*/
	//Back button click.
	jQuery('#Back').click(function(){
		if(jQuery(this).hasClass('absolute') == false){
			history.go(-1);
		}
		else{
			window.location = "http://greatrace.com.au/contact-us"
		}
	});

	/* Corporate */	
	//Moves the #DropDown div inside the "Activities" li.
	var DropDownDiv = '<div id="DropDown">' + jQuery('#DropDown').html() + '</div>';
	jQuery('#DropDown').remove();
	jQuery('#Corporate #SubSectionNavigation ul:first li:first').append(DropDownDiv);
	
	//Fades the menu down when required.
	jQuery('#Corporate #SubSectionNavigation ul:first li:first').hover(function(){
		jQuery('#DropDown')
			.stop('true', 'true')
			.fadeIn('300');
	}, function(){	
		jQuery('#DropDown')
			.stop('true', 'true')
			.fadeOut('300');	
	});
	
	//Fades the menu down when required.
	jQuery('#DropDown').hover(function(){
		jQuery('#DropDown').prev('a').addClass('OnHover');
	}, function(){	
		jQuery('#DropDown').prev('a').removeClass('OnHover');
	});
	
	/* FAQ */
	//Hide all FAQ Answers on load.
	var CurrentHtml, CurrentHtmlLength, QuestionLength
	jQuery('ul.FAQ li').each(function(){
		CurrentHtml = jQuery(this).html();
		CurrentHtmlLength = CurrentHtml.length;
		QuestionLength  = CurrentHtml.lastIndexOf('?') + 1;
		var Question = jQuery(this).children('strong:eq(0)').text();
		var Answer = "";
		if(CurrentHtml.indexOf('<br') == -1){
			//Question = CurrentHtml.substring(8,QuestionLength);
			Answer = CurrentHtml.substring(QuestionLength + 13,CurrentHtmlLength);
		}
		else{ //FF
			//Question = CurrentHtml.substring(13,QuestionLength);
			Answer = CurrentHtml.substring(QuestionLength + 18,CurrentHtmlLength);
		}
		jQuery(this).html('<span class="Question"><strong>' + Question + '</strong></span><span class="Answer">' + Answer + '</span>');
	});
	
	
	//Add "+" icon on load.
	jQuery('ul.FAQ .Question').prepend('<div id="Signal" class="Closed"></div>');
	
	//Show corresponding answer when a question is clicked.
	//Switch from "+" to "-" and the opposite.
	jQuery('ul.FAQ li span.Question').click(function(){
													 
		if (jQuery(this).children('div').hasClass('Open') == true){
			jQuery(this).children('div').removeClass('Open').addClass('Closed');
			//Shirnk the currently open answer
			jQuery('span.Answer').slideUp(350);
		}
		else{											 
			//Shirnk the currently open answer
			jQuery('span.Answer').slideUp(350);
			jQuery(this).parent('li').children('span.Answer').delay(350).slideDown(350, function(){
				jQuery(this).css('display', 'block');
			});
			jQuery('ul.FAQ li span.Question .Open').removeClass('Open').addClass('Closed');
			jQuery(this).children('div').removeClass('Closed').addClass('Open');
		}
	});
	
	
	/* Comparison Chart */
	//Set first column class.
	jQuery('#ComparisonMain table tr').each(function(){
		jQuery(this).children('td:first').addClass('ActivityTitle');								 
	});
	
	/*
	//Add hover functionality.
	jQuery('#ComparisonMain table tr').hover(function(){
		jQuery(this).addClass('ActivityRowHover');
	}, function(){
		jQuery(this).removeClass('ActivityRowHover');
	});
	*/
	
	/* Category Landing Page */
	if(jQuery('table.LandingPage').length > 0){
		
		jQuery('table.LandingPage').each(function(index){
		
			if(jQuery('table.LandingPage:eq(' + index + ') tr').length == 2){
				//Add background for the first column
				jQuery('table.LandingPage:eq(' + index + ') tr:first').addClass('LandingColTitle')
					.next('tr').addClass('LandingColImage');
				var BaseRow = "";
				jQuery('table.LandingPage:eq(' + index + ') tr:last td').each(function(){
					if (jQuery(this).hasClass('LanderSpacer') == false){
						BaseRow = BaseRow + '<td></td>';
					}
					else{
						BaseRow = BaseRow + '<td class="LanderSpacer"></td>';
					}
				});
				jQuery(this).append('<tr class="BaseRow">' + BaseRow + '</tr>');			
			}
			
			
			else if (jQuery('table.LandingPage:eq(' + index + ') tr').length == 4){
				//Add background for the first column
				jQuery('table.LandingPage:eq(' + index + ') tr:first').addClass('LandingColTitle')
					.next('tr').addClass('LandingColImage')
					.next('tr').addClass('LandingColList')
					.next('tr').addClass('LandingColBase');
			}
		
		});
	}
	
});
