//if(!console) { var console = { log: function(){ } }; }

$(document).ready(function(){
	// Team
	$('#team').hide().after('<a href="#" id="team-show" class="bgimage">Conheça nossa Equipe</a>');
	$('#team-show').click(function(){
		$('#team').slideDown(1000);
		$(this).fadeOut(900);
		return false;
	});
	
	// Links em outras janelas
	$('#quem-somos a, #o-que-fazemos a').attr('target', '_blank');
	
	// PNG Fix for IE 6
	$('img[src$=png]').pngFix();
	
	// Hashtag scrolling
	var scrollToItem = function(item) {
		var pos = Math.min($('body').height(), $(item.hash)[0].offsetTop);
		var dur = Math.pow(Math.abs($(window).scrollTop() - pos), .8) + 200;
		$('html, body').stop().animate( { scrollTop: pos },{duration: 1000, complete: function(){ /*window.location.hash = item.hash.substring(1);*/ } } );
	}
	
	// Menu navigation
	var selectMenuItem = function(item, scroll) {
		if(scroll) { scrollToItem(item); }
		$('#menu li').removeClass('selected');
		$(item).parent().addClass('selected');
	}
	$('#menu a').click(function(){
		selectMenuItem(this, true);
		return false;
	});

	$('#menu').css('position','absolute');
	var top = 0;
	var scrollThreshold = 0;
	var handleScroll = function(event) {
		top = $(window).scrollTop() + 30;
		
		// Position
		if($(window).scrollTop() < 380) {
			top = 410;
			if($.browser.msie && $.browser.version < 7) top = 455;
		} else if($(window).scrollTop() + $(window).height() > $('#contato')[0].offsetTop + $('menu').height() + 90) {
			//console.log($(window).scrollTop(), $(window).scrollTop() + $(window).height());
			top = $('#contato')[0].offsetTop + 25;
		}
		
		// Marked
		if($(window).scrollTop() < $('#o-que-fazemos')[0].offsetTop - 50) {
			selectMenuItem(($('#menu-quem-somos a')[0]), false);
		} else if($(window).scrollTop() > $('#o-que-fazemos')[0].offsetTop - 50 && ($(window).scrollTop() + $(window).height() < $('#contato')[0].offsetTop + $('menu').height() + 90)) {
			selectMenuItem(($('#menu-o-que-fazemos a')[0]), false);
		} else if($(window).scrollTop() + $(window).height() > $('#contato')[0].offsetTop + $('menu').height() + 90) {
			selectMenuItem(($('#menu-contato a')[0]), false);
		}
		$('#menu').stop().animate({'top': top}, 600);
	}
	$(window).scroll(handleScroll);
	handleScroll();
	
	var textReplace = function(){
		$('#projects h3, #projects .delivered, #projects .client').each(function(){
			if($('object, embed',this).length > 0) return; 
			$this = $(this);
			$this.flash(
					{src:'/swf/tradegothic.swf'},{version:8},
					function(htmlOptions) {
						htmlOptions.flashvars.txt = $this.html().replace(/%/,'%25').replace(/<span>/,'<span class="ast">');
						htmlOptions.flashvars.css = "* { color: #282327;}";
						htmlOptions.flashvars.fitToArea = 'no';
						htmlOptions.flashvars.fontSize = $this.css('font-size').substring(0, $this.css('font-size').toString().length - 2);
						htmlOptions.wmode = 'transparent';
						htmlOptions.height = $this.height() + 2;
						if($this.is('h3')) {
							htmlOptions.height = 110;
						} else if($this.attr('class').indexOf('client') > -1) {
							htmlOptions.height = $this.height() * 2;
						}
						htmlOptions.width = '99%';
						if($this.css('font-weight') == 'bold' || $this.css('font-weight') == 700) { htmlOptions.flashvars.txt = '<strong>' + htmlOptions.flashvars.txt + '</strong>'; }
						$this.empty().addClass('replaced').prepend($.fn.flash.transform(htmlOptions));
					}  
			); 
		});
	}
	textReplace();
	
	
	$('#projects').addClass('enhanced');
	var addImageNav = function(item){
		if($(item).hasClass('enhanced')) return;
		if($('li', item).length <= 1) return;
		
		$(item).addClass('enhanced').prepend('<span title="Próxima imagem" class="bgimage next">&gt;</span><span title="Imagem anterior" class="bgimage prev">&lt;</span>');
		
		$('li', item).each(function(){
			$(this).fadeTo(50, 0);
			this.navTo = function(next) {
				$('li',$(this).parent()).stop().fadeTo(1000, 0);
				$(this).stop().fadeTo(1000, 1);
			}
		});
		
		$('.next',item).click(function(){
			$curr = $('li.viewable', $(this).parent());
			if($curr.next()[0]) {
				$next = $curr.next();
			} else {
				$next = $('li:first', $(this).parent());
			}
			$next[0].navTo(true);
			$next.addClass('viewable')
			$curr.removeClass('viewable');
		}).hover(function(){ $(this).addClass('next-hover'); }, function(){ $(this).removeClass('next-hover'); });
		
		$('.prev',item).click(function(){
			$curr = $('li.viewable', $(this).parent());
			if($curr.prev()[0]) {
				$prev = $curr.prev();
			} else {
				$prev = $('li:last', $(this).parent());
			}
			$prev[0].navTo(true);
			$prev.addClass('viewable')
			$curr.removeClass('viewable');
		}).hover(function(){ $(this).addClass('prev-hover'); }, function(){ $(this).removeClass('prev-hover'); });
		
		$('li:first', item).addClass('viewable').fadeTo(100, 1);
		
	}
	$('#projects>li').each(function(){ addImageNav(this); });
	
	$('#projects').after('<span id="projects-more-indicator" class="bgimage">&nbsp;</span><a href="#" id="projects-more" class="bgimage">Mostrar mais projetos</a>');
	$('#projects-more-indicator').fadeTo(1, 0);
	$('#projects-more').data('offset', 0).click(function(){
		$link = $(this);
		if($link.data('loading')) return false;
		$link.data('loading', true);
		$('#projects-more-indicator').fadeTo(300, .5);
		$.get('/projects/more/' + $(this).data('offset'), null, function(data){
			if(data.toString().indexOf('<div') == -1) {
				$('#projects-more').fadeOut(200);
				$('#projects').after('<p id="no-more" class="offset">' + data + '</p>');
			} else {
				var count = $('#projects>li').length;
				$('#projects').append(data);
				textReplace();
				$('#projects>li').each(function(){ addImageNav(this); });
				$('#projects-more').data('offset', $('#projects-more').data('offset') + $('#projects>li').length - count);
			}
			$('#projects-more-indicator').fadeTo(300, 0);
			$link.data('loading', false);
			$link.blur();
		});
		return false;
	});
	
	// AJAX contact form
	$('#ContactSendForm button').wrap('<div class="submit"></div>');
	$('#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;
		});
});