//Set ready state
$(document).ready(function(){
	
	setupNavigation();
	setupHomeFeatures();				   
	setupForms();
	setupTabs();
	setupAuthors();
	setupSubnav();
	setupTooltips();
	
	//Remove First/Last (IE 6 Sucks)
	$('#forumBlock tr:last').addClass('last');
	$('#categoryHeader ul li:first').addClass('first');
	$('#categoryHeader ul li:last').addClass('last');
	
	//Alternating Rows
	$('#past-honorees .featureArticle:odd').addClass('odd');
	
	//Add check boxes to checklists (this is dynamic help to show checkboxes when printed
	$('.checklist li').wrapInner('<span></span>').prepend('<img src="http://dev2.scrubsmag.com/wp-content/themes/default/img/checkbox.jpg" alt="Checkbox" title="Print this list out and check off items as completed" />');
	
  

 }); //End Ready



/*---------- Setup Navigation ---------------------------------------------------------*/

function setupNavigation(){

	//Show/Hide Menus
	$(".navMain li").not(".noHover").bind("mouseenter mouseleave", function(){
	$(this).find('ul.subnav').toggleClass('block');
	$(this).toggleClass('on');
	});

}


/*---------- Setup Home Features ---------------------------------------------------------*/

function setupHomeFeatures(){

	$('.related-articles li').each(function(){
	$(this).find('.category-parent:last').append(':');
	});

	//Truncate related Articles and Must Read Homepage
	$(".related-articles a").each(function(){
		$(this).truncate( 44 );
	});

	$(".mustRead .mr-excerpt p").truncate( 50, {
        chars: /\s/,
        trail: [ "...", "" ]
    	});
    
    $('.set').find('.mustRead:first').addClass('first');
    $('.set').find('.mustRead:last').addClass('last');

	
	//Feature Article Gallery
	$('.cycle').cycle({ 
    fx:     'fade',
    timeout: 10000, 
    pause: 1,
    next:   '.next', 
    prev:   '.prev',
    pager:  '.pager'
}).show().next().find('.pager a').each(function(){
	$(this).wrap('<span></span>');
	$('.pager').find('a:first').parent().addClass('f1').next().find('a').parent().addClass('f2').next().find('a').parent().addClass('f3').next().find('a').parent().addClass('f4').next().find('a').parent().addClass('f5').next().find('a').parent().addClass('f6');
});

	//Must Read Showcase
	$('#mustRead .features').cycle({ 
    fx:     'scrollHorz',  
    timeout: 0, 
    next:   '.mustread-next', 
    prev:   '.mustread-prev' 
});

	$('.mustread-next').click(function(){
		$(this).hide();
		$('.mustread-prev').show();	
	});
	
	$('.mustread-prev').click(function(){
		$(this).hide();
		$('.mustread-next').show();	
	}).hide();
	
}


/*---------- Setup Tab Content ---------------------------------------------------------*/


function setupTabs(){
$('#popularityFilter a').click(function(event){
$(this).parent().parent().find('.on').removeClass('on');
$(this).addClass('on');
$('.tabbedContent').hide();
var content_show = $(this).attr("href");  
$(content_show).show();
event.preventDefault();	
});

$('.tabbedContent').hide();
$('.tabbedContent:first').show();
}


/*---------- Setup Forms ---------------------------------------------------------*/

