/* code by milkandtang.com
      made on a macintosh */

//var resizeTimer = null;

$(document).ready(function() {
	/*$(window).bind('resize', function() {
	    if (resizeTimer) clearTimeout(resizeTimer);
	    resizeTimer = setTimeout(doResize, 100);
	});*/
	
	$("ul li a").bigTarget(); //Apply bigtarget to menus
	
	doResize(); //resize content to window size on main page under the right conditions
	
	$("#pageHeaderNav ul > li").hover(function() { //Apply hover style to main menu
		$(this).find('div').not('.active').animate({width: 207}, 100) }, function() {
		$(this).find('div').not('.active').animate({width: 16}, 100)
	});
	
	$("#pageSidebar ul > li").hover(function() { //Apply hover style to sidebar menu
		$(this).find('div').not('.active').css("opacity", "1").css("display", "block").animate({opacity: .7}, 200) }, function() {
		$(this).find('div').not('.active').animate({opacity: 0}, 200).css("display", "none")
	});
	$(".separateBar ul > li").hover(function() { //Apply hover style to footer menu
		$(this).animate({paddingTop: "5px"}, 100) }, function() {
		$(this).animate({paddingTop: "0px"}, 100)
	});
	
	$("#contactButton").unbind("click");
	$("#contactButton").click(function(event) {
		event.preventDefault();
		toggleContact();
	});
	
	$("#socialButton").unbind("click");
	$("#socialButton").click(function(event) {
		event.preventDefault();
		toggleSocial();
	});
	
	$("#contactTabs li").unbind("click");
	$("#contactTabs li a").unbind("click");
	$("#contactTabs li").click(function(event) {
		event.preventDefault();
		$("#contactTabs > li").each(function() {
			$(this).toggleClass("activeTab");
		});
		$(".contactInfo, #contactInfoArrow").each(function() {
			$(this).toggleClass("red");
		});
		$(".contactInfo > ul > li").each(function() {
			if(!$(this).hasClass("email")) $(this).toggleClass("activeContact");
		});
	});
	
	$("#contactInfoArrow").corner("bevel bl br");
	
	
	//VALIDATION RULES
	$("#resumeForm").validate({
			rules: {
				name: "required",
				email: { 
					required: true, 
					email: true 
				},
				phone: "required",
				degree: "required",
				resume: "required"
			},
			messages: {
				name: "Please enter your name",
				email: "Please enter a valid email address",
				phone: "Please enter a phone number where we can contact you",
				degree: "Please enter your certifications or degrees",
				resume: "Please copy and paste your resume or CV into the field above"
			}
	});
	$("#contactForm").validate({
			rules: {
				name: "required",
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				name: "Please enter your name",
				email: "Please enter a valid email address"
			}
	});
	$("#brochureForm").validate({
			rules: {
				name: "required",
				street: "required",
				city: "required",
				state: "required",
				postal: "required",
				phone: "required",
				email: {
					required: true,
					email: true
				},
				industry: "required"
			},
			messages: {
				name: "Please enter a contact name.",
				street: "Please enter your street address.",
				city: "Fill Above.",
				state: "Fill Above.",
				postal: "Fill Above.",
				phone: "Please enter a phone number.",
				email: "Please enter a valid email address.",
				industry: "Please select an industry."
			}
	});
	
	
	//AJAX SUBMIT
	$('#contactForm').ajaxForm({target: '#emailResponse', beforeSubmit: showRequest, success: showResponse});
	$('#resumeForm').ajaxForm({target: '#resumeResponse', beforeSubmit: showRequest, success: showResponseResume});
	$('#brochureForm').ajaxForm({target: '#brochureResponse', beforeSubmit: showRequest, success: showResponseBrochure});
});

function showRequest(formData, jqForm, options) { 
    if(jqForm.valid()) return true;
    return false; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
	$("#contactForm input[type='submit']").attr("disabled", "disabled");
    $('#contactForm').slideUp(100);
}

function showResponseResume(responseText, statusText)  { 
    $("#resumeForm input[type='submit']").attr("disabled", "disabled");
	$("#resumeForm").slideUp(100);
	$("#resumeResponse").slideDown(500);
}

function showResponseBrochure(responseText, statusText)  {
	$("#brochureForm input[type='submit']").attr("disabled", "disabled");
    $("#brochureForm").slideUp(100);
	$("#brochureResponse").slideDown(500);
}

function doResize() {
	var sidebarHeight = $("#pageSidebar").height() + 50;
	var placeHeight = getPlaceHeight(sidebarHeight);
	var imageHeight = getImageHeight(sidebarHeight);
	if(placeHeight) $("#pageContentPlaceholder").animate({ height: placeHeight }, 100);
	if(imageHeight) $("#pageHeaderImage").animate({ height: imageHeight }, 100);
}

function toggleContact() {
	var contact = $("#contactInfoWrapper");
	var arrow = $("#contactInfoArrow");
	if(contact.css("display") == "none") {
		contact.css("position", "absolute");
		contact.css("margin", 0);
		contact.css("bottom", 0);
		contact.animate({bottom: 30, opacity: "show"});
	}
	else {
		contact.animate({bottom: 0, opacity: "hide"});
	}
}

function toggleSocial() {
	var social = $("#socialWrapper");
	if(social.css("display") == "none") {
		social.css("position", "absolute");
		social.animate({height: 60, opacity: "show"});
	}
	else {
		social.animate({height: 0, opacity: "hide"});
	}
}

function getPlaceHeight(min) {
	if($(window).height() > 700) {
		return false;
	}
	else {
		var value = $(window).height() - 205;
		if (value < min) return min;
		else return value;
	}
}

function getImageHeight(min) {
	if($(window).height() > 700) {
		return false;
	}
	else {
		var value = $(window).height() - 50;
		if(value < min + 160) { return min + 160;}
		else return value;
	}
}
