// Slide + Fade animation effects
jQuery.each({
	slideFadeIn: { opacity: 'show', height: 'show' },
	slideFadeOut: { opacity: 'hide', height: 'hide' }
}, function( name, props ) {
	jQuery.fn[ name ] = function( speed, easing, callback ) {
		return this.animate( props, speed, easing, callback );
	};
});

$(window).load(function() {
	
	/* GLOBAL
	 ------------------------------------- */
	// sets the coloured background and the header height.
	$('#wrapper').css({ backgroundPosition: 'left ' + $('#header_container header').innerHeight() + 'px' });
	$('#header_container').css({backgroundPosition: 'left ' + ($('#header_container header').innerHeight()-700) + 'px'});
	
	// sets the header height of the inner pages.
	$('body:not(.front_page) #header_container').height($('#header_container header').innerHeight());
	
	// overlaps the tagline with the slider
	$('#tagline').css({
		top: -(($('#tagline').outerHeight()/3)*2.2)
	})
	$('#tagline').fadeTo(200, 0.9);
	
	// sets the wdith of the homepage left coloured container
	// $('#frontpage_container_left').width( ($('#wrapper').width()/2) - 20 );
	
	// blog
	$('div.blogpage-post:last-child, #sidebar .widget_container .wizy_twitter_widget li:last-child, footer .footer_widget_container .widget_container:last-child').css({borderBottom: 'none', paddingBottom: 0, marginBottom: 0});
	
	$('.commentlist .comment-container').each(function() {
		$('.comment_num', this).css({ lineHeight : $(this).height() - 1 + 'px' })
	})
	
	// Image animations
	$('a.view_project, a.zoom').each(function() {		
		$(this).prepend('<span class="image_inner_button">View Project</span>');		
		$(this).hover(function() {
			$('.image_inner_button', this).stop(true, true).fadeIn(300);
		}, function() {
			$('.image_inner_button', this).stop(true, true).fadeOut(200);
		});
	});
	
	$('.wp-caption').each(function() {
		$(this).width( $('img', this).width() );
	});


	/* LOGO
	 ------------------------------------- */
	$('header #logo').each(function() {		
		$(this).hover(function() {
			$(this).stop().animate({opacity: 0.7}, 300);	
		}, function() {
			$(this).stop().animate({opacity: 1}, 200);			
		});
	});
	
	
	/* NAVIGATION
	 ------------------------------------- */
	$("nav ul > li").each(function() {		
		$("ul li:odd", this).addClass('menu_item_odd')
		
		$(this).has('ul').hover(function() {	
			$('a:first', this).addClass('hover');
			$('ul:first', this).stop(true, true).slideFadeIn(300);		
		}, function() {
			$('ul:first', this).stop(true, true).delay(500).slideFadeOut(200, function() {
				var li = $(this).parent();
				$('a:first', li).removeClass('hover');
			});	
		});
		
		$('ul li:first-child', this).css({borderTop: 'none'});
		$('ul li:last-child', this).css({borderBottom: 'none'});
	});
	
	
	/* PORTFOLIO
	 ------------------------------------- */
	$('.front_portfolio_projects li:last-child, .wizy_flickr_images_container a.flickr_photo:nth-child(3n+3)').css({marginRight: 0});
	$('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>');
	$('.single_porject_gallery ul.project_gallery li:nth-child(5n+5)').addClass('last');	
	$('a.zoom').colorbox();

	
	/* SHORTCODES
	 ------------------------------------- */
	 
	 /* slider */
	$('.wizy_slider_inline_container .wizy_slider_nav_inline').each(function() {
		$(this).css({marginLeft: -($(this).innerWidth()/2)});
	});	
	
	/* tabs */
	$('.wizy_tabbed').each(function() {
		$(this).width($(this).innerWidth() - 30);
	});
	
	/* Collapsable content */
	$('.wizy_collapsible').each(function() {
		if($('dt', this).hasClass('expanded')) {
			$('dd', this).show();
		} else {
			$('dd', this).hide();			
		}
		
		$('dt', this).live('click', function() {
			var dt = $(this),
				dl = dt.parent(),
				dd = $('dd', dl);
			
			if(dt.hasClass('expanded')) {
				dd.slideUp(300, function() {
					dt.removeClass('expanded');
				});
			} else {
				dt.addClass('expanded');
				dd.slideDown(300);
			}
			return false;
		});
	});
	
	/* notices */
	$('.success_notice, .error_notice, .info_notice, .warning_notice').live('click', function() {
		$(this).slideFadeOut(function(){
			$(this).remove();	
		});	
	});
	
	
	/* CONTACT 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.slideFadeOut(1000, function(){
							$(this).remove();	
						});
					}
				});
			}
			return false;
		});		
	});
	
});
