/**
 * @desc Web Teks Custom JavaScript (Log in form and forgot password form only)
 * @author Shawn Melton <shawn.melton@webteks.com>
 * @version $id: $
 */
jQuery(document).ready(function(){
	(function($){
		var searchForm = ($("#search-block-form").length > 0);
		if( searchForm ) {
			$("#search-block-form .form-text").attr("placeholder", "SEARCH").addClass("required");
			$("#search-block-form .form-submit").val("Go");
		}
		
		var searchResults = ($("ol.search-results").length > 0);
		if( searchResults ) {
			$("ol.search-results > li").each(function(){
				$(this).find(".search-snippet").append("<br />"+ $(this).find("h3").html());
				$(this).find(".search-snippet > a").html($(this).find(".search-snippet > a").attr("href"));
			});
		}
		
		if( $("body.page-search div.tabs > ul.tabs").length ) {
			$("div.tabs > ul.tabs > li:last .tab").html("Members");
		}
	})(jQuery);
});;
/**
 * @desc Web Teks Custom JavaScript (Calendar only)
 * @author Shawn Melton <shawn.melton@webteks.com>
 * @version $id: $
 */
jQuery(document).ready(function(){
	(function($){
		var calendarPage = ($("body.page-calendar").length > 0);
		if( calendarPage ) {
			$("div.day > a").each(function(){
				var linkInfo = $(this).html();
				$(this).parent().html(linkInfo);
			});
			
			$(".page-calendar .date-next > span > a").html("&raquo;");
			$(".page-calendar .date-prev > span > a").html("&laquo;");
		}
	})(jQuery);
});
;
/**
 * @desc Web Teks Custom JavaScript (Log in form and forgot password form only)
 * @author Shawn Melton <shawn.melton@webteks.com>
 * @version $id: $
 */
jQuery(document).ready(function(){
	(function($){
		var forgotPasswordPage = ($(".page-user-password").length > 0);
		var loginPage = ($(".page-user-login").length > 0);

		if( forgotPasswordPage  ) { // Adjust Text on submit button.
			$("form .form-submit").val("Submit");
		}
		
		if( forgotPasswordPage || loginPage ) {
			// Make h1 link to about page.
			$("h1").click(function(){
				location.href = "/about";
			});
		}
		
	})(jQuery);
});;
/**
 * @desc Web Teks Custom JavaScript (Resource Links only)
 * @author Shawn Melton <shawn.melton@webteks.com>
 * @version $id: $
 */
jQuery(document).ready(function(){
	(function($){
		var resourceLinksPage = ($("body.page-resource-links").length > 0 || $("body.node-type-resource-link").length > 0);
		if( resourceLinksPage ) {
			var validLinkRE = /^http\:\/\//i;
			
			$(".field-name-field-resource-link").each(function(){
				var link = $(this).find(".field-item").html();
				
				if( link != "" ) {
					if( !link.match(validLinkRE) ) { // Make sure links start with http://
						link = "http://"+ link;
					}
					
					$(this).find(".field-item").html('<a href="'+ link +'" target="_blank">'+ link +'</a>'); // Create link from text
					
					// Update node title.
					$(this).parent().parent().find(".node-title > a").attr("href", link).attr("target", "_blank");
				}
			});
		}
	})(jQuery);
});;
/**
 * @desc Web Teks Custom JavaScript (Sponsors only)
 * @author Shawn Melton <shawn.melton@webteks.com>
 * @version $id: $
 */
jQuery(document).ready(function(){
	(function($){
		var resourceLinksPage = ($("body.page-sponsors").length > 0);
		if( resourceLinksPage ) {
			var validLinkRE = /^http\:\/\//i;
			
			// Link logos to company url.
			$(".views-field-field-sponsor-link").each(function(){
				var link = $(this).find(".field-content").html();
				
				if( link != "" ) {
					if( !link.match(validLinkRE) ) { // Make sure links start with http://
						link = "http://"+ link;
					}
					
					var img = $(this).prev().find(".field-content").html();
					$(this).prev().find(".field-content").html('<a href="'+ link +'" target="_blank">'+ img +'</a>');
					
					var sponsor = $(this).prev().prev().find(".field-content").html();
					$(this).prev().prev().find(".field-content").html('<a href="'+ link +'" target="_blank">'+ sponsor +'</a>');
				}
			});
			
			// Add clear after sponsor clears
			$(".sponsor-row:last").after('<br class="clear">');
		}
	})(jQuery);
});;
/**
 * @desc Web Teks Custom JavaScript (Members/Executive Committee pages only)
 * @author Shawn Melton <shawn.melton@webteks.com>
 * @version $id: $
 */
