/* NU CUSTOM */
$(document).ready(function() {
	function addMega() {
		$(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
		    (function($) {
		        jQuery.fn.calcSubWidth = function() {
		            rowWidth = 0;

		            $(this).find("ul").each(function() {
						
		                rowWidth += $(this).width();
		            });
		        };
		    })(jQuery);
		
		    if ( $(this).find(".row").length > 0 ) {
		        var biggestRow = 0;	

		        $(this).find(".row").each(function() {
		            $(this).calcSubWidth();

		            if(rowWidth > biggestRow) {
		                biggestRow = rowWidth;
		            }
		        });
		
		        $(this).find(".sub").css({'width' :biggestRow});
		        $(this).find(".row:last").css({'margin':'0'});
		    } else {
		        $(this).calcSubWidth();
		        $(this).find(".sub").css({'width' : rowWidth});
		    }
	}

	function removeMega() {
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
	      $(this).hide();
	  });
	}
	
	var megaConfig = {
         interval: 100,
         sensitivity: 4,
         over: addMega,
         timeout: 100,
         out: removeMega
    };

    $("li.mega").hoverIntent(megaConfig)
    
    $("#searchBox").keypress(function(e) {
        if ((e.keyCode || e.which) == 13) {
            if($(this).val() != null && $(this).val() != "") {
                window.location = '/search.aspx?searchtext=' + escape($(this).val());
            }
            return false;
        }
    return true;
    });
	
    $("#searchBox").focus(function() {
		if(this.value == "type and press enter") {
			this.value = "";
		}
	});
	
	$("#searchBox").blur(function() {
		if(this.value == "") {
			this.value = "type and press enter";
		}
	});
	
	/* contact form */
    $("#project_form_name").focus(function() {
        if($(this).val().toLowerCase() == 'name') $(this).val('');
    });
    
    $("#project_form_name").blur(function() {
        if($(this).val() == '') $(this).val('Name');
    });
    
    $("#project_form_org").focus(function() {
        if($(this).val().toLowerCase() == 'organization') $(this).val('');
    });
    
    $("#project_form_org").blur(function() {
        if($(this).val() == '') $(this).val('Organization');
    });
    
    $("#project_form_email").focus(function() {
        if($(this).val().toLowerCase() == 'email') $(this).val('');
    });
    
    $("#project_form_email").blur(function() {
        if($(this).val() == '') $(this).val('Email');
    });
    
    $("#project_form_phone").focus(function() {
        if($(this).val().toLowerCase() == 'phone') $(this).val('');
    });
    
    $("#project_form_phone").blur(function() {
        if($(this).val() == '') $(this).val('Phone');
    });
    
    $("#project_form_details").focus(function() {
        if($(this).val().toLowerCase() == 'how can we help you?') $(this).val('');
    });
    
    $("#project_form_details").blur(function() {
        if($(this).val() == '') $(this).val('How can we help you?');
    });
    
    $("#project_form_button").RecaptchaFacebox({
            url: '/Handlers/Recaptcha.ashx',
            successCallback: RecaptchaProjectFormSuccessCallback
        }
    );
});

function RecaptchaProjectFormSuccessCallback() {
    
    projectName = $("#project_form_name").val();
    projectOrg = $("#project_form_org").val();
    projectEmail = $("#project_form_email").val();
    projectPhone = $("#project_form_phone").val();
    projectState = $("#project_form_state").val();
    projectBusinessType = $("#project_form_business_type").val();
    projectIndustry = $("#project_form_industry").val();
    projectDetails = $("#project_form_details").val();
    
    $.ajax({
        url: "/Handlers/PostProjectForm.ashx", 
        data: { 
            name: projectName,
            org: projectOrg,
            email: projectEmail,
            phone: projectPhone,
            state: projectState,
            businessType: projectBusinessType,
            industry: projectIndustry,
            details: projectDetails
        },
        success: function(result) {
            $("#project_form").hide();
            $("#project_form_result").show();
            if(result.toLowerCase() == "false") {
                $("#project_form_result").html("<p>An error occurred when processing your information. Our support team has been notified of this error. Please refresh this page and try again.</p>");
            }
        }
    });
    
}