$(document).ready(function(){

	// Añadir metodo reset para formularios
	jQuery.fn.reset = function () {
		$(this).each (function() { if (!$(this).is(':hidden')) this.reset(); });
	}

	// Obtener la url del servidor
	if($('.url_full').length > 0){ 
		var url_full = $('.url_full').attr('value'); 
	}

	redimencionar_iframe = function (){
		var height = $("iframe#iframeblog").contents().find("body").height();
		var h_img = $("iframe#iframeblog").contents().find(".img_blog").height();

		if(height > h_img){ 
			$("iframe#iframeblog").attr('height',height+40);
		}else{
			$("iframe#iframeblog").attr('height',h_img+40);	
		}
	};

	if($('iframe#iframeblog').length > 0){
		$('iframe#iframeblog').ready(function(){
			redimencionar_iframe();
		});
	}

	/*if($('iframe#iframeblog').length > 0){
		setTimeout(function(){ redimencionar_iframe(); }, 5000 );
		setTimeout(function(){ redimencionar_iframe(); }, 10000 );
		setTimeout(function(){ redimencionar_iframe(); }, 15000 );
	}

	// Cambio de programacion de radio
	if($('.nuevo_banner').length > 0){
		setInterval(function(){ 
			$.post(url_full+'ajax.php',{
					programacion: '1'
				},
				function (response) {
					var record = response.data;
					$('.p_titulo').html( record[0].titulo );
					$('.p_des').html( record[0].des );
					$('.p_inicio').html( record[0].termino );
					$('.p_termino').html( record[0].inicio );
				}, 'json'
			);
		}, 3000);
	}*/

	// Programas
	if($('.cnt_progs .cnt').length > 0){
   		$('.cnt_progs .cnt').cycle({ fx: 'fade' });
	}

	// Sistema de votacion
	$('#btnEnviarVoto').click(function(){

		if($('#txtNombre').val() == ''){ $('#msj_alerta').html('Ingrese un nombre'); return; }
		if($('#txtEmail').val() == ''){ 
			$('#msj_alerta').html('Ingrese un email'); return;
		}else{
			var valemail = validarEmail($('#txtEmail').val());
			if(valemail == false){
				$('#msj_alerta').html('El email es incorrecto'); return;
			}else{

				$.post(url_full+'ajax.php',{
						ver_ranking: '1',
						programa: $("input[@name='rdoItem']:checked").val(),
						ranking: $('#hid_ranking').val(),
						nombre: $('#txtNombre').val(),
						email: $('#txtEmail').val()
					},
					function (response) {
						if(response == 1){ $('#msj_alerta').html('El email no es valido.'); }
						if(response == 0){ 
							$('#txtNombre').val('');
							$('#txtEmail').val('');
							$('#msj_alerta').html('Voto registrado, gracias!'); 
							setTimeout(function(){ 
								$('#msj_alerta').html(''); 
							}, 5000 );
						}
					}, 'json'
				);

			}
		}

	});

	// Galeria de videos
	if($('.lista').length > 0){
		if($('.lista_videos').length > 0){
			$("ul.lista").ytplaylist({addThumbs:true, autoPlay: false, holderId: 'ytvideo2'});
		}
	}

	// Galeria de noticias inicio
	if($('.galeria_flash').length > 0){
		$('#featured > ul').tabs( {fx:{opacity: 'toggle'} } ).tabs('rotate', 8000, true);
	}

	// Galeria de fotos de inicio
	if($('.ad-gallery').length > 0){
		var galleries = $('.ad-gallery').adGallery();
		galleries[0].settings.effect = 'none';
		galleries[0].slideshow.toggle();
	}

	// Efecto modal fondo obscuro seccion programacion.
	if($('.background_opaco_programas').length > 0){
		he_bod = $('#cuerpo').css("height");
		if(he_bod != 'auto'){
			he_bod = he_bod.substring(0,4);
			he_bod = parseInt(he_bod) + 560;
			heg = he_bod.toString()+'px';
			$('.background_opaco_programas').css("height",heg);
		}else{
			$('.background_opaco_programas').css("height","0px");
		}

		$('.img_programacion .cerrar').click(function(){
			$('.background_opaco_programas').hide('normal');
			$('.img_programacion').hide('normal');
		});
	}

	// Efecto zoom seccion staff
	if( $('.itm_img_staf a').length > 0 ){
		$('.itm_img_staf a').lightBox();
	}

	// Efecto zoom seccion concursos
	if( $('.table_align_concurso_seccion a').length > 0 ){
		$('.table_align_concurso_seccion a').lightBox();
	}

	// FORMULARIO DE SUSCRIPCION

	// metodo agregado para validar fecha ... no me gusta ...
	$.validator.addMethod(
		"date",
		function (value, element) {
			return value.match(/^\d\d?\-\d\d?\-\d\d\d\d$/)
		},
		"Por favor ingrese una fecha con el formato dd-mm-yyyy"
	);

	// Mostrar suscripcion
	$('.inx_suscribete').click(function(){
		$('.background_opaco').show();
		$('.form_suscribir').show('normal');
	});

	// Ocultar suscripcion
	$('.cerrar_inscripcion').click(function(){
		$('.background_opaco').hide('normal');
		$('.form_suscribir').hide('normal');
	});

	// Validar suscripcion
	$('#frmSuscripcion').validate({
		rules:{
			nombre: 'required',
			telefono: 'required',
			email: { required: true, email: true },
			fecnac: { date: true },
			pais: 'required',
			ciudad: 'required',
			distrito: 'required'
		},
		messages:{
			nombre: 'Por favor ingrese su nombre.',
			email: 'Por favor ingrese un correo electronico valido.',
			telefono: 'Por favor ingrese su tel&eacute;fono.',
			pais: 'Por favor ingrese el pais.',
			ciudad: 'Por favor ingrese una ciudad.',
			distrito: 'Por favor ingrese un distrito.'
		},
		submitHandler: function(form) {
			$.post(url_full+'ajax.php',
				{
					inscripcion: 1,
					nombre: $('#nombre').attr('value'),
					dni: $('#dni').attr('value'),
					telefono: $('#telefono').attr('value'),
					email: $('#email').attr('value'),
					sexo: $("input[@name='rdoSexo']:checked").val(),
					fecnac: $('#fecnac').attr('value'),
					pais: $('#pais').attr('value'),
					ciudad: $('#ciudad').attr('value'),
					distrito: $('#distrito').attr('value')
				},
				function (response) {
				}, 'json'
			);
			$('#frmSuscripcion').reset();
			$('.form_suscribir').hide('normal');
			$('.frmSuscripcion_ok').attr('style','display:block');
			setTimeout(function(){ 
				$('.background_opaco').hide('normal');
				$('.frmSuscripcion_ok').hide('normal');
			}, 2000 );
		}
	});

	//Validar quienes somos
	$('#frm_quienessomos').validate({
		rules:{
			nombre: 'required',
			email: { required: true, email: true },
			comentario: 'required'
		},
		messages:{
			nombre: 'Por favor ingrese su nombre.',
			email: 'Por favor ingrese un correo electronico valido.',
			comentario: 'Por favor ingrese un comentario.'
		},
		submitHandler: function(form) {
			$.post('../ajax.php',
				{
					quienes_somos: 1,
					nombre: $('#nombre').attr('value'),
					email: $('#email').attr('value'),
					slArea: $('#slArea').attr('value'),
					comentario: $('#comentario').attr('value')
				},
				function (response) {
				}, 'json'
			);
			$('#frm_quienessomos').reset();
			$('#exito').attr('style','display:block');
			setTimeout(function(){ $('#exito').fadeOut() }, 4000 );
		}
	});

	// FORMULARIO COMENTARIOS NOTICIAS

	// Validar comentario y agregarlo o no
	$('#frm_comentar_noticia').validate({
		rules:{
			nombre: 'required',
			email: { required: true, email: true },
			comentario: 'required'
		},
		messages:{
			nombre: 'Por favor ingrese su nombre.',
			email: 'Por favor ingrese un correo electronico valido.',
			comentario: 'Por favor ingrese un comentario.'
		},
		submitHandler: function(form) {
			$.post('../../ajax.php', 
				{
					insertar_comentario_noticia: 1,
					id_noticia: $('#hid_id_not').attr('value'),
					nombre: $('#nombre').attr('value'),
					email: $('#email').attr('value'),
					comentario: $('#comentario').val()
				},
				function (response) {
					var record = response.data;

					var aryDiv = $('.item_articulo').length;
					$('.cont_int_evento2').append('<div class="item_articulo"><div class="nombre"><b>'+record[0].nombre+'</b> | '+record[0].fecha+'</div><div class="comentario">'+record[0].comentario+'</div><div class="numero_comentario">'+(aryDiv+1)+'</div></div>').slideDown();
					$('.numero_comentarios').html(aryDiv+1);
				}, 'json'
			);
			$('#frm_comentar_noticia').reset();
			$('#exito').attr('style','display:block');
			setTimeout(function(){ $('#exito').fadeOut() }, 2000 );
		}
	});

	// Validar compartir y enviar o no
	$('#frm_compartir_noticia').validate({
		rules:{
			txtComDe: 'required',
			txtComPara: 'required',
			txtComEml: { required: true, email: true },
			txtComCom: 'required'
		},
		messages:{
			txtComDe: 'Por favor ingrese su nombre.',
			txtComPara: 'Por favor ingrese el nombre de la persona a enviar.',
			txtComEml: 'Por favor ingrese un correo electronico valido.',
			txtComCom: 'Por favor ingrese un comentario.'
		},
		submitHandler: function(form) {
			$.post('../../ajax.php', 
				{
					compartir_noticia: 1,
					id_noticia: $('#hid_id_not').attr('value'),
					txtComDe: $('#txtComDe').attr('value'),
					txtComPara: $('#txtComPara').attr('value'),
					txtComEml: $('#txtComEml').attr('value'),
					txtComCom: $('#txtComCom').val()
				},
				function (response) {
				}, 'json'
			);
			$('#frm_compartir_noticia').reset();
			$('#exito_comnot').attr('style','display:block');
			setTimeout(function(){ $('#exito_comnot').fadeOut() }, 2000 );
		}
	});

	// FORMULARIO COMENTARIOS EVENTOS

	// Validar comentario y enviar o no
	$('#frm_comentar_evento').validate({
		rules:{
			nombre: 'required',
			email: { required: true, email: true },
			comentario: 'required'
		},
		messages:{
			nombre: 'Por favor ingrese su nombre.',
			email: 'Por favor ingrese un correo electronico valido.',
			comentario: 'Por favor ingrese un comentario.'
		},
		submitHandler: function(form) {
			$.post('../../ajax.php', 
				{
					insertar_comentario_evento: 1,
					id_noticia: $('#hid_id_not').attr('value'),
					nombre: $('#nombre').attr('value'),
					email: $('#email').attr('value'),
					comentario: $('#comentario').val()
				},
				function (response) {
					var record = response.data;
					var aryDiv = $('.item_articulo').length;
					$('.cont_int_evento2').append('<div class="item_articulo"><div class="nombre"><b>'+record[0].nombre+'</b> | '+record[0].fecha+'</div><div class="comentario">'+record[0].comentario+'</div><div class="numero_comentario">'+(aryDiv+1)+'</div></div>').slideDown();
					$('.numero_comentarios').html(aryDiv+1);
				}, 'json'
			);
			$('#frm_comentar_evento').reset();
			$('#exito').attr('style','display:block');
			setTimeout(function(){ $('#exito').fadeOut() }, 2000 );
		}
	});

	// Validar compartir y enviar o no
	$('#frm_compartir_eventos').validate({
		rules:{
			txtComDe: 'required',
			txtComPara: 'required',
			txtComEml: { required: true, email: true },
			txtComCom: 'required'
		},
		messages:{
			txtComDe: 'Por favor ingrese su nombre.',
			txtComPara: 'Por favor ingrese el nombre de la persona a enviar.',
			txtComEml: 'Por favor ingrese un correo electronico valido.',
			txtComCom: 'Por favor ingrese un comentario.'
		},
		submitHandler: function(form) {
			$.post('../../ajax.php', 
				{
					compartir_evento: 1,
					id_evento: $('#hid_id_not').attr('value'),
					txtComDe: $('#txtComDe').attr('value'),
					txtComPara: $('#txtComPara').attr('value'),
					txtComEml: $('#txtComEml').attr('value'),
					txtComCom: $('#txtComCom').val()
				},
				function (response) {
				}, 'json'
			);
			$('#frm_compartir_eventos').reset();
			$('#exito_comnot').attr('style','display:block');
			setTimeout(function(){ $('#exito_comnot').fadeOut() }, 2000 );
		}
	});

	// FORMULARIO COMENTARIOS GALERIAS

	// Validar compartir y enviar o no
	$('#frm_compartir_galeria').validate({
		rules:{
			txtde: 'required',
			txtpara: 'required',
			txtemail: { required: true, email: true }
		},
		messages:{
			txtde: 'Por favor ingrese su nombre.',
			txtpara: 'Por favor ingrese el nombre de la persona a enviar.',
			txtemail: 'Por favor ingrese un correo electronico valido.'
		},
		submitHandler: function(form) {
			$.post($('.oculto_idgal').attr('title')+'ajax.php', 
				{
					compartir_galeria: 1,
					id_galeria: $('#idgal').attr('value'),
					txtde: $('#txtde').attr('value'),
					txtpara: $('#txtpara').attr('value'),
					txtemail: $('#txtemail').attr('value')
				},
				function (response) {
				}, 'json'
			);
			$('#frm_compartir_galeria').reset();
			$('#exito_comnot').attr('style','display:block');
			setTimeout(function(){ $('#exito_comnot').fadeOut() }, 2000 );
		}
	});

	// Validar comentar y enviar o no
	$('#frm_comentar_galeria').validate({
		rules:{
			nombre: 'required',
			email: { required: true, email: true },
			comentario: 'required'
		},
		messages:{
			nombre: 'Por favor ingrese su nombre.',
			email: 'Por favor ingrese un correo electronico valido.',
			comentario: 'Por favor ingrese un comentario.'
		},
		submitHandler: function(form) {
			$.post($('.oculto_idgal').attr('title')+'ajax.php', 
				{
					insertar_comentario_galeria: 1,
					id_galeria: $('#idgal').attr('value'),
					nombre: $('#nombre').attr('value'),
					email: $('#email').attr('value'),
					comentario: $('#comentario').val()
				},
				function (response) {
					var record = response.data;
					var aryDiv = $('.item_com_even').length;
					if(aryDiv%2 == 0){ sty = 1; }else{ sty = 2; }
					$('.cont_int_evento2').append('<div class="item_com_even cnt_det_even_comentario'+sty+'"><div class="num_fec_coment"><b id="b'+sty+'">'+(aryDiv+1)+'</b><p>'+record[0].fecha+'</p></div><div class="tit_coment">'+record[0].nombre+'</div><div class="com_coment">'+record[0].comentario+'</div></div>').slideDown();
					$('.cnt_txt_fotos .num').html(aryDiv+1);
;
				}, 'json'
			);
			$('#frm_comentar_galeria').reset();
			$('#exito').attr('style','display:block');
			setTimeout(function(){ $('#exito').fadeOut() }, 2000 );
		}
	});


	// FORMULARIO COMENTARIOS ARTICULOS

	// Validar comentario y agregarlo o no
	$('#frm_comentar_articulo').validate({
		rules:{
			nombre: 'required',
			email: { required: true, email: true },
			comentario: 'required'
		},
		messages:{
			nombre: 'Por favor ingrese su nombre.',
			email: 'Por favor ingrese un correo electronico valido.',
			comentario: 'Por favor ingrese un comentario.'
		},
		submitHandler: function(form) {
			$.post('../../../ajax.php', 
				{
					insertar_comentario_articulo: 1,
					id_articulo: $('#hid_id_art').attr('value'),
					nombre: $('#nombre').attr('value'),
					email: $('#email').attr('value'),
					comentario: $('#comentario').val()
				},
				function (response) {
					var record = response.data;
					var aryDiv = $('.item_articulo').length;
					$('.cont_articulos_comentarios').append('<div class="item_articulo"><div class="nombre"><b>'+record[0].nombre+'</b> | '+record[0].fecha+'</div><div class="comentario">'+record[0].comentario+'</div><div class="numero_comentario">'+(aryDiv+1)+'</div></div>').slideDown();
					//$('.cont_minitext_articulo .num_comentarios').html(aryDiv);
				}, 'json'
			);
			$('#frm_comentar_articulo').reset();
			$('#exito').attr('style','display:block');
			setTimeout(function(){ $('#exito').fadeOut() }, 2000 );
		}
	});

	// Validar compartir y enviar o no
	$('#frm_comentar_articulo').validate({
		rules:{
			txtComDe: 'required',
			txtComPara: 'required',
			txtComEml: { required: true, email: true },
			txtComCom: 'required'
		},
		messages:{
			txtComDe: 'Por favor ingrese su nombre.',
			txtComPara: 'Por favor ingrese el nombre de la persona a enviar.',
			txtComEml: 'Por favor ingrese un correo electronico valido.',
			txtComCom: 'Por favor ingrese un comentario.'
		},
		submitHandler: function(form) {
			$.post('../../ajax.php', 
				{
					compartir_noticia: 1,
					id_noticia: $('#hid_id_not').attr('value'),
					txtComDe: $('#txtComDe').attr('value'),
					txtComPara: $('#txtComPara').attr('value'),
					txtComEml: $('#txtComEml').attr('value'),
					txtComCom: $('#txtComCom').val()
				},
				function (response) {
				}, 'json'
			);
			$('#frm_comentar_articulo').reset();
			$('#exito_comnot').attr('style','display:block');
			setTimeout(function(){ $('#exito_comnot').fadeOut() }, 2000 );
		}
	});



	//Efectos mostrar u ocultar formulario de compartir (Noticias, Eventos y fotos)
	$('.correo_compartir').click(function(){
		$('#form_compartir_evento').slideToggle('normal');
	});

	$('.correo_compartir_galeria').click(function(){
		$('.cont_form_fotos').slideToggle('normal');
	});

	/*	PAGINACION CON JQUERY	*/
	
	/*	Paginacion de eventos y noticias	*/
	if($('.cont_eventos').length > 0){
		$("#paginacion_eventos").paginate({
			count 					: $("#numpage").attr('value'),
			start 					: 1,
			display     			: 6,
			border					: true,
			border_color			: '#fff',
			text_color  			: '#fff',
			background_color    	: 'black',	
			border_hover_color		: '#ccc',
			text_hover_color  		: '#000',
			background_hover_color	: '#fff', 
			images					: false,
			mouse					: 'press',
			onChange     			: function(page){
										$('._current','#paginationdemo').removeClass('_current').hide();
										$('#p'+page).addClass('_current').show();
									  }
		});
	}

	/*	Paginacion de concursos	*/
	if($('.cont_concursos').length > 0){
		$("#paginacion_concursos").paginate({
			count 					: $("#numpage").attr('value'),
			start 					: 1,
			display     			: 3,
			border					: true,
			border_color			: '#fff',
			text_color  			: '#fff',
			background_color    	: 'black',	
			border_hover_color		: '#ccc',
			text_hover_color  		: '#000',
			background_hover_color	: '#fff', 
			images					: false,
			mouse					: 'press',
			onChange     			: function(page){
										$('._current','#paginationdemo').removeClass('_current').hide();
										$('#p'+page).addClass('_current').show();
									  }
		});
	}


	// Validar concurso
	$('#frmConcurso1').validate({
		errorElement: 'div',
		errorClass: 'dv_error',
		rules:{
			txtNombre: 'required',
			txtEdad: { required: true, number: 20 },
			txtMail: { required: true, email: true },
			txtDireccion: 'required',
			areaDebes: 'required',
		},
		messages:{
			txtNombre: 'Ingresa tu nombre.',
			txtEdad: { required: 'Ingresa tu edad', number: 'solo numeros' },
			txtMail: { required: 'Ingrese email' , email: 'Email invalido' },
			txtDireccion: 'Ingresa tu dirección.',
			areaDebes: 'Ingresa tu mensaje',
		}
	});

	$('#frmConcurso2').validate({
		errorElement: 'div',
		errorClass: 'dv_error',
		rules:{
			txtNombre: 'required',
			txtEdad: { required: true, number: 20 },
			txtMail: { required: true, email: true },
			txtDireccion: 'required'
		},
		messages:{
			txtNombre: 'Ingresa tu nombre.',
			txtEdad: { required: 'Ingresa tu edad', number: 'solo numeros' },
			txtMail: { required: 'Ingrese email' , email: 'Email invalido' },
			txtDireccion: 'Ingresa tu dirección.'
		}
	});

});

function radio_insomnio(ruta){
	var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=255, height=340, top=85, left=140";
	window.open(ruta,"",opciones);
}

function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return (true)
	} else {
		return (false);
	}
}
