$(document).ready(function() {
	var cont_open = false;
	var adv_open = false;
	var pub_open = false;
	var inmotion = false;	
	var active_cell = "";
	var hovering = false;
	
	$(window).scroll(function() {
		
		var where = getYOffset();
		var startAt = 53;

		if (adv_open == true || pub_open == true) {
			startAt = 525;
		}
		
		if (where >= startAt) {
			$('#fixedNav').fadeIn('fast');
			$('#regNav').hide();
		} else {
			$('#fixedNav').fadeOut('fast');
			$('#regNav').show();
		}

		if (where < document.getElementById('techStart').offsetTop) {
			setTab('homeTab');
		} else if (where >= document.getElementById('techStart').offsetTop && where <= document.getElementById('techEnd').offsetTop) {
			setTab('techTab');
		} else if (where >= document.getElementById('wwdStart').offsetTop && where <= document.getElementById('wwdEnd').offsetTop) {
			setTab('wwdTab');
		} else if (where >= document.getElementById('jobStart').offsetTop && where <= document.getElementById('jobEnd').offsetTop) {	
			if (where > document.getElementById('jobEnd').offsetTop - 600) {
				setTab('aboutTab');
			} else {
				setTab('jobTab');
			}
		}
		
	});
	
/*
	$('body').click(function() {
		alert(window.pageYOffset);
	});
*/
	
	
	
	$('.techCell').hover(function() {
		
		var maskBox = $(this).find('.techImg');
		
		$(maskBox).find('.maskTop').animate({ height: "35px" }, 300);
		$(maskBox).find('.maskBtm').animate({ top: "130px" }, 300);
		
		

	}, function() {

		var maskBox = $(this).find('.techImg');
		
		
		$(maskBox).find('.maskTop').animate({ height: "0px" }, 150);
		$(maskBox).find('.maskBtm').animate({ top: "150px" }, 150);
		
	});
	
	
	
	
	
	$('.swapMathTab').click(function() {
		
		if ($('#mathOne').is(":visible")) {
			$('#mathOne').fadeOut('fast', function() {
			
				$('.mathBullet1').attr('src','/assets/images/ico-slide-inactive-sml.png');
				$('.mathBullet2').attr('src','/assets/images/ico-slide-active-sml.png');
				
				$('#mathTwo').fadeIn('fast');
			});
		} else if ($('#mathTwo').is(":visible")) {
			$('#mathTwo').fadeOut('fast', function() {
			
				$('.mathBullet2').attr('src','/assets/images/ico-slide-inactive-sml.png');
				$('.mathBullet1').attr('src','/assets/images/ico-slide-active-sml.png');
				
				$('#mathOne').fadeIn('fast');
			});
		}
		
	});
	
	
	$('.menuRow').click(function() {
		$(this).closest('.dropDown').find('span').attr('innerHTML', $(this).attr('innerHTML'));
		$('#cFormSubject').val($(this).attr('innerHTML'));
	});
	
	
	$('.canForm, .lowCanForm').click(function() {
		$('#mainOverlay').hide();
	});
	
	
	$('#mainOverlay').click(function() {
		$(this).hide();
	})
	
	
	$('.mathForm').click(function(e){
		e.stopPropagation();
	});
	
	
	$('.solveBtn').click(function() {
		var diff = $(this).attr('rel');
		var theheight = $(document).height();
		
		$('#mainOverlay').height(theheight);
		
		$('#mainOverlay').show();
		
		if (diff == 'easy') {
			$('.mathForm').css('margin-top','1460px');
			$('#hardContent').hide();
		} else {
			$('.mathForm').css('margin-top','1760px');
			$('#hardContent').show();
		}
		
		$('.mathForm').show();
	
	});


	$('.closeBtn, .cancelBtn').click(function() {
		var target = $(this).attr('rel');
	
		if (target == "contact") {
			$('#contDiv').animate({ height: "0px"}, 300);
			$('#tabContact').removeClass('active');
			cont_open = false;
			
			clearErrors('contactForm');
			
		} else if (target == "publishers") {
		
			$('#pubDiv').animate({ height: "0px"}, 300);
			pub_open = false;
			clearErrors('pubForm');
			
		} else if (target == "advertisers") {
		
			clearErrors('advForm');
			adv_open = false;
			$('#advDiv').animate({ height: "0px"}, 300);	
		}
		
		setTab('homeTab');
	});
	
	
	
	$('.drkBox').focus(function() {
		$(this).css('backgroundColor', '#908c8c');
		$(this).css('color','#000');
	});
	
	$('.drkBox').focusout(function() {
		$(this).css('backgroundColor', '#16191b');
		$(this).css('color', '#FFF');
	});
	



	$('.togglePub').click(function() {
		if (inmotion == true) return false;
	
		if ($('#pubDiv').css("height") == "0px") {
			inmotion = true;
			
			if (adv_open || cont_open) {
				$('#advDiv').animate({ height: "0px"}, 300);
				$('#tabContact').removeClass('active');
				$('#contDiv').animate({ height: "0px"}, 300);
			}
		
			$('#pubDiv').animate({ height: "470px"}, {duration: 800, complete: function() {
				inmotion = false;
				pub_open = true;
			}});
		} else {
			inmotion = true;
		
			$('#pubDiv').animate({ height: "0px"}, 300, function () {
				inmotion = false;
				pub_open = false;
			});
		}
	
	});
	
	$('.toggleAdv').click(function() {
		if (inmotion == true) return false;

		if ($('#advDiv').css("height") == "0px") {
			inmotion = true;
			
			if (pub_open || cont_open) {
				$('#pubDiv').animate({ height: "0px"}, 300);
				$('#tabContact').removeClass('active');
				$('#contDiv').animate({ height: "0px"}, 300);
			}
			
			$('#advDiv').animate({ height: "470px"}, {duration: 800, complete: function() {
				inmotion = false;
				adv_open = true;
			}});
		} else {
			inmotion = true;
		
			$('#advDiv').animate({ height: "0px"}, 300, function() {
				inmotion = false;
				adv_open = false;
			});
		}
	
	});
	
	
	$('#tabContact').click(function() {
		if (inmotion == true) return false;
						
		if ($('#contDiv').css('height') == "0px") {
			inmotion = true;
			
			
			setTab('contactTab');
			
			if (pub_open || adv_open) {
				$('#pubDiv').animate({ height: "0px"}, 300);
				$('#advDiv').animate({ height: "0px"}, 300);
				
				pub_open = false;
				adv_open = false;
			}
			
			$('#contDiv').animate({ height: "315px"}, {duration: 800, complete: function() {
				inmotion = false;
				cont_open = true;
			}});
			
		} else {
			inmotion = true;
			$('#tabContact').removeClass('active');
			
			$('#contDiv').animate({ height: "0px"}, 300, function() {
				inmotion = false;
				cont_open = false;
			});
		}
	});
	
	
	$('.showContact').click(function() {
		scrollTo("home");
		
		$('#contDiv').delay(500).animate({ height: "315px"}, {duration: 800, complete: function() {
			setTab('contactTab');
		}});
		
		cont_open = true;
		
	});
	
	$('.showAdv').click(function () {
		scrollTo("home");
	
		$('#advDiv').delay(300).animate({ height: "470px"}, {duration: 800});
		adv_open = true;
	});
	
	
	$('.showPub').click(function () {
		scrollTo("home");
	
		$('#pubDiv').delay(300).animate({ height: "470px"}, {duration: 800});
		pub_open = true;
	});
});

