var def = function(log_k) {
	
  $('#menu').mousedown(function() {return false;});
  $('#request-submenu').click(function() {
    $('#submenu-1').animate({'bottom':'-120px'}, 300);
  });
  //---- Code for menu animation ----//
  var timer;
  var sub;
  var sub_request_height = $('#sub-request-items').height();
  var sub_training_height = $('#sub-training-items').height();
  $('.submenu').hover(
    function() {
      $(this).addClass('active');
    },
    function() {
      $(this).removeClass('active');
      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
      timer = setTimeout(closeSubmenu, 800);
    }
  );
  function closeSubmenu() {
    if ($('.active').length == 0) {
      $('.sub-open').stop(true, true).animate({'top':'-500px'}, 100).removeClass('sub-open');
      $('.sub-content-div').each(function(i) {
        $(this).width(0);
      });
    }
  }
  $('#menu li').find('a').hoverIntent(
    function() {
      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
      var n = $('.sub-open').length;
      sub = $(this).attr('id');
      if (n != 0) {
        if ($('.sub-open').attr('id') != sub+'-content') {
          $('.sub-open').stop(true, true).animate({'top':'-500px'}, 100, function() { 
            $('#'+sub+'-content').animate({'top':'30px'}, 300).addClass('sub-open'); 
            $('.sub-content-div').each(function(i) {
              $(this).width(0);
            });
          }).removeClass('sub-open');
        }
      } else {
        $('#'+$(this).attr('id')+'-content').animate({'top':'30px'}, 300).addClass('sub-open');
      }
    },
    function() {
      timer = setTimeout(closeSubmenu, 800);
    }
  );
  
  $('.sub-items li').find('a').hoverIntent(
    function() {
      var s = $(this).attr('id');
      var w = $('#'+s+'-div').find('div').width() + 22;
      var add_delay = false;
      $('.sub-content-div').stop(true, true).each(function(i) {
        if ($(this).width() != 0) {
          $(this).animate({'width': 0}, 300); 
          add_delay = true;  
        }
      });
      if (add_delay) {
        $('#'+s+'-div').delay(300).animate({'width':w}, 500);  
      } else {
        $('#'+s+'-div').animate({'width':w}, 500);
      }
    },
    function() {
      // do nothing  
    }
  );
    
  $('#menu1').children('li').hoverIntent( function() {
	$(this).find('.submenu-1').children('li').stop(true, true).slideDown(1000, 'easeOutBounce').hoverIntent( function() {
	  $(this).find('.submenu-2').children('li').stop(true, true).slideDown(1000, 'easeOutBounce');
	}, function() {
	  $(this).find('.submenu-2').children('li').stop(true, true).slideUp(0);
  });
  }, function() {
	$(this).find('.submenu-1').children('li').stop(true, true).slideUp(300, 'easeInBounce');
  });
  
  //---- Code for client login ----//
    var log_timer;
    $('#login-form').submit(function(e) {
        if (log_timer) {
            clearTimeout(log_timer);
            log_timer = null;
        }
        log_timer = setTimeout(login_no_response, 5000);
        enc_k = log_k.substr(0, 5);
        var pw = hex_sha256(hex_sha256($('#login-email').val()+$('#login-pw').val())+log_k);
        var data_obj = {
            'token':log_k,
            'email':$('#login-email').val(),
            'pword':pw
        }
        enc_data_str = Aes.Ctr.encrypt(JSON.stringify(data_obj), enc_k, 256);
        $.post('/process-login.php', {'z':enc_data_str}, function(result) {
            clearTimeout(log_timer);
            if (result=='success') {
                window.location.reload();
            } else {
                $('#login_error').show();
            }
        });
        e.preventDefault();
    });
    function login_no_response() {
        var content = '<div style="background: url(/images/error-icon.png) no-repeat 40px 36px; padding: 40px 70px;">The server is not responding. Please <a onClick="window.location.reload()" style="color: #0000ff; text-decoration: underline; cursor: pointer;">reload</a> your browser and try again.</div>';
        $('body').html(content).css({'background':'#ffffff'});
    }
 
  /*
  $('#loginbtn').click( function() {
	$('#login_div_frame').animate({'top':0}, 0, function() {
		$('#login_top_div').delay(300).animate({'left':'-250px'}, 300, function(){
			$('#login_div').animate({'height':'220px'}, 300); 
		});
	});
  });
  function login_user() {
	$('.login_error_msg').hide();
	$('#loading_div').show();
	$.post('../process-login.php', $('#login-form').serialize(), function(data) {
		if (data=='Success') {
			$('#loading_div').hide();
			$('#login_div').animate({'height':'0px'}, 300, function(){
				$('#login_top_div').animate({'left':'0px'}, 300, function() {
					location.reload();
				}); 
			});
				
		} else {
			$('.login_error_msg').text(data).show();
			$('#loading_div').hide();
		}
	});  
  }
  
  $('#submit-login').click( function() {
	login_user();
  });
  
  $('#login-form input').bind('keypress', function(e) {
	if (e.keyCode == 13) {
	  login_user();
	}
  });
  
  //---- Code for closing client login prompt ----//
  $('#close-login').click( function() {
	$('#login_div').animate({'height':'0px'}, 300, function(){
		$('#login_top_div').animate({'left':'0px'}, 300, function() {
			$('#login_div_frame').animate({'top':'-100%'}, 0);
			$('.login_error_msg').hide();
			$(':input','#login-form').val('');
			$(':input','#pw-reset-form').val('');
			$('.pw_reset').hide();
			$('.login').show();
		}); 
	});
  });
  
  //---- Code for password reset ----//
  $('#pw-reset').click( function() {
	$('#login_div').animate({'height':'0px'}, 300, function(){
		$('.login_error_msg').hide();
		$('.login').hide();
		$('.pw_reset').show(0, function() {
			$('#login_div').animate({'height':'220px'}, 300);
		});
	});
  });
  
  $('#submit-pw-reset').click( function() {
	$('.login_error_msg').hide();
	$('#loading_div').show();
	$.post('../process-pw-reset.php', $('#pw-reset-form').serialize(), function(data) {
		if (data=='Success') {
			$('#loading_div').hide();
			window.alert(data);
		} else {
			$('#loading_div').hide();
			$('.login_error_msg').text(data).show();
		}
	});
  });
  
  
  //---- Code for exiting password reset ----//
  $('#cancel-pw-reset').click( function() {
	$('#login_div').animate({'height':'0px'}, 300, function(){
		$('.login_error_msg').hide();
		$('.pw_reset').hide();
		$('.login').show(0, function() {
			$('#login_div').animate({'height':'220px'}, 300);
		});
	});
  });
  */
  
  //---- Code for signing out ----//
  $('#logout').click( function() {
	  window.location = "../process-logout.php";
  });
  
  //---- Code for styling ----//
  //$('#content-back').gradient({ from:'f8d800', to:'ffffff', length:100 });
  //$('#footer').gradient({ from:'e0dede', to:'fbfbfc', length:100 });
  //$('.content-header').corner('tl 4px');
  //$('.content-header-back').corner('top 5px');
  //$('.subcontent').corner('5px');
  //$('.subcontent2').corner('bottom 5px');
    


}
/*
var regexclass = function() {
  $.validator.addMethod("alpha", function(value, element) {
  	return this.optional(element) || /^[a-zA-Z ]+$/i.test(value);
  }, "Please use letters and spaces only.");

  $.validator.addMethod("phone", function(value, element) {
  	return this.optional(element) || /^[2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4}$/i.test(value);
  }, "Please use correct format. e.g. 250-525-5252.");

  $.validator.addMethod("phn", function(value, element) {
  	return this.optional(element) || /^[0-9]{3} [0-9]{3} [0-9]{4}$/i.test(value);
  }, "Please use correct format. e.g. 123 123 1234.");
  
}
*/
