var featureSlide = 1;
var featureTimer;
var noSlide = 0;

$(document).ready(function()
{
    if($('.spotlight')){
        featureTimer = setTimeout('featureSwitch()',5000);
    }
    $.fn.placeholder = function(){

            // quit if there's support  for html5 placeholder
            if (this[0] && 'placeholder' in document.createElement('input')) return;

            return this
            .live('focusin',function(){
                if ($(this).val() === $(this).attr('placeholder')) {
                    $(this).val('');
                }
            }).live('focusout',function(){
                if ($(this).val() === ''){
                    $(this).val( $(this).attr('placeholder') );
                    if($(this).attr('type')==='password') {
                        $(".password-field label").removeClass('hidden');
                        $(".password-field").addClass('needed');
                    }
                }
            }).focusout();
    }
    $('input[placeholder]').placeholder();
    $('.spotlight-inner header').hover(function(){
        clearTimeout(featureTimer);
        if(noSlide==0 && !$(this).hasClass('selected')){
            featureSlide = $('.spotlight-inner header').index($(this));
            featureSwitch(1);
        } else if (!$(this).hasClass('selected')){
            setTimeout('delayFeatureSwitch('+$('.spotlight-inner header').index($(this))+')',50);
        }
    },function(){
        featureTimer = setTimeout('featureSwitch()',5000);
    })
    $('.nav_list li').hover(function(){$(this).toggleClass('hover')},function(){$(this).toggleClass('hover')});
    

    $('A[rel="_blank"]').each(function(){
               $(this).attr('target', '_blank');
          });

    
});

function delayFeatureSwitch(featured){
    if(noSlide==0){
        featureSlide = featured;
        featureSwitch(1);
    } else {
        setTimeout('delayFeatureSwitch('+featured+')',50);
    }
}

function featureSwitch (hold) {
    noSlide=1;
    clearTimeout(featureTimer);
    $('.spotlight-inner article.selected').removeClass('selected');
    $('.spotlight-inner header.selected').addClass('post-selected').removeClass('selected');
    $('.spotlight-inner .feature-image:eq('+featureSlide+')').hide();
    $('.spotlight-inner header:eq('+featureSlide+')').addClass('pre-select');
    $('.spotlight-inner article:eq('+featureSlide+')').addClass('selected');
    $('.pre-select .feature-image').fadeIn(750,function(){
        $('header.post-selected').removeClass('post-selected');
        $('header.pre-select').addClass('selected').removeClass('pre-select');
        noSlide=0;
        featureSlide++;
        if(featureSlide==$('.spotlight-inner article').length){
            featureSlide = 0;
        }
        if(hold!=1){
            featureTimer = setTimeout('featureSwitch()',5000);
        }
    });
}
