$(document).ready(function(){
	/* FUNCTIONS ----------------------------- */
	// Function to get the Max value in Array
    Array.max = function( array ){
        return Math.max.apply( Math, array );
    };
	
	
	/* CODE ---------------------------------- */

	/* Apply external libraries */
	// Cycle front page slideshow
	$('#slideshow').cycle({ timeout: 8500, });

	// Validate contact page form
	$("#contact_form").validate();

	// Carousel our_work page sliders
	$('#our_work_slider').jcarousel();
	
	// TODO: delete? Might all be in AJAX
	// Cycle our_work page images
	$('.our_work_item .images').cycle({
		prev: '.page_back',
		next: '.page_forward',
		timeout: 0,

	});
	
	/* Apply our own code */
	
	
	// Prep items which will be shown and hidden (toggled)
	$('.showhide_target').hide();
	$('.showhide').live('click', function(e){
		e.preventDefault();
		var selector = $(this).attr('href').replace('#.', '.'); // Allows for class-based selectors
		$(selector).each(function(){
			if($(this).hasClass('slide')){
				$(this).slideToggle();		
			} else {
				$(this).toggle();
			}		
		});
	});
	
	$('.our_work_item_pager a').live('click', function(e){
		e.preventDefault();
	});
	
	
	// Set our_work page body to stretch to fit tallest portfolio image
	
	$('.our_work_item .images img, .our_work_item .images_dynamic img').imagesLoaded(function(){
		adjustImagesHeight();
	});
});

function adjustImagesHeight(){
	// Max function and widths from here: 
	// http://stackoverflow.com/questions/5052673/jquery-min-max-property-from-array-of-elements
	var widths= $('.our_work_item .images img, .our_work_item .images_dynamic img').map(function() {
        return $(this).height();
	}).get();
	var max_height = Array.max(widths);
	$('.our_work_item .images, .our_work_item .images_dynamic').css('min-height', max_height);
}
