var masch = masch || {};

masch.init = function() {
	jQuery('div#portfolio-items a img').each(function() {
		var elm = this,
			$otherImages = $(elm).parents('div#portfolio-items').find('img').not(elm);
		jQuery(elm).hover(function() {
			$otherImages.stop();
			$otherImages.animate({
				opacity: 0.2
			}, 500);
		}, function() {
			$otherImages.stop();
			$otherImages.animate({opacity: 1}, 500);
		});
	});
	
	//make portfolio items lightbox enabled
	jQuery('div#portfolio-items a, a[rel=home-lowry-exhib]').fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		padding: 15,
		cyclic: true,
		overlayColor: '#000',
		overlayOpacity: 0.6,
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			var titleDisplay = title && title.length > 0 ? title + '<br /><br />' : ''; 
		    return '<span id="fancybox-title-over">' + titleDisplay +  (currentIndex + 1) + ' of ' + currentArray.length + '</span>';
	}});
	
	//hide show form field values on blur and focus
	var $formElms = jQuery('form#contact-form input.textfield, form#contact-form textarea');
	$formElms.each(function() {
		jQuery(this).data('default', jQuery(this).val());
	});
	
	$formElms.focus(function() {
		if(jQuery(this).val() === jQuery(this).data('default')) {
			jQuery(this).val("");
		}		
	}).blur(function() {
		if(jQuery(this).val().length === 0) {
			jQuery(this).val(jQuery(this).data('default'));
		}
	});
	
	/** animate the front page fading images **/
	//we need to first show the images wrapper lis since they are hidden to begin with
	//and we don't begin the animation until all the images have loaded
	var containerUl = jQuery('#image-container ul'),
		containerListElms = containerUl.find('li'),
		numImages = containerListElms.length,
		loadCounter = 0;
	jQuery('#image-container ul li').each(function() {
		var tempImg = new Image();
		tempImg.onload = function() {
			loadCounter++;
			if(loadCounter === numImages) {
				containerListElms.show();
				var index = numImages - 1;
				(function() {
					if(containerListElms.eq(index + 1)) {
						containerListElms.eq(index + 1).prependTo(containerUl).find('img').css('opacity', 1);
					}
					index = index === -1 ? numImages - 1 : index;

					containerListElms.eq(index).find('img').animate({
						opacity: 0
					}, 8000, 'easeInQuart', arguments.callee);

					index -= 1;
				})();
			}
		}
		tempImg.src = $(this).find('img').attr('src');
	});
	
	/** end of front page animation **/
	
	var shufflerContainer = masch.shufflerContainer = jQuery('#testimonial-shuffler ul'),
		testimonial = 0,
		numTestimonials = shufflerContainer.children('li').length,
		testimonialWidth = 327;
		
	shufflerContainer.children('li:first').clone().appendTo(shufflerContainer);
		
	shufflerContainer.width(((numTestimonials + 1) * testimonialWidth) + 'px');
	shufflerContainer.css('position', 'relative');
	
	if(numTestimonials > 1) {
		setInterval(function() {
			masch.shufflerContainer.animate({
				left: '-=327px'
			}, 1000, 'easeOutQuart', function() {
				testimonial++;
				if(testimonial === numTestimonials) {
					testimonial = 0;
					shufflerContainer.animate({
						left: '0px'
					}, 0);
				}
			});
		}, 9000);
	}
	
}

jQuery(document).ready(function() {
	masch.init();
})