var counter = 1;

$jq.fn.nextOrFirst = function(selector){
  var next = this.next(selector);
  return (next.length) ? next : this.prevAll(selector).last();
}

$(document).ready(function(context) {
  
  // Switch value proposition frame
  var p = setInterval(nextFrame, '5000');
  // Switch notification
  var n = setInterval(nextNote, '15000');
  
  // Login and register flyouts
  $(".black_overlay_non_modal").click(function(){  
    $(".black_overlay_non_modal").fadeOut(300);
    $('#connect').attr('style', 'z-index:999;');
    $('#login').removeClass('open');
  }); 
  
  $('#login').click(function() {
    $(this).addClass('open');
    $(".black_overlay_non_modal").fadeIn(300);
    $('#connect').attr('style', 'z-index:1000;');
    $('#register').attr('style', 'z-index:998;');
    $('#register .error').hide();
  });
  
  $('#register').click(function() {
    if (!$(this).hasClass('open')) {
      $(this).addClass('open');
    //  $('#register-link').hide();
    //  $('#register-box').fadeIn(300);
      $('.black_overlay_non_modal').attr('style', 'z-index:999;');
      $(".black_overlay_non_modal").fadeIn(300);
      $('#register').attr('style', 'z-index:1001;');
    }
  });
  
  //Notifications
  $('#notifications ul li').click(function() {
    if (!$(this).hasClass('active')) {
      var nid = $(this).attr('id').match(/[0-9]+/i);
      changeNote(nid);      
    }
  });
  
  $('#notifications a').hide();
  $('#notifications a.active').show();
  
  // Value propositions element
  $("#propositions ul li").mouseover(function() {
    //var old_active = $("#propositions ul li.active");
    clearInterval(p);
    $("#propositions ul li.active").removeClass('active');
    $(this).addClass("active");
  }).mouseout(function(){
    //$(this).removeClass("active");
    p = setInterval(nextFrame, '5000');
  });
  
  $(".black_overlay_non_modal").click(function() {  
    closeFlyouts();
  });
  
  $("#forgot-password a.lb_close").click(function() {
    closeFlyouts();
    $('#forgot-password .messages').remove();
  });
  
});

function closeFlyouts() {
  $(".black_overlay_non_modal").fadeOut(300);
  $('#login, #register').attr('style', 'z-index:999;');
  $('#login-box, #register-box').fadeOut(300);
  $('#login, #register').removeClass('open');
  $('#register-link').show();
  $('#connect .error').hide();
}

function nextFrame() {
  var next;
  
  $('#propositions ul li.active').removeClass('active');
  switch (counter) {
    case 1:
      next = '#grow';
      counter++;
      break;
    case 2:
      next = '#connect-collab';
      counter++;
      break;
    case 3:
      next = '#showcase';
      counter++;
      break;
    case 4:
      next = '#be-seen';
      counter = 1;
      break;
  }
  
  $(next).addClass('active');
}

function nextNote() {
  var next = $jq('#notifications ul li.active').nextOrFirst();
  var nid  = next.attr('id').match(/[0-9]+/i);
  changeNote(nid);
}

function changeNote(nid) {
  $('#notifications ul li').each(function(i) {
    $(this).removeClass('active');
  });
  
  $('#notifications a.active').fadeOut(300);
  
  $('#notifications a').each(function(i) {
    $(this).removeClass('active');
  });
  $('#thumb_'+ nid).addClass('active');
  $('#note_'+ nid).addClass('active');
  
  $('#notifications a.active').fadeIn(300);
}
