function initLayout() {
	var slides = $('.autoSize');

	slides.css('width', function() {
		return $(document.body).width() - parseInt($(this).css('padding-left'), 10) * 2
	});

	$('#sectionGallery .slideshow .wrapper:visible').each(function() {
		var slides = $(this).find('.slide').css('margin-left', '').filter(':visible'),
			max = $(document.body).width(),
			actual = $(this).width();

		if(actual < max) {
			slides.first().css({ marginLeft: (max - actual) / 2 });
			$(this).parent().css({ marginLeft: 0 });
		}
	})

	$('[href="' + window.location.hash + '"]').trigger('click', [false]);
}

$(function() {
	$(window).resize(function(){ initLayout() }).trigger('resize');

	$('a.galleryLightbox').fancybox({
		titleShow: true,
		transitionIn: 'elastic',
		transitionOut: 'elastic'
	});

	// Setup service menu links
	$('.menuservices ul > li').click(function() {
		
		console.trace();
		
		var index = $(this).index(),
			slider = $('.contentTextSlide');

		slider.stop().animate({
			scrollTop : index * slider.height()
		},
		350, 'easeInOutExpo');
	});

	$('#sectionGallery .slideshow .container')
		.live('mousemove', function(event) {
			var container = $(this).children(),
				limit = 100,
				divWidth = $(this).width(),
				ulWidth = $(this).find('.wrapper').width(),
				newOffset = ((event.pageX - $(this).offset().left) * ((ulWidth + (limit*2)) - divWidth) / divWidth) - limit;

			if(newOffset < 0) { newOffset = 0 }
			else if((newOffset + divWidth) > ulWidth) { newOffset = ulWidth - divWidth; }

			container.css({ marginLeft: -newOffset });
		})
	.find('.slide').fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		speedIn: 600,
		speedOut: 200,
		overlayShow: true,
		showNavArrows: true,
		titlePosition: 'over',
		onComplete: function() {
			Cufon.replace('#fancybox-title-over');
		}
	});


	// Setup gallery categories
	$('#sectionGallery ul.categories li').click(function() {
		if( $(this).hasClass('selected') ) {
			return false;
		}

		var category = $(this).attr('rel').match(/-(.+)$/i)[1],
			master = $('#sectionGallery .slideshow'),
			shoes = category === 'shoes';

		//alert(category);

		if(!shoes) { $('#galleryBrands').attr('disabled', 'disabled'); }
		else { $('#galleryBrands').removeAttr('disabled'); }
			
		$(this).addClass('selected').siblings().removeClass('selected');

		master.animate({ opacity: 0 }, 350, 'easeOutExpo', function() {
			var newProducts = $('#'+category+'-products'),
				oldProducts = newProducts.siblings('.container');

			oldProducts.hide().addClass('hidden');
			newProducts.show().removeClass('hidden');

			$('#sectionGallery .slideshow .slide').css('margin-left', '').show();
			$('#galleryBrands option').removeAttr('selected').first().attr('selected', 'selected');
			initLayout();

			master.animate({ opacity: 1 }, 350, 'easeInExpo', function() {
				// Fix IE opacity
				if($.browser.msie && $(this).get(0).style) {
					$(this).get(0).style.removeAttribute('filter');
				}
			});
		})

		return false;
	});

	// Setup shoe brands
	$('#galleryBrands option').bind('click', function() {
		var self = $(this),
			master = $('#sectionGallery .slideshow'),
			slides = master.find('.slide');

		master.stop().animate({ opacity: 0 }, 350, 'easeOutExpo', function() {
			if(self.val() === '') {
				slides.show();
			}
			else {
				slides.each(function(i) {
					$(this).toggle($(this).attr('rel') === self.val());
				});
			}
			initLayout();

			master.animate({ opacity: 1 }, 350, 'easeInExpo', function() {
				// Fix IE opacity
				if($.browser.msie && $(this).get(0).style) {
					$(this).get(0).style.removeAttribute('filter');
				}
			});
			$(this).dequeue();
		})
	})
	.parent().bind('change', function() {
		$(this).find(':selected').trigger('click');
	})

	// Setup navigation links
	$('#nav ul li a, [href^="#"]').bind('click', function(event, animate) {
		var slide = $('#section' + $(this).attr('href').substr(1)),
			index = slide.index(),
			navItem = $('#nav li').eq(index),
			offset = index * $(document.body).width();

		navItem.addClass('selected').siblings().removeClass('selected');

		$('#carouselWrapper').stop()
			.animate({ scrollLeft : offset }, animate === false ? 0 : 750, 'easeInOutExpo');
	})

	// Auto select page on document load
	$('#nav ul li a[href="' + window.location.hash + '"]:first').trigger('click', [false]);
	
	
	// Setup contact form
	$('#ContentFormSubmit').bind('click', function(event) {
		var form = $(this).parents('form'),
			inputs = form.find(':input:not([type="image"])').attr('disabled', 'disabled'),
			data = {},
			valid = true;

		// Check form data
		inputs.each(function() {
			if(
				($(this).hasClass('required') && !$(this).val().length) ||
				($(this).is('[name="email"]') && !(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/).test($(this).val()))
			) {
				var name = $(this).is('textarea') ? $(this).attr('rel') : $(this).parent().prev().text();
				name = name.substr(0, name.length - ($(this).hasClass('required') ? 2 : 1));

				alert(name + ' invalido');
				valid = false;

				inputs.removeAttr('disabled');
				$(this).focus();
				
				return false;
			}
		})
		
		if(valid) {
			// Show loading
			$.fancybox.showActivity();

			// Setup data
			inputs.each(function(){ data[ $(this).attr('name') ] = $(this).val()  });

			// Send form
			$.ajax({
				url: 'emailit.php',
				data: data,
				success: function(message) {
					// Setup fancybox message and remove disabled attribute
					$.fancybox(message);
					inputs.removeAttr('disabled');
					
					// Clear fields and send user to showroom
					inputs.val('');
					$('#nav li a[href="#Gallery"]').trigger('click');
					document.location.hash = 'Gallery';
				},
				type: 'post'
			})
		}
		
		event.preventDefault();
		event.stopImmediatePropagation();
		return false;
	})
});

Cufon.replace('.menuservices li', {hover: true});
Cufon.replace('h1, h2');
