/********************************************
 - Validations & document ready functions
********************************************/
$(document).ready(function() {
						   
	/********************************************
	 - START - Telefono Registro
	********************************************/
						   
	$("#tel_casa").blur(function(){
	
		if($(this).attr("value") != "")
		{
			$("#phone_type").show();
		}
	
	});

						   
	/********************************************
	 - START - Preguntas Registro
	********************************************/					   
						   
	$("#q1 input[name='habitos']").click(function(){

		if($(this).attr("id") != "q1_1")
		{
			$("#q2").show();

		}
		else
		{
			$("#q2").hide();
		}
	
	});
						   
	/********************************************
	 - START - Browser detection
	********************************************/
	   if($.browser.msie)
	   {
		   if(Number($.browser.version) < 7)
		   {
		  	$(".header").append('<div id="IE6-alert"><p class="title">Atención</p><p>Para disfrutar al máximo el sitio de WHISKAS, te recomendamos actualizar tu navegador. Sólo da clic en el navegador de tu preferencia:</p><ul><a href="http://www.microsoft.com/latam/windows/internet-explorer/" target="_blank"><li><img src="images/browser_02.gif" width="30" height="36" />Internet Explorer</li></a><a href="http://www.mozilla-europe.org/es/firefox/" target="_blank"><li><img src="images/browser_01.gif" width="34" height="36" />Firefox</li></a><a href="http://www.google.com/chrome?hl=es-419" target="_blank"><li><img src="images/browser_03.gif" width="30" height="36" />Google Chrome</li></a><a href="http://www.apple.com/mx/safari/" target="_blank"><li style="padding-right:0px;"><img src="images/browser_04.gif" width="30" height="36" />Safari</li></a></ul></div>');
			$("#IE6-alert").slideDown("slow");
		   }
	   }

	/********************************************
	 - END - Browser detection
	********************************************/



	/********************************************
	 - Carrusel
	********************************************/
	$("#carrusel-thumbs").scrollable({size:4});
						   
						   
	/********************************************
	 - Spots de Tv
	********************************************/
	$('.spots-list img').click(function(){
		
		var videoTitle = $(this).attr("title");
		var videoURL = $(this).attr("rel");
		var videoHolder = $("#spot-holder");
		var videoTitleHolder = $("h3.spot-title");
		
		$.post("backends/videoPlayerAjax.php",{videoTitle:videoTitle, videoURL:videoURL}, function($data){
		
			videoHolder.html($data);
			videoTitleHolder.html(videoTitle);
			$("#spot_name").val(videoTitle)
		
		});

	});
						   
						   

	/********************************************
	 - Ocultar las imagenes en testimoniales
	********************************************/
	$("#detalleTestimonial_pictures a").css('display','none');


	/**
	 * Inician funciones para las validaciones de formularios
	 */
	$("#register_form").validationEngine({
		success :  false,
		failure : function() {}
	});
	$("#update_register_form").validationEngine({
		success :  false,
		failure : function() {}
	});
	$("#add_member_cats").validationEngine({
		success :  false,
		failure : function() {}
	});	
	$("#comment_testimonial_form").validationEngine({
		success :  false,
		failure : function() {}
	});
	$("#recovery_pass_form").validationEngine({
		success :  false,
		failure : function() {}
	});
	$("#login_form").validationEngine({
		success :  false,
		failure : function() {}
	});
	$("#logout_form").validationEngine({
		success :  false,
		failure : function() {}
	});
	
	$("#momentos_form").validationEngine({
		success :  false,
		failure : function() {}
	});
	
	$("#insertar_imagen").validationEngine({
		success :  false,
		failure : function() {}
	});
	$("#update_member_cats").validationEngine({
		success :  false,
		failure : function() {}
	});
	$("#spots_comment").validationEngine({
		success :  false,
		failure : function() {}
	});
	
	$("#momentos_form").submit(onMomentosSubmit);
		
	/**
	 * Inicia el Rating
	 */
	
	$('.video-star').rating({ 
		starWidth: 8,
		required: true,
		callback: function(value, link){ 
			setRating(value); 
		} 
	});
	
	/**
	 * Inicia el Rating Productos
	 */
	
	$('.video-star-products').rating({ 
		starWidth: 8,
		required: true,
		callback: function(value, link){ 
			setRatingProducts(value); 
		} 
	});
	
	/**
	 * Inician funciones para formulario de: Subir imagenes para el calendario
	 */
	$("#upload_image").css("display","none");
	$(".btn_add_photo").click(function(){
		
		$("#upload_image").slideDown("fast");
		$("#monthHiden").val($(this).attr("alt"));
		//alert($("#monthHiden").attr("value"));
		calendarCheck();
		
	});
	
	$("#upload_image").submit(function(){
		$("#ajaxImageLoader").show();
		$("#submit_image").parent().hide();
		$("#submit_image").parent().parent().next().hide();
		calendarCheck();
	});
	
	$("#btn_calendar_download").click(function(){
	
		$("#calendar_download").show();
	
	});
	
});

/********************************************
 - displayError
********************************************/
function displayError(msg){

	$("#display_errors").show();
	$("#display_errors").html(msg);

}

