jQuery(document).ready(function($) {
		
	// Let's Go!
	$('body').addClass('hasjs');
	
	// Uber VARS
	var	ie = ($.browser.msie ? true : false);

	// ============
	// = HOMEPAGE =
	// ============
	$('body#home nav#primary').addClass('low'); // set the position of the main nav element to a lower one on the home page
	$('body#home nav#primary').find('img').attr({
		// 'src': '/static/images/pill-home.jpg',
		'width': '138',
		'height': '121'
	}).css('marginBottom','15px');
	// ===========
	// = CLASSES =
	// ===========
	$('.box').firstnlast(); // Add .first and .last classes to items
	
	// ===================
	// = NAV HIGHLIGHTER =
	// ===================
	function navHighlight() { // highlight current page in the right side nav (main nav)
		var bodyId = $('body').attr('id'); // based on body id
		
		$('nav li').each(function(i) {
			if ($(this).attr('data-page') == bodyId ) {
				$(this).addClass('active');
			};
		});
	};
	navHighlight();
	
	
	function subnavHighlight() { // highlight current page(s) in left side nav
		var current = $('body').attr('data-subnav'); // based on the data-subnav attribute on the body
		
		$('nav li').each(function(i) {
			if ($(this).attr('data-page') == current ) {
				$(this).addClass('active');
			};
		});
	};
	subnavHighlight();

	// ===============
	// = LARGE IMAGE =
	// ===============
	function largeImage() {
		if ( $('body#about,body#about-viagra,body#how').not('body.hand').length ) {
			$('#background_image').remove(); // remove image from DOM
		}
	}
	largeImage();

	if ( $('body#home').length ) { // if on home page
		$('body#home header').height(600); // make the height of the header container 600 so the footer does not overlap with the image.
	};

	// ===============
	// = SAME HEIGHT =
	// ===============
	function eHeight() { // equalize the height of the .box containers
		var c = $('#main article'),
			t = 0;
			
			c.each(function(i) {
				if ( $(this).height() >= t ) {
					t = $(this).height();
				}
			});
			
			c.height(t);
	}
	eHeight();

	// =========================
	// = VIAGRA TEST FAUX FORM =
	// =========================
	function fauxForm() {
		var form = $('#viagra_test'),
			button = form.find('button').not('button.restart'),
			radios = $('#viagra_test input:radio:checked'),
			checked = true,
			isValid = true;
			
			function check() {
				checked = true; // set to checked, disable on fail later
				form.find('.error').removeClass('error'); // force reevaluation upon recheck

				var radioGroup = $('#viagra_test div:not(:has(:radio:checked))'); // get all divs not having a checked radio inside them.
				
				if (radioGroup.length > 0) { // if there is 1 or more unfilled radios
					radioGroup.addClass('error');
					checked = false;
				};
			}
			
			function eval() {
				// evaluate inputs and give feedback
				isValid = true;
				var freshr = $('#viagra_test input:radio:checked'); // get a fresh set of elements on runtime

				$.each(freshr, function(i, item) {
				  	if ($(item).val() == "No") { // if any No's have been selected run
						isValid = false; // set status to false
						return isValid;
					}
				});


				form.find('div,label,input,h2,h1,p,button').fadeOut('fast',function() {$(this).remove()});
				if (!isValid) {
					form.prepend('<h1>You have answered "No" to at least one of the questions. This indicates that you could benefit from Viagra. Please consult with your GP to discuss your options.</h1>');
					form.append('<button class="restart">Click here to retake the test.</button>');
				} else {
					form.prepend('<h1>It seems everything is perfectly fine, if the answer to these questions changes please consult with your GP to discuss your options.</h1>');
					form.append('<button class="restart">Click here to retake the test.</button>');
				};
			}
			
			$('button.restart').live('click', function(event) {
				window.location.reload(); // reload page
			});
			
			button.click(function() {
				check(); // run check to see if all radio groups have a selected one
				if (checked) {
					eval(); // run check
				}
			});
			
	}
	fauxForm();
	
	function policies() {
		// bring up document.write content in a lightbox.
		var links = $('a[href="/legal_policy.html"],a[href="/privacy_policy.html"]'),
			href = '',
			overlay = $('#overlay'),
			box = $('#content-box'),
			close = $('#close_btn'),
			closePos = {};
			
			function modal(href) {
				var policy = ( href=='/legal_policy.html' ? '#legal_policy' : '#privacy_policy' ); // check which policy is being called
				
				if (!ie) {
					$('body').css('overflow-y', 'hidden'); // hide scrollbars
				} else {
					$('html').css('overflow-y', 'hidden'); // hide scrollbars
				}

				$(overlay).fadeTo('fast', 1, function() { // fade in the modal

					closePos.t =  box.position().top-11; // set the close button top position
					closePos.l =  (box.position().left-11) - parseInt(box.css('width'))/2; // set the close button left position

					$('<div />', {id: 'close_btn'}).prependTo(overlay).css({ // add close button
					  'top': closePos.t+'px',
					  'left': closePos.l+'px'
					}).hide().fadeTo('fast', 1);
					$(policy).show(); // display the clicked policy
				});
				
				$('#overlay, #content-box, #close_btn').click(function(e) {
					e.stopPropagation(); // disable event bubble so a click on the content doesn't close the overlay
					
					if (this.id=="overlay" || this.id=="close_btn") {
						$('#close_btn').hide('fast').remove(); // remove the close button
						$(overlay).fadeTo('fast', 0, function() { // fade out the modal
							$(overlay).hide(); // set overlay to display:none
							$(policy).hide(); // set policy to display:none
							 
							if (!ie) { // return of the scroll bar
								$('body').css('overflow-y', 'scroll'); // hide scrollbars
							} else {
								$('html').css('overflow-y', 'scroll'); // hide scrollbars
							}
							
						});
					};
				});
			}
			
			links.click(function(e) {
				e.preventDefault(); // make sure the actual fallback page isn't being loaded
				href = $(this).attr('href'); // set the href of clicked item to 'src'
				modal(href); // bring up the modal
			});
			
	}
	// if (!ie) {
	policies();
	// };
	
}); // close .ready

