jQuery(document).ready(function() {

//	jQuery('#cycle').cycle({
//		fx: 'fade'
//	});

//	jQuery('figure.gallery-item a').attr('rel','gallery');
//	jQuery('figure.gallery-item a[rel="gallery"]').fancybox();
	
//	Hide and show the contact bars for individual execs
//	Employs an anonymous tracker to determine which executives are receiving more hits than others
//	The tracker simply alerts an email account that a particular exec's link has been clicked
//	No personal information is passed, whatsoever
	
	var showText = 'Click to Contact';
	var hideText = 'Click to Hide';
	
	var is_visible = false;
	var firePreventer = new Array();
	
	$('ul.follow-exec').hide().before('<div class="follow-exec-link"><a class="follow">'+showText+'</a></div>');
	$('a.follow').click(function() {
		is_visible = !is_visible;
		$(this).html( (!is_visible) ? showText : hideText);
		$(this).parent().next().toggle('slow');
		var currentId = $(this).parent().next().attr('id');
		var datastr = 'currentId='+ currentId;
		var preventFire = $.inArray(currentId, firePreventer);
		if (preventFire < 0) {
			$.ajax({
			type: "POST",
			url: "/wp-content/themes/blue-and-gold/js/mail.php",
			data: datastr
			});
			firePreventer.push(currentId);
		}
		return false;
	});
	
// Sidebar Operator
// This sidebar is NOT driven by the options panel as of right now
// The sidebar options panel will require a large reconfiguration due to the fact that there will be a lot of changeable information contained herein

	$('ul.sidelink li#twitter').append('<li class="drawer" id="twitterdraw"><a href="/our-feeds" class="drawerlink">Our Feeds</a></li>');
	$('ul.sidelink li#facebook').append('<li class="drawer" id="facebookdraw"><a href="http://www.facebook.com/rmamarketing" class="drawerlink">Fan Page</li>');
	$('ul.sidelink li#calendar').append('<li class="drawer" id="calendardraw"><a href="/upcoming-events" class="drawerlink">Upcoming</a></li>');
	$('ul.sidelink li#contact a').attr('href', '/contact-us');
	$('ul.sidelink li#contact').append('<li class="drawer" id="contactdraw"><a href="/contact-us" class="drawerlink">Contact</a></li>');
	$('ul.sidelink li.drawer').hide();
	
	$('ul.sidelink li').hover(function(){
		$(this).find('.drawer').animate({width: 'toggle'});
	});

// Feed Page Assistant
// This detects how the page was coded before the twitter feed content is displayed, so that it ends up in the right spot on the page
// It ensures that headings and content line up properly

	var searchClass = 'leftside';
	var prevClassFeed = $('.twitterfeedcontainer').parent().prev().attr('class');
	if ((prevClassFeed !== undefined) && (prevClassFeed.indexOf(searchClass) != -1)){
		$('.twitterfeedcontainer').parent().removeClass('prepend-11');
	}
	
// Contact Page Assistant
// This detects how the page was coded before the content form is displayed, so that it ends up in the right spot on the page
// It ensures that headings and content line up properly

	var searchClass = 'leftside';
	var prevClassForm = $('form#contactform').parent().prev().attr('class');
	if ((prevClassForm !== undefined) && (prevClassForm.indexOf(searchClass) != -1)){
		$('form#contactform').parent().removeClass('prepend-11');
	} 

// Contact Form Mailer
// Interprets data from the contact form, and sanitizes the inputs before passing them to the php mailing assistant 	

	$('form#contactform input#name')
		.focus(function(){
			if(this.value === 'Your Name') {
				this.value = '';
			}
		})
		.blur(function(){
			if(this.value ==='') {
				this.value = 'Your Name';
			}
		});
		
	$("form#contactform #email")
		.focus(function(){
			if(this.value === 'E-mail Address') {
				this.value = '';
			}
		})
		.blur(function(){
			if(this.value ==='') {
				this.value = 'E-mail Address';
			}
		});
		
	$("form#contactform #content")
		.blur(function(){
		});		
		
	$('#submit').click(function () {
		var nameEntered = $("input#name").val();
		var emailEntered = $("input#email").val();
		var subjectEntered = $("input#subject").val();
		var receiverEntered = $("select#receivername option:selected").val();
		var contentEntered = $("textarea#content").val();
		
		var illegalCharsName = /[^A-z \'\-]/;
		var illegalCharsEmail = /^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/;
		var illegalCharsSubject = /[^\w \&\!\?\-\/\.\,\'\"]/;
		var illegalCharsNone = /[^\w\s \&\!\?\-\/\.\,\'\"]/;
		
		$("#error").empty();
		
		if (nameEntered === 'Your Name') {
				$("#error").append('You have not entered your name.');
				$("input#name").focus();
				return false;
			} else {
				if (illegalCharsName.test(nameEntered)) {
					$("#error").append('Please enter only letters, apostrophes, spaces or dashes in your name.');
					$("input#name").focus();
					return false;
				}
			}
			
		if (emailEntered === 'E-mail Address') {
				$("#error").append('You have not entered your email.');
				$("input#email").focus();
				return false;
			} else {
				if (!(illegalCharsEmail.test(emailEntered))) {
					$("#error").append('Please enter a valid email address.');
					$("input#email").focus();
					return false;
				}
			}
			
		if (subjectEntered === '') {
				$("#error").append('You have not entered a subject.');
				$("input#subject").focus();
				return false;
			} else {
				if (illegalCharsSubject.test(subjectEntered)) {
					$("#error").append('Please do not enter any illegal characters in the subject.');
					$("input#subject").focus();
					return false;
				}
			}
			
		if (contentEntered === '') {
				$("#error").append('You have not entered any content');
				$("input#content").focus();
				return false;
			} else {
				if (illegalCharsNone.test(contentEntered)) {
					$("#error").append('Please do not enter any illegal characters in the content area.');
					$("input#content").focus();
					return false;
				}
			}
		$("#error").empty().append('<span class="noterror">Sending...</span>');
		
		var dataSubmit = 'name='+ nameEntered +'&email='+ emailEntered +'&subject='+ subjectEntered +'&receiver='+receiverEntered+'&content='+ contentEntered;
			
		$.ajax({
			type: "POST",
			url: "/wp-content/themes/blue-and-gold/contactprocess.php",
			data: dataSubmit,
			success: function(data){
				if (data["msg"] === "Email Sent") {
					$('form#contactform').slideUp('slow');
					$('form#contactform').empty();
					$('form#contactform').parent().append('<h5 style="color: #edb41d;">Message Sent!</h5>');
					return false;
				} else {
					if (data["msg"] === "InvalidChars") {
						$("#error").empty().append('The data entered appears to be invalid. Please try again.');
						$("input#name").focus();
						return false;
					} else {
						if (data["msg"] === "Email Error" || processedData.msg === "UnknownError") {
							$("#error").empty().append('An error occurred. Please try again.');
							$("#submit").focus();
							return false;
						}
					}
				}
			}
		});
			
		return false;
	});

//End of scripts.js
});
