
var galleries = new Array();
var loadCount = 1;

function scrollCheck(){

	if ($(window).scrollTop() + 300 >= $(document).height() - $(window).height()) { 
		
		loadCount++;
		
		subSet = galleries.splice(0, 50);
		
		if(subSet.length > 1)
		{
			$.each(subSet, function(index, value) {
				$('#container').append('<a href="?send=' + value.i + '" target="_blank"><img src="http://picselection.com/st/thumbs/' + value.d + '/' + value.i + '.jpg" /></a>');
			});	
		}
		else
		{
			$(window).unbind('scroll', scrollCheck);
		}
	}
}

function sinceStart () 
{
	var currentTime = new Date();
	var difference = (currentTime - startTime) / 1000;
	return parseInt(Math.round(difference));
}

$(document).ready(function(){
	$.getJSON('http://picselection.com/json.php?callback=?', function(data) {
		galleries = data;
		$(window).scroll(scrollCheck);
		scroll(0, 0); 
		scrollCheck();
	});
});

$(function() {
	
	var clickCount = 0;
	
	$('#container a').live('click', function(){
		
		var findEquals = $(this).attr('href').indexOf('=');
		var galCode = '?';
		
		if(findEquals++ >= 0)
		{
			galCode = $(this).attr('href').substring(findEquals);
		}
		
		_gaq.push(['_trackEvent', 'Thumb', 'Click', galCode, ++clickCount]);
		
		/* POP UNDER
		if(clickCount == 3)
		{
			win = window.open("http://pornrebates.com");
			win.blur();
			window.focus();
		}
		*/
	});
	
	$('#ad').live('click', function() {
		_gaq.push(['_trackEvent', 'Footer', 'Ad', $(this).attr('href'), sinceStart()]);
	});
	
	$('#close').click(function(){
		_gaq.push(['_trackEvent', 'Footer', 'Hide', '', sinceStart()]);
		document.getElementById('footer').style.display = 'none';
	});
	
});
