var direct = true;

function submitEvent(){
    var p = this;
    while(p.tagName.toUpperCase()!='FORM')
        p=p.parentNode;
    p.submit();
}


function startScroll(){    
    var frompos = parseInt($('.jspPane').css('top').replace('px'));
    if(direct){
        var topos1 = (-1)*($('.jspPane').height()-$('.jspContainer').height()+50);
        var topos2 = $('.jspTrack').height()-$('.jspDrag').height();
    }else{
        var topos1 = 0;
        var topos2 = 0;
    }
    
    $('.jspPane').animate(
        {top: '' + topos1 + 'px'}, Math.abs(topos1-frompos)*20,'linear',function(){
            direct = !direct;
            startScroll();
        }
    )
    $('.jspDrag').animate(
        {top: '' + topos2 + 'px'}, Math.abs(topos1-frompos)*20,'linear'
    )
}

function stopScroll(){
    $('.jspPane').stop(true);
    $('.jspDrag').stop(true);
}


$(function(){
    $("select").selectbox();
    if($("select#order").length>0)
        $("select#order").change(submitEvent);
    if( $('.scroll-pane').length>0){
        scrollheight = Math.floor(($('.scroll-pane .ad').length - 1) / 4 ) * 216;
        $('.scroll-pane').jScrollPane({
            showArrows: true,
            animateScroll: true
        });
        $(window).ready(function(){
            startScroll();
            $('#highlightedAds').hover(stopScroll,startScroll);
        });
    }
    if($('#submitAdForm').length>0)
        fileInput();
});
