// JavaScript Document
 
//===================================================//
//	start of BIG HEADLINES 
//==================================================//
	var big_headline_timer = null;
	var big_headline_selected = 0;

	var show_big_headline = function(index){
		clearTimeout(big_headline_timer);
		for(i=0; i<3 ; i++){
			if(i!=index){//If it's not the selected one, hide it
				$('#big_headlines .big_headline:eq('+i+')').hide();
			}
		}
		$('#big_headlines .big_headline:eq('+index+')').fadeIn('fast');
		big_headline_selected = index;
		big_headline_timer = setTimeout(next_big_headline, 10000);
	}
 
	var next_big_headline = function(){
		var next_index = big_headline_selected+1;//The next index
		if(next_index>2){next_index=0;}//If it's bigger, reset
		show_big_headline(next_index);//Show the next headline
	}

	var set_big_headlines = function(){
		for(i=0; i<3 ; i++){$('#big_headlines .big_headline:eq('+i+') .description_block .left .big_headlines_nav li:eq('+i+') a').addClass('selected');}//Pre-select each big headline navigation link
		big_headline_timer = setTimeout(next_big_headline, 10000);//Set the timer
		show_big_headline(0)//Show the first one
	}
//===================================================//
//	end of BIG HEADLINES
//==================================================//

//===================================================//
//	start of VERIFY COMMENT
//==================================================//

	var enviar_comentario = function(article){
		
		//var recaptcha_challenge = Recaptcha.get_challenge();
		//var recaptcha_response = Recaptcha.get_response();
		
		$('#comment_form .loader').show();
		
		$.ajax({
			type:"POST",
			url:host+'/Comentarios/enviar/'+article,
			//url:'send_comment.php',
			//data:$('#comment_form').serialize()+'&challenge='+recaptcha_challenge+'&response='+recaptcha_response,
			data:$('#comment_form').serialize(),
			dataType:'json',
			error:function(jqXHR,textStatus,errorThrown){alert(jqXHR.responseText);},
			success:function(response){
				
				if(response.success==='true'){
					
					$('#comment_form .loader').hide();
					$('#comment_form .notice').css('background-color','green').css('color','#ffffff').html(response.msg).fadeIn('fast');
					ver_comentarios(host+'/Comentarios/ver/'+article);
					$('#comment_form').clearForm();
					//Recaptcha.destroy();
					//create_recaptcha();
				
				}else if(response.success==='false'){
					
					$('#comment_form .loader').hide();
					$('#comment_form .notice').css('background-color','red').css('color','#ffffff').html(response.msg).fadeIn('fast');
					//Recaptcha.reload();
				
				}else{
				
					alert(response);
				
				}
			 
			}
			
		});
		
	}
	
	var enviar_comentario_video = function(video){
				
		$('#comment_form .loader').show();
		
		$.ajax({
			type:"POST",
			url:host+'/Videos/enviar_comentario/'+video,
			data:$('#comment_form').serialize(),
			dataType:'json',
			error:function(jqXHR,textStatus,errorThrown){alert(jqXHR.responseText);},
			success:function(response){
				
				if(response.success==='true'){
					
					$('#comment_form .loader').hide();
					$('#comment_form .notice').css('background-color','green').css('color','#ffffff').html(response.msg).fadeIn('fast');
					ver_comentarios(host+'/Videos/comentarios/'+video);
					$('#comment_form').clearForm();
				
				}else if(response.success==='false'){
					
					$('#comment_form .loader').hide();
					$('#comment_form .notice').css('background-color','red').css('color','#ffffff').html(response.msg).fadeIn('fast');
				
				}else{
				
					alert(response);
				
				}
			 
			}
			
		});
		
	}

//===================================================//
//	end of VERIFY COMMENT
//==================================================//