/********************************************
 - setRating
********************************************/
function setRating(value){

	var testimonialArray = $("div.rating").attr("id").split("_");
	alert("Gracias por tu voto!");
	$.post("backends/setTestimonialRating.php",{testimonialID:testimonialArray[1], ratingValue:value});

}

/********************************************
 - setRatingProducts
********************************************/
function setRatingProducts(value){
	
	var testimonialArray = $("div.recomend_rating").attr("id").split("_");
	//$.post("backends/setProductRating.php",{productID:testimonialArray[1], ratingValue:value});
	window.location = '../backends/setProductRating.php?productID='+testimonialArray[1]+'&ratingValue='+value;

}

/********************************************
 - hideVideoForm
********************************************/
function hideVideoForm(){
	
	$("#momentos_form").hide();
	$("#video_msg").show();
	
}

/********************************************
 - onMomentosSubmit
********************************************/
function onMomentosSubmit(){
	
	if($("#video_file").attr("value") != ""){
		$("#momentos_form").hide();
		$("#video_msg").show();
	}
	
}

/********************************************
 - calendarPhotoChange
********************************************/
function calendarPhotoChange(photoURL,monthID){
	
	$("#photo_calendar_"+monthID).attr("src",photoURL);
	$("#monthHiden").val("");
	$("#imgURL").val("");
	$("#upload_image").hide();
	$("#submit_image").parent().show();
	$("#ajaxImageLoader").hide();
	calendarCheck();
	
}

/********************************************
 - calendarCheck
********************************************/
function calendarCheck(){

	var imageCheck = $("base").attr("href")+"images/trans_5x5.gif";
	var flag = 0;

	$("ul.calendar li").each(function(i){
		
		if($(this).children().attr("src") == imageCheck){
			
			flag = 1;
		
		}
		
	});
	
	if(flag == 0){
		window.location = $("base").attr("href")+"descargas/";
	}

}
/********************************************
 - Numero de gatos
********************************************/
function showCatsFields(categoryID){
	$("#tr_cats").show();	
	
	if(categoryID == "0")
	{
		//$("#catRows").html('<div class="adopcion">&iquest;Te gustar&iacute;a adoptar un gatito? <a href="about:blank" target="_blank">haz click aqu&iacute;</a></div>');
		$("#catRows").html('');
	}
	else
	{	
		$.post("includes/cats_number.php",{categoryID:categoryID}, function($data){
			$("#catRows").html($data);
		});
	}

}

/********************************************
 - Mostrar pregunta 2
********************************************/
$('#pregunta2').hide();

$('.chooser').change(function() {
    if($(this).val() != '1') {
        $('#pregunta2').show();
    } 
    else {
        $('#pregunta2').hide();
    }       
});


/********************************************
 - Numero de gatos update
********************************************/
function showCatsFieldsUpdate(categoryID){
	$("#tr_cats").show();	
	
	if(categoryID == "0")
	{
		/*$("#catRows").html('<div class="adopcion">&iquest;Tee gustar&iacute;a adoptar un gatito? <a href="about:blank" target="_blank">haz click aqu&iacute;</a></div>');*/
	}
	else
	{	
		$.post("includes/cats_number_member.php",{categoryID:categoryID}, function($data){
			$("#catRows").html($data);
		});
	}

}
/**********************************************************************************
 - Get Asetamiento -
**********************************************************************************/
function getAsentamiento(cpNumber, className){
	var cpScope = $('#tr_asentamiento');
	var dataScope = $('#asentaCombo');

	if(cpNumber.length == 5){
	
		cpScope.show();
		dataScope.text("Cargando...");
		$.post("backends/getAsentamiento.php",{'cp':cpNumber,'class':className},function($data){
			dataScope.html($data);
		});
				
	}
	
}
/*********************************************************************************/
/********************************************
 - Numero de fotos en un testimonio
********************************************/
function showMomentPicturesFields(categoryID){

	$("#tr_cats").show();
	
	$.post("includes/testimonial_pictures_number.php",{categoryID:categoryID}, function($data){
		$("#catRows").html($data);
	});
	

}

function blockButton(buttonID){

	$("#"+buttonID).attr("disabled","disabled");

}

/********************************************
 - funcion para refrescar el captcha
********************************************/
function refreshCaptcha(){

	$("#captcha_image").attr("src", 'classes/securimage/securimage_show.php?sid=' + Math.random());

}

/********************************************
 - funcion para quitar el botón de enviar del formulario
********************************************/
function showProccess(){

	$("#send_tr").children().html("Procesando registro, por favor espera...");

}

function showSendButton(){

	$("#send_tr").children().html('<input type="image" src="images/trans_5x5.gif" class="btn_send" />');

}

/**
 * Función para agregar un nuevo asentamiento
 */
function checkNewAsenta(comboID){
	
	if(comboID == "new"){
	   
		$("#new_asenta").show();
		$("#new_asenta").focus();
		$("#nuevo_asenta").addClass("validate[required]")

	}else{
		
		$("#new_asenta").hide();
		$("#nuevo_asenta").removeClass("validate[required]")
		
	}
	
}