jQuery(function($){
	// Remove no-js class from <html>
	$('html').removeClass('no-js').addClass('js');
	
	
	// Code for dropdown menu
	$('#header-nav').delegate( 'li', 'mouseenter mouseleave', function(e){
		if( e.type === 'mouseenter' ) {
			$(this).children('ul').stop(true, true).show(400);
		}
		else {
			$(this).children('ul').stop(true, true).hide(100);
		}
	});		 
	
	
	
	
	/***** Portfolio code *****/
	var $folioGallery = $('#folioGallery');
	
	
	// Setup prettyPhoto
	if($.fn.prettyPhoto) {
		$folioGallery.find('a[rel^="prettyphoto"]').prettyPhoto({
			overlay_gallery: false,
			social_tools: false,
			deeplinking: false
		})
		.append( '<span class="zoom-hover"/>' );
	}
	
	// Show an icon signifying a Lightbox, when hovering over images 	
	$folioGallery.delegate('span.zoom-hover', 'mouseenter mouseleave', function(e){
		if( e.type === 'mouseenter' ) {
			$(this).stop(true, true).fadeTo(600, 1);
		}
		else {
			$(this).stop(true, true).fadeTo(600, 0);
		}
	});
	
	
	
	
	// Code to filter gallery items			
	if( $folioGallery.hasClass('filter') ) {
		$folioGallery.find('li').each(function(){
			var categories = $(this).data('categories').split(','),
				categoryObj = {};
			
			$.each(categories, function(key, value){
				value = $.trim(value);
				categoryObj[value] = true;
			});
				
			// delete the old 'categories' data attribute and create a new one
			$(this).removeAttr('data-categories').data('categories', categoryObj);
		});
		
		
		//increase the width by 20px (right margin value) which is required by IE8 and lower
		if( $.browser.msie && $.browser.version < 9 ) {
			$folioGallery.width( $folioGallery.width() + 20 );
		}
		
		
		var $galleryClone = $folioGallery.find('ul').clone(true);
		
		$('#filterButtons').delegate('li', 'click', function(){
			var $this = $(this),
				thisindex = $this.parent().find('li').index($this),
				category = $this.data('category'),
				filteredItems;
								
			$this.addClass('active').siblings().removeClass('active');			
		
			if( category === 'all') {
				filteredItems = $galleryClone.find('li');
			}
			else {
				filteredItems = $galleryClone.find('li').filter(function(){
					return ( category in $(this).data('categories') )
				});
			}
							
			
			$folioGallery.find('ul').quicksand(filteredItems, {
				duration: 800,
				adjustHeight: 'dynamic',
				useScaling: false
			}, function(){				
				$(this).css('height', 'auto');
				
				// after filtering again setup pretyyPhoto
				$folioGallery.find('a[rel^="prettyphoto"]').prettyPhoto({
					overlay_gallery: false,
					social_tools: false,
					deeplinking: false
				});
			});
		});
	}
		
	
	
	
	// Replicate placeholder for input boxes in older browsers
	var $placeInput = $('input[placeholder]');
	
	if( 'placeholder' in document.createElement('input') ) {
		var placeholder = true;
	}
	else {
		var placeholder = false;
		$placeInput.val( $placeInput.attr('placeholder') );
	}
	
	$placeInput.focusin(function(){
		if( !placeholder && $placeInput.val() === $placeInput.attr('placeholder') ) {				
			$placeInput.val('');				
		}
	})
	.focusout(function(){
		if( !placeholder && $placeInput.val() === '' ) {
			$placeInput.val( $placeInput.attr('placeholder') );
		}
	});
	
	
	
	// Blog sidebar tabs
	$('#blog-sidebar').find('section.tabs span.tab').each(function(){
		var $this = $(this),
			$parent = $this.parent(),
			$panes = $this.siblings('ul.pane'),
			topheight = $parent.height() - $panes.filter(':visible').height();
		
		$this.click(function(){
			var index = $parent.find('span.tab').index($this);
			if( $panes.eq(index).is(':visible') ) {
				return;
			}
			else {
				$parent.height( $parent.height() );
				$panes.filter(':visible').fadeOut(400, function(){
					$panes.eq(index).fadeIn(400);
					
					if( $parent.height() !== topheight + $panes.eq(index).height() ) {
						$parent.animate({ height: topheight + $panes.eq(index).height() }, 400);
					}
				});
			}
		});
	});	
	
	
	
	// Validate the contact form
	if( $.fn.validator ) {
		$.tools.validator.fn("[data-minlength]", function(input, value) {
			var min = input.attr("data-minlength");
			
			return value.length >= min ? true : 'Please provide at least ' +min+ ' characters';
		});
		
		$('#contact-form').validator({ position: 'top center', offset: [-20, 80] })
			.submit(function(e){
				var form = $(this),
					formData = form.serialize(),
					action = form.attr('action'),
					loading = $('#loading');
					
				if( !e.isDefaultPrevented() ) {
					loading.fadeIn(400);
					
					$.ajax({
						type: 'post',
						url: action,
						data: formData,
						success: function( data ) {
							loading.fadeOut(600);
							if( data == 'success' ) {
								var parent = form.parent(),
									scroll = parent.offset().top;
								parent.height( parent.height() );
								
								form.fadeOut(600, function(){
									$('#success').fadeIn(600);
									$('html, body').animate({ scrollTop: scroll }, 600);
								});
							}
							else {
								alert('There was an error in sending your message. Please try again later.');
							}
						}
					});
					
					// prevent default form submission
					e.preventDefault();
				}	
				
			});
	}
	
});
