/*****************************VER NOTICIAS*****************************/
function ver_noticia(id){
	History.pushState({noticia:1}, "VM Latino | Noticias", "?noticia="+id);
	inter_pagina(3)
	$.post('gestion_cp.php', {datos:'noticia', id_noticia:id}, function(datos){
		$('#lista_noticias_detalles').html(datos)
	})
}
function ver_reglamento(id){
	History.pushState({noticia:1}, "VM Latino", "?");
	inter_pagina(11)
	$.post('gestion_cp.php', {datos:'reglamento', id_reglamento:id}, function(datos){
		$('#lista_reglamento_detalles').html(datos)
	})
}
/*****************************PAGINACION NOTICIAS Y EVENTOS*****************************/
function inter_pagina(opcion){
	switch(opcion){
		
		case 1://MOSTRAR - LISTA DE NOTICIAS
			$('#columna_izq .inicio').animate({marginLeft:-535}, 350)
			$('#lista_noticias').show()
			History.pushState({noticia:1}, "VM Latino", "?noticia=lista_de_noticias");
		break;
		
		case 2://OCULTAR - LISTA DE NOTICIAS
			$('#lista_noticias').hide()
			$('#columna_izq .inicio').animate({marginLeft:0}, 350)
			History.pushState({noticia:1}, "VM Latino", "?");
		break;
		
		case 3://MOSTRAR - NOTICIAS DETALLES
			$('#columna_izq .inicio').animate({marginLeft:-535}, 350)
			var img_cargando = $('#pop_eventos_cargando_cont').html()
			$('#lista_noticias_detalles').html(img_cargando)
			$('#lista_noticias_detalles').show()
			$('#lista_noticias').hide()
		break;
		
		case 4://OCULTAR - NOTICIAS DETALLES
			History.pushState({noticia:1}, "VM Latino | Noticias", "?noticia=lista_de_noticias");
			$('#lista_noticias_detalles').hide()
			$('#lista_noticias').show()
		break;
		
		case 5://MOSTRAR - LISTA EVENTOS
			$('#columna_izq .inicio').animate({	marginLeft:-535}, 350)
			$('#eventos').show()
		break;
		
		case 6://OCULTAR - LISTA EVENTOS
			$('#columna_izq .inicio').animate({marginLeft:0}, 350)
			$('#eventos').hide()
		break;
		case 7: //MOSTRAR - MAS FOTOS
			$('#columna_izq .inicio').animate({marginLeft:-535}, 350)
			$('#lista_noticias, #lista_noticias_detalles, #eventos').hide()
			$('#mas_fotos').show()
			History.pushState({noticia:1}, "VM Latino", "?");
		break;
		case 8://OCULTAR - MAS FOTOS
			$('#columna_izq .inicio').animate({marginLeft:0}, 350)
			$('#mas_fotos').hide()
			History.pushState({noticia:1}, "VM Latino", "?");
		break;
		case 9: //MOSTRAR - MAS FOTOS
			$('#columna_izq .inicio').animate({marginLeft:-535}, 350)
			$('#lista_noticias, #lista_noticias_detalles, #eventos, #mas_fotos #encuestas_listas_cont').hide()
			$('#lista_reglamento').show()
			History.pushState({noticia:1}, "VM Latino", "?");
		break;
		case 10://OCULTAR - MAS FOTOS
			$('#columna_izq .inicio').animate({marginLeft:0}, 350)
			$('#lista_reglamento').hide()
			History.pushState({noticia:1}, "VM Latino", "?");
		break;
		case 11://MOSTRAR - DETALLES REGLAMENTOS
			History.pushState({noticia:1}, "VM Latino", "?");
			$('#lista_reglamento').hide()
			$('#lista_reglamento_detalles').show()
		break;
		case 12://OCULTAR - NOTICIAS DETALLES
			History.pushState({noticia:1}, "VM Latino", "?");
			$('#lista_reglamento_detalles').hide()
			$('#lista_reglamento').show()
		break;
		case 13://MOSTRAR - ENCUESTA LISTA
			History.pushState({noticia:1}, "VM Latino", "?");
			$('#columna_izq .inicio').animate({marginLeft:-535}, 350)
			$('#lista_noticias, #lista_noticias_detalles, #eventos, #mas_fotos').hide()
			$('#encuestas_listas_cont').show()
		break;
		case 14://OCULTAR - ENCUESTA LISTA
			History.pushState({noticia:1}, "VM Latino", "?");
			$('#columna_izq .inicio').animate({marginLeft:0}, 350)
			$('#encuestas_listas_cont').hide()
		break;
	}
}
/*****************************EVENTO*****************************/
function rand (n){
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
function evento_lista_detalles(id){
		$('#evento_'+id).find('.detalle').slideToggle(500)	
}
/*POP DETALLE EVENTO*/
function evento_detalles_pop(id_evento){
	var titulo = 'Detalle'
	var img_cargando = $('#pop_eventos_cargando_cont').html()
	var cerrar = '<div class="cerrar_b" onclick="Boxy.get(this).hide();"></div>'
	new Boxy('<div id="d_eventos_pop">'+img_cargando+'</div>', {title:titulo, closeText:cerrar, fixed:false, unloadOnHide:true})
	$.post('gestion_cp.php',{datos:'detalles_evento', id_evento:id_evento}, function(datos){
		$('#d_eventos_pop').html(datos)
	})
}
/*POP NUEVO EVENTO*/
function pop_evento(){
	var aleatorio = rand(100);
	var img_cargando = $('#pop_eventos_cargando_cont').html()
	var etiketa_padre = 'papa_pop_eventos'+aleatorio
	var btn_cerrar = '<div class="cerrar_b" onclick="Boxy.get(this).hide();"></div>';
	var boxyevento = new Boxy('<div id="'+etiketa_padre+'">'+img_cargando+'</div>',{title:'Nuevo evento', closeText:btn_cerrar, fixed:false, unloadOnHide:true}, function(){})
	
	$.post('gestion_cp.php',{datos:'form_eventos'}, function(datos){
		var long = datos.length
	if(long > 200){
		boxyevento.tween(300, 440)
	}
	$('#papa_pop_eventos'+aleatorio).html(datos)
	$('#papa_pop_eventos'+aleatorio).find('#evento_fecha').attr('id','evento_fecha'+aleatorio)
	
	$('#'+etiketa_padre+' #evento_fecha'+aleatorio).datepicker({inline: true, monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'], dayNamesMin:['Do','Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'], dateFormat: 'dd-mm-yy', showButtonPanel: true, currentText:'Hoy', closeText:'ok'});
	$('#'+etiketa_padre+' #dialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);

	})
/*CREAR UN EVENTO*/	
	this.crear_evento = function(){
		var nombre	= $('#'+etiketa_padre+' #evento_nombre').val()
		var fecha	= $('#'+etiketa_padre+' #evento_fecha'+aleatorio).val()
		var hora = $('#'+etiketa_padre+' #evento_hora').val()
		var lugar	= $('#'+etiketa_padre+' #evento_lugar').val()
		var restriccion	= $('#'+etiketa_padre+' #evento_restriccion').val()
		var detalles = $('#'+etiketa_padre+' #evento_detalles').val()
		$.post('gestion_cp.php', {crear:'evento', nombre:nombre, fecha:fecha, hora:hora, lugar:lugar,restriccion:restriccion, detalles:detalles}, function(){
		})
		$('.close').click()
	}
}
/*ELIMINAR EVENTO*/
function del_evento(id){
	Boxy.confirm("Estas seguro que quieres eliminar este evento?", function() {
	 $.post('gestion_cp.php', {eliminar:'evento', id_evento:id}, function(){
		 $('#evento_'+id).fadeOut(500, function(){
		 $('#evento_'+id).remove()})
	})
	}, {title: 'Eliminar Evento'});
}
/*****************************VER PROGRAMACION COMPLETA*****************************/
function programacion_completa(){
	var titulo = 'Programacion completa'
	var img_cargando = $('#pop_eventos_cargando_cont').html()
	var cerrar = '<div class="cerrar_b" onclick="Boxy.get(this).hide();"></div>'
	var boxy_programacion = new Boxy('<div id="programacion_completa_pop">'+img_cargando+'</div>', {title:titulo, closeText:cerrar, fixed:false, unloadOnHide:true})
	boxy_programacion.tween(820, 1470)
	$.post('gestion_cp.php',{datos:'programacion'}, function(datos){
		$('#programacion_completa_pop').html(datos)

	})
}
function eliminar_noticia(id){
	Boxy.confirm("Estas seguro que quieres eliminar esta noticia?", function() {
	 $.post('gestion_cp.php', {eliminar:'noticia', idn:id}, function(){
		 $('#lista_noticia_'+id).fadeOut(500, function(){
		 $('#lista_noticia_'+id).remove()})
	})
	}, {title: 'Eliminar noticia'});
}
/*************************ENCUENTAS**********************************************/
function nueva_encuesta(){
	var titulo = 'Nueva encuesta'
	var img_cargando = $('#pop_eventos_cargando_cont').html()
	var cerrar = '<div class="cerrar_b" onclick="Boxy.get(this).hide();"></div>'
	new Boxy('<div id="encuesta_cont">'+img_cargando+'</div>', {title:titulo, closeText:cerrar, fixed:false, unloadOnHide:true})
	$.post('gestion_cp.php',{datos:'encuesta'},function(datos){
		$('#encuesta_cont').html(datos)
	})
}
//ADD AUTO OPCIONES ENCUESTA
$(function(){
	$('.encuesta_cont input[name="opciones"]:last').live('focus', function(event){
		var etiqueta = '<input name="opciones" type="text" class="form_txt add" value="Agregar opcion" />';
		$('.encuesta_cont input').clearField();
		$('.encuesta_cont .divisor').append(etiqueta)
	})	
})
//SUBIR FOTO ENCUESTA
function subir_foto_encuesta(){
	var uploader = new qq.FileUploader({
		element: document.getElementById('subir_img'),
		action: 'subir/img_encuesta.php',
		//debug: true,
		allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
		onSubmit: function(id, fileName){
			$('.qq-upload-list').remove()
			$('#barra_cargando1').show()
			$('#subir_foto_portada .qq-upload-list').remove()
			$('#barra_cargando1 .barra').css('width','1%').css('background-color', '#57b0e6')
			
		},
		template: '<div class="qq-uploader"><div class="qq-upload-button"><span>Subir una imagen</span></div><ul class="qq-upload-list"></ul></div>',
		onProgress: function(id, fileName, loaded, total){
			var progreso = (loaded/total)*100; progreso=parseInt(progreso); 
			$('#barra_cargando1 .barra').css('width',progreso+'%').css('background-color', '#57b0e6')
		},
		onComplete: function(id, fileName, responseJSON){
				$('.encuesta_cont #img_encuesta').attr('value', responseJSON.img)
				$('#barra_cargando1').hide()
				$('#barra_cargando1 .barra').css('width','0%');
		},
		onCancel: function(id, fileName){
			//$('.qq-upload-success').hide()
			$('.barra_progreso_cont .progreso').css('width','0%')
		},
		messages: {
			typeError: "Solo imagenes",
			sizeError: "Esta foto pesa mucho",
			emptyError: "No has seleccionado una imgen",
			onLeave: "Se ha comenzado a subir la foto, si sales, se cancelará"            
	
		},
		
		showMessage: function(message){alert(message);}
	
	});
}
//VOTAR EN LA ENCUESTA
function votar(id_encuesta){
		var opcion_id = $('#encuesta_opciones_'+id_encuesta+' input[name=opcion]:checked').val()
	$.post('gestion_cp.php',{crear:'voto', opcion_id:opcion_id, id_encuesta:id_encuesta}, function(){
		votar_resultados(id_encuesta)
	})
	
}
function votar_resultados(id_encuesta){
	$.post('gestion_cp.php', {datos:'voto', id_encuesta:id_encuesta}, function(resultados){
		$('body').find('#encuesta_opciones_'+id_encuesta).html(resultados)
	})
}
function votar_n(){
	var titulo = 'Votar'
	var cerrar = '<div class="cerrar_b" onclick="Boxy.get(this).hide();"></div>'
	Boxy.alert("Disculpa, tienes que iniciar sesión para poder votar", null, {title:titulo, closeText:cerrar, fixed:false, unloadOnHide:true});
}
//ELIMINAR ENCUESTA
function eliminar_encuesta(id){
	$.post('gestion_cp.php', {eliminar:'encuesta', id:id})
	$('#encuesta_'+id).remove()
}

//VIDEOS - TV VIVO
function videos(cod,p){
	
	this.titulo_mostrar = function(){
		$('#tv_vivo_cont_senal .titul_cerrar').fadeIn(1).delay(2000).fadeOut(100)
	}
	this.cerrar_video = function(){
		$('.noticias_recientes_cont').show()
		$('#tv_vivo_cont_senal').hide()

	}
	
	var img_cargando = $('#pop_eventos_cargando_cont').html()
		$('#tv_vivo_cont_senal .tv_envivo_st').html(img_cargando)
		$('#tv_vivo_cont_senal').show()
		$('.noticias_recientes_cont').hide()
		this.titulo_mostrar()
	
	if(p=='0'){
		
		$.post('crea_video_vmlive.php',{plata:0, auto:1},function(data){
		data = data.replace('552', '526').replace('386', '359')
		$("#tv_vivo_cont_senal .tv_envivo_st").html(data);
		this.titulo_mostrar()
	})
	
	}else if(p=='1'){
	
		$("#tv_vivo_cont_senal .tv_envivo_st").html('<embed src="http://www.youtube.com/v/'+cod+'&amp;autoplay=1" type="application/x-shockwave-flash" wmode="transparent" width="520" height="359">');
		titulo_mostrar()
		
		}else if(p=='2'){
			$.post('crea_video_vmlive.php',{vid:cod, plata:2},function(data){
			$("#tv_vivo_cont_senal .tv_envivo_st").html(data);
			this.titulo_mostrar()
		})
		
	} else if(p=='3'){
		$.post('crea_video_vmlive.php',{vid:cod, plata:3},function(data){
		data = data.replace('552', '526').replace('386', '359')
		$("#tv_vivo_cont_senal .tv_envivo_st").html(data);
		this.titulo_mostrar()	
	
	})
	
	}else if(p=='tope'){
		$.post('tope.php',{plata:'tope'},function(data){
			data = data.replace('552', '526').replace('386', '359')
			$("#tv_vivo_cont_senal .tv_envivo_st").html(data);
		})
	}
}
