var curslide = null;
var slslides = {};
var sltimer = null;
var hlcon = null;
var hlfirst = null;
var hllast = null;
var hllimit = 3;
var fadetime = 200;
var rotatetime = 7000;

function ng_init_spotlight(){

    var onestory = $('#spotlight .nj_single_story');
    if(onestory.length > 0){
        $('#spotlight .nj_single_story .pagemgrpos').show();
        return;
    }

    curslide = $('#spotlight .pagemgrrow div:first').get(0) || null;
    $(curslide).show();
    var slcontrols = $('#slcontrols');
    $('#spotlight .pagemgrpos').each(function(idx){
                                         var posid = $(this).attr('posid');
                                         slslides[posid] = this;
                                         if(idx == 0) this.style.display = 'block';
                                         var sli = $('<a href="#" class="slide'+idx+'" rel="'+posid+'">'+(idx+1)+'</a>', document);
                                         if(idx == 0) sli.addClass('on');
                                         sli.click(function(ev){
                                                       clearInterval(sltimer);
                                                       sltimer = null;
                                                       unsethover('pause');
                                                       sethover('play');
                                                       var sidx = $(this).attr('rel');
                                                       $(curslide).fadeOut(fadetime, function(){
                                                                            curslide = slslides[sidx];
                                                                            $(curslide).fadeIn(fadetime);
                                                                        });
                                                       $('#slcontrols a').removeClass('on');
                                                       $(this).addClass('on');
                                                       ev.preventDefault();
                                                  });
                                         slcontrols.append(sli);
                                    });
    $('#slplay').click(function(ev){
                         if(!sltimer){
                             ng_next_slide();
                             sltimer = setInterval(ng_next_slide, rotatetime);
                             unsethover('play');
                             sethover('pause');
                         }
                         ev.preventDefault();
                       });
    unsethover('play');
    $('#slpause').click(function(ev){
                          if(sltimer){
                              clearInterval(sltimer);
                              sltimer = null;
                              unsethover('pause');
                              sethover('play');
                          }
                          ev.preventDefault();
                        });
    sethover('pause');
    sltimer = setInterval(ng_next_slide, rotatetime);
    slcontrols.show();
}

function sethover(btnname){
      $('#sl'+btnname+' img').get(0).src = '/images/sl_'+btnname+'_btn.gif';
}
function unsethover(btnname){
  $('#sl'+btnname+' img').get(0).src = '/images/sl_'+btnname+'_btn_on.gif';
}
function ng_next_slide(){
    var sidx = $(curslide).attr('posid');
    var idx = parseInt(sidx.substr(1), 10);
    if(!idx || !slslides['c'+(idx+1)]) idx = 0;
    $(curslide).fadeOut(fadetime, function(){
                            curslide = slslides['c'+(idx+1)];
                            $(curslide).fadeIn(fadetime);
                            });
    $('#slcontrols a').removeClass('on');
    $('#slcontrols a:eq('+(idx+2)+')').addClass('on');
}

function ng_get_first_hl(){
    return $('#highlights .pagemgrpos:first').get(0);
}

function ng_get_last_hl(){
    var elast = $('#highlights .pagemgrpos:last').get(0);
    $('#highlights .pagemgrpos:last').remove();
    return elast;
}

function ng_init_highlights(){
    var hlnum = $('#highlights .pagemgrpos').length;
    if(hlnum > hllimit){
        hlcon = $('#highlights .pagemgrrow').get(0);
        hlfirst = ng_get_first_hl();
        hllast = ng_get_last_hl();
        $('#hlprev').click(function(ev){
                             var row = $(hlcon);
                             row.fadeOut(fadetime, function(){
                                           $(hlcon).prepend(hllast);
                                           $(hllast).show();
                                           hlfirst = hllast;
                                           hllast = ng_get_last_hl();
                                           row.fadeIn(fadetime);
                                         });
                             ev.preventDefault();
                           });
        $('#hlnext').click(function(ev){
                             var row = $(hlcon);
                             row.fadeOut(fadetime, function(){
                                           row.append(hllast);
                                           $(hlfirst).show();
                                           $('#highlights .pagemgrpos:first').remove();
                                           row.fadeIn(fadetime);
                                           hllast = hlfirst;
                                           hlfirst = ng_get_first_hl();
                                         });
                             ev.preventDefault();
                           });
        $('#highlights .hlcontrols').show();
    }
}

$(document).ready(function(){
        ng_init_spotlight();
        //ng_init_highlights();
    });