$(document).ready(function() {
	$('#sub-nav li li.on').parent().parent().addClass('down');
	$('#sub-nav li.on').parent().parent().addClass('down');
	$('#sub-nav li ul').hide();
	$('#sub-nav li.on ul').show();
	$('#sub-nav li.down ul').show();
	
	$('.sub-info p+ul').prev().addClass('no-space');
	$('h2:first, h3:first').addClass('first');
	$('#main-nav li:last').addClass('last');

// Toggling Questions and Answers 
	$("p.toggler").click(function(){ 
		$(this).next().slideToggle("slow");
	});
	$("p#sign-up").click(function(){ 
		$(this).next().show("slow");
		$(this).hide("slow");
	});

// Search	label text to input and button image rollover											 
	var searchLabel = $('#interstim-search label').remove().text();
	$('#qt').addClass('placeholder').val(searchLabel).focus(function() {
			if(this.value == searchLabel) {
				$(this).removeClass('placeholder').val('');
			};
		}).blur(function() {
			if(this.value == '') {
				$(this).addClass('placeholder').val(searchLabel);
			}
		});
	
	$('#interstim-search').submit(function() {
		if($('#qt').val() == searchLabel) {
			$('#qt').val('');
		}
	 });
// end Search enhancements

// Change URLs by domain
	if((window.location.host == 'www-test.everyday-freedom.com') || (window.location.host == 'wwwp-test.medtronic.com')){
		$('#interstim-search').attr({action: 'http://websearch-test.medtronic.com/edfwomen/query.html'});
		$('#qp').val('+url:http://www-test.everyday-freedom.com/women/');
		$('#find-doctor').attr({action: 'http://www-test.mymedpages.com/pf/search.do?actionCode=2&websiteParm=interstim.com&templateName=interstim'});
		$('#log-out').attr({href: 'https://msa-test.medtronic.com/logout/logout.jsp?logoffurl=http://www-test.everyday-freedom.com/index.htm?'});
		$('#change-password').attr({href: 'https://msa-test.medtronic.com/idm/ext/imcss/index.jsp?task.tag=ChangeMyPassword'});
		$('#change-question').attr({href: 'https://msa-test.medtronic.com/idm/ext/imcss/index.jsp?task.tag=ChangeMySecurityQuestion'});
		$('#change-contact').attr({href: 'https://msa-test.medtronic.com/idm/ext/imcss/index.jsp?task.tag=ModifyMyProfile'});
	};
	if((window.location.host == 'www.everyday-freedom.com') || (window.location.host == 'everyday-freedom.com') || (window.location.host == 'wwwp.medtronic.com')){
		$('#find-doctor').attr({action: 'http://www.mymedpages.com/pf/search.do?actionCode=2&websiteParm=interstim.com&templateName=interstim'});
		$('#log-out').attr({href: 'https://msa.medtronic.com/logout/logout.jsp?logoffurl=http://www.everyday-freedom.com/index.htm?'});
		$('#change-password').attr({href: 'https://msa.medtronic.com/idm/ext/imcss/index.jsp?task.tag=ChangeMyPassword'});
		$('#change-question').attr({href: 'https://msa.medtronic.com/idm/ext/imcss/index.jsp?task.tag=ChangeMySecurityQuestion'});
		$('#change-contact').attr({href: 'https://msa.medtronic.com/idm/ext/imcss/index.jsp?task.tag=ModifyMyProfile'});
	};
// End Change URLs by domain

// Email a Friend thickbox
	$("a[rel='email-page']").each(function(){
		var thickbox_href = "?TB_iframe=true&height=450&width=360";
		var this_href = $(this).attr("href");
		$(this).attr('href', this_href + thickbox_href);
	});
	
// Video thickbox
	$("a[rel='video']").each(function(){
		var thickbox_href = "?TB_iframe=true&height=295&width=445";
		var this_href = $(this).attr("href");
		$(this).attr('href', this_href + thickbox_href);
	});


// Print page
	//remove link to print-this-page.html, which remains if javascript is disabled
	//must repopulate href with location.href for IE
	$('#print-page a, a.print-this-page').click(function() {
		$(this).attr("href",location.href);	
		window.print();
		return false;
	});

// end Print page

// Create the tooltips only on document load
// Take the title text and store it in the new "tooltip" element							 
	$('abbr').each( function() {
		var myTip = $(this).attr('title');
		$(this).attr('tooltip',myTip);
		$(this).removeAttr('title');  // Remove title element to prevent default browser tooltip when using show/hide on click
	});
	
// Notice the use of the each() method to acquire access to each elements attributes
   $('abbr[tooltip]').each(function(){
      $(this).qtip({
				content: $(this).attr('tooltip'), // Use the tooltip attribute of the element for the content
				position: {
					corner: {
					 target: 'rightMiddle',
					 tooltip: 'leftMiddle'
					}
				},
				style: { 
					width: 200,
					padding: 5,
					background: '#7870B3',
					color: 'white',
					fontSize: '1.1em',
					lineHeight: '1.3em',
					border: {
						 width: 7,
						 radius: 5,
						 color: '#7870B3'
					},
					tip: 'leftMiddle'
				},
				hide: { 
					when: { event: 'unfocus' }
				},
				show: {
					delay: 0,
					when: { event: 'click' } 
				}
			});
   });


// HTML tips for a.tooltip PURPLE
	$('a.tooltip').each(function(){
		$(this).qtip({
				content: {
      		url: $(this).attr('href') // Use the href attribute of the element for the content
				},
				position: {
					corner: {
					 target: 'rightMiddle',
					 tooltip: 'leftMiddle'
					}
				},
				style: { 
					width: 250,
					padding: 5,
					background: '#7870B3',
					color: 'white',
					fontSize: '1.1em',
					lineHeight: '1.3em',
					border: {
						 width: 7,
						 radius: 5,
						 color: '#7870B3'
					},
					tip: 'leftMiddle'
				},
				hide: { 
					when: { event: 'unfocus' }
				},
				show: {
					delay: 0,
					when: { event: 'click' } 
				}
			}); 
			$(this).click(function() {
				return false;
			});
	 }); 

// End HTML tips

// HTML tips for a.infotip BLUE
	$('a.infotip').each(function(){
		$(this).qtip({
				content: {
      		url: $(this).attr('href') // Use the href attribute of the element for the content
				},
				position: {
					corner: {
					 target: 'TopMiddle',
					 tooltip: 'bottomMiddle'
					}
				},
				style: { 
					width: 250,
					padding: 5,
					background: '#3385B7',
					color: 'white',
					fontSize: '1.1em',
					lineHeight: '1.3em',
					border: {
						 width: 7,
						 radius: 5,
						 color: '#3385B7'
					}
				}
			}); 
			$(this).click(function() {
				return false;
			});
	 }); 

// End HTML tips

/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/

function switchStylestyle(styleName)
{
	$('link[rel*=style][title]').each(function(i) 
	{
		this.disabled = true;
		if (this.getAttribute('title') == styleName) this.disabled = false;
	});
	createCookie('style', styleName, 365);
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// /cookie functions

	$('.styleswitch').click(function()
	{
		switchStylestyle(this.getAttribute("rel"));
		return false;
	});
	var c = readCookie('style');
	if (c) switchStylestyle(c);
	
// End Styleswitch

// Form Validation
	var requiredFlag = ' * ';
	var requiredKey = $('input.required:first').prev('span').text();
	requiredKey = ('<span>') + requiredFlag + ('</span>') + requiredKey.replace(/^\((.+)\)$/,"$1");
	$('form :input').filter('.required')
	.prev('span').text(requiredFlag).end()
	.prev('label').addClass('req-label');
	$('<p></p>')
	.addClass('field-keys')
	.append(requiredKey)
	.insertBefore('#survey');

	$("form :input").blur(function() {
		$(this).parents('li:first').removeClass('warning')
			.find('span.error-message').remove();
		$(this).parents('li:first').removeClass('warning')
			.find('p.error-message').remove();
		
		if ($(this).is('.required')) {
 			var $listItem = $(this).parents('li:first');
 			if (this.value == '') {
				var errorMessage = 'This is a required field';
				$('<p></p>')
				.addClass('error-message')
				.text(errorMessage)
				.appendTo($listItem);
 				$listItem.addClass('warning');
 			};
 		};
 		if ($(this).is('.email')) {
 			var $listItem = $(this).parents('li:first');
 			if (this.value != '' && !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value)) {
 				var errorMessage = 'Please use proper email format (e.g. joe@example.com)';
				$('<p></p>')
				.addClass('error-message')
				.text(errorMessage)
				.appendTo($listItem);
				$listItem.addClass('warning');
 			};
 		};
 		if ($(this).is('.zip')) {
 			var $listItem = $(this).parents('li:first');
 			if (this.value != '' && !/^\d{5}([\-]\d{4})?$/.test(this.value)) {
 				var errorMessage = 'Please enter a 5 digit or a 5 digit+four zip code, like "12345-6789".';
				$('<p></p>')
				.addClass('error-message')
				.text(errorMessage)
				.appendTo($listItem);
				$listItem.addClass('warning');
 			};
 		};
	});
	
	$("form[name='survey']").submit(function() {
		 $('#submit-message').remove();
		 $(':input.required').trigger('blur');
		 
	// Call local validation
		 
		localValidation();
		
		 var numWarnings = $('.warning', this).length;
		 if (numWarnings) {
			 var fieldList = [];
			 $('.warning label').each(function() {
				fieldList.push($(this).text());
			  });
		 $('<div></div>').attr({'id': 'submit-message', 'class': 'warning'})
		 .append('<strong>Please correct errors with the following ' + numWarnings + ' fields:</strong><br />')
		 .append('&bull; ' + fieldList.join('<br />&bull; '))
		 .insertBefore('#submit');
		 return false;
		 };							 
	});

// end Form Validation


}); // end document.ready
