var tabs;
var initialY;
var box = 1;
var numboxes = 10;
var cumulative = 0;
var scroller;
var currentTab;

function startScroll()
{
    cumulative = 0;
    box = 1;
    scroller = new PeriodicalExecuter(slideBizFlow,5);
}

function stopScroll()
{
    alert('stopping');
    scroller.stop();
}

function slideBizFlow()
{
    var target = $('content'+box);
    var distance = target.getHeight();
    new Effect.Move($('featured_list'), {y:-distance});

    if(box < numboxes)
    {
        box++;
        cumulative += distance;
    }else{
        box = 1;
        new Effect.Move($('featured_list'), {y:cumulative});
        cumulative = 0;
    }

}

function slideBack()
{
    if(box>1)
    {
        var target = $('content'+box);
        var distance = target.getHeight();
        new Effect.Move($('featured_list'), {y:distance});
        box--;
    }

}

function onFocus()
{
    this.clear();
}

function clearTabs(e)
{
    // deactivate all tabs
    tabs.invoke('removeClassName', 'active').invoke('addClassName', 'disabled');
    // activate this tab
    this.removeClassName('disabled').addClassName('active');

    // toggle scroll arrows for tabs other than featured
    if(this.id != "featured_tab")
    {
        $('arrows').hide();
        new Effect.Move($('featured_list'), {y:0, mode:'absolute'});
    }else{
        $('arrows').show();
    }

    currentTab = this.id;

    isLoading(true);
}

function isLoading(status)
{
    if(status)
    {
        $(document.body).addClassName('waiting');
        $('featured_list').addClassName('loading');
    }else{
        $(document.body).removeClassName('waiting');
        $('featured_list').removeClassName('loading');
        $('featured_list').removeClassName($w($('featured_list').className));
        $('featured_list').addClassName(currentTab.split("_")[0]);
    }
}


document.observe('dom:loaded', function()
{
   initialY = $('featured_list').cumulativeOffset();
   tabs = $$('a[id$="_tab"]');

   tabs.invoke('observe', 'click', clearTabs);

   $('next').observe('click', function(event)
   {
        Event.stop(event);
        scroller.stop();
        slideBizFlow();
   })

    $('previous').observe('click', function(event)
   {
        Event.stop(event);
        scroller.stop();
        slideBack();
   })

   $('searchfield').observe('focus', onFocus).observe('blur', function(){
        if(this.getValue() == "")
        {
            this.setValue('Search for goods, services, and local businesses');
        }
   })

   $('locfield').observe('focus', onFocus).observe('blur', function(){
        if(this.getValue() == "")
        {
            this.setValue('Zip Code');
        }
   })

   startScroll();



})
