
var login_timeout = null;
var form_timeout_seconds = 2;

$(document).ready(function() {
   $('#membercarousel').jcarousel({
     auto: 4,
     scroll: 1,
     animation: 1500,
     size: 16,
     wrap : 'circular',
     initCallback: mycarousel_initCallback
   });

  // dialog creation
  $('.dia').dialog({
    autoOpen : false,
    width : 570,
    stack : false,
    show : 'fade',
    hide : 'fade',
    modal : true,
    resizable: false,
    draggable : false
  });
  
  // form handler
  $('#registration').submit(function() {
    var yr      = Math.round($('#dobyear option:selected', $(this)).val());
    // if(yr == null || yr == '') return false;

		if (yr) {
			var min_age = Math.round($('#min_age', $(this)).val());
	    var age     = ((new Date().getFullYear()) - yr);
	    return (age < min_age) ? showAgeDialog() : true;
		}
  });

  $('form#login')
		.mouseover(function() { disableLoginTimeout(); })
  	.mouseout(function(event) { (shouldIHide($(event.relatedTarget), $(this))) ? enableLoginTimeout() : disableLoginTimeout(); });

  $('.members-login-link')
		.click(function() { disableLoginTimeout(); return toggleLoginForm(); })
  	.mouseout(function(event) { if(shouldIHide($(event.relatedTarget), $(this))) { enableLoginTimeout(); } })
  	.mouseover(function() { disableLoginTimeout(); });
});

function shouldIHide(rel_el, el)  { return ($(rel_el)[0] == $(this)[0] || jQuery.inArray($(rel_el)[0], $("*",el).toArray()) == -1); }
function enableLoginTimeout()     {
  login_timeout = setTimeout(function() { if($('form#login').is(':visible')) toggleLoginForm(); return false; }, (form_timeout_seconds * 1000));
  return false;
}
function disableLoginTimeout()    { clearTimeout(login_timeout); return true; }
function toggleLoginForm()        {
  var frm = $('form#login');
  if(frm.is(':visible')) {
    $('.members-login-link').removeClass('open');
    $(frm).fadeOut('slow');
  } else {
    $('.members-login-link').addClass('open');
    $(frm).fadeIn('fast');
    // $('input[name="memberid"]', frm).focus();
  }
  return false;
}
function showAgeDialog()          { $('#errbox').dialog('open'); return false; }
function mycarousel_initCallback(carousel) {
  // Disable autoscrolling if the user clicks the prev or next button.
  carousel.buttonNext.bind('click', function() { carousel.startAuto(0); });
  carousel.buttonPrev.bind('click', function() { carousel.startAuto(0); });
  // Pause autoscrolling if the user moves with the cursor over the clip.
  carousel.clip.hover(function() { carousel.stopAuto(); }, function() { carousel.startAuto(); });
};
