$(function() {
	
	// No Margin
	$('.home #content-area .post:nth-child(odd)').addClass('no-margin');
	
	// Company Nav Class
	$('#nav ul li:nth-child(3)').addClass('company');
	
	// Drop Down
	$('#nav li li ul').removeClass('sub-menu');
	$('#nav li').hover(function(){
		$(this).find('ul.sub-menu').show();
	},function(){
		$(this).find('ul.sub-menu').hide();
	});
	
	// First / Last Class
	$('#nav ul ul li:first-child, #footer .sub-menu li:first-child').each(function(){
		$(this).addClass('first');
	});
	
	$('#nav ul ul li:last-child, #footer .menu li:last-child, #footer .sub-menu li:last-child').each(function(){
		$(this).addClass('last');
	});
	
	// Label Fade
	$('.newsletter input[type="text"]').focus(function(){
		$(this).parent().find('label').fadeOut();
	});
	
	// Form Validation
	$('form#commentform').submit(function(){
		var error = false;
		$('#commentform input[type="text"]:visible, #commentform textarea:visible').each(function(){
			var inputval = $(this).val();
			if($(this).attr('id') == 'email') {
				if(!validateEmail(inputval)) {
					$(this).parent().addClass('error').append('<div class="error-m">Valid Email</div>');
					error = true;
				}
			}else if(inputval == ''){
				$(this).parent().addClass('error').append('<div class="error-m">Required Field</div>');
				error = true;
			}
		});
		if(error) {
			return false;
		}
	});
	
	$('#commentform input[type="text"], #commentform textarea').focus(function(){
		if ($(this).parent().hasClass('error')) {
			$(this).keyup(function(){
				$(this).parent().removeClass('error');
				$(this).parent().find('.error-m').fadeOut();
			});
		}
	});
	
function validateEmail(email) { 
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
	return email.match(re) 
	}
	
	// Click handler for the "back to top" link in comments
	$('#comments-top-link').click(function(e) {
    
      // Scroll to the top of the page  
      $('html,body').animate({scrollTop: 0},'slow', function() {
    
      // Remove the fragment part of the URL
      var url = window.location.href.split('#')[0];
      
      // Remove the "comment-page-X" part if present
      if(url.match(/comment-page-[0-9]*\/?$/gi)) {
        
        // Strip trailing slash
        if(url.match(/\/$/gi)) {
          url = url.slice(0, -1);
        }
        
        // Take off the last bit (comment-page)
        url = url.split('/');
        url.pop();
        
        // Put it back together and add a trailing slash
        url = url.join('/') + '/';
      }

      if(history.pushState) {
        // Modern browser with support for HTML5 history API
        history.pushState(null, null, url);
      }
      else {
        // Fallback for older browsers & IE
        window.location.href = url;
      }
    
      // This isn't really a link
      e.preventDefault();
    });
});
	
});
