jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
};

$(window).load(function(){
	
	/* NAVIGATION
	 ------------------------------------- */
	$("ul#nav").superfish({ 
		animation:		{height:'show', opacity:'show'},
		delay:			600,
		autoArrows:		true,
    	dropShadows:	false
	});
	
	$("ul#nav ul li:first-child").addClass('menu_item_first');
	$("ul#nav ul li:last-child").addClass('menu_item_last');

	
	/* FOOTER
	 ------------------------------------- */
	$('#footer .footer_widget_area ul.footer_widget_container').each(function() {
		$('li:last a', this).css('backgroundImage', 'none')
	})
	
	
	/* SINGLE POST
	 ------------------------------------- */
	$('.blogpage-post:last, ol.commentlist li.comments-li .comment-container .comment-body-container .comment-body p:last-child').css({
		paddingBottom: 0,
		marginBottom: 0,
		background: 'none'
	});
	
	$('.comment-form-comment label, .comment-form-author label, .comment-form-email label, .comment-form-url label').addClass('auto_clear');
	
	
	/* PORTFOLIO
	 ------------------------------------- */
	$('ol#portfolio_2_col li.project_container:nth-child(2n+2)').after('<li class="clear"></li>');
	$('ol#portfolio_3_col li.project_container:nth-child(3n+3)').after('<li class="clear"></li>');
	
	$('ol#portfolio_2_col_s li.project_container:nth-child(2n+1)').addClass('first');
	$('ol#portfolio_2_col_s li.project_container:nth-child(2n+2)').addClass('last');
	$('ol#portfolio_2_col_s li.project_container:nth-child(2n+2)').after('<li class="clear"></li>');
	
	$('#front_portfolio_work .front_portfolio_work_projects li:last-child').css({background: 'none', paddingRight: '0'});
	
	$('#project_gallery_container ul.project_gallery li:nth-child(5n+5)').addClass('last');
	
	
	/* GLOBAL
	 ------------------------------------- */
	$('a.zoom, a.read_more, a.arrow_right, a.arrow_right_small, a.arrow_left, a.read_article, a.external_link, a.visit_website ').each(function() {
		$(this).hover(function() {
			$('img', this).stop().animate({ opacity: 0.3 }, 300);
		}, function() {
			$('img', this).stop().animate({ opacity: 1 }, 200);			
		});	
	});
	
	$('img.aligncenter').each(function() {
		$(this).wrap('<span class="aligncenter_container"></span>');
	});
	
	$('div.button_array ul li div.clear').remove();
	
	$('span.image_caption.right, span.image_caption.left').each(function() {
		var imgWidth = $('img', this).width();		
		$('.caption', this).width(imgWidth-10);	
	});
	
	
	$('#featured_entries .featured_entry:nth-child(3n+3)').after('<div class="clear"></div>');
	
	
	/* CONTACTFORM
	 ------------------------------------- */
	$('form.contactform').each(function() {
		$(this).submit(function() {
			$('.required', this).removeClass('error')
			$('em.error').remove();
			
			var error = false,
				submittedForm = $(this);
			
			$('.required', this).each(function() {	
				var fieldName = $(this).attr('name'),
					fieldLabel = $(this).prev().text();
					
				if( $.trim($(this).val()) == '' ) {
					$(this).parent().append('<em class="error">Please enter your ' + fieldLabel + '.</em>');
					$(this).addClass('error');
					error = true;
				} else if( $(this).hasClass('email') ) {	
					if( !$.trim( $(this).val() ).match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/) ) {
						$(this).parent().append('<em class="error">Please enter a valid '+fieldLabel+'.</em>');
						$(this).addClass('error');
						error = true;
					}
				}
			});
							
			if(!error){
				$('.submit', this).after('<span class="loading"></span>')
				var fieldsVal = $(this).serialize();
								
				$.ajax({
					type: "POST",
					url: ajaxurl,
					data: fieldsVal,
					success: function(result){
						submittedForm.before(result);
						submittedForm.slideFadeToggle(500, function(){
							$(this).remove();	
						});
					}
				});
			}
			return false;
		});		
	});
	
	$('.success_notice, .error_notice, .info_notice, .warning_notice').live('click', function() {
		$(this).slideFadeToggle(function(){
			$(this).remove();	
		});	
	});
	
	
	
	/* FANCYBOX
	 ------------------------------------- */
	$("a.zoom").fancybox({
		'transitionIn': 'elastic',
		'padding' : 0,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#222'
	});
});

