(function(doc) {
 
    var addEvent = 'addEventListener',
        type = 'gesturestart',
        qsa = 'querySelectorAll',
        scales = [1, 1],
        meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : [];
 
    function fix() {
        meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
        doc.removeEventListener(type, fix, true);
    }
 
    if ((meta = meta[meta.length - 1]) && addEvent in doc) {
        fix();
        scales = [.25, 1.6];
        doc[addEvent](type, fix, true);
    }
 
}(document));

window.onresize = function(event) {
	$("#f_contact,#f_copy,#f_tools").hide();
}


$(function() {
  $('.fbutton').click(function(e) {
	var thebutton = $(this).attr('rel');	
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	$('.fblock').slideUp("fast");
	if(isiPad) { 
		$('#'+thebutton).toggle(); 
	} else { 
		if ($('#'+thebutton).is(':visible')) { 
			$('#'+thebutton).slideUp("fast");
		} else { 
		$('#'+thebutton).slideDown("slow", function() { 
			$("html, body").animate({
	            'scrollTop': $(this).offset().top
        	}, 800);
//			$(window).scrollTop($(document).height());
//			$('html, body').animate({scrollTop: $(document).height()}, 'slow');
//	$(document).scrollTop($(document).height());
		}); 
		};
	};
  });

  $('.error').hide();
  
  $('.formitem').focus(function(){
    $(this).css({backgroundColor:"#DDD"});
  });
  $('.formitem').blur(function(){
    $(this).css({backgroundColor:"#FFF"});
  });

  $("#submit").click(function() {
    $('.error').hide();
		
	var name = $("input#name").val();
	if (name == "") {
      $(".name_error").show();
      return false;
	  $("input#name").focus();
    }
	var email = $("input#email").val();
	var validmail = validateEmail(email);
	if (email == "") {
      $(".email_error").show();
      $("input#email").focus();
      return false;
    }
	if (validmail == null) {
	  $(".email_error").html("Invalid Email Address");
	  $(".email_error").show();
      $("input#email").focus();
      return false;
	}
	var devtype = $("select#devtype").val();
	var message = $("#message").val();
	if (message == "") {
      $(".message_error").show();
      $("input#message").focus();
      return false;
    }
		
	var dataString = 'name='+ name + '&email=' + email + '&devtype=' + devtype + '&message=' + message;

	$("#submit").val("Working...");
	$("#submit").css({backgroundColor:"#2ac2e2"});
		
	$.ajax({
      type: "POST",
      url: "bin/process.php",
      data: dataString,
      success: function() {
		$(".formitem").val("");
		$("#contact").fadeOut(500, function() {
			$("#contact_success").fadeIn(500);
		});
      }
     });
    return false;
	});
});

function validateEmail(email) 
{ 
 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
 return email.match(re);
}

