$(function() {
    $('.lot').each(
        function() {
            var lot_id = $(this).attr('id');
            $(this).find('.lotImage img').wrap('<a href="#"></a>'); // add links to images
            $(this).find('h2 a, .lotImage a').attr('href', '#TB_inline?height=400&width=800&inlineId='+lot_id).addClass('thickbox');
        }
    );
    tb_init('a.thickbox');
    
    if ( location.hash.match(/rotate/) ) {
		var lotThickboxLinks = $('#lots .lot');
		var current = 0;
		
		setInterval(
			function() {
				tb_remove();
				setTimeout(
					function() {
						lotThickboxLinks.each(
							function(i) {
								if ( current == i ) {
									$(this).find('a.thickbox:first').click();
								}
							}
						);
	
						current++;
						if ( current >= lotThickboxLinks.size() ) {
							current = 0;
						}
					},
					1000
				);
			},
			20*1000
		);
	}
    
    function scrollTop() {
	    if ( document.documentElement && document.documentElement.scrollTop ) {
	        return document.documentElement.scrollTop;
	    }
	    if ( document.body && document.body.scrollTop ) {
	        return document.body.scrollTop;
	    }
	    return 0;
	}

	var down = true;
	
	function runScroll() {
		var docHeight = $(document).height();
    			
		var winTop    = scrollTop();
		var winHeight = $(window).height();
		var winBottom = winTop + winHeight;
		
		// change direction when we hit top or bottom
		if ( winBottom >= docHeight ) {
			down = false;
		}
		else if ( winTop <= 0 ) {
			down = true;
		}
		
		var targetOffset = down?winBottom:0;
		
		//$('html,body').animate({scrollTop: targetOffset}, 10*1000);
		$('html,body').attr('scrollTop', winTop + (down? 2 : -2));
	}
	
	if ( location.hash.match(/scroll/) ) {
    	setInterval( runScroll, 200 );
	}
});
