// ======================================================================
// JQUERY ANIMATION EXTENSIONS
// ======================================================================
jQuery.each({
	slideFadeIn: { opacity: 'show', height: 'show' },
	slideFadeOut: { opacity: 'hide', height: 'hide' },
	slideFadeToggle: { opacity: 'toggle', height: 'toggle' }
}, function( name, props ) {
	jQuery.fn[ name ] = function( speed, easing, callback ) {
		return this.animate( props, speed, easing, callback );
	};
});


$(document).ready(function() {

	/* STYLE SWITCHER
	-------------------------------------- */
	$('#switcher_container').delay(1000).animate({ left: -200 }, 400, function() {
		var obj = $('#switcher', this);
		obj.addClass('collapsed');
		obj.removeClass('expanded');
		$('.trigger', this).animate({ opacity: 0.6 });
	});	
	
	$('#switcher_container .trigger').tipsy({gravity: 'w', html: true, fade: true, opacity: 0.8});
	
	$('#switcher_container .trigger').click(function() {
		var objParent = $(this).parent(),
			obj = $('#switcher', objParent);
		
		if(obj.hasClass('collapsed')) {
			$('.trigger', objParent).animate({ opacity: 1 });
			objParent.animate({ left: 0 }, 400, function() {
				obj.addClass('expanded');
				obj.removeClass('collapsed');
			});
		} else {
			objParent.animate({ left: -200 }, 400, function() {
				obj.addClass('collapsed');
				obj.removeClass('expanded');
				$('.trigger', objParent).animate({ opacity: 0.6 });
			});			
		}
		
		return false;
	});
	
	$('#switcher_container #switcher select').change(function() {
		window.location.href = $(this).val();			
	});
	
});


// ======================================================================
// GLOBAL
// ======================================================================
$(window).load(function() {	

	/* NAVIGATION
	-------------------------------------- */
	$('nav #nav').superfish({
		animation: {opacity:'show', height: 'show'},
		autoArrows: false,
    	dropShadows: false,
		speed: 400
	});
	
	$('nav #nav ul.sub-menu').each(function() {
		var parent = $(this).parent(),
			gParent = parent.parent(),
			parentWidth = parent.width();
		
		if(!(gParent.hasClass('sub-menu'))) {
			$(this).css({ left: -(150-parentWidth)/2 });
		}
		
		$('li a', this).hover(function() {
			$(this).stop().animate({textIndent: 5}, 200);	
		}, function() {
			$(this).stop().animate({textIndent: 0}, 200);			
		});
		
	});
	
	
	/* FRONT PAGE
	-------------------------------------- */
	$('.front_page #body_content, .archive #body_content').masonry({ itemSelector: '.project:visible', animate: true }, function() {
		var	content = this.parent(),
			contentWidth = this.parent().innerWidth(),
			numProjects = this.size(),
			numProjectsHorz = (contentWidth/240) + '';
			wholeNumProjects = numProjectsHorz.replace(/\.[^.]+$/,  ""),
			num = '';
		
		// checks if width is less than 700px, if so, set header width to 700.
		// to prevent nav collapsing
		if(((wholeNumProjects * 240) - 20) < 700) {
			num = 700;
		} else {
			num = ((wholeNumProjects * 240) - 20);	
		}
		
		// sets header width to a multiple of projects width.
		$('.front_page header, .archive header').width(num);			
	});
	
	// project thumbnail
	$('.project .project_thumb, a.zoom').each(function() {
		var img = $('img', this),
			imgWidth = img.innerWidth();
			imgHeight = img.innerHeight();
			
		$(this).width(imgWidth);
		$(this).height(imgHeight);
		
		$(this).hover(function() {
			$('img', this).stop(true, true).animate({opacity: 0.5}, 300);	
		}, function() {
			$('img', this).stop(true, true).animate({opacity: 1.0}, 200);	
		});
	});
	
	// project featured badge
	$('.project .sticky_badge').hover(function() {
		var project = $(this).parent();
		$('.project_thumb img', project).stop(true, true).animate({opacity: 0.5}, 300);
	}, function() {
		var project = $(this).parent();
		$('.project_thumb img', project).stop(true, true).animate({opacity: 1.0}, 200);
	});
	
	// tooltips
	$('.projects_pagination a').each(function() {
		$(this).attr('original-title', $(this).html());
	})
		
	// tooltips
	$('.projects_pagination .pagi_before a').tipsy({gravity: 'w', html: true, fade: true, opacity: 0.8});
	$('.projects_pagination .pagi_after a').tipsy({gravity: 'e', html: true, fade: true, opacity: 0.8});
	$('#single_project .single_project_related_list li').tipsy({gravity: 's', html: true, fade: true, opacity: 0.8});
		
	
	/* FOOTER
	-------------------------------------- */
	$('.footer_widget_container ul:not(.wizy-thumb-list, .wizy_twitter_widget, .social_profiles) li a').hover(function() {
		$(this).animate({textIndent: '+=10'}, 200);	
	}, function() {
		$(this).animate({textIndent: '-=10'}, 200);			
	});
	
	
	/* SINGLE PROJECT
	-------------------------------------- */
	$('#single_project .single_project_share img, .social_profiles li a').hover(function() {
		$(this).stop(true, true).animate({opacity: 0.5}, 300);	
	}, function() {
		$(this).stop(true, true).animate({opacity: 1.0}, 200);	
	});
	
	
	

	/* SIDEBAR
	-------------------------------------- */
	$('#sidebar .sidebar_widgets_container .widget_container > ul').each(function() {
		$('a:last', this).css({ borderBottom: 'none' });	
	})


	// ======================================================================
	// SHORTCODES
	// ======================================================================

	$('.wp-caption').each(function() {
		$(this).width( $('img', this).width() );
	});
	 
	/* SLIDER
	-------------------------------------- */
	$('.wizy_slider_inline_container .wizy_slider_nav_inline').each(function() {
		$(this).css({marginLeft: -($(this).innerWidth()/2)});
	});	
	
	/* TABBED CONTENT
	-------------------------------------- */
	$('.wizy_tabbed').each(function() {
		$(this).width($(this).innerWidth() - 30);
	});
	
	/* COLLAPSIBL 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;
		});
	});
	
	/* MESSAGES
	-------------------------------------- */
	$('.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;
		});		
	});
	
});


// ======================================================================
// AJAX PAGINATION
// ======================================================================
$(document).ready(function() {
	
	function getPaged() {
		var	pagedInitial = $('#body_content').attr('data-paged'),
		paged = pagedInitial.replace(/_/, "");
		
		return paged;		
	}
	
	$('.projects_pagination a').live('click', function(e){
		
		e.preventDefault();
		var url = $(this).attr('href'),
			paged;
		
		$('#body_content_container').animate({opacity: 0.4});
		$('#wrapper').append('<div class="projects_loader"></div>');
		
		$('#body_content_container').load(url+' #body_content', function() {
			
			$('#body_content_container').animate({opacity: 1.0})
			
			$('.projects_loader').fadeOut(500, function() {
				$(this).remove();	
			});
			
			// paged = '?paged=' + getPaged();
			window.location.href = url;
						
		});
		
	});
	
	$('.zoom').colorbox();
	
});
