$(document).ready(function() {
	
	$('.paginacion .gris').each(function(i){
		$(this).click(function(){
			cambiarNoticia(i);
		});
	});
	
	$('.paginacion .anterior').click(function(){
		var noticia_actual = $('#contenedor-noticias div').attr('id').split('-');
		var anterior = parseInt(noticia_actual[1]) - 1;
		if(noticia_actual[1] == 0) {
			var anterior = 3;
		}
		cambiarNoticia(anterior);
	});
	
	$('.paginacion .siguiente').click(function(){
		var noticia_actual = $('#contenedor-noticias div').attr('id').split('-');
		var anterior = parseInt(noticia_actual[1]) + 1;
		if(noticia_actual[1] == 3) {
			var anterior = 0;
		}
		cambiarNoticia(anterior);
	});
	
	if($('#execphp-428335471').length > 0) {
		cambiarCliente(300);
	}
	
	if($('#execphp-435155481').length > 0) {
		cambiarCliente2(300);
	}
	
	$('.post-1031 h5').click(function(e) {
		var Clientes = $(e.target).next();
		Clientes.toggle();
		//$('.post-1031 p:visible').hide();
	});
	
	quotescollection_init('/wp-content/plugins/quotes-collection/quotes-collection-ajax.php', 'Otra referencia', 'Cargando...', 'Sucedió un error');
	
});

function cambiarNoticia(index) {
	$.ajax({
		type: "POST",
		url: '/wp-content/themes/portocarrero/ajax/get-post.php',
		cache: false,
		data: 'data=' + index,
		success: function(response) {
			$('#contenedor-noticias').empty().html(response);
		}
	});
}

function cambiarCliente(interval) {
	var target = $('#execphp-428335471 .execphpwidget');
	var logo_actual = $('#execphp-428335471 .execphpwidget img').attr('id').split('-');
	var logo_actual = parseInt(logo_actual[1]);
	
	target.children('img')
		.animate({opacity: 1.0}, interval, '', function() {
			$.ajax({
				type: "POST",
				url: '/wp-content/themes/portocarrero/ajax/get-logos.php',
				cache: false,
				data: 'data=' + logo_actual,
				success: function(response) {
					target.animate({opacity: 0}, 500, '', function() {
						target.empty().html(response).animate({opacity: 1.0}, 1000, '', function() {
							cambiarCliente(interval);
						});
					});
				}
			});
		});
}

function cambiarCliente2(interval) {
	var target = $('#execphp-435155481 .execphpwidget');
	var logo_actual = $('#execphp-435155481 .execphpwidget img').attr('id').split('-');
	var logo_actual = parseInt(logo_actual[1]);
	
	target.children('img')
		.animate({opacity: 1.0}, interval, '', function() {
			$.ajax({
				type: "POST",
				url: '/wp-content/themes/portocarrero/ajax/get-logos.php',
				cache: false,
				data: 'data=' + logo_actual,
				success: function(response) {
					target.animate({opacity: 0}, 500, '', function() {
						target.empty().html(response).animate({opacity: 1.0}, 1000, '', function() {
							cambiarCliente2(interval);
						});
					});
				}
			});
		});
}

//Quotes Collection
var quotcoll_requrl, quotcoll_nexquote, quotcoll_loading, quotcoll_errortext;

function quotescollection_init(requrl, nextquote, loading, errortext) {
	quotcoll_requrl = requrl;
	quotcoll_nextquote = nextquote;
	quotcoll_loading = loading;
	quotcoll_errortext = errortext;
}

function quotescollection_refresh(instance, exclude, show_author, show_source) {

	$.ajax({
		type: "POST",
		url: quotcoll_requrl,
		cache: false,
		data: 'refresh=' + instance + '&exclude=' + exclude + '&show_author=' + show_author + '&show_source=' + show_source,
		error: function() {
			$('#quotescollection_randomquote-'+instance).html(quotcoll_errortext);
		},
		beforeSend: function() {
			$('#quotescollection_randomquote-'+instance).html(quotcoll_loading);
		},
		success: function(response) {
			response = response.replace(/\\/g, '');
			$('#quotescollection_randomquote-'+instance).html(response);
		}
	});
}