jQuery(document).ready(function () {
	

	jQuery(".outro").hide();

	jQuery("#telefone").mask("(99) 9999-9999");
	jQuery("#telefone2").mask("(99) 9999-9999");
	
	jQuery("#id_curso").change(function () {
		if (jQuery(this).val() == '31') {
			jQuery(".outro").slideDown();
			jQuery(".outro").focus;
			}else{
			jQuery(".outro").slideUp();
		}
	});

	jQuery('.input-cursos, .input-newsletter, .busca').focus(function () {
		jQuery(this).val('');
	});

	jQuery('.course-filter a').click(function () {
		jQuery(this).next('.filter').slideToggle();
		return false;
	});

	jQuery('.course-filter .filter select').change(function () {
		if (jQuery(this).val() && course_filter_link) {
			window.location = course_filter_link + (course_filter_link.indexOf('?') > 0 ? '&' : '?') + this.name + '=' + jQuery(this).val();
		}
	});

	if (jQuery('div.calendar').length) {
		var data = {
			action: 'ctp_calendar'
		};

		jQuery.post(ajaxurl, data, function(response) {
			show_ctp_calendar(response);
		});
	}
});

function show_ctp_calendar(response) {
	jQuery('div.calendar').append(response);
	jQuery('div.calendar table a.nav').click(function () {
		var m = this.href.match(/(\d{4})\-(\d+)$/);
		if (m && m.length == 3) {
			var data = {
				action: 'ctp_calendar',
				year: m[1],
				month: m[2]
			};

			jQuery.post(ajaxurl, data, function(response) {
				jQuery('div.calendar table').remove();
				show_ctp_calendar(response);
			});
		}
		return false;
	});
}

function atualizaContador(YY,MM,DD,HH,MI,saida) {
	var SS = 00;
	var hoje = new Date();
	var futuro = new Date(YY,MM-1,DD,HH,MI,SS);

	var ss = parseInt((futuro - hoje) / 1000);
	var mm = parseInt(ss / 60);
	var hh = parseInt(mm / 60);
	var dd = parseInt(hh / 24) + 1;

	ss = ss - (mm * 60);
	mm = mm - (hh * 60);
	hh = hh - (dd * 24);
 
	var faltam = '<div id="date-limit" class="left">Voc&ecirc; ainda tem ';
	faltam += (dd && dd > 1) ? dd+' dias para fazer sua inscri&ccedil;&atilde;o com 15% de desconto.</div><!--date-limit-->' : (dd==1 ? '1 dia para fazer sua inscri&ccedil;&atilde;o com 15% de desconto.</div><!--date-limit-->' : '');
/*	faltam += (toString(hh).length) ? hh+':' : '';
	faltam += (toString(mm).length) ? mm+':' : '';
	faltam += ss;
*/
	if (dd+hh+mm+ss > 0) {
		document.getElementById(saida).innerHTML = faltam;
		atualizaContador(YY,MM,DD,HH,MI,saida);
	} else {
		document.getElementById(saida).innerHTML = '';
		atualizaContador(YY,MM,DD,HH,MI,saida);
	}

}