jQuery(document).ready(function(){
	(function($){
		var membersPage = ($("body.page-members").length > 0 || $("body.page-user-").length > 0);
		var execPage = ($("body.page-executive-committee").length > 0);
		var notLoggedIn = ($("body.not-logged-in").length);
		if( (execPage || membersPage) && notLoggedIn ) {
			$(".views-field-field-user-zip, .views-field-field-user-state, .views-field-field-user-city, .views-field-field-user-street-address, .views-field-mail, .views-field-field-user-office-phone, .views-field-field-user-cell-phone, .field-name-field-user-street-address, .field-name-field-user-office-phone, .field-name-field-user-cell-phone, .field-name-field-user-email, .field-name-field-user-city, .field-name-field-user-state, .field-name-field-user-zip, .views-field-field-taskforces").remove();
			
			if( execPage ) {
				$(".views-field-field-user-company").remove();
			} else {
				$(".views-field-field-user-company").after('<br class="clear">');
			}
		}
		
		if( membersPage && !notLoggedIn ) {
			$("ul.members-list > li").each(function() {
				var imageDiv = $(this).find("div.views-field-field-user-headshot").html();
				$(this).find("div.views-field-field-user-headshot").remove();
				var itemHTML = $(this).html();
				$(this).find("div").remove();
				$(this).append('<div class="views-field-field-user-headshot">'+ imageDiv +'</div>').append('<div class="members-fields-wrapper">'+ itemHTML +'</div><div class="clear"></div>');
			});
		}
		
		if( membersPage && $(".profile").length ) {
			var imageDiv = $(".profile div.field-name-field-user-headshot").html();
			$(".profile div.field-name-field-user-headshot").remove();
			var itemHTML = $(".profile").html();
			$(".profile > div").remove();
			$(".profile").append('<div class="field-name-field-user-headshot">'+ imageDiv +'</div>').append('<div class="members-fields-wrapper">'+ itemHTML +'</div><div class="clear"></div>');
		}
	})(jQuery);
});;
/**
 * @desc Web Teks Custom JavaScript (Log in form and forgot password form only)
 * @author Shawn Melton <shawn.melton@webteks.com>
 * @version $id: $
 */
jQuery(document).ready(function(){
	(function($){
		var emailRE = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		var phoneRE = /^((\([2-9]|[2-9])(\d{2}|\d{2}\))(-|.|\s)?)?\d{3}(-|.|\s)?\d{4}$/;
		var numberFieldRE = /phone|fax/i;
		var requiredErrorMsg = "Required!";
		
		/**
		 * @desc If a placeholder is in the form field, remove it.
		 */
		var removeFieldPlaceholder = (function(){
			if( $(this).val() == requiredErrorMsg ) {
				$(this).val("");
			}
			
			if( $(this).hasClass("error") ) {
				$(this).removeClass("error");
			}

			$(this).prev().find("small").remove();
		});
		
		
		/**
		 * @desc Validate a form submission
		 * 1. Check all the required fields for values
		 * 2. Make sure email addresses are a valid format.
		 * 3. Halt form submission if validations fail.
		 */
		var validateFormSubmission = (function(){
			var noErrors = true;
			var requiredFields = $(this).find(".required"); 
			
			$(this).find(".form-text, .form-textarea").each(removeFieldPlaceholder); // Remove any placeholders that are in place.
			requiredFields.each(function(){ // Make sure all required fields have values
				$(this).removeClass("error");
				if( $(this).val() == "" ) {
					$(this).addClass("error");
					noErrors = false;
					
					if( $(this).attr("type") != "password" ) {
						$(this).val(requiredErrorMsg);
					}
				}
			});
			
			// Other validations.
			$(this).find("input").each(function(){
				if( !$(this).hasClass("required") ) {
					$(this).removeClass("error");
				}
				
				if( // Other validations (email, phone, fax).
					!$(this).hasClass("error") && $(this).val() != "" && ( // Make sure this field has not been marked for error yet.
							($(this).hasClass("email") && !$(this).val().match(emailRE)) // E-mail address must validate
							|| ($(this).attr("name").match(numberFieldRE) && !$(this).val().match(phoneRE)) // Phone/Fax numbers must validate.
					)
				) {
					// Provide Error feedback
					var msg = "Invalid "+ $(this).prev().html() +" Entry";
					if( $(this).hasClass("email") ) {
						msg = "Invalid E-mail Entry.";
					}
					$(this).prev().append('<small>'+ msg +'</small>')
					
					$(this).addClass("error");
					noErrors = false;
				}
			});
			
			return noErrors;
		});
		
		
		// Shorten form buttons down to just submit if text is too long.
		$(".form-submit").each(function(){
			if( $(this).val().length > 6 ) {
				$(this).val("Submit");
			}
		});
		
		// Validate all forms
		$("form").submit(validateFormSubmission);
		$("form .form-text, form .form-textarea").focus(removeFieldPlaceholder);
		
		
		// Title correction - Make sure really long page titles will fit in the blue header.
		if( $("h1").html().length > 32 ) {
			$("h1").css("padding-top", "15px").css("height", "150px");
		}
		
		// Update Logo link
		$("#logo").attr("href", "/about");
		

		// Add break to floating categories for teasers
		$(".view-content .field-type-taxonomy-term-reference").after('<br class="clear" />');
		
		
		/**
		 * @desc Update CAPTCHA label from what is default in drupal
		 */
		if( $("#webform-client-form-1 .form-item-captcha-response label").length ) {
			$("#webform-client-form-1 .form-item-captcha-response label").html('ENTER CODE DISPLAYED ABOVE <span class="form-required" title="This field is required.">*</span>');
		}
		
		
		/**
		 * @desc About page JS
		 */
		if( $("body.page-about").length ) {
			$("h2.pane-title").hover(function(){
				$(this).css("text-decoration", "none");
			});
			
			$("h2.pane-title").mouseout(function(){
				$(this).css("text-decoration", "underline");
			});
			
			$("h2.pane-title").click(function(){
				location.href = "/news";
			});
		}
	})(jQuery);
});;

