$(document).ready(function() {

    $('.bar').each( function() {
        var bar = $(this);
        var section = bar.next('.section');
        var close = bar.find('a.arrow');
        close.click(function() {
            if ( bar.hasClass('closed') ) {
                bar.removeClass('closed');
                section.show();
            }
            else {
                bar.addClass('closed');
                section.hide();
            }
            return false;
        });
    });
    
    // "high quality" anti-theft
    //     $('img[src*=lots/image], img[src*=content/contentimage]').each(function() {
    //     	$(this).bind("contextmenu", function() { return false; });    
    //     });
    
    function placeElements(elements) {
        var left = 0;
        elements.each(
            function() {
                $(this).css("left", left);
                left += (20+$(this).width());
            }
        );
        
        return left;
    }
    
    function createCarousel(element) {
        var carouselInside = $("<div class='carouselInside'></div>");
        element.wrapInner(carouselInside);
        
        carouselInside = element.find('.carouselInside');
        
        var elements = carouselInside.find(".featuredLot");
        
    	var elementHeight = 0;
        var elementWidth = 0;
        elements.each(
            function() {
                elementHeight = Math.max($(this).height(), elementHeight);
                elementWidth  = Math.max($(this).width(), elementWidth);
            }
        );
        
        element.height(elementHeight + 20).css({overflow: "hidden", position: "relative"});
        
        elements.removeClass("first").css({position: "absolute"});
        var totalWidth = placeElements(elements);
        
        
        carouselInside.width(totalWidth).height(elementHeight + 20).css({position: "absolute", left: 0});
        
        // don't bother making carousel if enough space already
        if ( totalWidth <= element.width() )
            return;
        
        // add buttons to scroll lots left/right
        var buttonLeft = $("<a href='#' title='scroll left'>&lt;&lt;</a>");
        var buttonRight = $("<a href='#' title='scroll right'>&gt;&gt;</a>");
        
        var buttons = $("<div style='float: right'></div>").append(buttonLeft).append(" ").append(buttonRight);
        
        element.parent().prepend(buttons);
        
        buttons.css({top: -buttons.height()});
        
        function scroll(dir) {
            var currentLeft = carouselInside.offset().left - element.offset().left;
            
            var targetLeft = dir < 0? currentLeft - (elementWidth+20) : currentLeft + (elementWidth+20);
            
            targetLeft = Math.min(0, Math.max(-(totalWidth - element.width()), targetLeft) );
            var diff = Math.abs(targetLeft - currentLeft);
            if ( diff > 0 ) {
                carouselInside.animate({left: targetLeft}, 500);
            }
        }
        
        buttonLeft.click(function(event) { scroll(1); event.preventDefault(); });
        buttonRight.click(function(event) { scroll(-1); event.preventDefault(); });
    }
    
    $('.carousel').each(
    	function() {	
    		createCarousel($(this));
    	}
    );

    // hide the signup form (after first non-empty td)
    var hide = false;
    $('#email_signup table table td').each(function() {
        if ( hide ) {
            $(this).hide();
        }
        else if ( $(this).text() ) { 
            hide = true;
            // add a click handler to show rest of form, when click
            $(this)
                .attr('title', 'Click to Join Mailing List')
                .css({cursor:'pointer'})
                .click(function() { $('#email_signup table table td').show(); });
        }
    });
    
    
});