//===================================================//
//	start of NEWSLETTER
//==================================================//

	var newsletter = function(action){
		
		var action_url = '';
		
		if(action=='remove'){action_url='/Newsletter/remove';}else{action_url='/Newsletter/add';}
		
		$('#newsletter .loader').show();
		
		$.ajax({
			type:"POST",
			//url:action_url,
			url:host+action_url,
			data:$('#newsletter_form').serialize(),
			dataType:'json',
			error:function(jqXHR,textStatus,errorThrown){alert(jqXHR.responseText);},
			success:function(response){
				
				if(response.success==='true'){
				
					$('#newsletter .loader').hide();
					$('#newsletter .notice').css('background-color','green').css('color','#ffffff').html(response.msg).fadeIn('fast');
					$('#newsletter_form').clearForm();
				
				}else if(response.success==='false'){
				
					$('#newsletter .loader').hide();
					$('#newsletter .notice').css('background-color','red').css('color','#ffffff').html(response.msg).fadeIn('fast');
				
				}else{
				
					alert(response);
				
				}
			 
			}
			
		});
		
	}

//===================================================//
//	end of NEWSLETTER
//==================================================//

//===================================================//
//	start of QUESTIONARIO
//==================================================//

	var votar = function(){
		
		$('#questionario .loader').show();
		
		$.ajax({
			type:"POST",
			//url:"votar.php",
			url:host+"/Questionario/votar",
			data:$('#questionario_form').serialize(),
			dataType:'json',
			error:function(jqXHR,textStatus,errorThrown){alert(jqXHR.responseText);},
			success:function(response){
				
				if(response.success==='true'){
				
					$('#questionario .loader').hide();
					$('#questionario .content').load(host+'/Questionario/resultados');
				
				}else if(response.success==='false'){
				
					$('#questionario .loader').hide();
					$('#questionario .notice').css('background-color','red').css('color','#ffffff').html(response.msg).fadeIn('fast');
				
				}else{
				
					alert(response);
				
				}
			 
			}
			
		});
		
	}

//===================================================//
//	end of QUESTIONARIO
//==================================================//

//===================================================//
//	start of CONTACTO
//==================================================//

	var send_email = function(){
		
		$('#mail_form .loader').show();
		
		$.ajax({
			type:"POST",
			//url:"send_email.php",
			url:host+"/Contactos/send_email",
			data:$('#mail_form').serialize(),
			dataType:'json',
			error:function(jqXHR,textStatus,errorThrown){alert(jqXHR.responseText);},
			success:function(response){
				
				if(response.success==='true'){
				
					$('#mail_form .loader').hide();
					$('#mail_form .notice').css('background-color','green').css('color','#ffffff').html(response.msg).fadeIn('fast');
					$('#mail_form').clearForm();
				
				}else if(response.success==='false'){
				
					$('#mail_form .loader').hide();
					$('#mail_form .notice').css('background-color','red').css('color','#ffffff').html(response.msg).fadeIn('fast');
				
				}else{
				
					alert(response);
				
				}
			 
			}
			
		});
		
	}

//===================================================//
//	end of CONTACTO
//==================================================//

//===================================================//
//	start of CREATE reCaptcha
//==================================================//

	var create_recaptcha = function(){
		
		Recaptcha.create("6LdVb8gSAAAAAKtPFqLgL-vid0ssYR3wDmFn2g3W","recaptcha",{theme:'white',lang:'pt'});
				
	}

//===================================================//
//	end of CREATE reCaptcha
//==================================================//

//===================================================//
//	start of LOAD COMMENTS PAGE
//==================================================//

	var ver_comentarios = function(page){
		
		$("#comments .load_container").load(page);
		
	}

//===================================================//
//	end of LOAD COMMENTS PAGE
//==================================================//


//===================================================//
//	start of CLEAR FORMS
//==================================================//

	$.fn.clearForm = function(){
		
		return this.each(function(){
			
			if(this.tagName.toLowerCase() == "form"){
				
				return $(":input",this).clearForm();
				
			}
			
			switch(this.type){
				
				case "password":
				case "textarea":
				case "text":
				this.value = "";
				break;
				
				case "select-multiple":
				case "select-one":
				this.selectedIndex = -1;
				break;
				
				case "radio":
				case "checkbox":
				this.checked = false;
				break;
			
			}
		
		});
		
	};

//===================================================//
//	end of CLEAR FORMS
//==================================================//

