jQuery(document).ready(function($) {
	
	//prevent default on links with # as href
	$('body a[href=#]').click(function(event) { event.preventDefault(); });
	
	//nav hover effect
	$('.a-nav ul li:NOT(.no-highlight)').hover(function() {
		//on over
		if($(this).find('.nav-bg-left').length < 1 && !$(this).parent('ul').hasClass('hover')) {
			$(this).prepend("<div class='nav-bg-left'></div>");
			$(this).append("<div class='nav-bg-right'></div>");
			$(this).addClass('nav-bg');
		}
		
		//dd
		if($(this).find('ul.hover').css('display') == 'none') {
			$(this).find('ul.hover').show("slide", {direction:"left"}, 200);
		}
		
	}, function() {
		//on out
		$(this).find('.nav-bg-left').remove();
		$(this).find('.nav-bg-right').remove();
		$(this).removeClass('nav-bg');
		
		//dd
		$(this).find('ul.hover').hide("slide", {direction:"right"}, 400);
		
	});
	
	//portfolio open/close
	$('.p-item .p-foot, .p-item .p-header').click(function() {
		var a = $(this).closest('.p-item').find('.p-foot a');
		$(this).closest('.p-item').find('.p-content-wrap .p-content').slideToggle();
		if(a.html() == "click to open") {
			a.html("click to close");
		}else {
			a.html("click to open");
		}
	});
	
	//remove last border on testimonials
	$('.testimonials .testimonial:last').css({'border' : 'none', 'padding-right' : '0'});
	
	//remove the last comment border
	$('.post-comments .comment:last').css('border', 'none');
	
	//open/close post
	$('.post-title, .open-post, .close-post').click(function() {
		var wrap = $(this).closest('.post-content-wrap');
		var excpert = wrap.find('.post-excerpt').slideToggle();
		if(!$(this).hasClass('.open-post')) { var trig = wrap.find('.open-post, .close-post'); }else { var trig = $(this); }
		wrap.find('.post-hidden').slideToggle();
		if(trig.html() == 'CLICK TO OPEN') {
			trig.html('CLICK TO CLOSE');
			trig.removeClass('open-post').addClass('close-post');
		}else {
			trig.html('CLICK TO OPEN');
			trig.removeClass('close-post').addClass('open-post');
		}
	});
	
	//add comment button
	$('.add-comment').click(function(event) {
		var wrap = $(this).closest('.post-content-wrap');
		wrap.find('.comment-form').slideToggle();
	});
	
	//send to friend button
	$('.send-to-friend').click(function(event) {
		var wrap = $(this).closest('.post-content-wrap');
		wrap.find('.send-form').slideToggle();
	});
	
	//form auto fill
	$('.comment-form input[name=author]').autofill({value: 'name'});
	$('.comment-form input[name=email]').autofill({value: 'email'});
	$('.comment-form textarea').autofill({value: 'comment'});
	
	$('.send-form input[name=name]').autofill({value: 'your name'});
	$('.send-form input[name=email]').autofill({value: 'friends email'});
	$('.send-form textarea').autofill({value: 'message'});
	
	//submit comment
	$('.comment-submit').click(function(event) {
		var f = $(this).closest('form');
		var n = f.find('input[name=author]').val();
		var e = f.find('input[name=email]').val();
		var c = f.find('textarea').val();
		if(n != '' && n != 'name' && e != '' && e != 'email' && e.indexOf('@') != -1 && e.indexOf('.') != -1 && c != '' && c != 'comment') {
			f.find('input[type=submit]').click();
		}
	});
	
	//submit send to friend
	$('.send-submit').click(function(event) {
		var f = $(this).closest('form');
		var n = f.find('input[name=name]').val();
		var e = f.find('input[name=email]').val();
		if(n != '' && n != 'your name' && e != '' && e != 'friends email' && e.indexOf('@') != -1 && e.indexOf('.') != -1) {
			f.find('input[type=submit]').click();
		}
	});
	
	//social links hover
	$('.sidebar .social-links a img').hover(function() {
		//on over
		var i = $(this);
		i.attr('src', i.attr('src').replace('.png', '-hover.png'));
	}, function() {
		//on out
		var i = $(this);
		i.attr('src', i.attr('src').replace('-hover', ''));
	});
	
	//sidebar item open/close
	$('.sidebar .s-title, .sidebar .s-icon').click(function() {
		var wrap = $(this).closest('.s-item');
		var i = wrap.find('.s-icon img');
		if(wrap.find('.s-content').length > 0) {
		  wrap.find('.s-content').slideToggle('fast');
		  if(i.attr('src').indexOf('open') != -1) {
			  i.attr('src', i.attr('src').replace('open', 'close'));
		  }else if(i.attr('src').indexOf('close') != -1) {
			  i.attr('src', i.attr('src').replace('close', 'open'));
		  }
		}
	});
	
	//contact form
	$('.contact-form input[name=name]').autofill({value: 'name'});
	$('.contact-form input[name=email]').autofill({value: 'email'});
	$('.contact-form input[name=tele]').autofill({value: 'telephone'});
	$('.contact-form input[name=subject]').autofill({value: 'subject'});
	$('.contact-form textarea').autofill({value: 'message'});
	
	$('.contact-submit').click(function() {
		var f = $(this).closest('form');
		var n = f.find('input[name=name]').val();
		var e = f.find('input[name=email]').val();
		var m = f.find('textarea').val();
		if(n != '' && n != "name" && e.indexOf('@') != -1 && e.indexOf('.') != -1 && m != "") {
			f.find('input[type=submit]').click();
		}
	});
	
	$('.contact-cancel').click(function() {
		var f = $(this).closest('form');
		f.find('input[name=name]').val('name');
		f.find('input[name=email]').val('email');
		f.find('input[name=tele]').val('telephone');
		f.find('input[name=subject]').val('subject');
		f.find('textarea').val('message');
	});
	
	//newsletter signup
	$('.newsletter-signup input[name=e]').autofill({value: 'email address'});
	
	//remove border from last comment
	$('.post-content-wrap').each(function() { $(this).find('.comment:last').css('border', 'none');; });
	
	//remove margin on last contact-info div
	$('.contact-info div:last').css('margin-right', '0');
	
});