function showNotify() {
	$('#notifyBar').animate({ height: "40px" }, 300, function() {
		$(this).delay(3000).animate({ height: "0px" }, 300 );
	});
}

function getYOffset() {
    var pageY;
    
    if(typeof(window.pageYOffset)=='number') {
       pageY = window.pageYOffset;
    } else {
       pageY = document.documentElement.scrollTop;
    }
    
    return pageY;
}


function setTab(thetab) {
	$('#fixedNav').find('li').not('.' + thetab).removeClass('active');
	$('#regNav').find('li').not('.' + thetab).removeClass('active');

	$('.' + thetab).addClass('active');
}

function scrollTo(place){
	var location = "0px";
	
	// hide everything
	$('#pubDiv').animate({ height: "0px"}, 100);
	$('#advDiv').animate({ height: "0px"}, 100);
	$('#contDiv').animate({ height: "0px"}, 100);
	
	$('#tabContact').removeClass('active');
	
	
		if (place == "home") {
			location = "0px";
		} else if (place == "techSection") {
			location = document.getElementById('techStart').offsetTop + 2; // plus for >= call on tabSet
		} else if (place == "wwdSection") {
			location = document.getElementById('wwdStart').offsetTop + 2;
		} else if (place == "careersSection"){
			location = document.getElementById('jobStart').offsetTop + 2;
		} else if (place == "aboutSection") {
			location = document.getElementById('aboutStart').offsetTop + 2;
		}	
	
	
    $('html, body').animate({ scrollTop: location }, 'slow');


}


