// JavaScript Document
var TT = {};
var thisPage = window.location.href;
if ( thisPage.match(/^http:\/\/localhost/) ) {
	var SITE_URL = 'http://localhost/treinototal.com.br/www/';
} else {
	var SITE_URL = '/revista/';
}

jQuery( function($) {
	$('.tabs > ul').tabs({ cookie : { expires : 30 }, fxFade: { opacity: 'toggle' }, fxSpeed: 'fast'});
	$('.text').resetDefaultValue();
	TT.imc_calculator();
	$("#frm-select-antes-depois select").change(function() { window.location = $(this).val(); });
	$("#frm-select-exercises select").change(function() { window.location = $(this).val(); });
	
	// Antes e Depois lightBox
	$('.antes-e-depois-images a').lightBox({
		fixedNavigation : true
	});
	
	// Guia de exercícios lightBox
	$('.exercise-guide-images a').lightBox({
		fixedNavigation : true
	});
	
	// Academia lightBox
	$('#gym-image a:first').lightBox({
		fixedNavigation : true
	});
	
	// Profissional lightBox
	$('#professional-image a:first').lightBox({
		fixedNavigation : true
	});
	
	// Contest photo
	$('.participant-photo a').lightBox({
		fixedNavigation : true
	}).click( function() {
		var id = $(this).attr('rel');
		$('#participant_selected_' + id).attr('checked', 'checked');
	});
	
	// 
	$('.participant-photo-win a, .mural-product-photo a').lightBox({
		fixedNavigation : true
	});
	
	//
	$('#tt-photos-user').hide();
	
	TT.toggle_accept_terms('#tt-accept-terms');
	
});

/**
 *
 */
TT.toggle_accept_terms = function( jQuerySeletorObj ) {
	
	$(jQuerySeletorObj).click( function() {
		var is_checked = $(jQuerySeletorObj + ':checked').length;
		if ( is_checked )
			$('#tt-photos-user').slideDown();
		else
			$('#tt-photos-user').slideUp();
	} );
	
	var is_checked = $(jQuerySeletorObj + ':checked').length;
	if ( is_checked )
		$('#tt-photos-user').slideDown();
}

/**
 *
 *
 */
TT.trika_recomenda = function(tr_id) {
	$('#trika-recomenda-product').slideToggle().html('<p><strong>Carregando...</strong></p>').slideToggle().load(tt_trika_recomenda_ajax_url, { 'tr_id' : tr_id });
};

/**
 *
 *
 */
TT.imc_calculator = function() {
	var objIMCResult = jQuery('#imc-results');
	jQuery('#frm-imc').submit(function() {
		// Get weight and height values
		var weight 		= jQuery('#imc_weight').val();
		var height 		= jQuery('#imc_height').val();
		// Change comma to dot
		weight = weight.replace(',', '.');
		height = height.replace(',', '.');
		// Check if numbers was informed
		if ( isNaN(weight) || isNaN(height) ) {
			alert('Por favor, informe somente números nos campos de peso e altura. Obrigado.');
		}
		// The IMC calculator
		var imc_calc	= round(( weight / ( height * height ) ), 2);
		// Show the results to user
		jQuery('#imc-value').fadeOut().html(imc_calc).fadeIn();
		// IMC situtations
		var arrIMCSituations = [];
		arrIMCSituations[0] = 'Peso subnormal';
		arrIMCSituations[1] = 'Peso normal';
		arrIMCSituations[2] = 'Excesso de peso';
		arrIMCSituations[3] = 'Obesidade I (leve)';
		arrIMCSituations[4] = 'Obesidade II (moderada)';
		arrIMCSituations[5] = 'Obesidade III (extrema)';
		// Show the situations to user
		var imc_situation = '';
		if ( imc_calc < 18.5 ) {
			imc_situation = arrIMCSituations[0];
		}
		if ( (imc_calc >= 18.5) && (imc_calc < 24.9) ) {
			imc_situation = arrIMCSituations[1];
		}
		if ( (imc_calc >= 25) && (imc_calc < 29.9) ) {
			imc_situation = arrIMCSituations[2];
		}
		if ( (imc_calc >= 30) && (imc_calc < 34.9) ) {
			imc_situation = arrIMCSituations[3];
		}
		if ( (imc_calc >= 35) && (imc_calc < 39.9) ) {
			imc_situation = arrIMCSituations[4];
		}
		if ( imc_calc >= 40 ) {
			imc_situation = arrIMCSituations[5];
		}
		jQuery('#imc-situation').fadeOut().html(imc_situation).fadeIn();
		return false;
	});
};

/**
 * http://kevin.vanzonneveld.net
 *
 */
function round ( val, precision ) {
    var precision = (round.arguments.length > 1) ? round.arguments[1] : 0;
    return Math.round(val * Math.pow(10, precision))/Math.pow(10, precision);
};
