<!--
$(document).ready(function(){ 
	$("#main_navigation").superfish({
		animation: {height:'show'},
		autoArrows: false
	});  
});

$(function() {
		   
	$('a[rel*="popup"]').click("popupWindow").attr("title",$(this).attr("title") + " [Opens in pop-up window]");	   
	$('a[rel*="external"]').attr("target","_blank").attr("title",$(this).attr("title") + " [Opens in new window]");	   
	$('a[href*=".pdf"]').addClass("pdf");	   
	$('a[href*=".dov"]').addClass("doc");	   
	
	$('span.meta span.filename').each(function(){
		if ($(this).text().indexOf(".pdf") != -1) $(this).parent(".meta").addClass("pdf");
		else if ($(this).text().indexOf(".doc") != -1) $(this).parent(".meta").addClass("doc");
	});
	if($("a[href*='/contact.php']").length) {
		$("a[href*='/contact.php']:not('#main_navigation a[href*='/contact.php']')").click(function(){
			$("#quick_contact").append($('<p class="close"><span class="simplemodal-close">Close</span></p>'))
					.modal({close: false, onClose: closeModal, onOpen: openModal, persist: false, onShow: onshowModal});
			return false;
		});
	}
});

function popupWindow(e){
	if (window.event) 
	 {
	 window.event.returnValue = false;
	 window.event.cancelBubble = true;
	 } 
	else if (e) 
	 {
	 e.stopPropagation();
	 e.preventDefault();
	 }

	window.open(this.href, "popup", "");
	return false;
}
function closeModal (dialog) {
	dialog.data.fadeOut('slow', function () {
		dialog.container.fadeOut('slow', function () {
			dialog.overlay.slideUp('slow', function () {
				$.modal.close();
				$('.close').remove();
			});
		});
	});
}
function openModal (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.fadeIn('slow', function () {
			dialog.data.hide().slideDown('slow');	 
		});
	});
}
function onshowModal (dialog) {
	$(document).keypress(function(e){
	  var key  = (window.event) ?  event.keyCode : e.keyCode;
 	 if( key == 27 ) $.modal.close();
	});
	if($("#quick_contact").length) {
		$("#quick_contact input:not(.optional), #quick_contact textarea:not(.optional)").blur(function(){
			if ($(this).val() == "") $(this).addClass("form_error"); else $(this).removeClass("form_error");
		});
		$("#quick_contact input[name='email']").blur(function(){
			var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (!regex.test($(this).val())) {
				$(this).addClass("form_error");
				$(this).after($("<p class='e-mail_error form_error'>Invalid email format. Please correct.</p>"));
			} else {
				$('.e-mail_error').remove();
				$(this).removeClass("form_error");
			}
		});
		$("#quick_contact input[name='website']").blur(function(){
			var regex = /^(https?:\/\/)?(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+(:[0-9]{1,5})?$/;
			if (!regex.test($(this).val()) && 
					!($(this).val() =="" || $(this).val() == $('label[for="'+$(this).attr("name")+'"]').text() )) {
				$(this).addClass("form_error");
				$(this).after($("<p class='website_error form_error'>Invalid web address format. Please correct.</p>"));
			} else {
				$('.website_error').remove();
				$(this).removeClass("form_error");
			}
		});
		$("#quick_contact button[name='contact_send']").click(function(){
			$("#quick_contact input:not(.optional), #quick_contact textarea:not(.optional)").each(function(){
				if ($(this).val() == "")	$(this).addClass("form_error"); else $(this).removeClass("form_error");
			});
			if ($("#quick_contact input, #quick_contact textarea").hasClass('form_error')) {
				alert("Please complete all required fields, and make sure to add a valid email address.");
			} else { 
					$(this).attr("disabled", "true");
					$("#quick_contact form").append($('<p class="sending">Please wait while we send your message...</p>'));
					name = $("#quick_contact input[name='name']").val();			
					data_string = $("#quick_contact form").serialize();
					$('#quick_contact').fadeTo(500,.5);
					$.ajax({
					   type: "POST",
					   url: "/scripts/ajax/contactprocess.php",
					   data: data_string+"&ajax=true",
					   async: false,
					   success: function(msg){
						$("#quick_contact h2").after(
							$("<p class='success column span-4 last'>Hi, <strong>"
								+ name	+"</strong>:<br />" + 
								"Thank you for contacting us. You'll hear back from us soon.<br />&#8212; Quadsimia</p>")); 
						$('#quick_contact').fadeTo(500,1);
					   },
					   error: function(obj,text,error) {
						$("#quick_contact h2").after(
							$("<p class='error column span-4 last'>Hi, <strong>"
								+ name	+ "</strong>:<br />" + 
								"Something went wrong when we tried to send your message. You can try again later or "
								+ "contact us another way: by phone at 888-499-4440 or by email at "
								+ "<a href='mailto:info@quadsimia.com'>info@quadsimia.com</a>.")); 
						$('#quick_contact').fadeTo(500,1);
					   }
					 });
	
					$("#quick_contact form").remove();
			}
			return false; // We don't want to submit the form since AJAX handled it
		});
	}
}
