$(document).ready(function() {
	if ($('#sub_nav a.active').length > 0) nav_pointer();
	if ($('.large_cap').length > 0) large_cap();
	if ($('a.support').length > 0) support_over();
	if ($('a.lightbox').length > 0) fancybox();
	if ($('ul.select').length > 0) ie_dd();
	if ($('#story_submit_form').length > 0) form_validation();
	if ($('#newsletter_form').length > 0) newsletter_form_validation();
	if ($('#email_address').length > 0) email_submit();
});
function fancybox() {
	$("a.lightbox").fancybox({
		'padding' : 2,
		'margin' : 45,
		'overlayOpacity' : .9,
		'overlayColor' : '#0A2549'
	});
}
function support_over() {
	$('a.support').mouseover(function() { $('li.support img').attr('src','/library/images/icon-star-bubble-over.png'); });
	$('a.support').mouseout(function() { $('li.support img').attr('src','/library/images/icon-star-bubble.png'); });
}
function nav_pointer() {
	var h = 41; // cursor div is 82px wide, so half
	var x = $('#sub_nav a.active').position().left;
	var w = $('#sub_nav a.active').width();
	$('#sub_nav').prepend('<div id="cursor"></div>');
	$('#cursor').css('left',((x+(w/2))-h+14)+'px'); // 12 is the left padding for each link + 2 for border = 14
}
function large_cap() {
	$('.large_cap p:first').contents().filter(function() {
		return this.nodeType != 1;
	}).wrap('<div class="dropcap" />');
	var l = $('.dropcap').text().substring(0,1);
	//$('.dropcap').text().slice(1);
	$('.dropcap').html($('.dropcap').html().substr(1));
	$('.dropcap').prepend('<div class="letter">'+l+'</div>');
}
function ie_dd() {
	$('ul.select li').mouseover(function() { $(this).addClass('sfhover'); });
	$('ul.select li').mouseout(function() { $(this).removeClass('sfhover'); });
}
function form_validation() {
	$('#submit_story').click(function() {
		var error = 0;
		$('input.required, textarea.required, select.required').each(function() {
			// input or textarea fields
			if ($(this).val() == '') {
				error++;
				$('label[for="'+$(this).attr('id')+'"]').css({'color':'#BA0000'});
			} else {
				$('label[for="'+$(this).attr('id')+'"]').css({'color':'#000000'});
			}
		});
		if (error <= 0) {
			return true;
		}
		return false;
	});
	
	$('#b_reset').click(function() {
		$('input.required, textarea.required, select.required').each(function() {
			// input or textarea fields
			$(this).attr('value','');
		});
		return false;
	});
}
function newsletter_form_validation() {
	$('#submit_story').click(function() {
		var e = $('#email').val();
		var p = $('#phone').val();
		var p2 = $('#phone_2').val();
		var p3 = $('#phone_3').val();
		var fn = $('#first_name').val();
		var ln = $('#last_name').val();
		var a = $('#address').val();
		var c = $('#city').val();
		var s = $('#state').val();
		var z = $('#zip').val();
		
		var email_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		if (email_exp.test(e) && e != '' && e != ' ') {
			$('label[for="email"]').css({'color':'#000000'});
			$.ajax({
				type:'POST',
				url:'/myos/my-content/lib/plugins/newsletter-submit.php',
				data:'email='+e+'&phone='+p+'&phone_2='+p2+'&phone_3='+p3+'&first_name='+fn+'&last_name='+ln+'&address='+a+'&city='+c+'&state='+s+'&zip='+z,
				success:function(msg) {
					if (msg == 'success') {
						$('#main').html('<div class="border_bottom wysiwyg"><h2>Thank you</h2><p>Thank you for signing up for our newsletter.</p></div>');
					} else {
						$('p.form').prepend('An error occurred while trying to process your request.<br/ >');
					}
				}
			});	
		} else {
			$('label[for="email"]').css({'color':'#BA0000'});
		}
		return false;
	});
	
	$('#b_reset').click(function() {
		$('input.toclear, textarea.toclear, select.toclear').each(function() {
			// input or textarea fields
			$(this).attr('value','');
		});
		return false;
	});
}
function email_submit() {
	$('#email_address').click(function() { $(this).val(''); });
	$('#email_address').blur(function() { if ($(this).val() == '') $(this).val('Enter your email address'); });
	
	$('#submit').click(function() {
		var e = $('#email_address').val();
		var email_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		if (!email_exp.test(e)) {
			 $('#email_address').attr('value','Invalid email address');
		} else {
			if (e != '' && e != ' ' && e != 'Enter your email address' && e != 'Invalid email address') {
				$.ajax({
					type:'POST',
					url:'/myos/my-content/lib/plugins/email-submit.php',
					data:'email='+e,
					success:function(msg) {
						if (msg == 'success') {
							$('p.form').html('<span>Thank You</span>');
						} else {
							$('p.form').prepend('An error occurred while trying to process your request.<br/ >');
						}
					}
				});
			}
		}
		return false;
	});
}