function setupForms(){

	//Newsletter - Set Target and disable button
	$('.getNewsletter').attr('target', '_new');
	
	$('#newsletterSubscribe').click(function(event){
			var activateForm = $('.getNewsletter').attr('action');
				if(activateForm == ""){
					$("#validEmail").removeClass('valid-yes').addClass('valid-no');
					event.preventDefault();
					}
			});
	
	//Newsletter - Validate Email Address
	$("#validate").keyup(function(){
		
			var email = $("#validate").val();
		
			if(email != 0)
			{
				if(isValidEmailAddress(email))
				{
					$("#validEmail").removeClass('valid-no').addClass('valid-yes');
					$('.getNewsletter').attr('action', 'https://app.e2ma.net/app/view:Join/acctId:35984/signupId:61221');
				} else {
					$("#validEmail").removeClass('valid-yes').addClass('valid-no');
					$('.getNewsletter').attr('action', '');
					
				}
			}		
		});

	//Clear Search Field
	$(".searchfield").focus(function() {
			    if(this.value == "Search"){
					this.value = "";
					}
			});
	$(".searchfield").blur(function() {
			    if(this.value == ""){
					this.value = "Search";
					}
			});
	//Clear Name Field
	$("#author").focus(function() {
			    if(this.value == "Your name"){
					this.value = "";
					}
			});
	$("#author").blur(function() {
			    if(this.value == ""){
					this.value = "Your name";
					}
			});
	//Clear Website Field
	$('#url').attr('value', 'Your website');
	$("#url").focus(function() {
			    if(this.value == "Your website"){
					this.value = "";
					}
			});
	$("#url").blur(function() {
			    if(this.value == ""){
					this.value = "Your website";
					}
			});
	//Clear Comment Field
	$("textarea#comment").focus(function() {
			    if(this.value == "Your Comment"){
					this.value = "";
					}
			});
	$("textarea#comment").blur(function() {
			    if(this.value == ""){
					this.value = "Your Comment";
					}
			});
	
	//Clear Email Field
	$(".email").focus(function() {
			    if(this.value == "Your email"){
					this.value = "";
					}
			});
	$(".email").blur(function() {
			    if(this.value == ""){
					this.value = "Your email";
					}
			});
	
	//Clear Zip Code Field
	$(".zip").focus(function() {
			    if(this.value == "Enter your zip code"){
					this.value = "";
					}
			});
	$(".zip").blur(function() {
			    if(this.value == ""){
					this.value = "Enter your zip code";
					}
			});
	
	//Clear Scrubbies Award Nominee Field
	$(".nominee input").focus(function() {
			    if(this.value == "Nominee"){
					this.value = "";
					}
			});
	$(".your-name input").focus(function() {
			    if(this.value == "Your Name"){
					this.value = "";
					}
			});
	$(".nominee input").blur(function() {
			    if(this.value == ""){
					this.value = "Nominee";
					}
			});
	$(".your-name input").blur(function() {
			    if(this.value == ""){
					this.value = "Your Name";
					}
			});
	
	//Clear Comment Form Name Field
	$(".name").focus(function() {
			    if(this.value == "Your name"){
					this.value = "";
					}
			});
	$(".name").blur(function() {
			    if(this.value == ""){
					this.value = "Your name";
					}
			});
	
	//Clear Comment Form Name Field
	$(".website").focus(function() {
			    if(this.value == "Your website"){
					this.value = "";
					}
			});
	$(".website").blur(function() {
			    if(this.value == ""){
					this.value = "Your website";
					}
			});
			
			
	//Check URL in Comment Form and clear if "Your website"
	$('#commentform #submit').click(function(event){
	if($('#url').val() == "Your website"){
		$('#url').removeAttr('value');
		$('#commentform').submit();
	}else{
		$('#commentform').submit();
	}				
	event.preventDefault();
	});

}

//Some Regex to validate email pattern
function isValidEmailAddress(emailAddress) {
 		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 		return pattern.test(emailAddress);
}


/*---------- Setup Tooltips ---------------------------------------------------------*/

function setupTooltips(){

$(".poptip .close").click(function(event){ //Triggers close of tooltips from 'a' elements with class of 'close'
   		$(this).parent().hide('slow');
		event.preventDefault();
	});

// Tooltip events
$(".tooltip a").click(function(event){ //Triggers all tooltips from 'a' elements with class of 'tooltip'
		var popdiv_show = $(this).attr("href");  
		$(popdiv_show).toggle('slow');
		event.preventDefault();
	});

}


/*---------- Setup Authors ---------------------------------------------------------*/

function setupAuthors(){

	//Truncate Nurse Blog Author Bios on Blogs Landing Page
	$(".page-template-blogslanding-php .bioExcerpt span").each(function(){
		$(this).truncate( 120 );
	});

	$(".home .bioExcerpt span").each(function(){
		$(this).truncate( 400 );
	});
	
	//Truncate Author Leads
	$('.authorLead span').truncate( 200 );
	
}

/*---------- Setup Subnav ---------------------------------------------------------*/

function setupSubnav(){
	var current = $("#categoryHeader ul").find("a").filter(function() { return this.href.toLowerCase() == location.href.toLowerCase(); });
				if ( current.length ) {
					current.parent().addClass("current-cat");
				}
}





