$(function(){
	var t;
	var h = false;
	var tab = false;
	$('div.leadbox').hover(

		//onmouseover
		function(){
			clearTimeout(t);//if mouseover again ,stop close form
			$(this).addClass('active');
			h = true;
		},

		//onmouseout
		function(){
			if (!checkFocus()) {
				t = setTimeout(function(){
					$('div.leadbox').removeClass('active');
				},300);//delay close form
			}
			h = false;
		}
	);

	$('div.leadbox').click(function(){
		$.get("add-leadbox.php",function(data){
			$("#leadbox-form").empty();
			$("#leadbox-form").html(data);
			$("#leadbox-form").modal({
				containerCss: {
					height:'auto',
					width:'auto',
					border:0,
					background:'transparent'
					
					
				}
			});
		});
	});
	
	//check TAB button
	$('div.leadbox input').live('keydown', function(e) { 
		var keyCode = e.keyCode || e.which; 
		if (keyCode == 9) { 
			tab = true;
		} 
	});

	//blur input
	$("div.leadbox input").blur(function(){
		if (checkFocus() == false && h == false && tab == false){
			t = setTimeout(function(){
				$('div.leadbox').removeClass('active');
			},300);//delay close form
		}
		tab = false;
	});
	
	//check input focus
	function checkFocus(){
		var focusForm = $("div.leadbox input:focus");
		if (focusForm != null && focusForm.length > 0) { 
			return true; 
		} else {
			return false;
		}
	}

});
