///////////////
// SLIDESHOW //
///////////////

var slide_timeout;
var slide_seconds;

function initSlideshow() {
    slide_seconds = 5;
    
    if ($('#slide > *').length > 1) {
        // only init slide functions if there's more than 1
        
        // build nav, give each element a rel
        $("#slide > *").each(function(i) {
            $(this).attr('rel', i).addClass('slide');
            $('#slide-nav').append('<a href="#" rel="' + i + '"></a>');
        });

        // make first slide current
        changeSlide(0);

        // hover functions
        $('#slide, #slide-nav > a').hover(function() {
            clearTimeout(slide_timeout);
        }, function() {
            setSlideTimeout();
        });

        // click nav
        $('#slide-nav a').click(function(e) {
            e.preventDefault();
            rel = $(this).attr('rel');
            changeSlide(rel, true);
        })
    }
}

function changeSlide(rel, do_not_set_timeout) {
    clearTimeout(slide_timeout);
    
    $('#slide > .slide:visible').fadeOut(500).removeClass('cur');
    $('#slide > .slide[rel="' + rel + '"]').addClass('cur').fadeIn(500);
    $('#slide-nav .cur').removeClass('cur');
    $('#slide-nav a[rel="' + rel + '"]').addClass('cur');
    
    // set timeout for next slide
    if (!do_not_set_timeout) {
        setSlideTimeout();
    }
}

function setSlideTimeout() {
    slide_timeout = setTimeout(function() {
        current_rel = $('.slide.cur').attr('rel');
        if (current_rel == $('#slide .slide').length - 1) {
            next_rel = 0;            
        } else {
            next_rel = parseInt(current_rel, 10) + 1;
        }
        changeSlide(next_rel);
    }, slide_seconds * 1000)
}


///////////
// GRIDS //
///////////

function initGrid(selector, break_num) {
    $(selector).each(function() {
        $(this).children().each(function(i) {
            if ((i+1) % break_num == 0) {
                $(this).after('<div class="gridbreak"></div>');
            }
        })
    })
}

////////////
// WIDOWS //
////////////

function surpressWidows() {
    $('#content p').each(function() {
        replaceLastSpace($(this));
    })
    $('#content a').each(function() {
        replaceLastSpace($(this));
    })
}

function replaceLastSpace(obj) {
    orig = $(obj).html();
    split = orig.split(' ');
    if (split.length > 1) {
        end = split.pop();
        if (end.indexOf('\>') == -1) {
            end = split.pop() + '&nbsp;' + end;
            output = split.join(' ') + " " + end;
        } else {
            output = orig;
        }
    } else {
        output = orig;
    }
    
    $(obj).html(output);
}


/////////////////////////
// MAILING LIST SIGNUP //
/////////////////////////

function mailingListSubmit() {
    $('#pg-pulp form .submit').click(function(e) {
        e.preventDefault();
        formdata = $('#pg-pulp form').serialize();
        $.post('/mailing-list/', formdata, function(response) {
            $('#pg-pulp .msg').fadeIn().html(response);
            $('#pg-pulp input').val('');
        })
        // $('#pg-pulp form').submit();
    })
}


///////////////////
// STICKY FOOTER //
///////////////////

function initStickyFooter() {
    footerPos();
    $(window).resize(function() {
        footerPos();
    })
}


function footerPos() {
    footer_offset = $('#ft').offset().top;
    footer_height = $('#ft').height();
    window_height = $(window).height();
    
    if (footer_offset + footer_height < window_height) {
        $('#ft').addClass('sticky');
    } else {
        $('#ft').removeClass('sticky');
    }
}
