function checkEmail(email) { 
  var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var emailVal = $("#" + email).val();
  return pattern.test(emailVal);
}

function isotopeIt() {
  var $container = $('#content');
  colWidth = ($(window).width() >= 1250) ? 245 : 196;
  $container.isotope({
    itemPositionDataEnabled: true,
    masonry: {
      columnWidth: colWidth
    }
  });
}

function scrollToProject(){  
  var _top = scroll_target.data('isotope-item-position').y;
  var _scrollMax = document.body.scrollHeight;
  $('html,body').animate({scrollTop:_top}, 840, 'easeOutExpo');
}


var scroll_target;

$(function() {
  var $container = $('#content');
  var $window = $(window);
  $.iPhone = navigator.userAgent.match(/iPhone/i);
  $.iPad = navigator.userAgent.match(/iPad/i);
  $.Mobile = (($.iPhone) || ($.iPad));
  
  var viewport = $('meta[name="viewport"]');
  if ($.iPhone) {
    viewport.attr('content', 'width=device-width, initial-scale=0.67');
  }
    
  $('#left-col a.about').click(function(e){
    e.preventDefault();
    $("#left-col ul > li").removeClass('selected');
    $(this).parent().addClass('selected').find('ul').slideDown();
  });

  if ($('body').hasClass('portfolio')) {
  
    $.Project = $("#project");
   
    // if not iphone, trigger isotope/masonry
    if (!$.iPhone) {
    
      isotopeIt();
      
      $(window).resize(function() {
        isotopeIt();
      });
      
    }
    
    $(".col").hover(function() {
      if(!$(this).hasClass('open')) {
        $(this).find('.hover').show();
      }
    }, function() {
      $(this).find('.hover').hide();
    });
    
    // project click
    $('.col',$container).click(function(e){
	  if($(e.target).parent().hasClass('credits')){
		  $(e.target).click();
		  return false;
	  }
      if($(this).hasClass('open')){
  		  return false;
  	  } else {
  	   $.Project.empty().hide();
  	  }
  	  
  	  var proj = $(".open",$container);
  
  	  if(proj){
  		  $(proj).toggleClass('open');
  	  }
  	  
  	  $(this).toggleClass('open');
  	  
      if($(this).hasClass('open')) {
        $(this).append($.Project);
        $.ajax({
          url: "/project.php?id="+$(this).attr("data-id"),
          success: function(data){
            $.Project.html(data).fadeIn(500);
          }
        });
      }

      $container.isotope('reLayout').find($(this)).each(function(){
        scroll_target = $(this);
        if(scroll_target.hasClass('open')){
          setTimeout("scrollToProject()",400);
          return false;
        }
      });
      
    });
    
  }
  
  $("#copyright").mouseenter(function() {
    $("#copy-credits").fadeIn();
  });
  
  $("#copy-credits").mouseleave(function() {
    $(this).fadeOut();
  });
  
  
  // admin
  
  if ($('body').hasClass('add-project')) {
    $('#project-feature').click(function() {
      if (!$(this).is(':checked')) {
        $(this).prop('checked',false);
        $('#featured-project').slideUp();
      } else {
        $(this).prop('checked',true);
        $('#featured-project').slideDown();
      }
    });
  }
  
  $("#add h2 a").click(function(e) {
    e.preventDefault();
    $(this).parent().hide();
    $("#add form").slideDown();
  });
  
  
  // contact, campaign monitor
  
  $('#subForm').submit(function (e) {
		e.preventDefault();
		if ($("#quyjui-quyjui").val() === '') { 
			$("#quyjui-quyjui").focus(); 
		} else {
			$.getJSON(
				this.action + "?callback=?", 
				$(this).serialize(), function () {
					$("#subForm").hide();
          $('#confirmation').slideDown("slow");
			});
			var formAction = $('#subForm').action;
			var str = $('#subForm').serialize();
		  var serialized = str + "&action=" + formAction;
		  $.post('/proxy.php', serialized);
		}
	});
  
});
