//Use this function to be able to add delays to animations. jQuery doesn't have this built in for some reason.
jQuery.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

jQuery(document).ready(function() {

    var userAgent = navigator.userAgent.toLowerCase();

    $.browser = {
        version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
        safari: /webkit/.test( userAgent ),
        opera: /opera/.test( userAgent ),
        msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
        mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
    };
	
	// Setup scene
	
	if (!$.browser.msie || ($.browser.version - 0) > 7) {
		$('#wrapper').css('opacity', 0);
		$('#header h1 a').css('opacity', 0);
		$('#header h1 a span').css('opacity', 0);
		$('#header h2').css('opacity', 0);
		$('#content').css('opacity', 0);
	
	
		$('#wrapper').fadeTo('normal', 1);
	
		// Fade in content div
		$('h1#logo').delay(0).animate({opacity:1}, 600, "linear");
	
		// Fade in Logos
		$("#header h1 a").delay(0).animate({opacity:1},400, "linear", null);
		$("#header h1 a span").delay(400).animate({opacity:1},400, "linear", null);
		$("#header h2").delay(800).animate({opacity:1},400, "linear", null);
    
		$('ul#main-nav li a').each(function() {
			$(this).css('opacity', 0);
		});
	
		// Fade in menu elements one at a time
		var i = 0;
		$('ul#main-nav li a').each(function() {
			$(this).delay(200 * i).animate({opacity:1}, 1200, "linear");
			i++;
		});

		$("div#content").delay(800).animate({opacity:1},400, "linear", null);
	}
	
	$('body#page-contact-us input[name=enquiry_type]').click(function() {
	
		if($(this).val() == 'athlete') {
			$('div#enquiry-type-sport-wrapper').slideDown().addClass('expanded').removeClass('collapsed');
			$('div#enquiry-type-company-wrapper').slideUp().removeClass('expanded').addClass('collapsed').find('input').each(function() {
				$(this).val('');
			});
		}
		
		if($(this).val() == 'company') {
			$('div#enquiry-type-sport-wrapper').slideUp().addClass('collapsed').removeClass('expanded').find('input').each(function() {
				$(this).val('');
			});
			$('div#enquiry-type-company-wrapper').slideDown().removeClass('collapsed').addClass('expanded');
		}
	});
	
	$('.expanded').slideDown();
	$('.collapsed').slideUp();
	
	//put back in if we bring back the tooltips
	//$(" a.tool-tip[title]").tooltip();

	// EXPAND-COLLAPSE
	$('.expand-collapse h2').each(function() {
	
		var target = $(this);
		var container = $('.expand-collapse div#' + target.attr('rel'));
		
		container.hide();
		
		target.toggle(
			function(event) {
				container.slideDown();
				target.addClass('expanded').removeClass('collapsed');
			},
			function(event) {
				container.slideUp();
				target.addClass('collapsed').removeClass('expanded');
			}
		);
		
	});
	
});
