
var deluxeOpened = false;
var deluxe_;
var preloadcssbg = new Image();
 preloadcssbg.src="images/programs/filter-bgsmall.gif";
 var preloadcssbg2 = new Image();
 preloadcssbg2.src="images/programs/filter-bg.gif";
$(document).ready(function() {
  // deluxe nav
  //$('img[@src$=.png]').ifixpng(); 
  $('#overlay').width($(window).width()).height($(window).height());
  $(window).resize(function() {
    $('#overlay').width($(window).width()).height($(window).height());
  });
  
  $('li.deluxe').hover(function() {
	
    $('.drop-down ul').hide();
    $('.drop-down h4').hide();
    var deluxe = $(this);
    deluxe_ = deluxe;
    $('li.deluxe').removeClass('on');
    if(!deluxeOpened){
	  // Opening for the first time
	  timerId = setTimeout ("deluxeOpenFirst()", 1000);	
    } else {
	  // Opening for the second time and after
	  clearTimeout (timerId);
	  clearTimeout (timerId2);
      deluxeOpened = true;
      $('.drop-down').hide();
      deluxe.children('.drop-down').show();
      $('.drop-down ul').show();
      $('.drop-down h4').show();
      deluxe.addClass('on');
    }
  }, function() {
	// Nav item rollout
	clearTimeout (timerId);
	timerId2 = setTimeout ("deluxeClose()", 200);
  });
});

$(document).ready(function() {
  paginator($('#watchlisten-video'));
  paginator($('#watchlisten-audio'));
});

function deluxeOpenFirst () {
  var deluxe = deluxe_;
  deluxeOpened = true;
  deluxe.addClass('on');
  $('#overlay').css('z-index', '1').show();
  deluxe.children('.drop-down').slideDown("slow",function(){
    if (!deluxeOpened) {
      $('.drop-down').hide();
    } else {
      $('.drop-down ul').show();
      $('.drop-down h4').show();
    }

    $('#overlay').hover(function() {
      $(this).hide();
      deluxeOpened = false;
      $('li.deluxe').removeClass('on');
      $('.drop-down').hide();
      $(this).unbind();
    }, function() {
    });
  });
}

function deluxeClose () {
    $('#overlay').hide();
    deluxeOpened = false;
    $('li.deluxe').removeClass('on');
    $('.drop-down').hide();
    $('#overlay').unbind();
}

function paginator (_mod) {
  var mod = _mod;
  var ppage = 1;
  var plength = mod.find('div.pitem').length;
  mod.find('span.count').html("1-5 of " + plength)
  mod.find('div.hidden').removeClass('hidden').hide();
  
  if (plength>5) {
    mod.find('.list-controls').find('.hidden').removeClass('hidden');
    mod.find('.list-controls').show(); // blackwell
    mod.find('.list-controls .next').click(function() {
      if (ppage*5>plength-1) {
        return false;
      };
      ppage = ppage+1;
      var pagenum = ((ppage-1)*5+1);
      var endnum = pagenum+5 > plength ? plength : pagenum+4;
      mod.find('span.count').html(pagenum +"-"+endnum+" of " + plength);
      mod.find('.pitem').hide();
      for (var i=pagenum-1; i < endnum; i++) {
        mod.find('.pitem:eq('+i+')').fadeIn("slow");
      };
      return false;
    });
    mod.find('.list-controls .previous').click(function() {
      if (ppage==1) {
        return false;
      };
      ppage = ppage-1;
      var pagenum = ((ppage-1)*5+1);
      mod.find('span.count').html(pagenum +"-"+(pagenum+4)+" of " + plength);
      mod.find('.pitem').hide();
      for (var i=pagenum-1; i < pagenum+4; i++) {
        mod.find('.pitem:eq('+i+')').fadeIn("slow");
      };
      return false;
    });
  } else {
    mod.find('.list-controls').hide();
  }
}

var showBenefits = function(page){
    var url = page || '/membership_benefits.html';
    var name = 'winBenefits';
    var opts = 'width=600,height=600,toolbar=no,menubar=no,scrollbars=yes';
    var winBenefits = window.open(url, name, opts);
    if(!winBenefits){
        alert('Please disable popup blocker to see membership benefits');
        winBenefits = window.open('about:blank', name, opts);
        winBenefits.location.href = url;
    }
}

var lastThumbIndex = 0;
var numThumbs = 0;

var cycleThumbs = function(){
    
    if(lastThumbIndex >= numThumbs) {
        $('#overlay-content img.next').trigger('click');
        var recurse = (lastThumbIndex-2) >= numThumbs;
        //if(true || recurse) 
        setTimeout(cycleThumbs, 1000);
    } else {
        
        $('dd.thumb:eq('+ lastThumbIndex +')').children('a').trigger('click');
    }
}


$(document).ready(function(){
    numThumbs = $('dl#preview').find('dd.thumb:visible').length;
    totalThumbs = $('dl#preview').find('dd.thumb').length;
    
    $('img.overlay-hero').css('cursor', 'pointer');
    
    $('dd.thumb a').click(function(){
        lastThumbIndex = $(this).parent('dd').prevAll('dd.thumb:visible').length;
    });

    $('img.prev').click(function(){
        if(lastThumbIndex+1 < numThumbs) lastThumbIndex++;
        else lastThumbIndex = numThumbs
    });
    
    $('img.next').click(function(){
        if(lastThumbIndex) {
            lastThumbIndex--;

        } else {
            lastThumbIndex = 0;
        }
    });
    
    $('img.overlay-hero').each(function(){

        $(this).click(function(){
            lastThumbIndex++;
            cycleThumbs();
        });
    });

    $('img.larrow').click(function(){
        if(lastThumbIndex>0) lastThumbIndex--;
        else lastThumbIndex = $('dd.thumb').length -1;
        var thumb = $('dd.thumb:eq('+ lastThumbIndex +')');
        if(thumb.css('display') == 'none'){
            thumb.prevAll('dd').not('.thumb').children('img.prev').trigger('click');
        }
        thumb.children('a').click();
    });
    $('img.rarrow').click(function(){
        if(lastThumbIndex+1 < $('dd.thumb').length)
        lastThumbIndex++;
        else lastThumbIndex = 0;
        var thumb = $('dd.thumb:eq('+ lastThumbIndex +')');
        if(thumb.css('display') == 'none'){
            thumb.nextAll('dd').not('.thumb').children('img.next').trigger('click');
        }
        thumb.children('a').trigger('click');
    });
});
