jQuery(document).ready(function() {

	/*
var player = null;
	var playlist = null;
	var playItem = null;
	
	
	if(!(playItem = swfobject.getQueryParamValue('item'))) {
		//...default is first item (array counting starts at 0)
		playItem = 0;
	}
*/
	
	/*
function sendEvent(typ, prm) {
		thisMovie('WT1_1').sendEvent(typ, prm);
	};
	
	function thisMovie(movieName) {
		if(navigator.appName.indexOf('Microsoft') != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	};
	
	sendEvent('playitem', playItem);
*/
	//alert('Play Item: ' + playItem);
	
	/*
function playerReady(obj) {
		alert('playerReady');
		player = document.getElementById(obj.id);
		playAway();
	};
	
	//...start the player on the query parameter item
	function playAway()	{
		alert('playerAway');
		try {
			playlist = player.getPlaylist();
		} catch(e) {
			setTimeout("playAway()", 100);
		}
	
		function playerReady(obj) {
			player = document.getElementById(obj.id);
			alert('Player ID: ' + player.id);
			playAway();
		};
		
		alert('playItem:' + playItem);
		player.sendEvent('ITEM', playItem);
		setTimeout("player.sendEvent('PLAY', 'true')", 200);
	};
*/
	
	jQuery('.paginacion .gris').each(function(i){
		jQuery(this).click(function(){
			jQuery('#contenedor-noticias').stop();
			cambiarNoticia(i, 0);
		});
	});
	
	jQuery('.paginacion .anterior').click(function(){
		var noticia_actual = jQuery('#contenedor-noticias div').attr('id').split('-');
		var anterior = parseInt(noticia_actual[1]) - 1;
		if(noticia_actual[1] == 0) {
			var anterior = 3;
		}
		jQuery('#contenedor-noticias').stop();
		cambiarNoticia(anterior, 0);
	});
	
	jQuery('.paginacion .siguiente').click(function(){
		var noticia_actual = jQuery('#contenedor-noticias div').attr('id').split('-');
		var siguiente = parseInt(noticia_actual[1]) + 1;
		if(noticia_actual[1] == 3) {
			var siguiente = 0;
		}
		jQuery('#contenedor-noticias').stop();
		cambiarNoticia(siguiente, 0);
	});
	
	cambiarNoticia(1, 1);
	
	var busqueda = jQuery('#searchform #s').val();
	
	if(busqueda != '') {
		jQuery('#searchform #s').css({'background-image': 'url()'});
	}
	
	jQuery('#searchform #s').focus(function() {
		jQuery('#searchform #s').css({'background-image': 'url()'});
	});
	
	jQuery('#searchform #s').blur(function() {
		if(jQuery('#searchform #s').val() == '') {
			jQuery('#searchform #s').css({'background-image': 'url(http://www.google.com/coop/intl/es/images/google_site_search_watermark.gif)'});
		}
	});
	
	jQuery('.respuesta').click(function() {
	
		if(!jQuery(this).hasClass('seleccion')) {
			
			var ID = jQuery(this).attr('id');
			var actual = jQuery(this);
			var parent = jQuery(this).parent();
			
			jQuery.ajax({
				type: 'POST',
				url: '/wp-content/themes/circulo/ajax/confirmacion-asistencia.php',
				data: 'ID=' + ID,
				success: function(msg) {
					if (msg == 1) {
						jQuery(parent).find('span.seleccion').removeClass('seleccion');
						jQuery(actual).addClass('seleccion');
						alert('La información se actualizó con éxito.');
					} else {
						alert('Hubo un problema al actualizar la información. Por favor, inténtelo de nuevo pasados unos minutos.');
					}
				}
			});
		
		}
		
	});
		
});

var interval_noticias = 4000;
var totalIndex_noticias = 4;

function cambiarNoticia(index, stop) {
	var target = jQuery('#contenedor-noticias');
	var siguiente = index + 1;
	
	if(siguiente > totalIndex_noticias - 1)
		siguiente = 0;
	
	var interval = interval_noticias;
	
	if(stop == 0)
		interval = 1;
	
	target.animate({opacity: 1.0}, interval, '', function() {
		
		jQuery.ajax({
			type: "POST",
			url: '/wp-content/themes/circulo/ajax/get-post.php',
			cache: false,
			data: 'data=' + index,
			success: function(response) {
				
				jQuery('.paginacion .actual').removeClass('actual');
				
				target.animate({opacity: 0}, 500, '', function() {
					target.empty()
					.html(response)
					.animate({opacity: 1.0}, 1000, '', function() {
						cambiarNoticia(siguiente, 1);
					});
				});
				
				jQuery('.paginacion .gris:eq(' + index + ')').addClass('actual');
				
			}
		});
	});
}