$(document).ready(function(){
	jQuery.extend( jQuery.easing, { def: 'easeOutCubic', easeInCubic: function (x, t, b, c, d) { return c*(t/=d)*t*t + b; }, easeOutCubic: function (x, t, b, c, d) { return c*((t=t/d-1)*t*t + 1) + b; } });
	jQuery.easing.def = 'easeOutCubic';
	
	if($.browser.msie && $.browser.version <= 6) {
		$('#menu').css({'position': 'absolute'});
		var handleScroll = function(event) {
			$('#menu').stop().animate({'top': $(window).scrollTop() + 60}, 600);
		}
		$(window).scroll(handleScroll);
		handleScroll();
	}
	
	// Hashtag scrolling
	var scrollToItem = function(item) {
		if(pageTracker) pageTracker._trackPageview('/#' + item.hash);
		var pos = $(item.hash)[0].offsetTop - $(item)[0].offsetTop;
		var dur = Math.round(Math.pow(Math.abs($(window).scrollTop() - pos), .8) + 200);
		$('html, body').stop().animate( { scrollTop: pos },{duration: dur, complete: function(){ /* window.location.hash = item.hash.substring(1); */ } } );
	}
	
	// Menu navigation
	var selectMenuItem = function(item) {
		scrollToItem(item);
		$('#menu li').removeClass('selected');
		$(item).parent().addClass('selected');
	}
	$('#menu a').click(function(){ selectMenuItem(this); return false; });
	//if(window.location.hash.indexOf('#') > -1) { selectMenuItem($('#link-' + window.location.hash.substring(1))[0]); }
	
	// Product navigation handling
	$('div.has-nav').each(function(index){
		$('h2 strong',this).text('&nbsp;');
		$('li>p', this).hide();
		$('h2', this).append('<span title="Próximo" class="bgimage next">&gt;</span><span title="Anterior" class="bgimage prev">&lt;</span>');

		$('li', this).each(function(){
			this.navTo = function(next) {
				var newText = $('p:first',this).text();
				if($.browser.msie) {
					$('h2 strong',$(this).parent().parent()).fadeOut(200, function(){ $(this).text(newText).fadeIn(400); });
				} else {
					$('h2 strong',$(this).parent().parent()).animate({opacity: '.5', top: next ? '23px' : '-23px'}, {duration:200, complete:function(){$(this).text(newText).css('top', next ? '-23px' : '23px').animate({opacity: '1', top: '0px'}, {duration: 400});}});
				}
				$('li',$(this).parent()).stop().fadeTo(600, 0);
				$(this).stop().fadeTo(600, 1);
			}
		});
		
		$('li:first', this).addClass('viewable');
		
		$('.next',this).fadeTo(1,.4).click(function(){
			$curr = $('li.viewable', $(this).parent().parent());
			$next = ($curr.next()[0]) ? $curr.next() : $('li:first', $(this).parent().parent());
			$next.addClass('viewable')
			$curr.removeClass('viewable');
			$next[0].navTo(true);
		}).hover(function(){ $(this).fadeTo(350,1); }, function(){ $(this).fadeTo(350,.4); });
		
		$('.prev',this).fadeTo(1,.5).click(function(){
			$curr = $('li.viewable', $(this).parent().parent());
			$prev = ($curr.prev()[0]) ? $curr.prev() : $('li:last', $(this).parent().parent());
			$prev.addClass('viewable')
			$curr.removeClass('viewable');
			$prev[0].navTo(false);
		}).hover(function(){ $(this).fadeTo(350,1); }, function(){ $(this).fadeTo(350,.4); });
		
	});
	
	// Load first image
	$('div.has-nav').each(function(){
		$('li', this).fadeTo(5, 0);
		$('li:first',this)[0].navTo();
	});
	
	
	// AJAX contact form
	$('#ContactSendForm div.submit').append('<span style="display:none">Enviando...</span>');
	$('#ContactSendForm div.submit').before('<div style="display:none" class="result"></div>');
	$('#ContactSendForm').submit(function(){
			$form = $(this);
			$('div.submit span', $form).fadeIn(100);
			$('div.result', $form).slideUp(100);
			$.post($(this).attr('action') + '.js', $(this).serialize(), function(data){
				var message = '';
				if(data && data.success) {
					$('input:text, textarea', $form).val('');
					message = 'Sua mensagem foi enviada, obrigado.';
				} else {
					if(data.validation) {
						var i = 0;
						$.each(data.validation, function(index){ if(i > 0) message += '<br />'; message += this; i++; });
					} else {
						message = 'Ocorreu um erro enviando sua mensagem.';	
					}
				}
				$('div.submit span', $form).fadeOut(300);
				$('div.result', $form).html(message).slideDown(300);
			},'json');
			return false;
		});
	
	$('body').animate({opacity: 1}, {duration: 6000, complete: function(){
		$('img').each(function(){
			if($(this).attr('src') == '#') {
				$(this).attr('src', $(this).data('src'));
			}
		});
	}})
});