$(document).ready(function() {
	// Yay, we're in business
	$('body').removeClass('no-js');
	
	// Move #elsewhere to the top
	$('#elsewhere').insertAfter( $('#top .secondary .nav') );
	
  // ie6 and below coaxing 
  if (less_than_ie7 == true) {
		$('*:first-child').addClass('first_child');
		
		var alert_txt = '<div id="lt_ie7" class="alert">Your version of Internet Explorer is <strong>more than 9 years old</strong>. We are continuing to try and improve things here for your current browser but if you can <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" target="_blank" title="Internet Explorer home page will open in a new window"><strong>upgrade it</strong></a> this site will work much better for you. <span id="toggle_css"><span>You may find it helpful to </span><a href="#" class="enabled" title="You may re-enable styling here afterwards">remove most site styling</a></span>.</div>';
		
		$('#top .max').before(alert_txt);
		$('#toggle_css a').click(toggle_css);
	}
  
  // Local scroll settings 
  $.localScroll({
    event:'click', //on which event to react
    axis:'y',
    duration:500,
    offset:-30,
    hash:true,
		stop:true      //avoid queuing animations
  });
  
	// Form handling
	form_hinted();
	form_guidance();
	get_quote();
  
	// Sliders for Discover page
		// Set up individual details sliders
		$('.discover .details_list .summary').attr('title','Expand details').wrapInner('<a href="#" />');
		$( '.details > ul' ).hide();
		$('.details_list .summary a').click(single_details);
			
		// Set up per column sliders
		$('.column_details a').click(column_details);
	
		// Set up a global slider
		$('#global_details a').click(global_details);
	
	// Subscriber handlers
	$('.subscriber .summary').wrapInner('<a href="#" title="Show details" />');
	$('.subscriber .answer').hide();
	$('.subscriber .summary a').click(show_details);
		
});


function single_details() {	  
	var summary = $(this).parent();
	var details = summary.parent();
	if ( !details.hasClass('open') ) {
		details
			.addClass('open')
			.find('ul').slideDown()
			.prev('.summary').attr('title','Hide details');
	}
	else {
		details
			.removeClass('open')
			.find('ul').slideUp()
			.prev('.summary').attr('title','Expand details');
	}
	return false;
}


function show_details() {
	var summary = $(this).parent();
	var txt = $(this).text();

	summary.parent().find('.answer').slideDown('medium',function() {
		summary.text(txt);
	});
	
	return false;
}



function global_details() {	  
	if ( !$(this).hasClass('open') ) {
		$('.ctrl a').addClass('open').text('Hide all below');
		$(this).text('Hide all details');
		$('.details')
			.addClass('open')
			.find('ul').slideDown()
			.prev('.summary').attr('title','Hide details');
	}
	else {
		$('.ctrl a').removeClass('open').text('Expand all below');
		$(this).text('Expand all details');
		$('.details')
			.removeClass('open')
			.find('ul').slideUp()
			.prev('.summary').attr('title','Expand details');
	}
	return false;
}


function column_details() {	  
	var div = $(this).parent();
	if ( !$(this).hasClass('open') ) {
		$(this).addClass('open').text('Hide all below');
		div.parent().find('.details')
			.addClass('open')
			.find('ul').slideDown()
			.prev('.summary').attr('title','Hide details');
	}
	else {
		$(this).removeClass('open').text('Expand all below');
		div.parent().find('.details')
			.removeClass('open')
			.find('ul').slideUp()
			.prev('.summary').attr('title','Expand details');
	}
	return false;
}



function form_hinted() {	  
  $('.hinted input.text, .hinted textarea').each(function() {
			$(this)
				.val($(this).attr('title'))
				.data('default', $(this).val())
				.addClass('inactive')
				.focus(function() {
					$(this).removeClass('inactive');
					if($(this).val() == $(this).data('default') || '') {
						$(this).val('');
					}
				})
				.blur(function() {
					var default_val = $(this).data('default');
					if($(this).val() == '') {
						$(this).addClass('inactive');
						$(this).val($(this).data('default'));
					}
				});
  });
}

function form_guidance() {	  
  $('.guided input.text, .guided select').each(function() {
		$(this).focus(function() {
			$(this).next('.guide').addClass('active');
		})
		.blur(function() {
			$(this).next('.guide').removeClass('active');
		});
  });
}



function get_quote() {	  
	$('.quote_form:submit').click(function(e) {
		
	});
}


function toggle_css() {
	var stylesheets = $('link[rel="stylesheet"]');
	$('#toggle_css span').text('');
	
	if ($(this).hasClass('enabled')) {
		stylesheets.each(function(i) {
			stylesheets[i].disabled = true;
		});
		
		$('html').removeClass('wf-active');
		$(this).removeClass('enabled')
		       .addClass('disabled')
		       .text('Turn styling back on')
		       .attr('title','');
	}
	else {
		stylesheets.each(function(i) {
			stylesheets[i].disabled = false;
		});
		
		$('html').addClass('wf-active');
		$(this).removeClass('disabled')
		       .addClass('enabled')
		       .text('Remove most site styling')
		       .attr('title','You may re-enable styling here afterwards');
	}
	
	return false;
}


