// $(document).ready(function(){}) == $(function(){})

$(function()
{
	$.ajax(
	{
		url: '/atrapantes/listSortedPremios.xml?nocache='+Math.random()*1000,
		//url: 'http://localhost/Atrapantes/www/wp-content/themes/atrapantes/xml1.xml?nocache='+Math.random()*1000,
		type: 'GET',
		dataType: 'xml',
		error: function(e)
		{
			if( e.status == 200 && e.responseXML != null) this.success(e.responseXML)
		},		
		beforeSend: function()
		{
			//$('div#loading').show()
		},
		success: function(datos){
			successData(datos)
			//$('div#loading').hide()
		}
	});

	$(window).scroll(function()
	{
		if  ( ($(window).scrollTop() == $(document).height() - $(window).height()) || ($(window).scrollTop()+1 == $(document).height() - $(window).height()) )
		{
			var last = $('ul#lista_hallazgos li').length;
			$.ajax({
				url: '/atrapantes/listSortedPremios.xml?nocache='+Math.random()*1000,
				//url: 'http://localhost/Atrapantes/www/wp-content/themes/atrapantes/xml2.xml?nocache='+Math.random()*1000,
				type	: 'GET',
				dataType: 'xml',
				data	: 'pageNumber='+last,
				error	: function(e)
				{
					if( e.status == 200 && e.responseXML != null) this.success(e.responseXML)
				},
				beforeSend: function()
				{
					//$('div#loading').show();
				},
				success: function(datos)
				{
					successData(datos);
					//$('div#loading').hide();
				}
			});
		}
	});
});

function successData(datos)
{
	var time1 = 500; // delay en milisegundos
	var time2 = 200;

	$('data item', datos).each(function(i)
	{
		var title = $(this).find('title').text();
			title = title.replace("$","&#8364;");
		var desc  = $(this).find('description').text();
			desc = desc.replace("$","&#8364;");
		var found = $(this).attr("found");

		if (found == "true" || found == "2" )
		{
			var e = $('<li class="lifondo_tachado">')
				.html('<div class="badge"><img src="http://www.atrapantes.com/wp-content/themes/atrapantes/images/badge.gif" alt="ENCONTRADO" title="ENCONTRADO"/></div><div class="fondo"><div class="titulotachado">' + title + '</div>' + '<div class="desctachado">' + desc + '</div></div>')
				.css('opacity',0)
			e.appendTo('#caja_hallazgos_pg ul')	
		}
		else
		{
			var e = $('<li class="lifondo">')
				.html('<div class="titulo">' + title + '</div>' + '<div class="desc">' + desc + '</div>')
				.css('opacity',0)
			e.appendTo('#caja_hallazgos_pg ul')

		}
		setTimeout(function(){
			e.animate({opacity:1},time1)
		},time2*i)		
	});
}