$(function() {
	// setup content window
	/*Track height and width of the window and the content*/
	var scroll = "";
	function resize(){
	    $('#lbContainer').css('margin-top', ($(window).height()/2)-($('#lbContainer').height()/2));
		scroll = $(window.document).scrollTop();
		$('#lbContainer').css('top', scroll);
	    if($(window).width() < $('#content').width()){
	        $('#lbContainer').css('left', ($('#lbContainer').width()/2)+'px');
	    } else {
	        $('#lbContainer').css('left', '50%');
	    }
	    $('#lbContainer2').css('margin-top', ($(window).height()/2)-($('#lbContainer2').height()/2));
		scroll = $(window.document).scrollTop();
		$('#lbContainer2').css('top', scroll);
	    if($(window).width() < $('#content').width()){
	        $('#lbContainer2').css('left', ($('#lbContainer2').width()/2)+'px');
	    } else {
	        $('#lbContainer2').css('left', '50%');
	    }
	}

	//Bind events on resize
	$(document).ready(function(){
	    resize();
	})
	$(window).bind("resize", function(){
	    resize();
	});
	$(window).scroll(function () { 
	     resize();
	});
	
	var userEmail = "";
	// add overlay background
	$(".lbLink").click(function () {
		divID = $(this).attr("name");
		overlayShow(divID);
	});
    
// Newsletter Inputs submitting after hitting 'Enter'/'Return'
	$("input#subscribeEdress").keypress(function(e) {
		if (e.which == 13) {
			divID = $("input#subscribeEdress").parent().children("a").attr("name");
			overlayShow(divID);
			return false;
		}
	});
	
	$("input#newsletterEdress").keypress(function(e) {
		if (e.which == 13) {
			divID = $("input#newsletterEdress").parent().children("a").attr("name");
			overlayShow(divID);
			return false;
		}
	});

// Show and Hide Next / Previous buttons 
	$("#lbContainer").hover(
	      function () {
	        $('.next,.previous').fadeIn(100);
	      }, 
	      function () {
	        $('.next,.previous').fadeOut(100);
	      }
	    );
// Next / Previous clicks
	$(".next,.previous").click(function () {
			$("#lbOverlay").queue(function () {
		        $(this).fadeIn("slow");
		        $(this).dequeue();	
			});
			oldDivID = $(this).attr("rel");
			divID = $(this).text();
			$("#"+oldDivID).hide();
			$("#"+divID).show();
			$(".previous").show();
			$("#lbContainer").queue(function () {
		        $(this).fadeIn("slow");
		        $(this).dequeue();	
			});
			$("#lbOverlay").click(overlayRemove);
			$("a.close").click(overlayRemove);
			resize();
    });
	// remove overlay & form field values
	function overlayRemove() {
		$("#lbContainer").hide();
		$("#lbContainer2").hide();
		$('#lbContainer2 :input').val('');
		$('#lbContainer2 :checkbox').attr('checked','');
		$("#lbOverlay").fadeOut("slow");
		$("#"+divID).hide();
	}
	
	function overlayShow(div) {
		$("#lbOverlay").queue(function () {
		        $(this).fadeIn("slow");
		        $(this).dequeue();	
		});
		switch (div) {
			case 'askJoeyBox':
				$("#"+div).show();
				$("#lbContainer2 .top h4").text("Ask Joey a Question");
				$("#newsletterBox, #assocSignupBox").hide();
				formScroll();
				containerSwitch("#lbContainer2");
				break;
			case 'newsletterBox':
				userEmail = $("input#subscribeEdress").val();
				$("input#emAddr1").val(userEmail);
				$("#"+div).show();
				$("#lbContainer2 .top h4").text("Newsletter Sign Up");
				$("#askJoeyBox, #assocSignupBox").hide();				
				formScroll();
				containerSwitch("#lbContainer2");
				break;
			case 'newsletterBox2':
				userEmail = $("input#newsletterEmail").val();
				$("input#emAddr1").val(userEmail);
				$("#newsletterBox").show();
				$("#lbContainer2 .top h4").text("Newsletter Sign Up");
				$("#askJoeyBox, #assocSignupBox").hide();
				formScroll();
				containerSwitch("#lbContainer2");
				break;
			case 'assocSignupBox':
				$("#assocSignupBox").show();
				$("#lbContainer2 .top h4").text("Raceday Volunteer Sign Up");
				$("#newsletterBox, #askJoeyBox").hide();
				formScroll();
				containerSwitch("#lbContainer2");
				break;
			default:
				$("#"+div).show();
				containerSwitch("#lbContainer");
				break;
		}
		
		$("#lbOverlay").click(overlayRemove);
		$("a.close").click(overlayRemove);
		resize();
	}
	
	function formScroll() {
		var wHeight = $(window).height();
		if (wHeight < '636') {
			$('#lbContainer2 .mid').height(wHeight-116).css("overflow", "auto");
		}
	}
	
	function containerSwitch(content) {
		$(content).queue(function () {
	        $(this).fadeIn("slow");
	        $(this).dequeue();	
		});
	}
});



