﻿/*
Name: Javascript Functions
Version: 1.0
Description: Javascripts para o site
Author: Alex Koti
Author URI: http://alexkoti.com
 */


jQuery(document).ready(function($){
	//remover status de javascript do documento
	$('html').removeClass('no-js');
	
	
	
	/**
	 * CUSTOM MODERNIZR
	 * Workarounds para IEs
	 * 
	 * 
	 * 
	 */
	// Aplicar index para nth-child
	$('ol').each(function(){
		$(this).find('li').each(function(){
			var index = $(this).index() + 1;
			$(this).addClass('item_' + index);
		});
	});
	
	
	
	/**
	 * VALIDATION
	 * Ações para validação js de comentários
	 * 
	 * 
	 * 
	 */
	$("#commentform").validate({
		rules: {
			// name_do_input: 'método de validação para aplicar, separado por espaços'
			comment: 'defaultInvalid'
		},
		messages: {
			// name_do_input: 'Mensagem'
			comment: 'Digite uma mensagem',
			author: 'Este campo é obrigatório',
			email: 'Digite um endereço de e-mail válido'
		},
		errorPlacement: function(label, element) {
			// label - elemento <label> gerado pelo validator
			// element - input validado
			// pegar o id do elmento e assim desconbrir o <label> como 'for' correspondente
			label.insertAfter( $('label[for=' + element.attr('id') + ']') );
		}
	});
	//remover label de error ao focar os inputs
	$('.form_element input, .form_element textarea').hover(function(){
		$(this).siblings('.error').fadeOut();
	});
	// método personalizado de validação - 'defaultInvalid'
	jQuery.validator.addMethod('defaultInvalid', function(value, element) {
		switch (element.value) {
			case 'Digite sua mensagem aqui':
				if (element.name == 'comment')
				return false;
				break;
			default: return true; 
				break;
		}
	});
	
	
	
	/**
	 * RESETAR INPUTS DE TEXTO
	 * Pega o valor padrão e remove em focus() e retoma em blur(), em caso de valor não preenchido
	 * 
	 * 
	 * 
	 */
	function onfocus_reset(){
		$('.ipt_text').each(function(){
			$(this).attr( 'default', $(this).val() );
		}).blur(function(){
			var default_val = $(this).attr('default');
			if( $(this).val() == '' ){
				$(this).val( default_val );
			}
		}).focus(function(){
			var default_val = $(this).attr('default');
			if( $(this).val() == default_val ){
				$(this).val('');
			}
		});
	}
	onfocus_reset();
	
	
	
	/**
	 * NEWSLETTER
	 * 
	 * 
	 * 
	 */
	$('.newsletter_box form:not(.form_error) .hide').hide();
	$('.form_msg').click(function(){
		$(this).slideUp(function(){
			$(this).next('fieldset').slideDown(function(){
				$(this).find('.ipt_submit').fadeIn();
			});
		});
	});
	$('#ipt_email').click(function(){
		$('.newsletter_box .hide').slideDown();
	});
	
	
	
	/**
	 * TABS
	 * 
	 */
	$('.tab_box .tab_content:gt(0)').hide();
	$tabs = $('.tab_box .tabs a');
	$tabs.first().addClass('active');
	
	$tabs.click(function(){
		change_tab( $(this).attr('href') );
		return false;
	});
	
	$('.aba_proxima').click(function(){
		var next = $(this).parent().next().attr('id');
		if( next == undefined ){
			next = $( '.tab_content:first', $(this).closest('.tab_box') ).attr('id');
		}
		change_tab( '#'+next );
	});
	
	function change_tab( id ){
		$tabs.attr('class', ''); // Remover class 'active' de todas as abas
		
		$button = $(id + '_anchor');
		$button.addClass('active'); // Atribuir 'active' ao link clicado
		
		//indicador
		$indicator = $(id).parent().find('.aba_indicador');
		var anchor_p = $button.position();
		$indicator.animate({
			left: (anchor_p.left + 33)
		});
		
		$('.tab_box .tab_content').hide();
		$(id).show(); // Mostrar conteúdo da aba requisitada
	}
	
	
	
	/**
	 * RESENHAS
	 * 
	 */
	$('.aquisicao_resenha, .aquisicao_resenha .btn').hide().addClass('closed');
	$('.ver_resenha').click(function(){
		$resenha = $( '.aquisicao_resenha', $(this).closest('.aquisicao_item') );
		$btn = $resenha.find('.btn');
		if( $resenha.is('.closed') ){
			$resenha.slideDown(
				400, function(){$btn.fadeIn();}
			).removeClass('closed');
		}
		else{
			$btn.fadeOut(
				'fast', function(){$resenha.slideUp().addClass('closed');}
			);
		}
		return false;
	});
	$('.aquisicao_resenha .btn').click(function(){
		$(this).fadeOut(
			'fast', 
			function(){
				$(this).parent().slideUp().addClass('closed');
			}
		);
	});
	
	
	
});




window.log = function(){
	log.history = log.history || [];   
	log.history.push(arguments);
	if(this.console){
		//console.log( Array.prototype.slice.call(arguments) );
	}
};
(function(doc){
	var write = doc.write;
	doc.write = function(q){ 
	log('document.write(): ',arguments); 
		if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
	};
})(document);
